]> git.agnieray.net Git - galette.git/blob - tests/Galette/Core/tests/units/Preferences.php
Add some infos for cards on prefs. Cheks for cards possible overflow
[galette.git] / tests / Galette / Core / tests / units / Preferences.php
1 <?php
2
3 /* vim: set expandtab tabstop=4 shiftwidth=4 softtabstop=4: */
4
5 /**
6 * Preferences 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-19
36 */
37
38 namespace Galette\Core\test\units;
39
40 use \atoum;
41
42 /**
43 * Preferences tests class
44 *
45 * @category Core
46 * @name Preferences
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 Preferences extends atoum
55 {
56 private $preferences = null;
57 private $zdb;
58
59 /**
60 * Set up tests
61 *
62 * @param string $testMethod Calling method
63 *
64 * @return void
65 */
66 public function beforeTestMethod($testMethod)
67 {
68 $this->zdb = new \Galette\Core\Db();
69 $this->preferences = new \Galette\Core\Preferences(
70 $this->zdb
71 );
72 }
73
74 /**
75 * Test preferences initialization
76 *
77 * @return void
78 */
79 public function testInstallInit()
80 {
81 $result = $this->preferences->installInit(
82 'en_US',
83 'da_admin',
84 password_hash('da_secret', PASSWORD_BCRYPT)
85 );
86 $this->boolean($result)->isTrue();
87
88 //new object with values loaded from database to compare
89 $prefs = new \Galette\Core\Preferences($this->zdb);
90
91 foreach ($prefs->getDefaults() as $key => $expected) {
92 $value = $prefs->$key;
93
94 switch ($key) {
95 case 'pref_admin_login':
96 $this->variable($value)->isIdenticalTo('da_admin');
97 break;
98 case 'pref_admin_pass':
99 $pw_checked = password_verify('da_secret', $value);
100 $this->boolean($pw_checked)->isTrue();
101 break;
102 case 'pref_lang':
103 $this->variable($value)->isIdenticalTo('en_US');
104 break;
105 case 'pref_card_year':
106 $this->variable($value)->isIdenticalTo(date('Y'));
107 break;
108 default:
109 $this->variable($value)->isEqualTo($expected);
110 break;
111 }
112 }
113
114 //tru to set and get a non existent value
115 $prefs->doesnotexists = 'that *does* not exists.';
116 $false_result = $prefs->doesnotexists;
117 $this->boolean($false_result)->isFalse();
118
119 //change slogan
120 $slogan = 'One Galette to rule them all';
121 $prefs->pref_slogan = $slogan;
122 $check = $prefs->pref_slogan;
123 $this->string($check)->isIdenticalTo($slogan);
124
125 //change password
126 $new_pass = 'anoth3er_s3cr3t';
127 $prefs->pref_admin_pass = $new_pass;
128 $pass = $prefs->pref_admin_pass;
129 $pw_checked = password_verify($new_pass, $pass);
130 $this->boolean($pw_checked)->isTrue();
131
132 $this->preferences->pref_nom = 'Galette';
133 $this->preferences->pref_ville = 'Avignon';
134 $this->preferences->pref_cp = '84000';
135 $this->preferences->pref_adresse = 'Palais des Papes';
136 $this->preferences->pref_adresse2 = 'Au milieu';
137 $this->preferences->pref_pays = 'France';
138
139 $expected = "Palais des Papes\nAu milieu\n84000 Avignon - France";
140 $address = $this->preferences->getPostalAddress();
141
142 $this->variable($address)->isIdenticalTo($expected);
143
144 $slogan = $this->preferences->pref_slogan;
145 $this->variable($slogan)->isEqualTo('');
146
147 $slogan = 'One Galette to rule them all';
148 $this->preferences->pref_slogan = $slogan;
149 $result = $this->preferences->store();
150
151 $this->boolean($result)->isTrue();
152
153 $prefs = new \Galette\Core\Preferences($this->zdb);
154 $check_slogan = $prefs->pref_slogan;
155 $this->variable($check_slogan)->isEqualTo($slogan);
156
157 //reset database value...
158 $this->preferences->pref_slogan = '';
159 $this->preferences->store();
160 }
161
162 /**
163 * Test fields names
164 *
165 * @return void
166 */
167 public function testFieldsNames()
168 {
169 $this->preferences->load();
170 $fields_names = $this->preferences->getFieldsNames();
171 $expected = array_keys($this->preferences->getDefaults());
172
173 sort($fields_names);
174 sort($expected);
175
176 $this->array($fields_names)->isIdenticalTo($expected);
177 }
178
179 /**
180 * Test preferences updating when some are missing
181 *
182 * @return void
183 */
184 public function testUpdate()
185 {
186 $delete = $this->zdb->delete(\Galette\Core\Preferences::TABLE);
187 $delete->where(
188 array(
189 \Galette\Core\Preferences::PK => 'pref_facebook'
190 )
191 );
192 $this->zdb->execute($delete);
193
194 $delete = $this->zdb->delete(\Galette\Core\Preferences::TABLE);
195 $delete->where(
196 array(
197 \Galette\Core\Preferences::PK => 'pref_viadeo'
198 )
199 );
200 $this->zdb->execute($delete);
201
202 $this->preferences->load();
203 $fb = $this->preferences->pref_facebook;
204 $viadeo = $this->preferences->pref_viadeo;
205
206 $this->boolean($fb)->isFalse();
207 $this->boolean($viadeo)->isFalse();
208
209 $prefs = new \Galette\Core\Preferences($this->zdb);
210 $fb = $prefs->pref_facebook;
211 $viadeo = $prefs->pref_viadeo;
212
213 $this->variable($fb)->isIdenticalTo('');
214 $this->variable($viadeo)->isIdenticalTo('');
215 }
216
217 /**
218 * Test public pages visibility
219 *
220 * @return void
221 */
222 public function testPublicPagesVisibility()
223 {
224 $this->preferences->load();
225 $session = new \RKA\Session();
226
227 $visibility = $this->preferences->pref_publicpages_visibility;
228 $this->variable($visibility)->isEqualTo(
229 \Galette\Core\Preferences::PUBLIC_PAGES_VISIBILITY_RESTRICTED
230 );
231
232 $anon_login = new \Galette\Core\Login(
233 $this->zdb,
234 new \Galette\Core\I18n(),
235 $session
236 );
237
238 $admin_login = new \mock\Galette\Core\Login(
239 $this->zdb,
240 new \Galette\Core\I18n(),
241 $session
242 );
243 $this->calling($admin_login)->isAdmin = true;
244
245 $user_login = new \mock\Galette\Core\Login(
246 $this->zdb,
247 new \Galette\Core\I18n(),
248 $session
249 );
250 $this->calling($user_login)->isUp2Date = true;
251
252 $visible = $this->preferences->showPublicPages($anon_login);
253 $this->boolean($visible)->isFalse();
254
255 $visible = $this->preferences->showPublicPages($admin_login);
256 $this->boolean($visible)->isTrue();
257
258 $visible = $this->preferences->showPublicPages($user_login);
259 $this->boolean($visible)->isTrue();
260
261 $this->preferences->pref_publicpages_visibility
262 = \Galette\Core\Preferences::PUBLIC_PAGES_VISIBILITY_PUBLIC;
263
264 $visible = $this->preferences->showPublicPages($anon_login);
265 $this->boolean($visible)->isTrue();
266
267 $visible = $this->preferences->showPublicPages($admin_login);
268 $this->boolean($visible)->isTrue();
269
270 $visible = $this->preferences->showPublicPages($user_login);
271 $this->boolean($visible)->isTrue();
272
273 $this->preferences->pref_publicpages_visibility
274 = \Galette\Core\Preferences::PUBLIC_PAGES_VISIBILITY_PRIVATE;
275
276 $visible = $this->preferences->showPublicPages($anon_login);
277 $this->boolean($visible)->isFalse();
278
279 $visible = $this->preferences->showPublicPages($admin_login);
280 $this->boolean($visible)->isTrue();
281
282 $visible = $this->preferences->showPublicPages($user_login);
283 $this->boolean($visible)->isFalse();
284 }
285
286 /**
287 * Data provider for cards sizes tests
288 *
289 * @return array
290 */
291 protected function sizesProvider()
292 {
293 return [
294 [//defaults
295 15, //vertical margin
296 20, //horizontal margin
297 5, //vertical spacing
298 10, //horizontal spacing
299 0 //expected number of warnings
300 ], [ //OK
301 0, //vertical margin
302 20, //horizontal margin
303 11, //vertical spacing
304 10, //horizontal spacing
305 0 //expected number of warnings
306 ], [ //vertical overflow
307 0, //vertical margin
308 20, //horizontal margin
309 12, //vertical spacing
310 10, //horizontal spacing
311 1 //expected number of warnings
312 ], [//horizontal overflow
313 15, //vertical margin
314 20, //horizontal margin
315 5, //vertical spacing
316 61, //horizontal spacing
317 1 //expected number of warnings
318 ], [//vertical and horizontal overflow
319 0, //vertical margin
320 20, //horizontal margin
321 12, //vertical spacing
322 61, //horizontal spacing
323 2 //expected number of warnings
324 ], [//vertical overflow
325 17, //vertical margin
326 20, //horizontal margin
327 5, //vertical spacing
328 10, //horizontal spacing
329 1 //expected number of warnings
330 ]
331 ];
332 }
333
334 /**
335 * Test checkCardsSizes
336 *
337 * @dataProvider sizesProvider
338 *
339 * @param integer $vm Vertical margin
340 * @param integer $hm Horizontal margin
341 * @param integer $vs Vertical spacing
342 * @param integer $hs Horizontal spacing
343 * @param integer $count Number of expected errors
344 *
345 * @return void
346 */
347 public function testCheckCardsSizes($vm, $hm, $vs, $hs, $count)
348 {
349 $this->preferences->pref_card_marges_v = $vm;
350 $this->preferences->pref_card_marges_h = $hm;
351 $this->preferences->pref_card_vspace = $vs;
352 $this->preferences->pref_card_hspace = $hs;
353 $this->array($this->preferences->checkCardsSizes())->hasSize($count);
354 }
355 }