summaryrefslogtreecommitdiff
path: root/tests/Unit/Renderer/Twig/Extensions/AuthenticationTest.php
blob: b67d4eed1e2317ab75acb36827895d9758417879 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
<?php

namespace Engelsystem\Test\Unit\Renderer\Twig\Extensions;

use Engelsystem\Helpers\Authenticator;
use Engelsystem\Models\User\User;
use Engelsystem\Renderer\Twig\Extensions\Authentication;
use PHPUnit\Framework\MockObject\MockObject;

class AuthenticationTest extends ExtensionTest
{
    /**
     * @covers \Engelsystem\Renderer\Twig\Extensions\Authentication::__construct
     * @covers \Engelsystem\Renderer\Twig\Extensions\Authentication::getFunctions
     */
    public function testGetFunctions()
    {
        /** @var Authenticator|MockObject $auth */
        $auth = $this->createMock(Authenticator::class);

        $extension = new Authentication($auth);
        $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()
    {
        /** @var Authenticator|MockObject $auth */
        $auth = $this->createMock(Authenticator::class);
        $user = new User();

        $auth->expects($this->exactly(4))
            ->method('user')
            ->willReturnOnConsecutiveCalls(
                null,
                null,
                $user,
                $user
            );

        $extension = new Authentication($auth);

        $this->assertFalse($extension->isAuthenticated());
        $this->assertTrue($extension->isGuest());

        $this->assertTrue($extension->isAuthenticated());
        $this->assertFalse($extension->isGuest());
    }

    /**
     * @covers \Engelsystem\Renderer\Twig\Extensions\Authentication::checkAuth
     */
    public function testCheckAuth()
    {
        global $privileges;
        $privileges = [];

        /** @var Authenticator|MockObject $auth */
        $auth = $this->createMock(Authenticator::class);

        $extension = new Authentication($auth);

        $this->assertFalse($extension->checkAuth('foo.bar'));

        $privileges = ['foo.bar'];
        $this->assertTrue($extension->checkAuth('foo.bar'));
    }
}