Added ?theme parameter to temporary change theme

main
Igor Scheller 3 years ago committed by Michael Weimann
parent 0398efad65
commit 19fc114cf7
No known key found for this signature in database
GPG Key ID: 34F0524D4DA694A1

@ -29,11 +29,9 @@ class DesignController extends BaseController
/** /**
* Show the design overview page * Show the design overview page
* *
* @param Request $request
*
* @return Response * @return Response
*/ */
public function index(Request $request) public function index()
{ {
$demoUser = (new User())->forceFill([ $demoUser = (new User())->forceFill([
'id' => 42, 'id' => 42,
@ -53,21 +51,12 @@ class DesignController extends BaseController
])); ]));
$themes = $this->config->get('themes'); $themes = $this->config->get('themes');
$data = [ $data = [
'demo_user' => $demoUser, 'demo_user' => $demoUser,
'demo_user_2' => $demoUser2, 'demo_user_2' => $demoUser2,
'themes' => $themes, '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( return $this->response->withView(
'pages/design', 'pages/design',
$data $data

@ -33,6 +33,7 @@ class Globals extends TwigExtension implements GlobalsInterface
public function getGlobals(): array public function getGlobals(): array
{ {
$user = $this->auth->user(); $user = $this->auth->user();
$themes = config('themes');
if ($user === null) { if ($user === null) {
$themeId = config('theme'); $themeId = config('theme');
@ -40,13 +41,18 @@ class Globals extends TwigExtension implements GlobalsInterface
$themeId = $user->settings->theme; $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 [ return [
'user' => $user ?? [], 'user' => $user ?? [],
'request' => $this->request, 'request' => $this->request,
'themeId' => $themeId, 'themeId' => $themeId,
'theme' => $theme, 'theme' => $theme,
]; ];
} }
} }

@ -4,7 +4,6 @@ namespace Engelsystem\Test\Unit\Controllers;
use Engelsystem\Config\Config; use Engelsystem\Config\Config;
use Engelsystem\Controllers\DesignController; use Engelsystem\Controllers\DesignController;
use Engelsystem\Http\Request;
use Engelsystem\Http\Response; use Engelsystem\Http\Response;
use Engelsystem\Test\Unit\TestCase; use Engelsystem\Test\Unit\TestCase;
use PHPUnit\Framework\MockObject\MockObject; use PHPUnit\Framework\MockObject\MockObject;
@ -22,37 +21,18 @@ class DesignControllerTest extends TestCase
$response->expects($this->once()) $response->expects($this->once())
->method('withView') ->method('withView')
->with('pages/design') ->with('pages/design')
->willReturn($response); ->willReturnCallback(function (string $view, array $data) use ($response) {
$request = new Request(['theme' => 42]); $this->assertTrue(isset($data['demo_user']));
$config = new Config(); $this->assertTrue(isset($data['demo_user_2']));
$this->assertIsArray($data['themes']);
$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']);
return $response; return $response;
}); });
$request = new Request(); $config = new Config(['themes' => [42 => ['name' => 'Foo']]]);
$request->attributes->set('theme', '42');
$config = new Config(['themes' => [42 => $theme]]);
$controller = new DesignController($response, $config); $controller = new DesignController($response, $config);
$controller->index($request); $return = $controller->index();
$this->assertEquals($response, $return);
} }
} }

@ -25,19 +25,20 @@ class GlobalsTest extends ExtensionTest
/** @var Authenticator|MockObject $auth */ /** @var Authenticator|MockObject $auth */
$auth = $this->createMock(Authenticator::class); $auth = $this->createMock(Authenticator::class);
/** @var Request|MockObject $request */ $request = new Request();
$request = $this->createMock(Request::class);
$theme = ['name' => 'Testtheme', 'navbar_classes' => 'something']; $theme = ['name' => 'Testtheme', 'navbar_classes' => 'something'];
$theme2 = ['name' => 'Bar']; $theme2 = ['name' => 'Bar'];
$theme3 = ['name' => 'Lorem'];
$user = new User(['name' => '', 'email' => '', 'password' => '', 'api_key' => '']); $user = new User(['name' => '', 'email' => '', 'password' => '', 'api_key' => '']);
$userSettings = new Settings(['theme' => 42, 'language' => '']); $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') ->method('user')
->willReturnOnConsecutiveCalls( ->willReturnOnConsecutiveCalls(
null, null,
$user $user,
null
); );
$user->save(); $user->save();
@ -60,5 +61,11 @@ class GlobalsTest extends ExtensionTest
$this->assertGlobalsExists('user', $user, $globals); $this->assertGlobalsExists('user', $user, $globals);
$this->assertGlobalsExists('themeId', 42, $globals); $this->assertGlobalsExists('themeId', 42, $globals);
$this->assertGlobalsExists('theme', $theme, $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);
} }
} }

Loading…
Cancel
Save