diff --git a/src/Renderer/Twig/Extensions/Globals.php b/src/Renderer/Twig/Extensions/Globals.php index 1b21d241..7be96d60 100644 --- a/src/Renderer/Twig/Extensions/Globals.php +++ b/src/Renderer/Twig/Extensions/Globals.php @@ -49,7 +49,7 @@ class Globals extends TwigExtension implements GlobalsInterface } if (array_key_exists($themeId, $themes) === false) { - $themeId = 1; + $themeId = array_key_first($themes); } $theme = $themes[$themeId]; diff --git a/tests/Unit/Renderer/Twig/Extensions/AssetsTest.php b/tests/Unit/Renderer/Twig/Extensions/AssetsTest.php index 2d7ba6cd..dc186562 100644 --- a/tests/Unit/Renderer/Twig/Extensions/AssetsTest.php +++ b/tests/Unit/Renderer/Twig/Extensions/AssetsTest.php @@ -12,7 +12,7 @@ class AssetsTest extends ExtensionTest * @covers \Engelsystem\Renderer\Twig\Extensions\Assets::__construct * @covers \Engelsystem\Renderer\Twig\Extensions\Assets::getFunctions */ - public function testGetGlobals() + public function testGetFunctions() { /** @var UrlGenerator|MockObject $urlGenerator */ $urlGenerator = $this->createMock(UrlGenerator::class); diff --git a/tests/Unit/Renderer/Twig/Extensions/GlobalsTest.php b/tests/Unit/Renderer/Twig/Extensions/GlobalsTest.php index f471e804..66aabb0a 100644 --- a/tests/Unit/Renderer/Twig/Extensions/GlobalsTest.php +++ b/tests/Unit/Renderer/Twig/Extensions/GlobalsTest.php @@ -29,16 +29,18 @@ class GlobalsTest extends ExtensionTest $theme = ['name' => 'Testtheme', 'navbar_classes' => 'something']; $theme2 = ['name' => 'Bar']; $theme3 = ['name' => 'Lorem']; + /** @var User $user */ $user = User::factory() ->has(Settings::factory(['theme' => 42])) ->create(); $config = new Config(['theme' => 23, 'themes' => [42 => $theme, 23 => $theme2, 1337 => $theme3]]); - $auth->expects($this->exactly(3)) + $auth->expects($this->exactly(4)) ->method('user') ->willReturnOnConsecutiveCalls( null, $user, + $user, null ); @@ -46,17 +48,25 @@ class GlobalsTest extends ExtensionTest $extension = new Globals($auth, $request); + // No user $globals = $extension->getGlobals(); $this->assertGlobalsExists('user', [], $globals); $this->assertGlobalsExists('request', $request, $globals); $this->assertGlobalsExists('themeId', 23, $globals); $this->assertGlobalsExists('theme', $theme2, $globals); + // User $globals = $extension->getGlobals(); $this->assertGlobalsExists('user', $user, $globals); $this->assertGlobalsExists('themeId', 42, $globals); $this->assertGlobalsExists('theme', $theme, $globals); + // User with not available theme configured + $user->settings->theme = 9999; + $globals = $extension->getGlobals(); + $this->assertGlobalsExists('themeId', 42, $globals); + + // Request query parameter $request->query->set('theme', 1337); $globals = $extension->getGlobals(); $this->assertGlobalsExists('user', [], $globals);