]> git.agnieray.net Git - galette.git/blob - tests/Galette/Core/tests/units/Preferences.php
Merge branch 'release/0.9.6'
[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 private $login;
59 private $mocked_router;
60
61 /**
62 * Set up tests
63 *
64 * @param string $method Calling method
65 *
66 * @return void
67 */
68 public function beforeTestMethod($method)
69 {
70 $this->mocked_router = new \mock\Slim\Router();
71 $this->calling($this->mocked_router)->pathFor = function ($name, $params) {
72 return $name;
73 };
74
75 $app = new \Galette\Core\SlimApp();
76 $plugins = new \Galette\Core\Plugins();
77 require GALETTE_BASE_PATH . '/includes/dependencies.php';
78 $container = $app->getContainer();
79 $_SERVER['HTTP_HOST'] = '';
80
81 $this->zdb = $container->get('zdb');
82 $this->login = $container->get('login');
83 $this->preferences = $container->get('preferences');
84 $container->set('router', $this->mocked_router);
85 $container->set(Slim\Router::class, $this->mocked_router);
86
87 global $router;
88 $router = $this->mocked_router;
89 }
90
91 /**
92 * Tear down tests
93 *
94 * @param string $method Calling method
95 *
96 * @return void
97 */
98 public function afterTestMethod($method)
99 {
100 if (TYPE_DB === 'mysql') {
101 $this->array($this->zdb->getWarnings())->isIdenticalTo([]);
102 }
103
104 $delete = $this->zdb->delete(\Galette\Entity\Social::TABLE);
105 $this->zdb->execute($delete);
106 }
107
108 /**
109 * Test preferences initialization
110 *
111 * @return void
112 */
113 public function testInstallInit()
114 {
115 $result = $this->preferences->installInit(
116 'en_US',
117 'da_admin',
118 password_hash('da_secret', PASSWORD_BCRYPT)
119 );
120 $this->boolean($result)->isTrue();
121
122 //new object with values loaded from database to compare
123 $prefs = new \Galette\Core\Preferences($this->zdb);
124
125 foreach ($prefs->getDefaults() as $key => $expected) {
126 $value = $prefs->$key;
127
128 switch ($key) {
129 case 'pref_admin_login':
130 $this->variable($value)->isIdenticalTo('da_admin');
131 break;
132 case 'pref_admin_pass':
133 $pw_checked = password_verify('da_secret', $value);
134 $this->boolean($pw_checked)->isTrue();
135 break;
136 case 'pref_lang':
137 $this->variable($value)->isIdenticalTo('en_US');
138 break;
139 case 'pref_card_year':
140 $this->variable($value)->isIdenticalTo(date('Y'));
141 break;
142 default:
143 $this->variable($value)->isEqualTo($expected);
144 break;
145 }
146 }
147
148 //tru to set and get a non existent value
149 $prefs->doesnotexists = 'that *does* not exists.';
150 $false_result = $prefs->doesnotexists;
151 $this->boolean($false_result)->isFalse();
152
153 //change slogan
154 $slogan = 'One Galette to rule them all';
155 $prefs->pref_slogan = $slogan;
156 $check = $prefs->pref_slogan;
157 $this->string($check)->isIdenticalTo($slogan);
158
159 //change password
160 $new_pass = 'anoth3er_s3cr3t';
161 $prefs->pref_admin_pass = $new_pass;
162 $pass = $prefs->pref_admin_pass;
163 $pw_checked = password_verify($new_pass, $pass);
164 $this->boolean($pw_checked)->isTrue();
165
166 $this->preferences->pref_nom = 'Galette';
167 $this->preferences->pref_ville = 'Avignon';
168 $this->preferences->pref_cp = '84000';
169 $this->preferences->pref_adresse = 'Palais des Papes';
170 $this->preferences->pref_adresse2 = 'Au milieu';
171 $this->preferences->pref_pays = 'France';
172
173 $expected = "Galette\nPalais des Papes\nAu milieu\n84000 Avignon - France";
174 $address = $this->preferences->getPostalAddress();
175
176 $this->variable($address)->isIdenticalTo($expected);
177
178 $slogan = $this->preferences->pref_slogan;
179 $this->variable($slogan)->isEqualTo('');
180
181 $slogan = 'One Galette to rule them all';
182 $this->preferences->pref_slogan = $slogan;
183 $result = $this->preferences->store();
184
185 $this->boolean($result)->isTrue();
186
187 $prefs = new \Galette\Core\Preferences($this->zdb);
188 $check_slogan = $prefs->pref_slogan;
189 $this->variable($check_slogan)->isEqualTo($slogan);
190
191 //reset database value...
192 $this->preferences->pref_slogan = '';
193 $this->preferences->store();
194 }
195
196 /**
197 * Test fields names
198 *
199 * @return void
200 */
201 public function testFieldsNames()
202 {
203 $this->preferences->load();
204 $fields_names = $this->preferences->getFieldsNames();
205 $expected = array_keys($this->preferences->getDefaults());
206
207 sort($fields_names);
208 sort($expected);
209
210 $this->array($fields_names)->isIdenticalTo($expected);
211 }
212
213 /**
214 * Test preferences updating when some are missing
215 *
216 * @return void
217 */
218 public function testUpdate()
219 {
220 $delete = $this->zdb->delete(\Galette\Core\Preferences::TABLE);
221 $delete->where(
222 array(
223 \Galette\Core\Preferences::PK => 'pref_footer'
224 )
225 );
226 $this->zdb->execute($delete);
227
228 $delete = $this->zdb->delete(\Galette\Core\Preferences::TABLE);
229 $delete->where(
230 array(
231 \Galette\Core\Preferences::PK => 'pref_new_contrib_script'
232 )
233 );
234 $this->zdb->execute($delete);
235
236 $this->preferences->load();
237 $footer = $this->preferences->pref_footer;
238 $new_contrib_script = $this->preferences->pref_new_contrib_script;
239
240 $this->boolean($footer)->isFalse();
241 $this->boolean($new_contrib_script)->isFalse();
242
243 $prefs = new \Galette\Core\Preferences($this->zdb);
244 $footer = $prefs->pref_footer;
245 $new_contrib_script = $prefs->pref_new_contrib_script;
246
247 $this->variable($footer)->isIdenticalTo('');
248 $this->variable($new_contrib_script)->isIdenticalTo('');
249 }
250
251 /**
252 * Test public pages visibility
253 *
254 * @return void
255 */
256 public function testPublicPagesVisibility()
257 {
258 $this->preferences->load();
259
260 $visibility = $this->preferences->pref_publicpages_visibility;
261 $this->variable($visibility)->isEqualTo(
262 \Galette\Core\Preferences::PUBLIC_PAGES_VISIBILITY_RESTRICTED
263 );
264
265 $anon_login = new \Galette\Core\Login(
266 $this->zdb,
267 new \Galette\Core\I18n()
268 );
269
270 $admin_login = new \mock\Galette\Core\Login(
271 $this->zdb,
272 new \Galette\Core\I18n()
273 );
274 $this->calling($admin_login)->isAdmin = true;
275
276 $user_login = new \mock\Galette\Core\Login(
277 $this->zdb,
278 new \Galette\Core\I18n()
279 );
280 $this->calling($user_login)->isUp2Date = true;
281
282 $visible = $this->preferences->showPublicPages($anon_login);
283 $this->boolean($visible)->isFalse();
284
285 $visible = $this->preferences->showPublicPages($admin_login);
286 $this->boolean($visible)->isTrue();
287
288 $visible = $this->preferences->showPublicPages($user_login);
289 $this->boolean($visible)->isTrue();
290
291 $this->preferences->pref_publicpages_visibility
292 = \Galette\Core\Preferences::PUBLIC_PAGES_VISIBILITY_PUBLIC;
293
294 $visible = $this->preferences->showPublicPages($anon_login);
295 $this->boolean($visible)->isTrue();
296
297 $visible = $this->preferences->showPublicPages($admin_login);
298 $this->boolean($visible)->isTrue();
299
300 $visible = $this->preferences->showPublicPages($user_login);
301 $this->boolean($visible)->isTrue();
302
303 $this->preferences->pref_publicpages_visibility
304 = \Galette\Core\Preferences::PUBLIC_PAGES_VISIBILITY_PRIVATE;
305
306 $visible = $this->preferences->showPublicPages($anon_login);
307 $this->boolean($visible)->isFalse();
308
309 $visible = $this->preferences->showPublicPages($admin_login);
310 $this->boolean($visible)->isTrue();
311
312 $visible = $this->preferences->showPublicPages($user_login);
313 $this->boolean($visible)->isFalse();
314 }
315
316 /**
317 * Data provider for cards sizes tests
318 *
319 * @return array
320 */
321 protected function sizesProvider()
322 {
323 return [
324 [//defaults
325 15, //vertical margin
326 20, //horizontal margin
327 5, //vertical spacing
328 10, //horizontal spacing
329 0 //expected number of warnings
330 ], [ //OK
331 0, //vertical margin
332 20, //horizontal margin
333 11, //vertical spacing
334 10, //horizontal spacing
335 0 //expected number of warnings
336 ], [ //vertical overflow
337 0, //vertical margin
338 20, //horizontal margin
339 12, //vertical spacing
340 10, //horizontal spacing
341 1 //expected number of warnings
342 ], [//horizontal overflow
343 15, //vertical margin
344 20, //horizontal margin
345 5, //vertical spacing
346 61, //horizontal spacing
347 1 //expected number of warnings
348 ], [//vertical and horizontal overflow
349 0, //vertical margin
350 20, //horizontal margin
351 12, //vertical spacing
352 61, //horizontal spacing
353 2 //expected number of warnings
354 ], [//vertical overflow
355 17, //vertical margin
356 20, //horizontal margin
357 5, //vertical spacing
358 10, //horizontal spacing
359 1 //expected number of warnings
360 ]
361 ];
362 }
363
364 /**
365 * Test checkCardsSizes
366 *
367 * @dataProvider sizesProvider
368 *
369 * @param integer $vm Vertical margin
370 * @param integer $hm Horizontal margin
371 * @param integer $vs Vertical spacing
372 * @param integer $hs Horizontal spacing
373 * @param integer $count Number of expected errors
374 *
375 * @return void
376 */
377 public function testCheckCardsSizes($vm, $hm, $vs, $hs, $count)
378 {
379 $this->preferences->pref_card_marges_v = $vm;
380 $this->preferences->pref_card_marges_h = $hm;
381 $this->preferences->pref_card_vspace = $vs;
382 $this->preferences->pref_card_hspace = $hs;
383 $this->array($this->preferences->checkCardsSizes())->hasSize($count);
384 }
385
386 /**
387 * Data provider for colors
388 *
389 * @return array
390 */
391 protected function colorsProvider(): array
392 {
393 return [
394 [
395 'prop' => 'tcol',
396 'color' => '#f0f0f0',
397 'expected' => '#f0f0f0'
398 ], [
399 'prop' => 'tcol',
400 'color' => '#f0f0f0f0',
401 'expected' => '#FFFFFF'
402 ], [
403 'prop' => 'tcol',
404 'color' => 'f0f0f0',
405 'expected' => '#f0f0f0'
406 ], [
407 'prop' => 'tcol',
408 'color' => 'azerty',
409 'expected' => '#FFFFFF'
410
411 ]
412 ];
413 }
414
415 /**
416 * Test colors
417 *
418 * @dataProvider colorsProvider
419 *
420 * @param string $prop Property to be set
421 * @param string $color Color to set
422 * @param string $expected Expected color
423 *
424 * @return void
425 */
426 public function testColors($prop, $color, $expected)
427 {
428 $prop = 'pref_card_' . $prop;
429 $this->preferences->$prop = $color;
430 $this->string($this->preferences->$prop)->isIdenticalTo($expected);
431 }
432
433 /**
434 * Test social networks
435 *
436 * @return void
437 */
438 public function testSocials()
439 {
440 $preferences = [];
441 foreach ($this->preferences->getDefaults() as $key => $value) {
442 $preferences[$key] = $value;
443 }
444
445 $preferences = array_merge($preferences, [
446 'pref_nom' => 'Galette',
447 'pref_ville' => 'Avignon',
448 'pref_cp' => '84000',
449 'pref_adresse' => 'Palais des Papes',
450 'pref_adresse2' => 'Au milieu',
451 'pref_pays' => 'France'
452 ]);
453
454 //will create 2 social networks in table
455 $post = [
456 'notasocial' => 'notasocial', //must be ignored
457 'social_new_type_1' => \Galette\Entity\Social::MASTODON,
458 'social_new_value_1' => 'Galette mastodon URL',
459 'social_new_type_2' => \Galette\Entity\Social::JABBER,
460 'social_new_value_2' => 'Galette jabber ID',
461 'social_new_type_3' => \Galette\Entity\Social::FACEBOOK,
462 'social_new_value_3' => '', //empty value, no entry
463 'social_new_type_4' => \Galette\Entity\Social::BLOG, //no value, no entry
464 ];
465
466 $post = array_merge($preferences, $post);
467
468 $this->boolean($this->preferences->check($post, $this->login))->isTrue(print_r($this->preferences->getErrors(), true));
469 $this->boolean($this->preferences->store())->isTrue();
470
471 $socials = \Galette\Entity\Social::getListForMember(null);
472 $this->array($socials)->hasSize(2);
473
474 $this->array(\Galette\Entity\Social::getListForMember(null, \Galette\Entity\Social::MASTODON))->hasSize(1);
475 $this->array(\Galette\Entity\Social::getListForMember(null, \Galette\Entity\Social::JABBER))->hasSize(1);
476 $this->array(\Galette\Entity\Social::getListForMember(null, \Galette\Entity\Social::FACEBOOK))->hasSize(0);
477 $this->array(\Galette\Entity\Social::getListForMember(null, \Galette\Entity\Social::BLOG))->hasSize(0);
478
479 //create one new social network
480 $post = [
481 'social_new_type_1' => \Galette\Entity\Social::FACEBOOK,
482 'social_new_value_1' => 'Galette does not have facebook',
483 ];
484
485 //existing social networks, change jabber ID
486 foreach ($socials as $social) {
487 $post['social_' . $social->id] = $social->url . ($social->type == \Galette\Entity\Social::JABBER ? ' - modified' : '');
488 }
489
490 $post = array_merge($preferences, $post);
491
492 $this->boolean($this->preferences->check($post, $this->login))->isTrue(print_r($this->preferences->getErrors(), true));
493 $this->boolean($this->preferences->store())->isTrue();
494
495 $socials = \Galette\Entity\Social::getListForMember(null);
496 $this->array($socials)->hasSize(3);
497
498 $search = \Galette\Entity\Social::getListForMember(null, \Galette\Entity\Social::MASTODON);
499 $this->array($search)->hasSize(1);
500 $masto = array_pop($search);
501 $this->string($masto->url)->isIdenticalTo('Galette mastodon URL');
502
503 $search = \Galette\Entity\Social::getListForMember(null, \Galette\Entity\Social::JABBER);
504 $this->array($search)->hasSize(1);
505 $jabber = array_pop($search);
506 $this->string($jabber->url)->isIdenticalTo('Galette jabber ID - modified');
507
508 $search = \Galette\Entity\Social::getListForMember(null, \Galette\Entity\Social::FACEBOOK);
509 $this->array($search)->hasSize(1);
510 $facebook = array_pop($search);
511 $this->string($facebook->url)->isIdenticalTo('Galette does not have facebook');
512
513 $post = [];
514
515 //existing social networks, drop mastodon
516 foreach ($socials as $social) {
517 if ($social->type != \Galette\Entity\Social::MASTODON) {
518 $post['social_' . $social->id] = $social->url;
519 }
520 }
521
522 $post = array_merge($preferences, $post);
523 $this->boolean($this->preferences->check($post, $this->login))->isTrue(print_r($this->preferences->getErrors(), true));
524 $this->boolean($this->preferences->store())->isTrue();
525
526 $socials = \Galette\Entity\Social::getListForMember(null);
527 $this->array($socials)->hasSize(2);
528
529 $this->array(\Galette\Entity\Social::getListForMember(null, \Galette\Entity\Social::MASTODON))->hasSize(0);
530 $this->array(\Galette\Entity\Social::getListForMember(null, \Galette\Entity\Social::JABBER))->hasSize(1);
531 $this->array(\Galette\Entity\Social::getListForMember(null, \Galette\Entity\Social::FACEBOOK))->hasSize(1);
532 }
533
534 /**
535 * Test email signature
536 *
537 * @return void
538 */
539 public function testGetMailSignature()
540 {
541 $this->string($this->preferences->getMailSignature())->isIdenticalTo("\r\n-- \r\nGalette\r\n\r\n");
542
543 $this->preferences->pref_website = 'https://galette.eu';
544 $this->string($this->preferences->getMailSignature())->isIdenticalTo("\r\n-- \r\nGalette\r\n\r\nhttps://galette.eu");
545
546 //with legacy values
547 $this->preferences->pref_mailsign = "NAME}\r\n\r\n{WEBSITE}\r\n{GOOGLEPLUS}\r\n{FACEBOOK}\r\n{TWITTER}\r\n{LINKEDIN}\r\n{VIADEO}";
548 $this->string($this->preferences->getMailSignature())->isIdenticalTo("\r\n-- \r\nGalette\r\n\r\nhttps://galette.eu");
549
550 $social = new \Galette\Entity\Social($this->zdb);
551 $this->boolean(
552 $social
553 ->setType(\Galette\Entity\Social::MASTODON)
554 ->setUrl('https://framapiaf.org/@galette')
555 ->setLinkedMember(null)
556 ->store()
557 )->isTrue();
558 $this->array(\Galette\Entity\Social::getListForMember(null, \Galette\Entity\Social::MASTODON))->hasSize(1);
559
560 $this->preferences->pref_mail_sign = "{ASSO_NAME}\r\n\r\n{ASSO_WEBSITE} - {ASSO_SOCIAL_MASTODON}";
561 $this->string($this->preferences->getMailSignature())->isIdenticalTo("\r\n-- \r\nGalette\r\n\r\nhttps://galette.eu - https://framapiaf.org/@galette");
562
563 $social = new \Galette\Entity\Social($this->zdb);
564 $this->boolean(
565 $social
566 ->setType(\Galette\Entity\Social::MASTODON)
567 ->setUrl('Galette mastodon URL - the return')
568 ->setLinkedMember(null)
569 ->store()
570 )->isTrue();
571 $this->array(\Galette\Entity\Social::getListForMember(null, \Galette\Entity\Social::MASTODON))->hasSize(2);
572 $this->string($this->preferences->getMailSignature())->isIdenticalTo("\r\n-- \r\nGalette\r\n\r\nhttps://galette.eu - https://framapiaf.org/@galette, Galette mastodon URL - the return");
573 }
574
575 /**
576 * Test getLegend
577 *
578 * @return void
579 */
580 public function testGetLegend()
581 {
582 $legend = $this->preferences->getLegend();
583 $this->array($legend)->hasSize(2);
584 $this->array($legend['main']['patterns'])->hasSize(8);
585 $this->array($legend['socials']['patterns'])->hasSize(9);
586 $this->array($legend['socials']['patterns']['asso_social_mastodon'])->isIdenticalTo([
587 'title' => __('Mastodon'),
588 'pattern' => '/{ASSO_SOCIAL_MASTODON}/'
589 ]);
590
591 $social = new \Galette\Entity\Social($this->zdb);
592 $this->boolean(
593 $social
594 ->setType('mynewtype')
595 ->setUrl('Galette specific social network URL')
596 ->setLinkedMember(null)
597 ->store()
598 )->isTrue();
599
600 $legend = $this->preferences->getLegend();
601 $this->array($legend)->hasSize(2);
602 $this->array($legend['socials']['patterns'])->hasSIze(10)->hasKey('asso_social_mynewtype');
603 $this->array($legend['socials']['patterns']['asso_social_mynewtype'])->isIdenticalTo([
604 'title' => 'mynewtype',
605 'pattern' => '/{ASSO_SOCIAL_MYNEWTYPE}/'
606 ]);
607 }
608 }