]> git.agnieray.net Git - galette.git/blob - tests/Galette/Core/tests/units/Preferences.php
Add pre_footer in replacements; closes #1808
[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-2024 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-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
34 * @since 2013-10-19
35 */
36
37 namespace Galette\Core\test\units;
38
39 use PHPMailer\PHPMailer\PHPMailer;
40 use PHPUnit\Framework\TestCase;
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-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
52 * @since 2013-01-13
53 */
54 class Preferences extends TestCase
55 {
56 private ?\Galette\Core\Preferences $preferences = null;
57 private \Galette\Core\Db $zdb;
58 private \Galette\Core\Login $login;
59
60 /**
61 * Set up tests
62 *
63 * @return void
64 */
65 public function setUp(): void
66 {
67 $gapp = new \Galette\Core\SlimApp();
68 $app = $gapp->getApp();
69 $app->addRoutingMiddleware();
70
71 $plugins = new \Galette\Core\Plugins();
72 require GALETTE_BASE_PATH . '/includes/dependencies.php';
73 $container = $app->getContainer();
74 $_SERVER['HTTP_HOST'] = '';
75
76 $this->zdb = $container->get('zdb');
77 $this->login = $container->get('login');
78 $this->preferences = $container->get('preferences');
79
80 global $routeparser;
81 $routeparser = $container->get(\Slim\Routing\RouteParser::class);
82
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';
86 }
87
88 /**
89 * Tear down tests
90 *
91 * @return void
92 */
93 public function tearDown(): void
94 {
95 if (TYPE_DB === 'mysql') {
96 $this->assertSame([], $this->zdb->getWarnings());
97 }
98
99 $delete = $this->zdb->delete(\Galette\Entity\Social::TABLE);
100 $this->zdb->execute($delete);
101 }
102
103 /**
104 * Test preferences initialization
105 *
106 * @return void
107 */
108 public function testInstallInit()
109 {
110 $result = $this->preferences->installInit(
111 'en_US',
112 'da_admin',
113 password_hash('da_secret', PASSWORD_BCRYPT)
114 );
115 $this->assertTrue($result);
116
117 //new object with values loaded from database to compare
118 $prefs = new \Galette\Core\Preferences($this->zdb);
119
120 foreach ($prefs->getDefaults() as $key => $expected) {
121 $value = $prefs->$key;
122
123 switch ($key) {
124 case 'pref_admin_login':
125 $this->assertSame('da_admin', $value);
126 break;
127 case 'pref_admin_pass':
128 $pw_checked = password_verify('da_secret', $value);
129 $this->assertTrue($pw_checked);
130 break;
131 case 'pref_lang':
132 $this->assertSame('en_US', $value);
133 break;
134 case 'pref_card_year':
135 $this->assertSame(date('Y'), $value);
136 break;
137 default:
138 $this->assertEquals($expected, $value);
139 break;
140 }
141 }
142
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);
147
148 //change slogan
149 $slogan = 'One Galette to rule them all';
150 $prefs->pref_slogan = $slogan;
151 $check = $prefs->pref_slogan;
152 $this->assertSame($slogan, $check);
153
154 //change password
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);
160
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';
167
168 $expected = "Galette\nPalais des Papes\nAu milieu\n84000 Avignon - France";
169 $address = $this->preferences->getPostalAddress();
170
171 $this->assertSame($expected, $address);
172
173 $slogan = $this->preferences->pref_slogan;
174 $this->assertEquals('', $slogan);
175
176 $slogan = 'One Galette to rule them all';
177 $this->preferences->pref_slogan = $slogan;
178 $result = $this->preferences->store();
179
180 $this->assertTrue($result);
181
182 $prefs = new \Galette\Core\Preferences($this->zdb);
183 $check_slogan = $prefs->pref_slogan;
184 $this->assertEquals($slogan, $check_slogan);
185
186 //reset database value...
187 $this->preferences->pref_slogan = '';
188 $this->preferences->store();
189 }
190
191 /**
192 * Test fields names
193 *
194 * @return void
195 */
196 public function testFieldsNames()
197 {
198 $this->preferences->load();
199 $fields_names = $this->preferences->getFieldsNames();
200 $expected = array_keys($this->preferences->getDefaults());
201
202 sort($fields_names);
203 sort($expected);
204
205 $this->assertSame($expected, $fields_names);
206 }
207
208 /**
209 * Test preferences updating when some are missing
210 *
211 * @return void
212 */
213 public function testUpdate()
214 {
215 $delete = $this->zdb->delete(\Galette\Core\Preferences::TABLE);
216 $delete->where(
217 array(
218 \Galette\Core\Preferences::PK => 'pref_footer'
219 )
220 );
221 $this->zdb->execute($delete);
222
223 $delete = $this->zdb->delete(\Galette\Core\Preferences::TABLE);
224 $delete->where(
225 array(
226 \Galette\Core\Preferences::PK => 'pref_new_contrib_script'
227 )
228 );
229 $this->zdb->execute($delete);
230
231 $this->preferences->load();
232 $footer = $this->preferences->pref_footer;
233 $new_contrib_script = $this->preferences->pref_new_contrib_script;
234
235 $this->assertFalse($footer);
236 $this->assertFalse($new_contrib_script);
237
238 $prefs = new \Galette\Core\Preferences($this->zdb);
239 $footer = $prefs->pref_footer;
240 $new_contrib_script = $prefs->pref_new_contrib_script;
241
242 $this->assertSame('', $footer);
243 $this->assertSame('', $new_contrib_script);
244 }
245
246 /**
247 * Test public pages visibility
248 *
249 * @return void
250 */
251 public function testPublicPagesVisibility()
252 {
253 $this->preferences->load();
254
255 $visibility = $this->preferences->pref_publicpages_visibility;
256 $this->assertEquals(
257 \Galette\Core\Preferences::PUBLIC_PAGES_VISIBILITY_RESTRICTED,
258 $visibility
259 );
260
261 $anon_login = new \Galette\Core\Login(
262 $this->zdb,
263 new \Galette\Core\I18n()
264 );
265
266 $admin_login = $this->getMockBuilder(\Galette\Core\Login::class)
267 ->setConstructorArgs(array($this->zdb, new \Galette\Core\I18n()))
268 ->onlyMethods(array('isAdmin'))
269 ->getMock();
270 $admin_login->method('isAdmin')->willReturn(true);
271
272 $user_login = $this->getMockBuilder(\Galette\Core\Login::class)
273 ->setConstructorArgs(array($this->zdb, new \Galette\Core\I18n()))
274 ->onlyMethods(array('isUp2Date'))
275 ->getMock();
276 $user_login->method('isUp2Date')->willReturn(true);
277
278 $visible = $this->preferences->showPublicPages($anon_login);
279 $this->assertFalse($visible);
280
281 $visible = $this->preferences->showPublicPages($admin_login);
282 $this->assertTrue($visible);
283
284 $visible = $this->preferences->showPublicPages($user_login);
285 $this->assertTrue($visible);
286
287 $this->preferences->pref_publicpages_visibility
288 = \Galette\Core\Preferences::PUBLIC_PAGES_VISIBILITY_PUBLIC;
289
290 $visible = $this->preferences->showPublicPages($anon_login);
291 $this->assertTrue($visible);
292
293 $visible = $this->preferences->showPublicPages($admin_login);
294 $this->assertTrue($visible);
295
296 $visible = $this->preferences->showPublicPages($user_login);
297 $this->assertTrue($visible);
298
299 $this->preferences->pref_publicpages_visibility
300 = \Galette\Core\Preferences::PUBLIC_PAGES_VISIBILITY_PRIVATE;
301
302 $visible = $this->preferences->showPublicPages($anon_login);
303 $this->assertFalse($visible);
304
305 $visible = $this->preferences->showPublicPages($admin_login);
306 $this->assertTrue($visible);
307
308 $visible = $this->preferences->showPublicPages($user_login);
309 $this->assertFalse($visible);
310 }
311
312 /**
313 * Data provider for cards sizes tests
314 *
315 * @return array
316 */
317 public static function sizesProvider()
318 {
319 return [
320 [//defaults
321 15, //vertical margin
322 20, //horizontal margin
323 5, //vertical spacing
324 10, //horizontal spacing
325 0 //expected number of warnings
326 ], [ //OK
327 0, //vertical margin
328 20, //horizontal margin
329 11, //vertical spacing
330 10, //horizontal spacing
331 0 //expected number of warnings
332 ], [ //vertical overflow
333 0, //vertical margin
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
345 0, //vertical margin
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
356 ]
357 ];
358 }
359
360 /**
361 * Test checkCardsSizes
362 *
363 * @dataProvider sizesProvider
364 *
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
370 *
371 * @return void
372 */
373 public function testCheckCardsSizes($vm, $hm, $vs, $hs, $count)
374 {
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());
380 }
381
382 /**
383 * Data provider for colors
384 *
385 * @return array
386 */
387 public static function colorsProvider(): array
388 {
389 return [
390 [
391 'prop' => 'tcol',
392 'color' => '#f0f0f0',
393 'expected' => '#f0f0f0'
394 ], [
395 'prop' => 'tcol',
396 'color' => '#f0f0f0f0',
397 'expected' => '#FFFFFF'
398 ], [
399 'prop' => 'tcol',
400 'color' => 'f0f0f0',
401 'expected' => '#f0f0f0'
402 ], [
403 'prop' => 'tcol',
404 'color' => 'azerty',
405 'expected' => '#FFFFFF'
406
407 ]
408 ];
409 }
410
411 /**
412 * Test colors
413 *
414 * @dataProvider colorsProvider
415 *
416 * @param string $prop Property to be set
417 * @param string $color Color to set
418 * @param string $expected Expected color
419 *
420 * @return void
421 */
422 public function testColors($prop, $color, $expected)
423 {
424 $prop = 'pref_card_' . $prop;
425 $this->preferences->$prop = $color;
426 $this->assertSame($expected, $this->preferences->$prop);
427 }
428
429 /**
430 * Test social networks
431 *
432 * @return void
433 */
434 public function testSocials()
435 {
436 $preferences = [];
437 foreach ($this->preferences->getDefaults() as $key => $value) {
438 $preferences[$key] = $value;
439 }
440
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'
448 ]);
449
450 //will create 2 social networks in table
451 $post = [
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
460 ];
461
462 $post = array_merge($preferences, $post);
463
464 $this->assertTrue(
465 $this->preferences->check($post, $this->login),
466 print_r($this->preferences->getErrors(), true)
467 );
468 $this->assertTrue($this->preferences->store());
469
470 $socials = \Galette\Entity\Social::getListForMember(null);
471 $this->assertCount(2, $socials);
472
473 $this->assertCount(
474 1,
475 \Galette\Entity\Social::getListForMember(null, \Galette\Entity\Social::MASTODON)
476 );
477 $this->assertCount(
478 1,
479 \Galette\Entity\Social::getListForMember(null, \Galette\Entity\Social::JABBER)
480 );
481 $this->assertCount(
482 0,
483 \Galette\Entity\Social::getListForMember(null, \Galette\Entity\Social::FACEBOOK)
484 );
485 $this->assertCount(
486 0,
487 \Galette\Entity\Social::getListForMember(null, \Galette\Entity\Social::BLOG)
488 );
489
490 //create one new social network
491 $post = [
492 'social_new_type_1' => \Galette\Entity\Social::FACEBOOK,
493 'social_new_value_1' => 'Galette does not have facebook',
494 ];
495
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' : '');
499 }
500
501 $post = array_merge($preferences, $post);
502
503 $this->assertTrue(
504 $this->preferences->check($post, $this->login),
505 print_r($this->preferences->getErrors(), true)
506 );
507 $this->assertTrue($this->preferences->store());
508
509 $socials = \Galette\Entity\Social::getListForMember(null);
510 $this->assertCount(3, $socials);
511
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);
516
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);
521
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);
526
527 $post = [];
528
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;
533 }
534 }
535
536 $post = array_merge($preferences, $post);
537 $this->assertTrue(
538 $this->preferences->check($post, $this->login),
539 print_r($this->preferences->getErrors(), true)
540 );
541 $this->assertTrue($this->preferences->store());
542
543 $socials = \Galette\Entity\Social::getListForMember(null);
544 $this->assertCount(2, $socials);
545
546 $this->assertCount(
547 0,
548 \Galette\Entity\Social::getListForMember(null, \Galette\Entity\Social::MASTODON)
549 );
550 $this->assertCount(
551 1,
552 \Galette\Entity\Social::getListForMember(null, \Galette\Entity\Social::JABBER)
553 );
554 $this->assertCount(
555 1,
556 \Galette\Entity\Social::getListForMember(null, \Galette\Entity\Social::FACEBOOK)
557 );
558 }
559
560 /**
561 * Test email signature
562 *
563 * @return void
564 */
565 public function testGetMailSignature()
566 {
567 $mail = new PHPMailer();
568 $this->assertSame("\r\n-- \r\nGalette", $this->preferences->getMailSignature($mail));
569
570 $this->preferences->pref_website = 'https://galette.eu';
571 $this->assertSame(
572 "\r\n-- \r\nGalette\r\n\r\nhttps://galette.eu",
573 $this->preferences->getMailSignature($mail)
574 );
575
576 //with legacy values
577 $this->preferences->pref_mail_sign = "{NAME}\r\n\r\n{WEBSITE}\r\n{FACEBOOK}\r\n{TWITTER}\r\n{LINKEDIN}\r\n{VIADEO}";
578 $this->assertSame(
579 "\r\n-- \r\nGalette\r\n\r\nhttps://galette.eu",
580 $this->preferences->getMailSignature($mail)
581 );
582
583 $social = new \Galette\Entity\Social($this->zdb);
584 $this->assertTrue(
585 $social
586 ->setType(\Galette\Entity\Social::MASTODON)
587 ->setUrl('https://framapiaf.org/@galette')
588 ->setLinkedMember(null)
589 ->store()
590 );
591 $this->assertCount(
592 1,
593 \Galette\Entity\Social::getListForMember(null, \Galette\Entity\Social::MASTODON)
594 );
595
596 $this->preferences->pref_mail_sign = "{ASSO_NAME}\r\n\r\n{ASSO_WEBSITE} - {ASSO_SOCIAL_MASTODON}";
597 $this->assertSame(
598 "\r\n-- \r\nGalette\r\n\r\nhttps://galette.eu - https://framapiaf.org/@galette",
599 $this->preferences->getMailSignature($mail)
600 );
601
602 $social = new \Galette\Entity\Social($this->zdb);
603 $this->assertTrue(
604 $social
605 ->setType(\Galette\Entity\Social::MASTODON)
606 ->setUrl('Galette mastodon URL - the return')
607 ->setLinkedMember(null)
608 ->store()
609 );
610 $this->assertCount(
611 2,
612 \Galette\Entity\Social::getListForMember(null, \Galette\Entity\Social::MASTODON)
613 );
614 $this->assertSame(
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)
617 );
618 }
619
620 /**
621 * Test getLegend
622 *
623 * @return void
624 */
625 public function testGetLegend()
626 {
627 $legend = $this->preferences->getLegend();
628 $this->assertCount(2, $legend);
629 $this->assertCount(9, $legend['main']['patterns']);
630 $this->assertCount(10, $legend['socials']['patterns']);
631 $this->assertSame(
632 [
633 'title' => __('Mastodon'),
634 'pattern' => '/{ASSO_SOCIAL_MASTODON}/'
635 ],
636 $legend['socials']['patterns']['asso_social_mastodon']
637 );
638
639 $social = new \Galette\Entity\Social($this->zdb);
640 $this->assertTrue(
641 $social
642 ->setType('mynewtype')
643 ->setUrl('Galette specific social network URL')
644 ->setLinkedMember(null)
645 ->store()
646 );
647
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']));
652 $this->assertSame(
653 [
654 'title' => 'mynewtype',
655 'pattern' => '/{ASSO_SOCIAL_MYNEWTYPE}/'
656 ],
657 $legend['socials']['patterns']['asso_social_mynewtype']
658 );
659 }
660
661 /**
662 * Test website URL
663 *
664 * @return void
665 */
666 public function testWebsiteURL(): void
667 {
668 $preferences = [];
669 foreach ($this->preferences->getDefaults() as $key => $value) {
670 $preferences[$key] = $value;
671 }
672
673 $post = array_merge($preferences, ['pref_website' => 'https://galette.eu']);
674 $this->assertTrue(
675 $this->preferences->check($post, $this->login),
676 print_r($this->preferences->getErrors(), true)
677 );
678
679 $post = array_merge($preferences, ['pref_website' => 'galette.eu']);
680 $this->assertFalse(
681 $this->preferences->check($post, $this->login),
682 print_r($this->preferences->getErrors(), true)
683 );
684 $this->assertSame(['- Invalid website URL.'], $this->preferences->getErrors());
685 }
686 }