Added ?theme parameter to temporary change theme

This commit is contained in:
Igor Scheller 2021-07-24 12:35:29 +02:00 committed by Michael Weimann
parent 0398efad65
commit 19fc114cf7
No known key found for this signature in database
GPG Key ID: 34F0524D4DA694A1
4 changed files with 32 additions and 50 deletions

View File

@ -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

View File

@ -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,7 +41,12 @@ 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 ?? [],

View File

@ -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);
}
}

View File

@ -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);
}
}