3 /* vim: set expandtab tabstop=4 shiftwidth=4 softtabstop=4: */
10 * Copyright © 2013-2014 The Galette Team
12 * This file is part of Galette (http://galette.tuxfamily.org).
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.
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.
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/>.
28 * @package GaletteTests
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
34 * @link http://galette.tuxfamily.org
38 namespace Galette\Core\test\units
;
43 * Preferences tests class
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
54 class Preferences
extends atoum
56 private $preferences = null;
59 private $mocked_router;
64 * @param string $method Calling method
68 public function beforeTestMethod($method)
70 $this->mocked_router
= new \mock\Slim\
Router();
71 $this->calling($this->mocked_router
)->pathFor
= function ($name, $params) {
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'] = '';
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
);
88 $router = $this->mocked_router
;
94 * @param string $method Calling method
98 public function afterTestMethod($method)
100 if (TYPE_DB
=== 'mysql') {
101 $this->array($this->zdb
->getWarnings())->isIdenticalTo([]);
104 $delete = $this->zdb
->delete(\Galette\Entity\Social
::TABLE
);
105 $this->zdb
->execute($delete);
109 * Test preferences initialization
113 public function testInstallInit()
115 $result = $this->preferences
->installInit(
118 password_hash('da_secret', PASSWORD_BCRYPT
)
120 $this->boolean($result)->isTrue();
122 //new object with values loaded from database to compare
123 $prefs = new \Galette\Core\
Preferences($this->zdb
);
125 foreach ($prefs->getDefaults() as $key => $expected) {
126 $value = $prefs->$key;
129 case 'pref_admin_login':
130 $this->variable($value)->isIdenticalTo('da_admin');
132 case 'pref_admin_pass':
133 $pw_checked = password_verify('da_secret', $value);
134 $this->boolean($pw_checked)->isTrue();
137 $this->variable($value)->isIdenticalTo('en_US');
139 case 'pref_card_year':
140 $this->variable($value)->isIdenticalTo(date('Y'));
143 $this->variable($value)->isEqualTo($expected);
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();
154 $slogan = 'One Galette to rule them all';
155 $prefs->pref_slogan
= $slogan;
156 $check = $prefs->pref_slogan
;
157 $this->string($check)->isIdenticalTo($slogan);
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();
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';
173 $expected = "Galette\nPalais des Papes\nAu milieu\n84000 Avignon - France";
174 $address = $this->preferences
->getPostalAddress();
176 $this->variable($address)->isIdenticalTo($expected);
178 $slogan = $this->preferences
->pref_slogan
;
179 $this->variable($slogan)->isEqualTo('');
181 $slogan = 'One Galette to rule them all';
182 $this->preferences
->pref_slogan
= $slogan;
183 $result = $this->preferences
->store();
185 $this->boolean($result)->isTrue();
187 $prefs = new \Galette\Core\
Preferences($this->zdb
);
188 $check_slogan = $prefs->pref_slogan
;
189 $this->variable($check_slogan)->isEqualTo($slogan);
191 //reset database value...
192 $this->preferences
->pref_slogan
= '';
193 $this->preferences
->store();
201 public function testFieldsNames()
203 $this->preferences
->load();
204 $fields_names = $this->preferences
->getFieldsNames();
205 $expected = array_keys($this->preferences
->getDefaults());
210 $this->array($fields_names)->isIdenticalTo($expected);
214 * Test preferences updating when some are missing
218 public function testUpdate()
220 $delete = $this->zdb
->delete(\Galette\Core\Preferences
::TABLE
);
223 \Galette\Core\Preferences
::PK
=> 'pref_footer'
226 $this->zdb
->execute($delete);
228 $delete = $this->zdb
->delete(\Galette\Core\Preferences
::TABLE
);
231 \Galette\Core\Preferences
::PK
=> 'pref_new_contrib_script'
234 $this->zdb
->execute($delete);
236 $this->preferences
->load();
237 $footer = $this->preferences
->pref_footer
;
238 $new_contrib_script = $this->preferences
->pref_new_contrib_script
;
240 $this->boolean($footer)->isFalse();
241 $this->boolean($new_contrib_script)->isFalse();
243 $prefs = new \Galette\Core\
Preferences($this->zdb
);
244 $footer = $prefs->pref_footer
;
245 $new_contrib_script = $prefs->pref_new_contrib_script
;
247 $this->variable($footer)->isIdenticalTo('');
248 $this->variable($new_contrib_script)->isIdenticalTo('');
252 * Test public pages visibility
256 public function testPublicPagesVisibility()
258 $this->preferences
->load();
260 $visibility = $this->preferences
->pref_publicpages_visibility
;
261 $this->variable($visibility)->isEqualTo(
262 \Galette\Core\Preferences
::PUBLIC_PAGES_VISIBILITY_RESTRICTED
265 $anon_login = new \Galette\Core\
Login(
267 new \Galette\Core\
I18n()
270 $admin_login = new \mock\Galette\Core\
Login(
272 new \Galette\Core\
I18n()
274 $this->calling($admin_login)->isAdmin
= true;
276 $user_login = new \mock\Galette\Core\
Login(
278 new \Galette\Core\
I18n()
280 $this->calling($user_login)->isUp2Date
= true;
282 $visible = $this->preferences
->showPublicPages($anon_login);
283 $this->boolean($visible)->isFalse();
285 $visible = $this->preferences
->showPublicPages($admin_login);
286 $this->boolean($visible)->isTrue();
288 $visible = $this->preferences
->showPublicPages($user_login);
289 $this->boolean($visible)->isTrue();
291 $this->preferences
->pref_publicpages_visibility
292 = \Galette\Core\Preferences
::PUBLIC_PAGES_VISIBILITY_PUBLIC
;
294 $visible = $this->preferences
->showPublicPages($anon_login);
295 $this->boolean($visible)->isTrue();
297 $visible = $this->preferences
->showPublicPages($admin_login);
298 $this->boolean($visible)->isTrue();
300 $visible = $this->preferences
->showPublicPages($user_login);
301 $this->boolean($visible)->isTrue();
303 $this->preferences
->pref_publicpages_visibility
304 = \Galette\Core\Preferences
::PUBLIC_PAGES_VISIBILITY_PRIVATE
;
306 $visible = $this->preferences
->showPublicPages($anon_login);
307 $this->boolean($visible)->isFalse();
309 $visible = $this->preferences
->showPublicPages($admin_login);
310 $this->boolean($visible)->isTrue();
312 $visible = $this->preferences
->showPublicPages($user_login);
313 $this->boolean($visible)->isFalse();
317 * Data provider for cards sizes tests
321 protected function sizesProvider()
325 15, //vertical margin
326 20, //horizontal margin
327 5, //vertical spacing
328 10, //horizontal spacing
329 0 //expected number of warnings
332 20, //horizontal margin
333 11, //vertical spacing
334 10, //horizontal spacing
335 0 //expected number of warnings
336 ], [ //vertical overflow
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
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
365 * Test checkCardsSizes
367 * @dataProvider sizesProvider
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
377 public function testCheckCardsSizes($vm, $hm, $vs, $hs, $count)
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);
387 * Data provider for colors
391 protected function colorsProvider(): array
396 'color' => '#f0f0f0',
397 'expected' => '#f0f0f0'
400 'color' => '#f0f0f0f0',
401 'expected' => '#FFFFFF'
405 'expected' => '#f0f0f0'
409 'expected' => '#FFFFFF'
418 * @dataProvider colorsProvider
420 * @param string $prop Property to be set
421 * @param string $color Color to set
422 * @param string $expected Expected color
426 public function testColors($prop, $color, $expected)
428 $prop = 'pref_card_' . $prop;
429 $this->preferences
->$prop = $color;
430 $this->string($this->preferences
->$prop)->isIdenticalTo($expected);
434 * Test social networks
438 public function testSocials()
441 foreach ($this->preferences
->getDefaults() as $key => $value) {
442 $preferences[$key] = $value;
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'
454 //will create 2 social networks in table
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
466 $post = array_merge($preferences, $post);
468 $this->boolean($this->preferences
->check($post, $this->login
))->isTrue(print_r($this->preferences
->getErrors(), true));
469 $this->boolean($this->preferences
->store())->isTrue();
471 $socials = \Galette\Entity\Social
::getListForMember(null);
472 $this->array($socials)->hasSize(2);
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);
479 //create one new social network
481 'social_new_type_1' => \Galette\Entity\Social
::FACEBOOK
,
482 'social_new_value_1' => 'Galette does not have facebook',
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' : '');
490 $post = array_merge($preferences, $post);
492 $this->boolean($this->preferences
->check($post, $this->login
))->isTrue(print_r($this->preferences
->getErrors(), true));
493 $this->boolean($this->preferences
->store())->isTrue();
495 $socials = \Galette\Entity\Social
::getListForMember(null);
496 $this->array($socials)->hasSize(3);
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');
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');
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');
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
;
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();
526 $socials = \Galette\Entity\Social
::getListForMember(null);
527 $this->array($socials)->hasSize(2);
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);
535 * Test email signature
539 public function testGetMailSignature()
541 $this->string($this->preferences
->getMailSignature())->isIdenticalTo("\r\n-- \r\nGalette\r\n\r\n");
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");
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");
550 $social = new \Galette\Entity\
Social($this->zdb
);
553 ->setType(\Galette\Entity\Social
::MASTODON
)
554 ->setUrl('https://framapiaf.org/@galette')
555 ->setLinkedMember(null)
558 $this->array(\Galette\Entity\Social
::getListForMember(null, \Galette\Entity\Social
::MASTODON
))->hasSize(1);
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");
563 $social = new \Galette\Entity\
Social($this->zdb
);
566 ->setType(\Galette\Entity\Social
::MASTODON
)
567 ->setUrl('Galette mastodon URL - the return')
568 ->setLinkedMember(null)
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");
580 public function testGetLegend()
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}/'
591 $social = new \Galette\Entity\
Social($this->zdb
);
594 ->setType('mynewtype')
595 ->setUrl('Galette specific social network URL')
596 ->setLinkedMember(null)
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}/'