diff --git a/src/Controllers/DesignController.php b/src/Controllers/DesignController.php index 899f1f35..9ec47b5c 100644 --- a/src/Controllers/DesignController.php +++ b/src/Controllers/DesignController.php @@ -29,11 +29,9 @@ class DesignController extends BaseController /** * Show the design overview page * - * @param Request $request - * * @return Response */ - public function index(Request $request) + public function index() { $demoUser = (new User())->forceFill([ 'id' => 42, @@ -53,21 +51,12 @@ class DesignController extends BaseController ])); $themes = $this->config->get('themes'); - $data = [ 'demo_user' => $demoUser, 'demo_user_2' => $demoUser2, 'themes' => $themes, ]; - $themeId = $request->get('theme'); - $this->config->set('theme', (int) $themeId); - - if (isset($themes[$themeId])) { - $data['theme'] = $themes[$themeId]; - $data['themeId'] = $themeId; - } - return $this->response->withView( 'pages/design', $data diff --git a/src/Renderer/Twig/Extensions/Globals.php b/src/Renderer/Twig/Extensions/Globals.php index 2b782557..862b892d 100644 --- a/src/Renderer/Twig/Extensions/Globals.php +++ b/src/Renderer/Twig/Extensions/Globals.php @@ -33,6 +33,7 @@ class Globals extends TwigExtension implements GlobalsInterface public function getGlobals(): array { $user = $this->auth->user(); + $themes = config('themes'); if ($user === null) { $themeId = config('theme'); @@ -40,13 +41,18 @@ class Globals extends TwigExtension implements GlobalsInterface $themeId = $user->settings->theme; } - $theme = config('themes')[$themeId]; + $query = $this->request->query->get('theme'); + if (!is_null($query) && isset($themes[$query])) { + $themeId = $query; + } + + $theme = $themes[$themeId]; return [ - 'user' => $user ?? [], - 'request' => $this->request, - 'themeId' => $themeId, - 'theme' => $theme, + 'user' => $user ?? [], + 'request' => $this->request, + 'themeId' => $themeId, + 'theme' => $theme, ]; } } diff --git a/tests/Unit/Controllers/DesignControllerTest.php b/tests/Unit/Controllers/DesignControllerTest.php index ac697584..4e60f793 100644 --- a/tests/Unit/Controllers/DesignControllerTest.php +++ b/tests/Unit/Controllers/DesignControllerTest.php @@ -4,7 +4,6 @@ namespace Engelsystem\Test\Unit\Controllers; use Engelsystem\Config\Config; use Engelsystem\Controllers\DesignController; -use Engelsystem\Http\Request; use Engelsystem\Http\Response; use Engelsystem\Test\Unit\TestCase; use PHPUnit\Framework\MockObject\MockObject; @@ -22,37 +21,18 @@ class DesignControllerTest extends TestCase $response->expects($this->once()) ->method('withView') ->with('pages/design') - ->willReturn($response); - $request = new Request(['theme' => 42]); - $config = new Config(); - - $controller = new DesignController($response, $config); - $return = $controller->index($request); - - $this->assertEquals($response, $return); - } - - /** - * @covers \Engelsystem\Controllers\DesignController::index - */ - public function testIndexSetTheme() - { - $theme = ['name' => 'Meaning of Live']; - /** @var Response|MockObject $response */ - $response = $this->createMock(Response::class); - $response->expects($this->once()) - ->method('withView') - ->willReturnCallback(function (string $view, array $data) use ($response, $theme) { - $this->assertTrue(isset($data['theme'])); - $this->assertEquals($theme, $data['theme']); + ->willReturnCallback(function (string $view, array $data) use ($response) { + $this->assertTrue(isset($data['demo_user'])); + $this->assertTrue(isset($data['demo_user_2'])); + $this->assertIsArray($data['themes']); return $response; }); - $request = new Request(); - $request->attributes->set('theme', '42'); - $config = new Config(['themes' => [42 => $theme]]); + $config = new Config(['themes' => [42 => ['name' => 'Foo']]]); $controller = new DesignController($response, $config); - $controller->index($request); + $return = $controller->index(); + + $this->assertEquals($response, $return); } } diff --git a/tests/Unit/Renderer/Twig/Extensions/GlobalsTest.php b/tests/Unit/Renderer/Twig/Extensions/GlobalsTest.php index fff7a1c7..62e3b2b4 100644 --- a/tests/Unit/Renderer/Twig/Extensions/GlobalsTest.php +++ b/tests/Unit/Renderer/Twig/Extensions/GlobalsTest.php @@ -25,19 +25,20 @@ class GlobalsTest extends ExtensionTest /** @var Authenticator|MockObject $auth */ $auth = $this->createMock(Authenticator::class); - /** @var Request|MockObject $request */ - $request = $this->createMock(Request::class); + $request = new Request(); $theme = ['name' => 'Testtheme', 'navbar_classes' => 'something']; $theme2 = ['name' => 'Bar']; + $theme3 = ['name' => 'Lorem']; $user = new User(['name' => '', 'email' => '', 'password' => '', 'api_key' => '']); $userSettings = new Settings(['theme' => 42, 'language' => '']); - $config = new Config(['theme' => 23, 'themes' => [42 => $theme, 23 => $theme2]]); + $config = new Config(['theme' => 23, 'themes' => [42 => $theme, 23 => $theme2, 1337 => $theme3]]); - $auth->expects($this->exactly(2)) + $auth->expects($this->exactly(3)) ->method('user') ->willReturnOnConsecutiveCalls( null, - $user + $user, + null ); $user->save(); @@ -60,5 +61,11 @@ class GlobalsTest extends ExtensionTest $this->assertGlobalsExists('user', $user, $globals); $this->assertGlobalsExists('themeId', 42, $globals); $this->assertGlobalsExists('theme', $theme, $globals); + + $request->query->set('theme', 1337); + $globals = $extension->getGlobals(); + $this->assertGlobalsExists('user', [], $globals); + $this->assertGlobalsExists('themeId', 1337, $globals); + $this->assertGlobalsExists('theme', $theme3, $globals); } }