[FEATURE] Storable Filters for Upgrade Analysis
[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\Components\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($this->registry->reveal(),
100 vfsStream::url('root/Changelog'));
101 }
102
103 /**
104 * dataprovider with invalid dir path. They should raise an exception and don't process.
105 * @return array
106 */
107 public function invalidDirProvider()
108 {
109 return [
110 [
111 'root' => '/'
112 ],
113 [
114 'etc' => '/etc'
115 ],
116 [
117 'etc/passwd' => '/etc/passwd'
118 ],
119 ];
120 }
121
122 /**
123 * @dataProvider invalidDirProvider
124 * @test
125 */
126 public function findDocumentationFilesThrowsExceptionIfPathIsNotInGivenChangelogDir(string $path)
127 {
128 $this->expectException(\InvalidArgumentException::class);
129 $this->expectExceptionCode(1485425530);
130 $documentationFileService = new DocumentationFile($this->registry->reveal());
131 $documentationFileService->findDocumentationFiles($path);
132 }
133
134 /**
135 * @test
136 */
137 public function findDocumentationFilesReturnsArrayOfFiles()
138 {
139 $expected = [
140 '1.2' => [],
141 '2.0' => [],
142 'master' => [],
143 ];
144
145 $result = $this->documentationFileService->findDocumentationFiles(vfsStream::url('root/Changelog'));
146 self::assertEquals(array_keys($expected), array_keys($result));
147 }
148
149 /**
150 * @test
151 */
152 public function extractingTagsProvidesTagsAsDesired()
153 {
154 $expected = [
155 'unittest',
156 'cat:Important',
157 ];
158 $result = $this->documentationFileService->findDocumentationFiles(vfsStream::url('root/Changelog'));
159 self::assertEquals($expected, $result['2.0'][98574]['tags']);
160 }
161
162 /**
163 * @test
164 */
165 public function filesAreFilteredByUsersChoice()
166 {
167 $ignoredFiles = ['vfs://root/Changelog/1.2/Breaking-12345-Issue.rst'];
168 $this->registry->get('upgradeAnalysisIgnoreFilter', 'ignoredDocumentationFiles',
169 Argument::any())->willReturn($ignoredFiles);
170
171 $result = $this->documentationFileService->findDocumentationFiles(vfsStream::url('root/Changelog'));
172 self::assertArrayNotHasKey(12345, $result['1.2']);
173 }
174
175 /**
176 * @return array
177 */
178 public function invalidFilesProvider(): array
179 {
180 return [
181 ['/etc/passwd' => '/etc/passwd'],
182 ['root' => '/'],
183 ];
184 }
185
186 /**
187 * @dataProvider invalidFilesProvider
188 * @param string $path
189 * @test
190 */
191 public function getListEntryThrowsExceptionForFilesNotBelongToChangelogDir(string $path)
192 {
193 $this->expectException(\InvalidArgumentException::class);
194 $this->expectExceptionCode(1485425531);
195 $this->documentationFileService->getListEntry($path);
196 }
197 }