ef94d110583de819b06e73d99a8f0c4757cfcda3
[Packages/TYPO3.CMS.git] / typo3 / sysext / install / Tests / Unit / UpgradeAnalysis / DocumentationFileTest.php
1 <?php
2 declare(strict_types = 1);
3
4 namespace TYPO3\CMS\Install\Tests\Unit\UpgradeAnalysis;
5
6 /*
7 * This file is part of the TYPO3 CMS project.
8 *
9 * It is free software; you can redistribute it and/or modify it under
10 * the terms of the GNU General Public License, either version 2
11 * of the License, or any later version.
12 *
13 * For the full copyright and license information, please read the
14 * LICENSE.txt file that was distributed with this source code.
15 *
16 * The TYPO3 project - inspiring people to share!
17 */
18
19 use org\bovigo\vfs\vfsStream;
20 use org\bovigo\vfs\vfsStreamDirectory;
21 use Prophecy\Argument;
22 use TYPO3\CMS\Core\Registry;
23 use TYPO3\CMS\Install\UpgradeAnalysis\DocumentationFile;
24
25 class DocumentationFileTest extends \TYPO3\TestingFramework\Core\Unit\UnitTestCase
26 {
27 /**
28 * @var DocumentationFile
29 */
30 protected $documentationFileService;
31
32 /**
33 * @var vfsStreamDirectory
34 */
35 protected $docRoot;
36
37 /**
38 * @var Registry
39 */
40 protected $registry;
41
42 /**
43 * set up test environment
44 */
45 public function setUp()
46 {
47 $content_12345 = [
48 '====',
49 'Breaking: #12345 - Issue',
50 '====',
51 '',
52 'some text content',
53 ];
54 $content_45678 = [
55 '====',
56 'Important: #45678 - Issue',
57 '====',
58 '',
59 'Some more text content',
60 ];
61
62 $content_98574 = [
63 '====',
64 'Important: #98574 - Issue',
65 '====',
66 '',
67 'Something else',
68 '',
69 '.. index:: unittest'
70 ];
71 $content_13579 = [
72 '====',
73 'Breaking: #13579 - Issue',
74 '====',
75 '',
76 'Some more content'
77 ];
78
79 $structure = [
80 'Changelog' => [
81 '1.2' => [
82 'Breaking-12345-Issue.rst' => implode("\n", $content_12345),
83 'Important-45678-Issue.rst' => implode("\n", $content_45678),
84
85 ],
86 '2.0' => [
87 'Important-98574-Issue.rst' => implode("\n", $content_98574),
88 ],
89 'master' => [
90 'Breaking-13579-Issue.rst' => implode("\n", $content_13579),
91 'Index.rst' => '',
92 ],
93 ],
94 ];
95
96 $this->docRoot = vfsStream::setup('root', null, $structure);
97
98 $this->registry = $this->prophesize(Registry::class);
99 $this->documentationFileService = new DocumentationFile(
100 $this->registry->reveal(),
101 vfsStream::url('root/Changelog')
102 );
103 }
104
105 /**
106 * dataprovider with invalid dir path. They should raise an exception and don't process.
107 * @return array
108 */
109 public function invalidDirProvider()
110 {
111 return [
112 [
113 'root' => '/'
114 ],
115 [
116 'etc' => '/etc'
117 ],
118 [
119 'etc/passwd' => '/etc/passwd'
120 ],
121 ];
122 }
123
124 /**
125 * @dataProvider invalidDirProvider
126 * @test
127 */
128 public function findDocumentationFilesThrowsExceptionIfPathIsNotInGivenChangelogDir(string $path)
129 {
130 $this->expectException(\InvalidArgumentException::class);
131 $this->expectExceptionCode(1485425530);
132 $documentationFileService = new DocumentationFile($this->registry->reveal());
133 $documentationFileService->findDocumentationFiles($path);
134 }
135
136 /**
137 * @test
138 */
139 public function findDocumentationFilesReturnsArrayOfFiles()
140 {
141 $expected = [
142 '1.2' => [],
143 '2.0' => [],
144 'master' => [],
145 ];
146
147 $result = $this->documentationFileService->findDocumentationFiles(vfsStream::url('root/Changelog'));
148 self::assertEquals(array_keys($expected), array_keys($result));
149 }
150
151 /**
152 * @test
153 */
154 public function extractingTagsProvidesTagsAsDesired()
155 {
156 $expected = [
157 'unittest',
158 'cat:Important',
159 ];
160 $result = $this->documentationFileService->findDocumentationFiles(vfsStream::url('root/Changelog'));
161 self::assertEquals($expected, $result['2.0'][98574]['tags']);
162 }
163
164 /**
165 * @test
166 */
167 public function filesAreFilteredByUsersChoice()
168 {
169 $ignoredFiles = ['vfs://root/Changelog/1.2/Breaking-12345-Issue.rst'];
170 $this->registry->get(
171 'upgradeAnalysisIgnoreFilter',
172 'ignoredDocumentationFiles',
173 Argument::any()
174 )->willReturn($ignoredFiles);
175
176 $result = $this->documentationFileService->findDocumentationFiles(vfsStream::url('root/Changelog'));
177 self::assertArrayNotHasKey(12345, $result['1.2']);
178 }
179
180 /**
181 * @return array
182 */
183 public function invalidFilesProvider(): array
184 {
185 return [
186 ['/etc/passwd' => '/etc/passwd'],
187 ['root' => '/'],
188 ];
189 }
190
191 /**
192 * @dataProvider invalidFilesProvider
193 * @param string $path
194 * @test
195 */
196 public function getListEntryThrowsExceptionForFilesNotBelongToChangelogDir(string $path)
197 {
198 $this->expectException(\InvalidArgumentException::class);
199 $this->expectExceptionCode(1485425531);
200 $this->documentationFileService->getListEntry($path);
201 }
202 }