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