]> git.agnieray.net Git - galette.git/blob - tests/Galette/Core/tests/units/PasswordImage.php
2baacff4389d2cc51ee89016c93574cd66d8c0c6
[galette.git] / tests / Galette / Core / tests / units / PasswordImage.php
1 <?php
2
3 /* vim: set expandtab tabstop=4 shiftwidth=4 softtabstop=4: */
4
5 /**
6 * PasswordImage tests
7 *
8 * PHP version 5
9 *
10 * Copyright © 2013-2014 The Galette Team
11 *
12 * This file is part of Galette (http://galette.tuxfamily.org).
13 *
14 * Galette is free software: you can redistribute it and/or modify
15 * it under the terms of the GNU General Public License as published by
16 * the Free Software Foundation, either version 3 of the License, or
17 * (at your option) any later version.
18 *
19 * Galette is distributed in the hope that it will be useful,
20 * but WITHOUT ANY WARRANTY; without even the implied warranty of
21 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
22 * GNU General Public License for more details.
23 *
24 * You should have received a copy of the GNU General Public License
25 * along with Galette. If not, see <http://www.gnu.org/licenses/>.
26 *
27 * @category Core
28 * @package GaletteTests
29 *
30 * @author Johan Cwiklinski <johan@x-tnd.be>
31 * @copyright 2013-2014 The Galette Team
32 * @license http://www.gnu.org/licenses/gpl-3.0.html GPL License 3.0 or (at your option) any later version
33 * @version SVN: $Id$
34 * @link http://galette.tuxfamily.org
35 * @since 2013-10-22
36 */
37
38 namespace Galette\Core\test\units;
39
40 use \atoum;
41
42 /**
43 * PasswordImage tests class
44 *
45 * @category Core
46 * @name PasswordImage
47 * @package GaletteTests
48 * @author Johan Cwiklinski <johan@x-tnd.be>
49 * @copyright 2013-2014 The Galette Team
50 * @license http://www.gnu.org/licenses/gpl-3.0.html GPL License 3.0 or (at your option) any later version
51 * @link http://galette.tuxfamily.org
52 * @since 2013-01-13
53 */
54 class PasswordImage extends atoum
55 {
56 private $pass = null;
57
58 /**
59 * Set up tests
60 *
61 * @param string $testMethod Method name
62 *
63 * @return void
64 */
65 public function beforeTestMethod($testMethod)
66 {
67 $this->pass = new \Galette\Core\PasswordImage(false);
68 }
69
70 /**
71 * Test new PasswordImage generation
72 *
73 * @return void
74 */
75 public function testGenerateNewPassword()
76 {
77 $pass = $this->pass;
78 $res = $pass->generateNewPassword();
79 $this->boolean($res)->isTrue();
80 $new_pass = $pass->getNewPassword();
81 $this->string($new_pass)
82 ->hasLength($pass::DEFAULT_SIZE);
83 $hash = $pass->getHash();
84 $this->string($hash)->hasLength(60);
85
86 $this->string($pass->getImageName())
87 ->isIdenticalTo('pw_' . md5($hash) . '.png');
88 }
89
90 /**
91 * Test new PasswordImage image generation
92 *
93 * @return void
94 */
95 public function testNewImage()
96 {
97 $pass = new \mock\Galette\Core\PasswordImage(false);
98 $password = 'azerty12';
99 $this->calling($pass)->makeRandomPassword = $password;
100
101 $pass->newImage();
102
103 $pw_checked = password_verify('azerty12', $pass->getHash());
104 $this->boolean($pw_checked)->isTrue();
105
106 $this->string($pass->getImageName())
107 ->isIdenticalTo('pw_' . md5($pass->getHash()) . '.png');
108
109 $exists = is_file(
110 GALETTE_TEMPIMAGES_PATH . '/' . $pass->getImageName()
111 );
112 $this->boolean($exists)->isTrue();
113 }
114
115 /**
116 * Test getImage
117 *
118 * @return void
119 */
120 public function testGetImage()
121 {
122 $this->assert('Image generated without exif')
123 ->given($pass = new \Galette\Core\PasswordImage(false))
124 ->if($this->function->function_exists = false)
125 ->then
126 ->string($pass->newImage())
127 ->hasLength(60)
128 ->string($pass->getImage())
129 ->hasLengthGreaterThan(200)
130 ->startWith('data:image/png;base64,');
131
132 if (function_exists('exif_imagetype')) {
133 $this->assert('Image generated without exif')
134 ->given($pass = new \Galette\Core\PasswordImage(false))
135 ->if($this->function->function_exists = true)
136 ->then
137 ->string($pass->newImage())
138 ->hasLength(60)
139 ->string($pass->getImage())
140 ->hasLengthGreaterThan(200)
141 ->startWith('data:image/png;base64,');
142 }
143 }
144
145 /**
146 * Test cleanExpired
147 *
148 * @return void
149 */
150 public function testCleanExpired()
151 {
152 $files = scandir(GALETTE_TEMPIMAGES_PATH);
153
154 if (count($files) == 2) {
155 $files = [
156 'pw_one.png',
157 'pw_two.png',
158 'pw_three.png'
159 ];
160 }
161
162 foreach ($files as $f) {
163 if ($f != '.' && $f != '..') {
164 touch(GALETTE_TEMPIMAGES_PATH . '/' . $f, time() - 3600);
165 }
166 }
167
168 $dirfiles = scandir(GALETTE_TEMPIMAGES_PATH);
169 $this->integer(count($dirfiles))->isGreaterThan(2);
170
171 //default constructor call clean
172 $pass = new \Galette\Core\PasswordImage();
173 $dirfiles = scandir(GALETTE_TEMPIMAGES_PATH);
174 $this->integer(count($dirfiles))->isEqualTo(2);
175 }
176 }