‪TYPO3CMS  ‪main
OpenDocumentServiceTest.php
Go to the documentation of this file.
1 <?php
2 
3 declare(strict_types=1);
4 
5 /*
6  * This file is part of the TYPO3 CMS project.
7  *
8  * It is free software; you can redistribute it and/or modify it under
9  * the terms of the GNU General Public License, either version 2
10  * of the License, or any later version.
11  *
12  * For the full copyright and license information, please read the
13  * LICENSE.txt file that was distributed with this source code.
14  *
15  * The TYPO3 project - inspiring people to share!
16  */
17 
19 
20 use PHPUnit\Framework\Attributes\Test;
21 use PHPUnit\Framework\MockObject\MockObject;
24 use TYPO3\TestingFramework\Core\Unit\UnitTestCase;
25 
26 final class ‪OpenDocumentServiceTest extends UnitTestCase
27 {
29 
30  protected function ‪setUp(): void
31  {
32  parent::setUp();
33  $this->backendUser = $this->getMockBuilder(BackendUserAuthentication::class)->disableOriginalConstructor()->getMock();
35  }
36 
37  #[Test]
38  public function ‪getsOpenDocumentsFromUserSession(): void
39  {
40  $this->backendUser->method('getModuleData')->with('FormEngine', 'ses')->willReturn([
41  [
42  'identifier1' => [ 'data1' ],
43  'identifier2' => [ 'data2' ],
44  ],
45  'identifier2',
46  ]);
47  $subject = new ‪OpenDocumentService();
48  $openDocuments = $subject->getOpenDocuments();
49  $expected = [
50  'identifier1' => [ 'data1' ],
51  'identifier2' => [ 'data2' ],
52  ];
53  self::assertEquals($expected, $openDocuments);
54  }
55 
56  #[Test]
58  {
59  $this->backendUser->method('getModuleData')->with('FormEngine', 'ses')->willReturn(null);
60  $subject = new ‪OpenDocumentService();
61  $openDocuments = $subject->getOpenDocuments();
62  self::assertEquals([], $openDocuments);
63  }
64 
65  #[Test]
66  public function ‪getsRecentDocumentsFromUserSession(): void
67  {
68  $this->backendUser->method('getModuleData')->with('opendocs::recent')->willReturn([
69  'identifier1' => [ 'data1' ],
70  ]);
71  $subject = new ‪OpenDocumentService();
72  $recentDocuments = $subject->getRecentDocuments();
73  $expected = [
74  'identifier1' => [ 'data1' ],
75  ];
76  self::assertEquals($expected, $recentDocuments);
77  }
78 
79  #[Test]
81  {
82  $this->backendUser->method('getModuleData')->with('opendocs::recent')->willReturn(null);
83  $subject = new ‪OpenDocumentService();
84  $recentDocuments = $subject->getRecentDocuments();
85  self::assertEquals([], $recentDocuments);
86  }
87 
88  #[Test]
89  public function ‪closesDocument(): void
90  {
91  $this->backendUser->method('getModuleData')->willReturnMap([
92  [
93  'FormEngine',
94  'ses',
95  [
96  [
97  'identifier8' => ['data8'],
98  'identifier9' => ['data9'],
99  ],
100  'identifier9',
101  ],
102  ],
103  [
104  'opendocs::recent',
105  '',
106  [
107  'identifier8' => [ 'data8' ],
108  'identifier7' => [ 'data7' ],
109  'identifier6' => [ 'data6' ],
110  'identifier5' => [ 'data5' ],
111  'identifier4' => [ 'data4' ],
112  'identifier3' => [ 'data3' ],
113  'identifier2' => [ 'data2' ],
114  'identifier1' => [ 'data1' ],
115  ],
116  ],
117  ]);
118 
119  $expectedOpenDocumentsData = [
120  [
121  'identifier8' => [ 'data8' ],
122  ],
123  'identifier9',
124  ];
125 
126  $expectedRecentDocumentsData = [
127  'identifier9' => [ 'data9' ],
128  'identifier8' => [ 'data8' ],
129  'identifier7' => [ 'data7' ],
130  'identifier6' => [ 'data6' ],
131  'identifier5' => [ 'data5' ],
132  'identifier4' => [ 'data4' ],
133  'identifier3' => [ 'data3' ],
134  'identifier2' => [ 'data2' ],
135  ];
136 
137  $series = [
138  ['FormEngine', $expectedOpenDocumentsData],
139  ['opendocs::recent', $expectedRecentDocumentsData],
140  ];
141  $this->backendUser->method('pushModuleData')->willReturnCallback(function (string $module, array $data) use (&$series): void {
142  $expectedArgs = array_shift($series);
143  self::assertSame($expectedArgs[0], $module);
144  self::assertSame($expectedArgs[1], $data);
145  });
146 
147  $subject = new ‪OpenDocumentService();
148  $subject->closeDocument('identifier9');
149  }
150 }
‪TYPO3\CMS\Opendocs\Tests\Unit\Service\OpenDocumentServiceTest\handlesUserSessionWithoutRecentDocuments
‪handlesUserSessionWithoutRecentDocuments()
Definition: OpenDocumentServiceTest.php:80
‪TYPO3\CMS\Opendocs\Tests\Unit\Service\OpenDocumentServiceTest
Definition: OpenDocumentServiceTest.php:27
‪TYPO3\CMS\Opendocs\Tests\Unit\Service\OpenDocumentServiceTest\handlesUserSessionWithoutOpenDocuments
‪handlesUserSessionWithoutOpenDocuments()
Definition: OpenDocumentServiceTest.php:57
‪TYPO3\CMS\Opendocs\Tests\Unit\Service\OpenDocumentServiceTest\setUp
‪setUp()
Definition: OpenDocumentServiceTest.php:30
‪TYPO3\CMS\Opendocs\Tests\Unit\Service\OpenDocumentServiceTest\getsOpenDocumentsFromUserSession
‪getsOpenDocumentsFromUserSession()
Definition: OpenDocumentServiceTest.php:38
‪TYPO3\CMS\Opendocs\Service\OpenDocumentService
Definition: OpenDocumentService.php:27
‪TYPO3\CMS\Opendocs\Tests\Unit\Service\OpenDocumentServiceTest\closesDocument
‪closesDocument()
Definition: OpenDocumentServiceTest.php:89
‪TYPO3\CMS\Core\Authentication\BackendUserAuthentication
Definition: BackendUserAuthentication.php:62
‪TYPO3\CMS\Opendocs\Tests\Unit\Service\OpenDocumentServiceTest\getsRecentDocumentsFromUserSession
‪getsRecentDocumentsFromUserSession()
Definition: OpenDocumentServiceTest.php:66
‪TYPO3\CMS\Opendocs\Tests\Unit\Service
Definition: OpenDocumentServiceTest.php:18
‪$GLOBALS
‪$GLOBALS['TYPO3_CONF_VARS']['EXTCONF']['adminpanel']['modules']
Definition: ext_localconf.php:25
‪TYPO3\CMS\Opendocs\Tests\Unit\Service\OpenDocumentServiceTest\$backendUser
‪BackendUserAuthentication &MockObject $backendUser
Definition: OpenDocumentServiceTest.php:28