From 817a3e7fd91dfa280ef34e325f795559543840e4 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Maur=C3=ADcio=20Meneghini=20Fauth?= Date: Mon, 11 Nov 2024 05:50:15 -0300 Subject: [PATCH] Move myphpnet_* functions to the UserPreferences class (#1075) --- error.php | 2 +- include/langchooser.inc | 2 +- include/layout.inc | 4 +- include/prepend.inc | 86 +-------------------------- my.php | 22 +++---- src/UserPreferences.php | 77 +++++++++++++++++++++--- tests/Unit/LangChooserTest.php | 5 +- tests/Unit/UserPreferencesTest.php | 94 ++++++++++++++++++++++++++++++ 8 files changed, 182 insertions(+), 110 deletions(-) create mode 100644 tests/Unit/UserPreferencesTest.php diff --git a/error.php b/error.php index a5d4522989..2801188f14 100644 --- a/error.php +++ b/error.php @@ -725,7 +725,7 @@ // ============================================================================ // If no match was found till this point, the last action is to start a // search with the URI the user typed in -$fallback = (myphpnet_urlsearch() === UserPreferences::URL_MANUAL ? "404manual" : "404quickref"); +$fallback = ($userPreferences->searchType === UserPreferences::URL_MANUAL ? "404manual" : "404quickref"); mirror_redirect( '/search.php?show=' . $fallback . '&lang=' . urlencode($LANG) . '&pattern=' . substr($_SERVER['REQUEST_URI'], 1), diff --git a/include/langchooser.inc b/include/langchooser.inc index 230a3d707d..61def940f8 100644 --- a/include/langchooser.inc +++ b/include/langchooser.inc @@ -37,7 +37,7 @@ $_SERVER['STRIPPED_URI'] = htmlspecialchars($_SERVER['REQUEST_URI'], ENT_QUOTES, // The code is encapsulated in a function, // so the variable namespace is not polluted -list($LANG, $EXPL_LANG) = (new LangChooser($LANGUAGES, $INACTIVE_ONLINE_LANGUAGES, myphpnet_language(), default_language() ?: ''))->chooseCode( +list($LANG, $EXPL_LANG) = (new LangChooser($LANGUAGES, $INACTIVE_ONLINE_LANGUAGES, $userPreferences->languageCode, default_language() ?: ''))->chooseCode( $_REQUEST['lang'] ?? null, $_SERVER['REQUEST_URI'], $_SERVER['HTTP_ACCEPT_LANGUAGE'] ?? null, diff --git a/include/layout.inc b/include/layout.inc index 471d8eeaa3..5174395e40 100644 --- a/include/layout.inc +++ b/include/layout.inc @@ -425,13 +425,13 @@ EOT; function site_header(string $title = 'Hypertext Preprocessor', array $config = []): void { - global $MYSITE; + global $MYSITE, $LANG; $meta_image_path = $MYSITE . 'images/meta-image.png'; $meta_description = "PHP is a popular general-purpose scripting language that powers everything from your blog to the most popular websites in the world."; $defaults = [ - "lang" => myphpnet_language(), + "lang" => $LANG, "current" => "", "meta-navigation" => [], 'classes' => '', diff --git a/include/prepend.inc b/include/prepend.inc index eaf07cab4b..1ffd80f1b0 100644 --- a/include/prepend.inc +++ b/include/prepend.inc @@ -75,8 +75,10 @@ unset($COUNTRY); unset($ONLOAD); unset($LAST_UPDATED); +$userPreferences = new UserPreferences(); + // Load the My PHP.net settings before any includes -myphpnet_load(); +$userPreferences->load(); // Site details (mirror site information) include __DIR__ . '/site.inc'; @@ -97,88 +99,6 @@ include __DIR__ . '/last_updated.inc'; // ----------------------------------------------------------------------------- -// Load in the user preferences -function myphpnet_load(): void -{ - UserPreferences::$languageCode = ''; - UserPreferences::$searchType = UserPreferences::URL_NONE; - UserPreferences::$isUserGroupTipsEnabled = false; - - if (!isset($_COOKIE['MYPHPNET']) || !is_string($_COOKIE['MYPHPNET']) || $_COOKIE['MYPHPNET'] === '') { - return; - } - - /** - * 0 - Language code - * 1 - URL search fallback - * 2 - Mirror site (removed) - * 3 - User Group tips - * 4 - Documentation developmental server (removed) - */ - $preferences = explode(",", $_COOKIE['MYPHPNET']); - UserPreferences::$languageCode = $preferences[0] ?? ''; - if (isset($preferences[1]) && in_array($preferences[1], [UserPreferences::URL_FUNC, UserPreferences::URL_MANUAL], true)) { - UserPreferences::$searchType = $preferences[1]; - } - - UserPreferences::$isUserGroupTipsEnabled = isset($preferences[3]) && $preferences[3]; -} - -// Get preferred language code -function myphpnet_language(): string -{ - return UserPreferences::$languageCode; -} - -// Set URL search fallback preference -function myphpnet_urlsearch($type = false) -{ - // Set type if specified and if correct - if ($type && in_array($type, [UserPreferences::URL_FUNC, UserPreferences::URL_MANUAL], true)) { - UserPreferences::$searchType = $type; - } - - return UserPreferences::$searchType; -} - -function myphpnet_showug($enable = null) { - if (isset($_GET["showug"])) { - $enable = true; - } - - if (is_bool($enable)) { - UserPreferences::$isUserGroupTipsEnabled = $enable; - } - - // Show the ug tips to lucky few, depending on time. - if ($_SERVER["REQUEST_TIME"] % 10) { - UserPreferences::$isUserGroupTipsEnabled = true; - } - - return UserPreferences::$isUserGroupTipsEnabled; -} - -// Save user settings in cookie -function myphpnet_save(): void -{ - /** - * 0 - Language code - * 1 - URL search fallback - * 2 - Mirror site (removed) - * 3 - User Group tips - * 4 - Documentation developmental server (removed) - */ - $preferences = [ - UserPreferences::$languageCode, - UserPreferences::$searchType, - '', - UserPreferences::$isUserGroupTipsEnabled, - ]; - - // Set all the preferred values for a year - mirror_setcookie("MYPHPNET", join(",", $preferences), 60 * 60 * 24 * 365); -} - // Embed Google Custom Search engine function google_cse(): void { $cse_snippet = <<languageCode = $_POST['my_lang']; // Add this as first option, selected $options[] = '\n"; + $options[] = '\n"; // Remove, so it is not listed two times - unset($langs[myphpnet_language()]); + unset($langs[$userPreferences->languageCode]); } // We have no cookie and no form submitted @@ -54,18 +54,18 @@ // Save URL shortcut fallback setting if (isset($_POST['urlsearch'])) { - myphpnet_urlsearch($_POST['urlsearch']); + $userPreferences->setUrlSearchType($_POST['urlsearch']); } if (isset($_POST["showug"])) { - myphpnet_showug($_POST["showug"] === "enable"); + $userPreferences->setIsUserGroupTipsEnabled($_POST["showug"] === "enable"); } // Prepare mirror array $mirror_sites = $MIRRORS; $mirror_sites["NONE"] = [7 => MIRROR_OK]; -myphpnet_save(); +$userPreferences->save(); site_header("My PHP.net", ["current" => "community"]); ?> @@ -177,7 +177,7 @@
Your setting: searchType; if ($type === UserPreferences::URL_NONE || $type === UserPreferences::URL_FUNC) { echo ' checked="checked"'; } @@ -196,8 +196,8 @@ We are experimenting with listing nearby user groups. This feature is highly experimental and will very likely change a lot and be broken at times.

- >
- > + isUserGroupTipsEnabled ? "checked=checked" : "" ?>>
+ isUserGroupTipsEnabled ? "" : "checked=checked" ?>>

diff --git a/src/UserPreferences.php b/src/UserPreferences.php index 6b7b1eef60..60e3d42362 100644 --- a/src/UserPreferences.php +++ b/src/UserPreferences.php @@ -1,7 +1,15 @@ languageCode = ''; + $this->searchType = self::URL_NONE; + $this->isUserGroupTipsEnabled = false; + + if (!isset($_COOKIE['MYPHPNET']) || !is_string($_COOKIE['MYPHPNET']) || $_COOKIE['MYPHPNET'] === '') { + return; + } + + /** + * 0 - Language code + * 1 - URL search fallback + * 2 - Mirror site (removed) + * 3 - User Group tips + * 4 - Documentation developmental server (removed) + */ + $preferences = explode(",", $_COOKIE['MYPHPNET']); + $this->languageCode = $preferences[0] ?? ''; + $this->setUrlSearchType($preferences[1] ?? self::URL_NONE); + $this->isUserGroupTipsEnabled = isset($preferences[3]) && $preferences[3]; + } + + public function setUrlSearchType(mixed $type): void + { + if (!in_array($type, [self::URL_FUNC, self::URL_MANUAL, self::URL_NONE], true)) { + return; + } + + $this->searchType = $type; + } + + public function setIsUserGroupTipsEnabled(bool $enable): void { + // Show the ug tips to lucky few, depending on time. + if ($_SERVER["REQUEST_TIME"] % 10) { + $enable = true; + } + + $this->isUserGroupTipsEnabled = $enable; + } - /** - * URL search fallback - * - * @var 'manual'|'quickref'|false - */ - public static string|false $searchType = self::URL_NONE; + public function save(): void + { + /** + * 0 - Language code + * 1 - URL search fallback + * 2 - Mirror site (removed) + * 3 - User Group tips + * 4 - Documentation developmental server (removed) + */ + $preferences = [$this->languageCode, $this->searchType, '', $this->isUserGroupTipsEnabled]; - public static bool $isUserGroupTipsEnabled = false; + // Set all the preferred values for a year + mirror_setcookie("MYPHPNET", join(",", $preferences), 60 * 60 * 24 * 365); + } } diff --git a/tests/Unit/LangChooserTest.php b/tests/Unit/LangChooserTest.php index 9706a67b50..8c8d3a60c2 100644 --- a/tests/Unit/LangChooserTest.php +++ b/tests/Unit/LangChooserTest.php @@ -6,7 +6,6 @@ use phpweb\LangChooser; use PHPUnit\Framework; -use phpweb\UserPreferences; #[Framework\Attributes\CoversClass(LangChooser::class)] class LangChooserTest extends Framework\TestCase @@ -107,9 +106,7 @@ public function testChooseCodeWithLangParameterAndManualPath(): void public function testChooseCodeWithManualPathAndUserPreference(): void { - UserPreferences::$languageCode = 'en'; - - $langChooser = new LangChooser(self::DEFAULT_LANGUAGE_LIST, [], '', 'en'); + $langChooser = new LangChooser(self::DEFAULT_LANGUAGE_LIST, [], 'en', 'en'); $result = $langChooser->chooseCode('', '/manual/de', null); self::assertSame(['de', 'de'], $result); diff --git a/tests/Unit/UserPreferencesTest.php b/tests/Unit/UserPreferencesTest.php new file mode 100644 index 0000000000..f1df981bee --- /dev/null +++ b/tests/Unit/UserPreferencesTest.php @@ -0,0 +1,94 @@ + $cookie */ + #[DataProvider('loadCookiesProvider')] + public function testLoad( + array $cookie, + string $languageCode, + string|false $searchType, + bool $isUserGroupTipsEnabled, + ): void { + $_COOKIE = $cookie; + + $userPreferences = new UserPreferences(); + $userPreferences->load(); + + self::assertSame($languageCode, $userPreferences->languageCode); + self::assertSame($searchType, $userPreferences->searchType); + self::assertSame($isUserGroupTipsEnabled, $userPreferences->isUserGroupTipsEnabled); + } + + /** @return array, string, string|false, bool}> */ + public static function loadCookiesProvider(): array + { + return [ + [[], '', UserPreferences::URL_NONE, false], + [['MYPHPNET' => ['en,manual,,1']], '', UserPreferences::URL_NONE, false], + [['MYPHPNET' => ''], '', UserPreferences::URL_NONE, false], + [['MYPHPNET' => ',,,'], '', UserPreferences::URL_NONE, false], + [['MYPHPNET' => ',,,0'], '', UserPreferences::URL_NONE, false], + [['MYPHPNET' => ',,ignored,,ignored'], '', UserPreferences::URL_NONE, false], + [['MYPHPNET' => 'en,,,'], 'en', UserPreferences::URL_NONE, false], + [['MYPHPNET' => ',manual,,'], '', UserPreferences::URL_MANUAL, false], + [['MYPHPNET' => ',quickref,,'], '', UserPreferences::URL_FUNC, false], + [['MYPHPNET' => ',invalid,,'], '', UserPreferences::URL_NONE, false], + [['MYPHPNET' => ',,,1'], '', UserPreferences::URL_NONE, true], + [['MYPHPNET' => 'en,manual,,1'], 'en', UserPreferences::URL_MANUAL, true], + ]; + } + + #[DataProvider('urlSearchTypeProvider')] + public function testSetUrlSearchType(mixed $type, string|false $expected): void + { + $userPreferences = new UserPreferences(searchType: UserPreferences::URL_NONE); + $userPreferences->setUrlSearchType($type); + self::assertSame($expected, $userPreferences->searchType); + } + + /** @return array */ + public static function urlSearchTypeProvider(): array + { + return [ + ['manual', UserPreferences::URL_MANUAL], + ['quickref', UserPreferences::URL_FUNC], + [false, UserPreferences::URL_NONE], + ['', UserPreferences::URL_NONE], + ['invalid', UserPreferences::URL_NONE], + [['manual'], UserPreferences::URL_NONE], + ]; + } + + public function testSetIsUserGroupTipsEnabled(): void + { + $timeBackup = $_SERVER['REQUEST_TIME']; + $_SERVER['REQUEST_TIME'] = 1726600070; + + $userPreferences = new UserPreferences(isUserGroupTipsEnabled: false); + $userPreferences->setIsUserGroupTipsEnabled(true); + self::assertTrue($userPreferences->isUserGroupTipsEnabled); + + $userPreferences = new UserPreferences(isUserGroupTipsEnabled: true); + $userPreferences->setIsUserGroupTipsEnabled(false); + self::assertFalse($userPreferences->isUserGroupTipsEnabled); + + $_SERVER['REQUEST_TIME'] = 1726600066; + + $userPreferences = new UserPreferences(isUserGroupTipsEnabled: false); + $userPreferences->setIsUserGroupTipsEnabled(false); + self::assertTrue($userPreferences->isUserGroupTipsEnabled); + + $_SERVER['REQUEST_TIME'] = $timeBackup; + } +}