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 PHPMailer\PHPMailer\PHPMailer
;
40 use PHPUnit\Framework\TestCase
;
43 * Preferences tests class
47 * @package GaletteTests
48 * @author Johan Cwiklinski <johan@x-tnd.be>
49 * @copyright 2013-2024 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 TestCase
56 private ?\Galette\Core\Preferences
$preferences = null;
57 private \Galette\Core\Db
$zdb;
58 private \Galette\Core\Login
$login;
65 public function setUp(): void
67 $gapp = new \Galette\Core\
SlimApp();
68 $app = $gapp->getApp();
69 $app->addRoutingMiddleware();
71 $plugins = new \Galette\Core\
Plugins();
72 require GALETTE_BASE_PATH
. '/includes/dependencies.php';
73 $container = $app->getContainer();
74 $_SERVER['HTTP_HOST'] = '';
76 $this->zdb
= $container->get('zdb');
77 $this->login
= $container->get('login');
78 $this->preferences
= $container->get('preferences');
81 $routeparser = $container->get(\Slim\Routing\RouteParser
::class);
83 $authenticate = new \Galette\Middleware\
Authenticate($container);
84 require GALETTE_ROOT
. 'includes/routes/main.routes.php';
85 require GALETTE_ROOT
. 'includes/routes/authentication.routes.php';
93 public function tearDown(): void
95 if (TYPE_DB
=== 'mysql') {
96 $this->assertSame([], $this->zdb
->getWarnings());
99 $delete = $this->zdb
->delete(\Galette\Entity\Social
::TABLE
);
100 $this->zdb
->execute($delete);
104 * Test preferences initialization
108 public function testInstallInit()
110 $result = $this->preferences
->installInit(
113 password_hash('da_secret', PASSWORD_BCRYPT
)
115 $this->assertTrue($result);
117 //new object with values loaded from database to compare
118 $prefs = new \Galette\Core\
Preferences($this->zdb
);
120 foreach ($prefs->getDefaults() as $key => $expected) {
121 $value = $prefs->$key;
124 case 'pref_admin_login':
125 $this->assertSame('da_admin', $value);
127 case 'pref_admin_pass':
128 $pw_checked = password_verify('da_secret', $value);
129 $this->assertTrue($pw_checked);
132 $this->assertSame('en_US', $value);
134 case 'pref_card_year':
135 $this->assertSame(date('Y'), $value);
138 $this->assertEquals($expected, $value);
143 //tru to set and get a non existent value
144 $prefs->doesnotexists
= 'that *does* not exists.';
145 $false_result = $prefs->doesnotexists
;
146 $this->assertFalse($false_result);
149 $slogan = 'One Galette to rule them all';
150 $prefs->pref_slogan
= $slogan;
151 $check = $prefs->pref_slogan
;
152 $this->assertSame($slogan, $check);
155 $new_pass = 'anoth3er_s3cr3t';
156 $prefs->pref_admin_pass
= $new_pass;
157 $pass = $prefs->pref_admin_pass
;
158 $pw_checked = password_verify($new_pass, $pass);
159 $this->assertTrue($pw_checked);
161 $this->preferences
->pref_nom
= 'Galette';
162 $this->preferences
->pref_ville
= 'Avignon';
163 $this->preferences
->pref_cp
= '84000';
164 $this->preferences
->pref_adresse
= 'Palais des Papes';
165 $this->preferences
->pref_adresse2
= 'Au milieu';
166 $this->preferences
->pref_pays
= 'France';
168 $expected = "Galette\nPalais des Papes\nAu milieu\n84000 Avignon - France";
169 $address = $this->preferences
->getPostalAddress();
171 $this->assertSame($expected, $address);
173 $slogan = $this->preferences
->pref_slogan
;
174 $this->assertEquals('', $slogan);
176 $slogan = 'One Galette to rule them all';
177 $this->preferences
->pref_slogan
= $slogan;
178 $result = $this->preferences
->store();
180 $this->assertTrue($result);
182 $prefs = new \Galette\Core\
Preferences($this->zdb
);
183 $check_slogan = $prefs->pref_slogan
;
184 $this->assertEquals($slogan, $check_slogan);
186 //reset database value...
187 $this->preferences
->pref_slogan
= '';
188 $this->preferences
->store();
196 public function testFieldsNames()
198 $this->preferences
->load();
199 $fields_names = $this->preferences
->getFieldsNames();
200 $expected = array_keys($this->preferences
->getDefaults());
205 $this->assertSame($expected, $fields_names);
209 * Test preferences updating when some are missing
213 public function testUpdate()
215 $delete = $this->zdb
->delete(\Galette\Core\Preferences
::TABLE
);
218 \Galette\Core\Preferences
::PK
=> 'pref_footer'
221 $this->zdb
->execute($delete);
223 $delete = $this->zdb
->delete(\Galette\Core\Preferences
::TABLE
);
226 \Galette\Core\Preferences
::PK
=> 'pref_new_contrib_script'
229 $this->zdb
->execute($delete);
231 $this->preferences
->load();
232 $footer = $this->preferences
->pref_footer
;
233 $new_contrib_script = $this->preferences
->pref_new_contrib_script
;
235 $this->assertFalse($footer);
236 $this->assertFalse($new_contrib_script);
238 $prefs = new \Galette\Core\
Preferences($this->zdb
);
239 $footer = $prefs->pref_footer
;
240 $new_contrib_script = $prefs->pref_new_contrib_script
;
242 $this->assertSame('', $footer);
243 $this->assertSame('', $new_contrib_script);
247 * Test public pages visibility
251 public function testPublicPagesVisibility()
253 $this->preferences
->load();
255 $visibility = $this->preferences
->pref_publicpages_visibility
;
257 \Galette\Core\Preferences
::PUBLIC_PAGES_VISIBILITY_RESTRICTED
,
261 $anon_login = new \Galette\Core\
Login(
263 new \Galette\Core\
I18n()
266 $admin_login = $this->getMockBuilder(\Galette\Core\Login
::class)
267 ->setConstructorArgs(array($this->zdb
, new \Galette\Core\
I18n()))
268 ->onlyMethods(array('isAdmin'))
270 $admin_login->method('isAdmin')->willReturn(true);
272 $user_login = $this->getMockBuilder(\Galette\Core\Login
::class)
273 ->setConstructorArgs(array($this->zdb
, new \Galette\Core\
I18n()))
274 ->onlyMethods(array('isUp2Date'))
276 $user_login->method('isUp2Date')->willReturn(true);
278 $visible = $this->preferences
->showPublicPages($anon_login);
279 $this->assertFalse($visible);
281 $visible = $this->preferences
->showPublicPages($admin_login);
282 $this->assertTrue($visible);
284 $visible = $this->preferences
->showPublicPages($user_login);
285 $this->assertTrue($visible);
287 $this->preferences
->pref_publicpages_visibility
288 = \Galette\Core\Preferences
::PUBLIC_PAGES_VISIBILITY_PUBLIC
;
290 $visible = $this->preferences
->showPublicPages($anon_login);
291 $this->assertTrue($visible);
293 $visible = $this->preferences
->showPublicPages($admin_login);
294 $this->assertTrue($visible);
296 $visible = $this->preferences
->showPublicPages($user_login);
297 $this->assertTrue($visible);
299 $this->preferences
->pref_publicpages_visibility
300 = \Galette\Core\Preferences
::PUBLIC_PAGES_VISIBILITY_PRIVATE
;
302 $visible = $this->preferences
->showPublicPages($anon_login);
303 $this->assertFalse($visible);
305 $visible = $this->preferences
->showPublicPages($admin_login);
306 $this->assertTrue($visible);
308 $visible = $this->preferences
->showPublicPages($user_login);
309 $this->assertFalse($visible);
313 * Data provider for cards sizes tests
317 public static function sizesProvider()
321 15, //vertical margin
322 20, //horizontal margin
323 5, //vertical spacing
324 10, //horizontal spacing
325 0 //expected number of warnings
328 20, //horizontal margin
329 11, //vertical spacing
330 10, //horizontal spacing
331 0 //expected number of warnings
332 ], [ //vertical overflow
334 20, //horizontal margin
335 12, //vertical spacing
336 10, //horizontal spacing
337 1 //expected number of warnings
338 ], [//horizontal overflow
339 15, //vertical margin
340 20, //horizontal margin
341 5, //vertical spacing
342 61, //horizontal spacing
343 1 //expected number of warnings
344 ], [//vertical and horizontal overflow
346 20, //horizontal margin
347 12, //vertical spacing
348 61, //horizontal spacing
349 2 //expected number of warnings
350 ], [//vertical overflow
351 17, //vertical margin
352 20, //horizontal margin
353 5, //vertical spacing
354 10, //horizontal spacing
355 1 //expected number of warnings
361 * Test checkCardsSizes
363 * @dataProvider sizesProvider
365 * @param integer $vm Vertical margin
366 * @param integer $hm Horizontal margin
367 * @param integer $vs Vertical spacing
368 * @param integer $hs Horizontal spacing
369 * @param integer $count Number of expected errors
373 public function testCheckCardsSizes($vm, $hm, $vs, $hs, $count)
375 $this->preferences
->pref_card_marges_v
= $vm;
376 $this->preferences
->pref_card_marges_h
= $hm;
377 $this->preferences
->pref_card_vspace
= $vs;
378 $this->preferences
->pref_card_hspace
= $hs;
379 $this->assertCount($count, $this->preferences
->checkCardsSizes());
383 * Data provider for colors
387 public static function colorsProvider(): array
392 'color' => '#f0f0f0',
393 'expected' => '#f0f0f0'
396 'color' => '#f0f0f0f0',
397 'expected' => '#FFFFFF'
401 'expected' => '#f0f0f0'
405 'expected' => '#FFFFFF'
414 * @dataProvider colorsProvider
416 * @param string $prop Property to be set
417 * @param string $color Color to set
418 * @param string $expected Expected color
422 public function testColors($prop, $color, $expected)
424 $prop = 'pref_card_' . $prop;
425 $this->preferences
->$prop = $color;
426 $this->assertSame($expected, $this->preferences
->$prop);
430 * Test social networks
434 public function testSocials()
437 foreach ($this->preferences
->getDefaults() as $key => $value) {
438 $preferences[$key] = $value;
441 $preferences = array_merge($preferences, [
442 'pref_nom' => 'Galette',
443 'pref_ville' => 'Avignon',
444 'pref_cp' => '84000',
445 'pref_adresse' => 'Palais des Papes',
446 'pref_adresse2' => 'Au milieu',
447 'pref_pays' => 'France'
450 //will create 2 social networks in table
452 'notasocial' => 'notasocial', //must be ignored
453 'social_new_type_1' => \Galette\Entity\Social
::MASTODON
,
454 'social_new_value_1' => 'Galette mastodon URL',
455 'social_new_type_2' => \Galette\Entity\Social
::JABBER
,
456 'social_new_value_2' => 'Galette jabber ID',
457 'social_new_type_3' => \Galette\Entity\Social
::FACEBOOK
,
458 'social_new_value_3' => '', //empty value, no entry
459 'social_new_type_4' => \Galette\Entity\Social
::BLOG
, //no value, no entry
462 $post = array_merge($preferences, $post);
465 $this->preferences
->check($post, $this->login
),
466 print_r($this->preferences
->getErrors(), true)
468 $this->assertTrue($this->preferences
->store());
470 $socials = \Galette\Entity\Social
::getListForMember(null);
471 $this->assertCount(2, $socials);
475 \Galette\Entity\Social
::getListForMember(null, \Galette\Entity\Social
::MASTODON
)
479 \Galette\Entity\Social
::getListForMember(null, \Galette\Entity\Social
::JABBER
)
483 \Galette\Entity\Social
::getListForMember(null, \Galette\Entity\Social
::FACEBOOK
)
487 \Galette\Entity\Social
::getListForMember(null, \Galette\Entity\Social
::BLOG
)
490 //create one new social network
492 'social_new_type_1' => \Galette\Entity\Social
::FACEBOOK
,
493 'social_new_value_1' => 'Galette does not have facebook',
496 //existing social networks, change jabber ID
497 foreach ($socials as $social) {
498 $post['social_' . $social->id
] = $social->url
. ($social->type
== \Galette\Entity\Social
::JABBER ?
' - modified' : '');
501 $post = array_merge($preferences, $post);
504 $this->preferences
->check($post, $this->login
),
505 print_r($this->preferences
->getErrors(), true)
507 $this->assertTrue($this->preferences
->store());
509 $socials = \Galette\Entity\Social
::getListForMember(null);
510 $this->assertCount(3, $socials);
512 $search = \Galette\Entity\Social
::getListForMember(null, \Galette\Entity\Social
::MASTODON
);
513 $this->assertCount(1, $search);
514 $masto = array_pop($search);
515 $this->assertSame('Galette mastodon URL', $masto->url
);
517 $search = \Galette\Entity\Social
::getListForMember(null, \Galette\Entity\Social
::JABBER
);
518 $this->assertCount(1, $search);
519 $jabber = array_pop($search);
520 $this->assertSame('Galette jabber ID - modified', $jabber->url
);
522 $search = \Galette\Entity\Social
::getListForMember(null, \Galette\Entity\Social
::FACEBOOK
);
523 $this->assertCount(1, $search);
524 $facebook = array_pop($search);
525 $this->assertSame('Galette does not have facebook', $facebook->url
);
529 //existing social networks, drop mastodon
530 foreach ($socials as $social) {
531 if ($social->type
!= \Galette\Entity\Social
::MASTODON
) {
532 $post['social_' . $social->id
] = $social->url
;
536 $post = array_merge($preferences, $post);
538 $this->preferences
->check($post, $this->login
),
539 print_r($this->preferences
->getErrors(), true)
541 $this->assertTrue($this->preferences
->store());
543 $socials = \Galette\Entity\Social
::getListForMember(null);
544 $this->assertCount(2, $socials);
548 \Galette\Entity\Social
::getListForMember(null, \Galette\Entity\Social
::MASTODON
)
552 \Galette\Entity\Social
::getListForMember(null, \Galette\Entity\Social
::JABBER
)
556 \Galette\Entity\Social
::getListForMember(null, \Galette\Entity\Social
::FACEBOOK
)
561 * Test email signature
565 public function testGetMailSignature()
567 $mail = new PHPMailer();
568 $this->assertSame("\r\n-- \r\nGalette", $this->preferences
->getMailSignature($mail));
570 $this->preferences
->pref_website
= 'https://galette.eu';
572 "\r\n-- \r\nGalette\r\n\r\nhttps://galette.eu",
573 $this->preferences
->getMailSignature($mail)
577 $this->preferences
->pref_mail_sign
= "{NAME}\r\n\r\n{WEBSITE}\r\n{FACEBOOK}\r\n{TWITTER}\r\n{LINKEDIN}\r\n{VIADEO}";
579 "\r\n-- \r\nGalette\r\n\r\nhttps://galette.eu",
580 $this->preferences
->getMailSignature($mail)
583 $social = new \Galette\Entity\
Social($this->zdb
);
586 ->setType(\Galette\Entity\Social
::MASTODON
)
587 ->setUrl('https://framapiaf.org/@galette')
588 ->setLinkedMember(null)
593 \Galette\Entity\Social
::getListForMember(null, \Galette\Entity\Social
::MASTODON
)
596 $this->preferences
->pref_mail_sign
= "{ASSO_NAME}\r\n\r\n{ASSO_WEBSITE} - {ASSO_SOCIAL_MASTODON}";
598 "\r\n-- \r\nGalette\r\n\r\nhttps://galette.eu - https://framapiaf.org/@galette",
599 $this->preferences
->getMailSignature($mail)
602 $social = new \Galette\Entity\
Social($this->zdb
);
605 ->setType(\Galette\Entity\Social
::MASTODON
)
606 ->setUrl('Galette mastodon URL - the return')
607 ->setLinkedMember(null)
612 \Galette\Entity\Social
::getListForMember(null, \Galette\Entity\Social
::MASTODON
)
615 "\r\n-- \r\nGalette\r\n\r\nhttps://galette.eu - https://framapiaf.org/@galette, Galette mastodon URL - the return",
616 $this->preferences
->getMailSignature($mail)
625 public function testGetLegend()
627 $legend = $this->preferences
->getLegend();
628 $this->assertCount(2, $legend);
629 $this->assertCount(8, $legend['main']['patterns']);
630 $this->assertCount(10, $legend['socials']['patterns']);
633 'title' => __('Mastodon'),
634 'pattern' => '/{ASSO_SOCIAL_MASTODON}/'
636 $legend['socials']['patterns']['asso_social_mastodon']
639 $social = new \Galette\Entity\
Social($this->zdb
);
642 ->setType('mynewtype')
643 ->setUrl('Galette specific social network URL')
644 ->setLinkedMember(null)
648 $legend = $this->preferences
->getLegend();
649 $this->assertCount(2, $legend);
650 $this->assertCount(11, $legend['socials']['patterns']);
651 $this->assertTrue(isset($legend['socials']['patterns']['asso_social_mynewtype']));
654 'title' => 'mynewtype',
655 'pattern' => '/{ASSO_SOCIAL_MYNEWTYPE}/'
657 $legend['socials']['patterns']['asso_social_mynewtype']
666 public function testWebsiteURL(): void
669 foreach ($this->preferences
->getDefaults() as $key => $value) {
670 $preferences[$key] = $value;
673 $post = array_merge($preferences, ['pref_website' => 'https://galette.eu']);
675 $this->preferences
->check($post, $this->login
),
676 print_r($this->preferences
->getErrors(), true)
679 $post = array_merge($preferences, ['pref_website' => 'galette.eu']);
681 $this->preferences
->check($post, $this->login
),
682 print_r($this->preferences
->getErrors(), true)
684 $this->assertSame(['- Invalid website URL.'], $this->preferences
->getErrors());