1<?php 2 3declare(strict_types=1); 4 5namespace phpweb\Test\Unit; 6 7use PHPUnit\Framework\Attributes\CoversClass; 8use PHPUnit\Framework\Attributes\DataProvider; 9use phpweb\UserPreferences; 10use PHPUnit\Framework\TestCase; 11 12#[CoversClass(UserPreferences::class)] 13final class UserPreferencesTest extends TestCase 14{ 15 /** @param array<string, mixed> $cookie */ 16 #[DataProvider('loadCookiesProvider')] 17 public function testLoad( 18 array $cookie, 19 string $languageCode, 20 string|false $searchType, 21 bool $isUserGroupTipsEnabled, 22 ): void { 23 $_COOKIE = $cookie; 24 25 $userPreferences = new UserPreferences(); 26 $userPreferences->load(); 27 28 self::assertSame($languageCode, $userPreferences->languageCode); 29 self::assertSame($searchType, $userPreferences->searchType); 30 self::assertSame($isUserGroupTipsEnabled, $userPreferences->isUserGroupTipsEnabled); 31 } 32 33 /** @return array<int, array{array<string, mixed>, string, string|false, bool}> */ 34 public static function loadCookiesProvider(): array 35 { 36 return [ 37 [[], '', UserPreferences::URL_NONE, false], 38 [['MYPHPNET' => ['en,manual,,1']], '', UserPreferences::URL_NONE, false], 39 [['MYPHPNET' => ''], '', UserPreferences::URL_NONE, false], 40 [['MYPHPNET' => ',,,'], '', UserPreferences::URL_NONE, false], 41 [['MYPHPNET' => ',,,0'], '', UserPreferences::URL_NONE, false], 42 [['MYPHPNET' => ',,ignored,,ignored'], '', UserPreferences::URL_NONE, false], 43 [['MYPHPNET' => 'en,,,'], 'en', UserPreferences::URL_NONE, false], 44 [['MYPHPNET' => ',manual,,'], '', UserPreferences::URL_MANUAL, false], 45 [['MYPHPNET' => ',quickref,,'], '', UserPreferences::URL_FUNC, false], 46 [['MYPHPNET' => ',invalid,,'], '', UserPreferences::URL_NONE, false], 47 [['MYPHPNET' => ',,,1'], '', UserPreferences::URL_NONE, true], 48 [['MYPHPNET' => 'en,manual,,1'], 'en', UserPreferences::URL_MANUAL, true], 49 ]; 50 } 51 52 #[DataProvider('urlSearchTypeProvider')] 53 public function testSetUrlSearchType(mixed $type, string|false $expected): void 54 { 55 $userPreferences = new UserPreferences(searchType: UserPreferences::URL_NONE); 56 $userPreferences->setUrlSearchType($type); 57 self::assertSame($expected, $userPreferences->searchType); 58 } 59 60 /** @return array<int, array{mixed, string|false}> */ 61 public static function urlSearchTypeProvider(): array 62 { 63 return [ 64 ['manual', UserPreferences::URL_MANUAL], 65 ['quickref', UserPreferences::URL_FUNC], 66 [false, UserPreferences::URL_NONE], 67 ['', UserPreferences::URL_NONE], 68 ['invalid', UserPreferences::URL_NONE], 69 [['manual'], UserPreferences::URL_NONE], 70 ]; 71 } 72 73 public function testSetIsUserGroupTipsEnabled(): void 74 { 75 $timeBackup = $_SERVER['REQUEST_TIME']; 76 $_SERVER['REQUEST_TIME'] = 1726600070; 77 78 $userPreferences = new UserPreferences(isUserGroupTipsEnabled: false); 79 $userPreferences->setIsUserGroupTipsEnabled(true); 80 self::assertTrue($userPreferences->isUserGroupTipsEnabled); 81 82 $userPreferences = new UserPreferences(isUserGroupTipsEnabled: true); 83 $userPreferences->setIsUserGroupTipsEnabled(false); 84 self::assertFalse($userPreferences->isUserGroupTipsEnabled); 85 86 $_SERVER['REQUEST_TIME'] = 1726600066; 87 88 $userPreferences = new UserPreferences(isUserGroupTipsEnabled: false); 89 $userPreferences->setIsUserGroupTipsEnabled(false); 90 self::assertTrue($userPreferences->isUserGroupTipsEnabled); 91 92 $_SERVER['REQUEST_TIME'] = $timeBackup; 93 } 94} 95