summaryrefslogtreecommitdiff
path: root/tests/Unit/Renderer/Twig
diff options
context:
space:
mode:
Diffstat (limited to 'tests/Unit/Renderer/Twig')
-rw-r--r--tests/Unit/Renderer/Twig/Extensions/AuthenticationTest.php56
-rw-r--r--tests/Unit/Renderer/Twig/Extensions/ExtensionTest.php12
-rw-r--r--tests/Unit/Renderer/Twig/Extensions/GlobalsTest.php34
-rw-r--r--tests/Unit/Renderer/Twig/Extensions/LegacyTest.php59
4 files changed, 158 insertions, 3 deletions
diff --git a/tests/Unit/Renderer/Twig/Extensions/AuthenticationTest.php b/tests/Unit/Renderer/Twig/Extensions/AuthenticationTest.php
new file mode 100644
index 00000000..0a72c0e7
--- /dev/null
+++ b/tests/Unit/Renderer/Twig/Extensions/AuthenticationTest.php
@@ -0,0 +1,56 @@
+<?php
+
+namespace Engelsystem\Test\Unit\Renderer\Twig\Extensions;
+
+use Engelsystem\Renderer\Twig\Extensions\Authentication;
+
+class AuthenticationTest extends ExtensionTest
+{
+ /**
+ * @covers \Engelsystem\Renderer\Twig\Extensions\Authentication::getFunctions
+ */
+ public function testGetFunctions()
+ {
+ $extension = new Authentication();
+ $functions = $extension->getFunctions();
+
+ $this->assertExtensionExists('is_user', [$extension, 'isAuthenticated'], $functions);
+ $this->assertExtensionExists('is_guest', [$extension, 'isGuest'], $functions);
+ $this->assertExtensionExists('has_permission_to', [$extension, 'checkAuth'], $functions);
+ }
+
+ /**
+ * @covers \Engelsystem\Renderer\Twig\Extensions\Authentication::isAuthenticated
+ * @covers \Engelsystem\Renderer\Twig\Extensions\Authentication::isGuest
+ */
+ public function testIsAuthenticated()
+ {
+ global $user;
+ $user = [];
+
+ $extension = new Authentication();
+
+ $this->assertFalse($extension->isAuthenticated());
+ $this->assertTrue($extension->isGuest());
+
+ $user = ['lorem' => 'ipsum'];
+ $this->assertTrue($extension->isAuthenticated());
+ $this->assertFalse($extension->isGuest());
+ }
+
+ /**
+ * @covers \Engelsystem\Renderer\Twig\Extensions\Authentication::checkAuth
+ */
+ public function testCheckAuth()
+ {
+ global $privileges;
+ $privileges = [];
+
+ $extension = new Authentication();
+
+ $this->assertFalse($extension->checkAuth('foo.bar'));
+
+ $privileges = ['foo.bar'];
+ $this->assertTrue($extension->checkAuth('foo.bar'));
+ }
+}
diff --git a/tests/Unit/Renderer/Twig/Extensions/ExtensionTest.php b/tests/Unit/Renderer/Twig/Extensions/ExtensionTest.php
index e1c5a378..ef7dba7a 100644
--- a/tests/Unit/Renderer/Twig/Extensions/ExtensionTest.php
+++ b/tests/Unit/Renderer/Twig/Extensions/ExtensionTest.php
@@ -2,8 +2,10 @@
namespace Engelsystem\Test\Unit\Renderer\Twig\Extensions;
+use PHPUnit\Framework\MockObject\MockObject;
use PHPUnit\Framework\TestCase;
use Twig_Function as TwigFunction;
+use Twig_Node as TwigNode;
abstract class ExtensionTest extends TestCase
{
@@ -35,7 +37,7 @@ abstract class ExtensionTest extends TestCase
* @param callable $callback
* @param TwigFunction[] $functions
*/
- protected function assertExtensionExists($name, $callback, $functions)
+ protected function assertExtensionExists($name, $callback, $functions, $options = [])
{
foreach ($functions as $function) {
if ($function->getName() != $name) {
@@ -43,6 +45,14 @@ abstract class ExtensionTest extends TestCase
}
$this->assertEquals($callback, $function->getCallable());
+
+ if (isset($options['is_save'])) {
+ /** @var TwigNode|MockObject $twigNode */
+ $twigNode = $this->createMock(TwigNode::class);
+
+ $this->assertArraySubset($options['is_save'], $function->getSafe($twigNode));
+ }
+
return;
}
diff --git a/tests/Unit/Renderer/Twig/Extensions/GlobalsTest.php b/tests/Unit/Renderer/Twig/Extensions/GlobalsTest.php
index 6cc3a4da..cdd9cd2a 100644
--- a/tests/Unit/Renderer/Twig/Extensions/GlobalsTest.php
+++ b/tests/Unit/Renderer/Twig/Extensions/GlobalsTest.php
@@ -2,24 +2,54 @@
namespace Engelsystem\Test\Unit\Renderer\Twig\Extensions;
+use Carbon\Carbon;
use Engelsystem\Renderer\Twig\Extensions\Globals;
+use PHPUnit\Framework\MockObject\MockObject;
class GlobalsTest extends ExtensionTest
{
/**
* @covers \Engelsystem\Renderer\Twig\Extensions\Globals::getGlobals
+ * @covers \Engelsystem\Renderer\Twig\Extensions\Globals::filterEventConfig
*/
public function testGetGlobals()
{
- $extension = new Globals();
+ global $user;
+ $user = [];
+
+ /** @var Globals|MockObject $extension */
+ $extension = $this->getMockBuilder(Globals::class)
+ ->setMethods(['getEventConfig'])
+ ->getMock();
+
+ $extension->expects($this->exactly(2))
+ ->method('getEventConfig')
+ ->willReturnOnConsecutiveCalls(
+ null,
+ [
+ 'lorem' => 'ipsum',
+ 'event_end_date' => 1234567890,
+ ]
+ );
+
$globals = $extension->getGlobals();
$this->assertGlobalsExists('user', [], $globals);
+ $this->assertGlobalsExists('event_config', [], $globals);
- global $user;
$user['foo'] = 'bar';
$globals = $extension->getGlobals();
$this->assertGlobalsExists('user', ['foo' => 'bar'], $globals);
+ $this->assertGlobalsExists('event_config', ['lorem' => 'ipsum'], $globals);
+
+ $config = $globals['event_config'];
+ $this->assertArrayHasKey('event_end_date', $config);
+ /** @var Carbon $eventEndDate */
+ $eventEndDate = $config['event_end_date'];
+ $this->assertInstanceOf(Carbon::class, $eventEndDate);
+
+ $eventEndDate->setTimezone('UTC');
+ $this->assertEquals('2009-02-13 23:31:30', $eventEndDate->format('Y-m-d H:i:s'));
}
}
diff --git a/tests/Unit/Renderer/Twig/Extensions/LegacyTest.php b/tests/Unit/Renderer/Twig/Extensions/LegacyTest.php
new file mode 100644
index 00000000..613af0da
--- /dev/null
+++ b/tests/Unit/Renderer/Twig/Extensions/LegacyTest.php
@@ -0,0 +1,59 @@
+<?php
+
+namespace Engelsystem\Test\Unit\Renderer\Twig\Extensions;
+
+use Engelsystem\Http\Request;
+use Engelsystem\Renderer\Twig\Extensions\Legacy;
+use PHPUnit\Framework\MockObject\MockObject;
+
+class LegacyTest extends ExtensionTest
+{
+ /**
+ * @covers \Engelsystem\Renderer\Twig\Extensions\Legacy::getFunctions
+ */
+ public function testGetFunctions()
+ {
+ $isSafeHtml = ['is_safe' => ['html']];
+ /** @var Request|MockObject $request */
+ $request = $this->createMock(Request::class);
+
+ $extension = new Legacy($request);
+ $functions = $extension->getFunctions();
+
+ $this->assertExtensionExists('menu', 'make_navigation', $functions, $isSafeHtml);
+ $this->assertExtensionExists('menuUserShiftState', 'User_shift_state_render', $functions, $isSafeHtml);
+ $this->assertExtensionExists('menuUserMessages', 'user_unread_messages', $functions, $isSafeHtml);
+ $this->assertExtensionExists('menuUserHints', 'header_render_hints', $functions, $isSafeHtml);
+ $this->assertExtensionExists('menuUserSubmenu', 'make_user_submenu', $functions, $isSafeHtml);
+ $this->assertExtensionExists('page', [$extension, 'getPage'], $functions);
+ }
+
+ /**
+ * @covers \Engelsystem\Renderer\Twig\Extensions\Legacy::getPage
+ * @covers \Engelsystem\Renderer\Twig\Extensions\Legacy::__construct
+ */
+ public function testIsAuthenticated()
+ {
+ /** @var Request|MockObject $request */
+ $request = $this->createMock(Request::class);
+
+ $extension = new Legacy($request);
+
+ $request->expects($this->exactly(2))
+ ->method('has')
+ ->with('p')
+ ->willReturnOnConsecutiveCalls(true, false);
+
+ $request->expects($this->once())
+ ->method('get')
+ ->with('p')
+ ->willReturn('foo-bar');
+
+ $request->expects($this->once())
+ ->method('path')
+ ->willReturn('batz');
+
+ $this->assertEquals('foo-bar', $extension->getPage());
+ $this->assertEquals('batz', $extension->getPage());
+ }
+}