3 /* vim: set expandtab tabstop=4 shiftwidth=4 softtabstop=4: */
10 * Copyright © 2013-2024 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-2024 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 * @link http://galette.tuxfamily.org
37 namespace Galette\Core\test\units
;
39 use PHPUnit\Framework\TestCase
;
42 * Preferences tests class
46 * @package GaletteTests
47 * @author Johan Cwiklinski <johan@x-tnd.be>
48 * @copyright 2013-2024 The Galette Team
49 * @license http://www.gnu.org/licenses/gpl-3.0.html GPL License 3.0 or (at your option) any later version
50 * @link http://galette.tuxfamily.org
53 class Preferences
extends TestCase
55 private ?\Galette\Core\Preferences
$preferences = null;
56 private \Galette\Core\Db
$zdb;
57 private \Galette\Core\Login
$login;
64 public function setUp(): void
66 $gapp = new \Galette\Core\
SlimApp();
67 $app = $gapp->getApp();
68 $app->addRoutingMiddleware();
70 $plugins = new \Galette\Core\
Plugins();
71 require GALETTE_BASE_PATH
. '/includes/dependencies.php';
72 $container = $app->getContainer();
73 $_SERVER['HTTP_HOST'] = '';
75 $this->zdb
= $container->get('zdb');
76 $this->login
= $container->get('login');
77 $this->preferences
= $container->get('preferences');
80 $routeparser = $container->get(\Slim\Routing\RouteParser
::class);
82 $authenticate = new \Galette\Middleware\
Authenticate($container);
83 require GALETTE_ROOT
. 'includes/routes/main.routes.php';
84 require GALETTE_ROOT
. 'includes/routes/authentication.routes.php';
92 public function tearDown(): void
94 if (TYPE_DB
=== 'mysql') {
95 $this->assertSame([], $this->zdb
->getWarnings());
98 $delete = $this->zdb
->delete(\Galette\Entity\Social
::TABLE
);
99 $this->zdb
->execute($delete);
103 * Test preferences initialization
107 public function testInstallInit()
109 $result = $this->preferences
->installInit(
112 password_hash('da_secret', PASSWORD_BCRYPT
)
114 $this->assertTrue($result);
116 //new object with values loaded from database to compare
117 $prefs = new \Galette\Core\
Preferences($this->zdb
);
119 foreach ($prefs->getDefaults() as $key => $expected) {
120 $value = $prefs->$key;
123 case 'pref_admin_login':
124 $this->assertSame('da_admin', $value);
126 case 'pref_admin_pass':
127 $pw_checked = password_verify('da_secret', $value);
128 $this->assertTrue($pw_checked);
131 $this->assertSame('en_US', $value);
133 case 'pref_card_year':
134 $this->assertSame(date('Y'), $value);
137 $this->assertEquals($expected, $value);
142 //tru to set and get a non existent value
143 $prefs->doesnotexists
= 'that *does* not exists.';
144 $false_result = $prefs->doesnotexists
;
145 $this->assertFalse($false_result);
148 $slogan = 'One Galette to rule them all';
149 $prefs->pref_slogan
= $slogan;
150 $check = $prefs->pref_slogan
;
151 $this->assertSame($slogan, $check);
154 $new_pass = 'anoth3er_s3cr3t';
155 $prefs->pref_admin_pass
= $new_pass;
156 $pass = $prefs->pref_admin_pass
;
157 $pw_checked = password_verify($new_pass, $pass);
158 $this->assertTrue($pw_checked);
160 $this->preferences
->pref_nom
= 'Galette';
161 $this->preferences
->pref_ville
= 'Avignon';
162 $this->preferences
->pref_cp
= '84000';
163 $this->preferences
->pref_adresse
= 'Palais des Papes';
164 $this->preferences
->pref_adresse2
= 'Au milieu';
165 $this->preferences
->pref_pays
= 'France';
167 $expected = "Galette\nPalais des Papes\nAu milieu\n84000 Avignon - France";
168 $address = $this->preferences
->getPostalAddress();
170 $this->assertSame($expected, $address);
172 $slogan = $this->preferences
->pref_slogan
;
173 $this->assertEquals('', $slogan);
175 $slogan = 'One Galette to rule them all';
176 $this->preferences
->pref_slogan
= $slogan;
177 $result = $this->preferences
->store();
179 $this->assertTrue($result);
181 $prefs = new \Galette\Core\
Preferences($this->zdb
);
182 $check_slogan = $prefs->pref_slogan
;
183 $this->assertEquals($slogan, $check_slogan);
185 //reset database value...
186 $this->preferences
->pref_slogan
= '';
187 $this->preferences
->store();
195 public function testFieldsNames()
197 $this->preferences
->load();
198 $fields_names = $this->preferences
->getFieldsNames();
199 $expected = array_keys($this->preferences
->getDefaults());
204 $this->assertSame($expected, $fields_names);
208 * Test preferences updating when some are missing
212 public function testUpdate()
214 $delete = $this->zdb
->delete(\Galette\Core\Preferences
::TABLE
);
217 \Galette\Core\Preferences
::PK
=> 'pref_footer'
220 $this->zdb
->execute($delete);
222 $delete = $this->zdb
->delete(\Galette\Core\Preferences
::TABLE
);
225 \Galette\Core\Preferences
::PK
=> 'pref_new_contrib_script'
228 $this->zdb
->execute($delete);
230 $this->preferences
->load();
231 $footer = $this->preferences
->pref_footer
;
232 $new_contrib_script = $this->preferences
->pref_new_contrib_script
;
234 $this->assertFalse($footer);
235 $this->assertFalse($new_contrib_script);
237 $prefs = new \Galette\Core\
Preferences($this->zdb
);
238 $footer = $prefs->pref_footer
;
239 $new_contrib_script = $prefs->pref_new_contrib_script
;
241 $this->assertSame('', $footer);
242 $this->assertSame('', $new_contrib_script);
246 * Test public pages visibility
250 public function testPublicPagesVisibility()
252 $this->preferences
->load();
254 $visibility = $this->preferences
->pref_publicpages_visibility
;
256 \Galette\Core\Preferences
::PUBLIC_PAGES_VISIBILITY_RESTRICTED
,
260 $anon_login = new \Galette\Core\
Login(
262 new \Galette\Core\
I18n()
265 $admin_login = $this->getMockBuilder(\Galette\Core\Login
::class)
266 ->setConstructorArgs(array($this->zdb
, new \Galette\Core\
I18n()))
267 ->onlyMethods(array('isAdmin'))
269 $admin_login->method('isAdmin')->willReturn(true);
271 $user_login = $this->getMockBuilder(\Galette\Core\Login
::class)
272 ->setConstructorArgs(array($this->zdb
, new \Galette\Core\
I18n()))
273 ->onlyMethods(array('isUp2Date'))
275 $user_login->method('isUp2Date')->willReturn(true);
277 $visible = $this->preferences
->showPublicPages($anon_login);
278 $this->assertFalse($visible);
280 $visible = $this->preferences
->showPublicPages($admin_login);
281 $this->assertTrue($visible);
283 $visible = $this->preferences
->showPublicPages($user_login);
284 $this->assertTrue($visible);
286 $this->preferences
->pref_publicpages_visibility
287 = \Galette\Core\Preferences
::PUBLIC_PAGES_VISIBILITY_PUBLIC
;
289 $visible = $this->preferences
->showPublicPages($anon_login);
290 $this->assertTrue($visible);
292 $visible = $this->preferences
->showPublicPages($admin_login);
293 $this->assertTrue($visible);
295 $visible = $this->preferences
->showPublicPages($user_login);
296 $this->assertTrue($visible);
298 $this->preferences
->pref_publicpages_visibility
299 = \Galette\Core\Preferences
::PUBLIC_PAGES_VISIBILITY_PRIVATE
;
301 $visible = $this->preferences
->showPublicPages($anon_login);
302 $this->assertFalse($visible);
304 $visible = $this->preferences
->showPublicPages($admin_login);
305 $this->assertTrue($visible);
307 $visible = $this->preferences
->showPublicPages($user_login);
308 $this->assertFalse($visible);
312 * Data provider for cards sizes tests
316 public static function sizesProvider()
320 15, //vertical margin
321 20, //horizontal margin
322 5, //vertical spacing
323 10, //horizontal spacing
324 0 //expected number of warnings
327 20, //horizontal margin
328 11, //vertical spacing
329 10, //horizontal spacing
330 0 //expected number of warnings
331 ], [ //vertical overflow
333 20, //horizontal margin
334 12, //vertical spacing
335 10, //horizontal spacing
336 1 //expected number of warnings
337 ], [//horizontal overflow
338 15, //vertical margin
339 20, //horizontal margin
340 5, //vertical spacing
341 61, //horizontal spacing
342 1 //expected number of warnings
343 ], [//vertical and horizontal overflow
345 20, //horizontal margin
346 12, //vertical spacing
347 61, //horizontal spacing
348 2 //expected number of warnings
349 ], [//vertical overflow
350 17, //vertical margin
351 20, //horizontal margin
352 5, //vertical spacing
353 10, //horizontal spacing
354 1 //expected number of warnings
360 * Test checkCardsSizes
362 * @dataProvider sizesProvider
364 * @param integer $vm Vertical margin
365 * @param integer $hm Horizontal margin
366 * @param integer $vs Vertical spacing
367 * @param integer $hs Horizontal spacing
368 * @param integer $count Number of expected errors
372 public function testCheckCardsSizes($vm, $hm, $vs, $hs, $count)
374 $this->preferences
->pref_card_marges_v
= $vm;
375 $this->preferences
->pref_card_marges_h
= $hm;
376 $this->preferences
->pref_card_vspace
= $vs;
377 $this->preferences
->pref_card_hspace
= $hs;
378 $this->assertCount($count, $this->preferences
->checkCardsSizes());
382 * Data provider for colors
386 public static function colorsProvider(): array
391 'color' => '#f0f0f0',
392 'expected' => '#f0f0f0'
395 'color' => '#f0f0f0f0',
396 'expected' => '#FFFFFF'
400 'expected' => '#f0f0f0'
404 'expected' => '#FFFFFF'
413 * @dataProvider colorsProvider
415 * @param string $prop Property to be set
416 * @param string $color Color to set
417 * @param string $expected Expected color
421 public function testColors($prop, $color, $expected)
423 $prop = 'pref_card_' . $prop;
424 $this->preferences
->$prop = $color;
425 $this->assertSame($expected, $this->preferences
->$prop);
429 * Test social networks
433 public function testSocials()
436 foreach ($this->preferences
->getDefaults() as $key => $value) {
437 $preferences[$key] = $value;
440 $preferences = array_merge($preferences, [
441 'pref_nom' => 'Galette',
442 'pref_ville' => 'Avignon',
443 'pref_cp' => '84000',
444 'pref_adresse' => 'Palais des Papes',
445 'pref_adresse2' => 'Au milieu',
446 'pref_pays' => 'France'
449 //will create 2 social networks in table
451 'notasocial' => 'notasocial', //must be ignored
452 'social_new_type_1' => \Galette\Entity\Social
::MASTODON
,
453 'social_new_value_1' => 'Galette mastodon URL',
454 'social_new_type_2' => \Galette\Entity\Social
::JABBER
,
455 'social_new_value_2' => 'Galette jabber ID',
456 'social_new_type_3' => \Galette\Entity\Social
::FACEBOOK
,
457 'social_new_value_3' => '', //empty value, no entry
458 'social_new_type_4' => \Galette\Entity\Social
::BLOG
, //no value, no entry
461 $post = array_merge($preferences, $post);
464 $this->preferences
->check($post, $this->login
),
465 print_r($this->preferences
->getErrors(), true)
467 $this->assertTrue($this->preferences
->store());
469 $socials = \Galette\Entity\Social
::getListForMember(null);
470 $this->assertCount(2, $socials);
474 \Galette\Entity\Social
::getListForMember(null, \Galette\Entity\Social
::MASTODON
)
478 \Galette\Entity\Social
::getListForMember(null, \Galette\Entity\Social
::JABBER
)
482 \Galette\Entity\Social
::getListForMember(null, \Galette\Entity\Social
::FACEBOOK
)
486 \Galette\Entity\Social
::getListForMember(null, \Galette\Entity\Social
::BLOG
)
489 //create one new social network
491 'social_new_type_1' => \Galette\Entity\Social
::FACEBOOK
,
492 'social_new_value_1' => 'Galette does not have facebook',
495 //existing social networks, change jabber ID
496 foreach ($socials as $social) {
497 $post['social_' . $social->id
] = $social->url
. ($social->type
== \Galette\Entity\Social
::JABBER ?
' - modified' : '');
500 $post = array_merge($preferences, $post);
503 $this->preferences
->check($post, $this->login
),
504 print_r($this->preferences
->getErrors(), true)
506 $this->assertTrue($this->preferences
->store());
508 $socials = \Galette\Entity\Social
::getListForMember(null);
509 $this->assertCount(3, $socials);
511 $search = \Galette\Entity\Social
::getListForMember(null, \Galette\Entity\Social
::MASTODON
);
512 $this->assertCount(1, $search);
513 $masto = array_pop($search);
514 $this->assertSame('Galette mastodon URL', $masto->url
);
516 $search = \Galette\Entity\Social
::getListForMember(null, \Galette\Entity\Social
::JABBER
);
517 $this->assertCount(1, $search);
518 $jabber = array_pop($search);
519 $this->assertSame('Galette jabber ID - modified', $jabber->url
);
521 $search = \Galette\Entity\Social
::getListForMember(null, \Galette\Entity\Social
::FACEBOOK
);
522 $this->assertCount(1, $search);
523 $facebook = array_pop($search);
524 $this->assertSame('Galette does not have facebook', $facebook->url
);
528 //existing social networks, drop mastodon
529 foreach ($socials as $social) {
530 if ($social->type
!= \Galette\Entity\Social
::MASTODON
) {
531 $post['social_' . $social->id
] = $social->url
;
535 $post = array_merge($preferences, $post);
537 $this->preferences
->check($post, $this->login
),
538 print_r($this->preferences
->getErrors(), true)
540 $this->assertTrue($this->preferences
->store());
542 $socials = \Galette\Entity\Social
::getListForMember(null);
543 $this->assertCount(2, $socials);
547 \Galette\Entity\Social
::getListForMember(null, \Galette\Entity\Social
::MASTODON
)
551 \Galette\Entity\Social
::getListForMember(null, \Galette\Entity\Social
::JABBER
)
555 \Galette\Entity\Social
::getListForMember(null, \Galette\Entity\Social
::FACEBOOK
)
560 * Test email signature
564 public function testGetMailSignature()
566 $this->assertSame("\r\n-- \r\nGalette\r\n\r\n", $this->preferences
->getMailSignature());
568 $this->preferences
->pref_website
= 'https://galette.eu';
569 $this->assertSame("\r\n-- \r\nGalette\r\n\r\nhttps://galette.eu", $this->preferences
->getMailSignature());
572 $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}";
573 $this->assertSame("\r\n-- \r\nGalette\r\n\r\nhttps://galette.eu", $this->preferences
->getMailSignature());
575 $social = new \Galette\Entity\
Social($this->zdb
);
578 ->setType(\Galette\Entity\Social
::MASTODON
)
579 ->setUrl('https://framapiaf.org/@galette')
580 ->setLinkedMember(null)
585 \Galette\Entity\Social
::getListForMember(null, \Galette\Entity\Social
::MASTODON
)
588 $this->preferences
->pref_mail_sign
= "{ASSO_NAME}\r\n\r\n{ASSO_WEBSITE} - {ASSO_SOCIAL_MASTODON}";
589 $this->assertSame("\r\n-- \r\nGalette\r\n\r\nhttps://galette.eu - https://framapiaf.org/@galette", $this->preferences
->getMailSignature());
591 $social = new \Galette\Entity\
Social($this->zdb
);
594 ->setType(\Galette\Entity\Social
::MASTODON
)
595 ->setUrl('Galette mastodon URL - the return')
596 ->setLinkedMember(null)
601 \Galette\Entity\Social
::getListForMember(null, \Galette\Entity\Social
::MASTODON
)
603 $this->assertSame("\r\n-- \r\nGalette\r\n\r\nhttps://galette.eu - https://framapiaf.org/@galette, Galette mastodon URL - the return", $this->preferences
->getMailSignature());
611 public function testGetLegend()
613 $legend = $this->preferences
->getLegend();
614 $this->assertCount(2, $legend);
615 $this->assertCount(8, $legend['main']['patterns']);
616 $this->assertCount(10, $legend['socials']['patterns']);
619 'title' => __('Mastodon'),
620 'pattern' => '/{ASSO_SOCIAL_MASTODON}/'
622 $legend['socials']['patterns']['asso_social_mastodon']
625 $social = new \Galette\Entity\
Social($this->zdb
);
628 ->setType('mynewtype')
629 ->setUrl('Galette specific social network URL')
630 ->setLinkedMember(null)
634 $legend = $this->preferences
->getLegend();
635 $this->assertCount(2, $legend);
636 $this->assertCount(11, $legend['socials']['patterns']);
637 $this->assertTrue(isset($legend['socials']['patterns']['asso_social_mynewtype']));
640 'title' => 'mynewtype',
641 'pattern' => '/{ASSO_SOCIAL_MYNEWTYPE}/'
643 $legend['socials']['patterns']['asso_social_mynewtype']