|
| 1 | +<?php |
| 2 | + |
| 3 | +namespace Tests\Unit; |
| 4 | + |
| 5 | +use EvolutionCMS\ManagerTheme; |
| 6 | +use PHPUnit\Framework\TestCase; |
| 7 | +use ReflectionMethod; |
| 8 | + |
| 9 | +/** |
| 10 | + * core/factory/settings.php reads the manager lexicon, so the ManagerTheme singleton is |
| 11 | + * already built - with the system theme and language - by the time Core::getSettings() |
| 12 | + * merges the per-user settings. Without an explicit refresh a user-level manager_language |
| 13 | + * or manager_theme never reaches the manager UI: the whole backend stays on the site-wide |
| 14 | + * value. |
| 15 | + */ |
| 16 | +final class ManagerPerUserLanguageTest extends TestCase |
| 17 | +{ |
| 18 | + private static string $coreSource = ''; |
| 19 | + |
| 20 | + public static function setUpBeforeClass(): void |
| 21 | + { |
| 22 | + $rootDir = dirname(__DIR__, 3); |
| 23 | + require_once $rootDir . '/core/vendor/autoload.php'; |
| 24 | + |
| 25 | + self::$coreSource = (string) file_get_contents($rootDir . '/core/src/Core.php'); |
| 26 | + } |
| 27 | + |
| 28 | + public function testFactorySettingsStillNeedTheManagerLexicon(): void |
| 29 | + { |
| 30 | + $factorySettings = (string) file_get_contents(dirname(__DIR__, 2) . '/factory/settings.php'); |
| 31 | + |
| 32 | + // This is what forces ManagerTheme to be resolved before the user settings are known. |
| 33 | + self::assertStringContainsString('ManagerTheme::getLexicon(', $factorySettings); |
| 34 | + } |
| 35 | + |
| 36 | + public function testManagerThemeCanReloadItsLexicon(): void |
| 37 | + { |
| 38 | + $method = new ReflectionMethod(ManagerTheme::class, 'reloadLang'); |
| 39 | + |
| 40 | + self::assertTrue($method->isPublic()); |
| 41 | + self::assertSame(1, $method->getNumberOfRequiredParameters()); |
| 42 | + } |
| 43 | + |
| 44 | + public function testCoreRefreshesTheManagerThemeAfterMergingUserSettings(): void |
| 45 | + { |
| 46 | + $mergePosition = mb_strpos(self::$coreSource, '$this->getUserSettings();'); |
| 47 | + $syncPosition = mb_strpos(self::$coreSource, '$this->syncManagerTheme();'); |
| 48 | + |
| 49 | + self::assertIsInt($mergePosition); |
| 50 | + self::assertIsInt($syncPosition); |
| 51 | + self::assertGreaterThan($mergePosition, $syncPosition); |
| 52 | + } |
| 53 | + |
| 54 | + public function testManagerThemeRefreshOnlyTouchesAnAlreadyBuiltBackendTheme(): void |
| 55 | + { |
| 56 | + $sync = mb_substr( |
| 57 | + self::$coreSource, |
| 58 | + (int) mb_strpos(self::$coreSource, 'protected function syncManagerTheme') |
| 59 | + ); |
| 60 | + $sync = mb_substr($sync, 0, (int) mb_strpos($sync, "\n }")); |
| 61 | + |
| 62 | + self::assertStringContainsString('$this->isBackend()', $sync); |
| 63 | + self::assertStringContainsString("\$this->resolved('ManagerTheme')", $sync); |
| 64 | + self::assertStringContainsString('reloadLang(', $sync); |
| 65 | + // A different theme cannot be patched in place - the instance has to go. |
| 66 | + self::assertStringContainsString("forgetInstance('ManagerTheme')", $sync); |
| 67 | + self::assertStringContainsString("Facade::clearResolvedInstance('ManagerTheme')", $sync); |
| 68 | + } |
| 69 | +} |
0 commit comments