2018-08-26 12:23:47 +02:00
|
|
|
<?php
|
|
|
|
|
2023-02-03 20:41:59 +01:00
|
|
|
declare(strict_types=1);
|
|
|
|
|
2018-08-26 12:23:47 +02:00
|
|
|
namespace Engelsystem\Test\Unit\Renderer\Twig\Extensions;
|
|
|
|
|
2021-06-19 17:03:18 +02:00
|
|
|
use Engelsystem\Config\Config;
|
2018-10-08 19:30:37 +02:00
|
|
|
use Engelsystem\Helpers\Authenticator;
|
2023-08-13 16:09:56 +02:00
|
|
|
use Engelsystem\Helpers\Carbon;
|
2020-11-15 18:47:30 +01:00
|
|
|
use Engelsystem\Http\Request;
|
2021-06-19 17:03:18 +02:00
|
|
|
use Engelsystem\Models\User\Settings;
|
2018-10-08 19:30:37 +02:00
|
|
|
use Engelsystem\Models\User\User;
|
2018-08-26 12:23:47 +02:00
|
|
|
use Engelsystem\Renderer\Twig\Extensions\Globals;
|
2021-06-19 17:03:18 +02:00
|
|
|
use Engelsystem\Test\Unit\HasDatabase;
|
2018-10-08 19:30:37 +02:00
|
|
|
use PHPUnit\Framework\MockObject\MockObject;
|
2018-08-26 12:23:47 +02:00
|
|
|
|
|
|
|
class GlobalsTest extends ExtensionTest
|
|
|
|
{
|
2021-06-19 17:03:18 +02:00
|
|
|
use HasDatabase;
|
|
|
|
|
2023-08-13 16:09:56 +02:00
|
|
|
public static function setUpBeforeClass(): void
|
|
|
|
{
|
|
|
|
Carbon::setTestNow(Carbon::createFromFormat('Y-m-d', '2023-08-15'));
|
|
|
|
}
|
|
|
|
|
|
|
|
public static function tearDownAfterClass(): void
|
|
|
|
{
|
|
|
|
Carbon::setTestNow();
|
|
|
|
}
|
|
|
|
|
2018-08-26 12:23:47 +02:00
|
|
|
/**
|
2018-10-08 19:30:37 +02:00
|
|
|
* @covers \Engelsystem\Renderer\Twig\Extensions\Globals::__construct
|
2018-08-26 12:23:47 +02:00
|
|
|
* @covers \Engelsystem\Renderer\Twig\Extensions\Globals::getGlobals
|
2023-12-13 14:42:25 +01:00
|
|
|
* @covers \Engelsystem\Renderer\Twig\Extensions\Globals::getGlobalValues
|
2018-08-26 12:23:47 +02:00
|
|
|
*/
|
2022-12-14 19:15:20 +01:00
|
|
|
public function testGetGlobals(): void
|
2018-08-26 12:23:47 +02:00
|
|
|
{
|
2021-06-19 17:03:18 +02:00
|
|
|
$this->initDatabase();
|
|
|
|
|
2018-10-08 19:30:37 +02:00
|
|
|
/** @var Authenticator|MockObject $auth */
|
|
|
|
$auth = $this->createMock(Authenticator::class);
|
2021-07-24 12:35:29 +02:00
|
|
|
$request = new Request();
|
2021-06-19 17:03:18 +02:00
|
|
|
$theme = ['name' => 'Testtheme', 'navbar_classes' => 'something'];
|
|
|
|
$theme2 = ['name' => 'Bar'];
|
2021-07-24 12:35:29 +02:00
|
|
|
$theme3 = ['name' => 'Lorem'];
|
2021-08-17 15:10:31 +02:00
|
|
|
/** @var User $user */
|
2021-08-17 14:16:05 +02:00
|
|
|
$user = User::factory()
|
|
|
|
->has(Settings::factory(['theme' => 42]))
|
|
|
|
->create();
|
2023-08-13 16:09:56 +02:00
|
|
|
$config = new Config(
|
|
|
|
[
|
|
|
|
'event_start' => Carbon::createFromFormat('Y-m-d', '2023-08-13'),
|
|
|
|
'theme' => 23,
|
|
|
|
'themes' => [
|
|
|
|
42 => $theme,
|
|
|
|
23 => $theme2,
|
|
|
|
1337 => $theme3,
|
|
|
|
],
|
|
|
|
]
|
|
|
|
);
|
2018-09-02 02:09:56 +02:00
|
|
|
|
2021-08-17 15:10:31 +02:00
|
|
|
$auth->expects($this->exactly(4))
|
2018-10-08 19:30:37 +02:00
|
|
|
->method('user')
|
|
|
|
->willReturnOnConsecutiveCalls(
|
|
|
|
null,
|
2021-07-24 12:35:29 +02:00
|
|
|
$user,
|
2021-08-17 15:10:31 +02:00
|
|
|
$user,
|
2021-07-24 12:35:29 +02:00
|
|
|
null
|
2018-10-08 19:30:37 +02:00
|
|
|
);
|
|
|
|
|
2021-06-19 17:03:18 +02:00
|
|
|
$this->app->instance('config', $config);
|
|
|
|
|
2020-11-15 18:47:30 +01:00
|
|
|
$extension = new Globals($auth, $request);
|
2023-08-13 16:09:56 +02:00
|
|
|
$globals = $extension->getGlobals();
|
|
|
|
|
2023-08-18 15:18:45 +02:00
|
|
|
$this->assertGlobalsExists('day_of_event', 3, $globals);
|
2018-09-02 02:09:56 +02:00
|
|
|
|
2021-08-17 15:10:31 +02:00
|
|
|
// No user
|
2018-08-26 12:23:47 +02:00
|
|
|
$this->assertGlobalsExists('user', [], $globals);
|
2022-06-06 12:32:07 +02:00
|
|
|
$this->assertGlobalsExists('user_messages', null, $globals);
|
2020-11-15 18:47:30 +01:00
|
|
|
$this->assertGlobalsExists('request', $request, $globals);
|
2021-06-19 17:03:18 +02:00
|
|
|
$this->assertGlobalsExists('themeId', 23, $globals);
|
|
|
|
$this->assertGlobalsExists('theme', $theme2, $globals);
|
2018-08-26 12:23:47 +02:00
|
|
|
|
2021-08-17 15:10:31 +02:00
|
|
|
// User
|
2023-12-13 14:42:25 +01:00
|
|
|
$extension = new Globals($auth, $request);
|
2018-08-26 12:23:47 +02:00
|
|
|
$globals = $extension->getGlobals();
|
2018-10-08 19:30:37 +02:00
|
|
|
$this->assertGlobalsExists('user', $user, $globals);
|
2022-06-06 12:32:07 +02:00
|
|
|
$this->assertGlobalsExists('user_messages', 0, $globals);
|
2021-06-19 17:03:18 +02:00
|
|
|
$this->assertGlobalsExists('themeId', 42, $globals);
|
|
|
|
$this->assertGlobalsExists('theme', $theme, $globals);
|
2021-07-24 12:35:29 +02:00
|
|
|
|
2021-08-17 15:10:31 +02:00
|
|
|
// User with not available theme configured
|
2023-12-13 14:42:25 +01:00
|
|
|
$extension = new Globals($auth, $request);
|
2021-08-17 15:10:31 +02:00
|
|
|
$user->settings->theme = 9999;
|
|
|
|
$globals = $extension->getGlobals();
|
|
|
|
$this->assertGlobalsExists('themeId', 42, $globals);
|
|
|
|
|
|
|
|
// Request query parameter
|
2023-12-13 14:42:25 +01:00
|
|
|
$extension = new Globals($auth, $request);
|
2021-07-24 12:35:29 +02:00
|
|
|
$request->query->set('theme', 1337);
|
|
|
|
$globals = $extension->getGlobals();
|
|
|
|
$this->assertGlobalsExists('user', [], $globals);
|
|
|
|
$this->assertGlobalsExists('themeId', 1337, $globals);
|
|
|
|
$this->assertGlobalsExists('theme', $theme3, $globals);
|
2023-12-13 14:42:25 +01:00
|
|
|
|
|
|
|
// Second retrieval is loaded directly
|
|
|
|
$globals = $extension->getGlobals();
|
|
|
|
$this->assertGlobalsExists('themeId', 1337, $globals);
|
2018-08-26 12:23:47 +02:00
|
|
|
}
|
|
|
|
}
|