testWithDisableConfigurationInSiteAndActiveBackendUser()   A
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 28
Code Lines 19

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 2
eloc 19
nc 2
nop 3
dl 0
loc 28
rs 9.6333
c 0
b 0
f 0
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Lochmueller\LanguageDetection\Tests\Unit\Check;
6
7
use Lochmueller\LanguageDetection\Check\BackendUserCheck;
8
use Lochmueller\LanguageDetection\Event\CheckLanguageDetectionEvent;
9
use Lochmueller\LanguageDetection\Service\SiteConfigurationService;
10
use Lochmueller\LanguageDetection\Tests\Unit\AbstractUnitTest;
11
use Psr\Http\Message\ServerRequestInterface;
12
use TYPO3\CMS\Core\Context\Context;
13
use TYPO3\CMS\Core\Context\UserAspect;
14
use TYPO3\CMS\Core\Site\Entity\Site;
15
use TYPO3\CMS\Core\Utility\GeneralUtility;
16
17
/**
18
 * @internal
19
 *
20
 * @coversNothing
21
 */
22
class BackendUserCheckTest extends AbstractUnitTest
23
{
24
    protected function setUp(): void
25
    {
26
        $this->resetSingletonInstances = true;
27
        parent::setUp();
28
    }
29
30
    /**
31
     * @covers \Lochmueller\LanguageDetection\Check\BackendUserCheck
32
     * @covers \Lochmueller\LanguageDetection\Domain\Model\Dto\SiteConfiguration
33
     * @covers \Lochmueller\LanguageDetection\Event\CheckLanguageDetectionEvent
34
     * @covers \Lochmueller\LanguageDetection\Service\SiteConfigurationService
35
     */
36
    public function testWithoutDisableInSite(): void
37
    {
38
        $site = $this->createStub(Site::class);
39
        $site->method('getConfiguration')
40
            ->willReturn(['disableRedirectWithBackendSession' => false])
41
        ;
42
        $request = $this->createMock(ServerRequestInterface::class);
43
        $event = new CheckLanguageDetectionEvent($site, $request);
44
45
        $backendUserCheck = new BackendUserCheck(new SiteConfigurationService());
46
        $backendUserCheck($event);
47
48
        self::assertTrue($event->isLanguageDetectionEnable());
49
    }
50
51
    /**
52
     * @covers \Lochmueller\LanguageDetection\Check\BackendUserCheck
53
     * @covers \Lochmueller\LanguageDetection\Domain\Model\Dto\SiteConfiguration
54
     * @covers \Lochmueller\LanguageDetection\Event\CheckLanguageDetectionEvent
55
     * @covers \Lochmueller\LanguageDetection\Service\SiteConfigurationService
56
     */
57
    public function testWithoutConfigurationInSite(): void
58
    {
59
        $site = $this->createStub(Site::class);
60
        $site->method('getConfiguration')
61
            ->willReturn([])
62
        ;
63
        $request = $this->createMock(ServerRequestInterface::class);
64
        $event = new CheckLanguageDetectionEvent($site, $request);
65
66
        $backendUserCheck = new BackendUserCheck(new SiteConfigurationService());
67
        $backendUserCheck($event);
68
69
        self::assertTrue($event->isLanguageDetectionEnable());
70
    }
71
72
    /**
73
     * @covers \Lochmueller\LanguageDetection\Check\BackendUserCheck
74
     * @covers \Lochmueller\LanguageDetection\Domain\Model\Dto\SiteConfiguration
75
     * @covers \Lochmueller\LanguageDetection\Event\CheckLanguageDetectionEvent
76
     * @covers \Lochmueller\LanguageDetection\Service\SiteConfigurationService
77
     *
78
     * @dataProvider data
79
     */
80
    public function testWithDisableConfigurationInSiteAndActiveBackendUser(bool $isLoginState, bool $disableRedirectWithBackendSession, bool $isEnabled): void
81
    {
82
        $site = $this->createStub(Site::class);
83
        $site->method('getConfiguration')
84
            ->willReturn(['disableRedirectWithBackendSession' => $disableRedirectWithBackendSession])
85
        ;
86
        $request = $this->createMock(ServerRequestInterface::class);
87
        $event = new CheckLanguageDetectionEvent($site, $request);
88
89
        $userAspect = new UserAspect();
90
        if ($isLoginState) {
91
            $user = new \stdClass();
92
            $user->user = ['uid' => 1];
93
            $propertyReflection = new \ReflectionProperty($userAspect, 'user');
94
            $propertyReflection->setAccessible(true);
95
            $propertyReflection->setValue($userAspect, $user);
96
        }
97
98
        $context = new Context();
99
        $context->setAspect('backend.user', $userAspect);
100
101
        GeneralUtility::setSingletonInstance(Context::class, $context);
102
103
        $backendUserCheck = new BackendUserCheck(new SiteConfigurationService());
104
        $backendUserCheck($event);
105
106
        self::assertSame($isLoginState, $context->getAspect('backend.user')->get('isLoggedIn'));
107
        self::assertSame($isEnabled, $event->isLanguageDetectionEnable());
108
    }
109
110
    /**
111
     * @return array<string, bool[]>
112
     */
113
    public static function data(): array
114
    {
115
        return [
116
            'Active be user and disableRedirectWithBackendSession' => [true, true, false],
117
            'Inactive be user and disableRedirectWithBackendSession' => [false, true, true],
118
            'Active be user and no disableRedirectWithBackendSession' => [true, false, true],
119
            'Inactive be user and no disableRedirectWithBackendSession' => [false, false, true],
120
        ];
121
    }
122
}
123