‪TYPO3CMS  ‪main
LinkAnalyzerTest.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\DataProvider;
21 use PHPUnit\Framework\Attributes\Test;
24 use TYPO3\TestingFramework\Core\Functional\FunctionalTestCase;
25 
26 final class ‪LinkAnalyzerTest extends FunctionalTestCase
27 {
28  protected array ‪$coreExtensionsToLoad = [
29  'info',
30  'seo',
31  'linkvalidator',
32  ];
33 
38  protected function ‪setUp(): void
39  {
40  parent::setUp();
41  ‪$GLOBALS['LANG'] = $this->get(LanguageServiceFactory::class)->create('default');
42  }
43 
44  public static function ‪findAllBrokenLinksDataProvider(): array
45  {
46  return [
47  'Test with one broken external link (not existing domain)' =>
48  [
49  __DIR__ . '/Fixtures/input_content_with_broken_link_external.csv',
50  [1],
51  __DIR__ . '/Fixtures/expected_output_content_with_broken_link_external.csv',
52  ],
53  'Test with one broken external link in pages:canonical_link' =>
54  [
55  __DIR__ . '/Fixtures/input_page_with_broken_link_external_in_canonical_link.csv',
56  [1],
57  __DIR__ . '/Fixtures/expected_output_with_broken_link_external_in_canonical_link.csv',
58  ],
59  'Test with one broken page link (not existing page)' =>
60  [
61  __DIR__ . '/Fixtures/input_content_with_broken_link_page.csv',
62  [1],
63  __DIR__ . '/Fixtures/expected_output_content_with_broken_link_page.csv',
64  ],
65  'Test with one broken file link (not existing file)' =>
66  [
67  __DIR__ . '/Fixtures/input_content_with_broken_link_file.csv',
68  [1],
69  __DIR__ . '/Fixtures/expected_output_content_with_broken_link_file.csv',
70  ],
71  'Test with several broken external, page and file links' =>
72  [
73  __DIR__ . '/Fixtures/input_content_with_broken_links_several.csv',
74  [1],
75  __DIR__ . '/Fixtures/expected_output_content_with_broken_links_several.csv',
76  ],
77  'Test with several pages with broken external, page and file links' =>
78  [
79  __DIR__ . '/Fixtures/input_content_with_broken_links_several_pages.csv',
80  [1, 2],
81  __DIR__ . '/Fixtures/expected_output_content_with_broken_links_several_pages.csv',
82  ],
83  ];
84  }
85 
86  #[DataProvider('findAllBrokenLinksDataProvider')]
87  #[Test]
88  public function ‪getLinkStatisticsFindAllBrokenLinks(string $inputFile, array $pidList, string $expectedOutputFile): void
89  {
90  $tsConfig = [
91  'searchFields' => [
92  'pages' => ['media', 'url', 'canonical_link'],
93  'tt_content' => ['bodytext', 'header_link', 'records'],
94  ],
95  'linktypes' => 'db,file,external',
96  'checkhidden' => '0',
97  ];
98  $linkTypes = explode(',', $tsConfig['linktypes']);
99 
100  $searchFields = $tsConfig['searchFields'];
101 
102  $this->importCSVDataSet($inputFile);
103 
104  $linkAnalyzer = $this->get(LinkAnalyzer::class);
105  $linkAnalyzer->init($searchFields, $pidList, $tsConfig);
106  $linkAnalyzer->getLinkStatistics($linkTypes);
107 
108  $this->assertCSVDataSet($expectedOutputFile);
109  }
110 
111  public static function ‪findFindOnlyFileBrokenLinksDataProvider(): array
112  {
113  return [
114  // Tests with one broken link
115  'Test with one broken external link' =>
116  [
117  __DIR__ . '/Fixtures/input_content_with_broken_link_external.csv',
118  [1],
119  __DIR__ . '/Fixtures/expected_output_content_with_broken_link_none.csv',
120  ],
121  'Test with one broken page link' =>
122  [
123  __DIR__ . '/Fixtures/input_content_with_broken_link_page.csv',
124  [1],
125  __DIR__ . '/Fixtures/expected_output_content_with_broken_link_none.csv',
126  ],
127  'Test with one broken file link' =>
128  [
129  __DIR__ . '/Fixtures/input_content_with_broken_link_file.csv',
130  [1],
131  __DIR__ . '/Fixtures/expected_output_content_with_broken_link_file.csv',
132  ],
133  ];
134  }
135 
136  #[DataProvider('findFindOnlyFileBrokenLinksDataProvider')]
137  #[Test]
138  public function ‪getLinkStatisticsFindOnlyFileBrokenLinks(string $inputFile, array $pidList, string $expectedOutputFile): void
139  {
140  $tsConfig = [
141  'searchFields' => [
142  'pages' => ['media', 'url'],
143  'tt_content' => ['bodytext', 'header_link', 'records'],
144  ],
145  'linktypes' => 'file',
146  'checkhidden' => '0',
147  ];
148  $linkTypes = explode(',', $tsConfig['linktypes']);
149 
150  $searchFields = $tsConfig['searchFields'];
151 
152  $this->importCSVDataSet($inputFile);
153 
154  $linkAnalyzer = $this->get(LinkAnalyzer::class);
155  $linkAnalyzer->init($searchFields, $pidList, $tsConfig);
156  $linkAnalyzer->getLinkStatistics($linkTypes);
157 
158  $this->assertCSVDataSet($expectedOutputFile);
159  }
160 
161  public static function ‪findFindOnlyPageBrokenLinksDataProvider(): array
162  {
163  return [
164  // Tests with one broken link
165  'Test with one broken external link' =>
166  [
167  __DIR__ . '/Fixtures/input_content_with_broken_link_external.csv',
168  [1],
169  __DIR__ . '/Fixtures/expected_output_content_with_broken_link_none.csv',
170  ],
171  'Test with one broken page link' =>
172  [
173  __DIR__ . '/Fixtures/input_content_with_broken_link_page.csv',
174  [1],
175  __DIR__ . '/Fixtures/expected_output_content_with_broken_link_page.csv',
176  ],
177  'Test with one broken file link' =>
178  [
179  __DIR__ . '/Fixtures/input_content_with_broken_link_file.csv',
180  [1],
181  __DIR__ . '/Fixtures/expected_output_content_with_broken_link_none.csv',
182  ],
183  ];
184  }
185 
186  #[DataProvider('findFindOnlyPageBrokenLinksDataProvider')]
187  #[Test]
188  public function ‪getLinkStatisticsFindOnlyPageBrokenLinks(string $inputFile, array $pidList, string $expectedOutputFile): void
189  {
190  $tsConfig = [
191  'searchFields' => [
192  'pages' => ['media', 'url'],
193  'tt_content' => ['bodytext', 'header_link', 'records'],
194  ],
195  'linktypes' => 'db',
196  'checkhidden' => '0',
197  ];
198  $linkTypes = explode(',', $tsConfig['linktypes']);
199 
200  $searchFields = $tsConfig['searchFields'];
201 
202  $this->importCSVDataSet($inputFile);
203 
204  $linkAnalyzer = $this->get(LinkAnalyzer::class);
205  $linkAnalyzer->init($searchFields, $pidList, $tsConfig);
206  $linkAnalyzer->getLinkStatistics($linkTypes);
207 
208  $this->assertCSVDataSet($expectedOutputFile);
209  }
210 
211  public static function ‪findFindOnlyExternalBrokenLinksDataProvider(): array
212  {
213  return [
214  // Tests with one broken link
215  'Test with one broken external link' =>
216  [
217  __DIR__ . '/Fixtures/input_content_with_broken_link_external.csv',
218  [1],
219  __DIR__ . '/Fixtures/expected_output_content_with_broken_link_external.csv',
220  ],
221  'Test with one broken page link' =>
222  [
223  __DIR__ . '/Fixtures/input_content_with_broken_link_page.csv',
224  [1],
225  __DIR__ . '/Fixtures/expected_output_content_with_broken_link_none.csv',
226  ],
227  'Test with one broken file link' =>
228  [
229  __DIR__ . '/Fixtures/input_content_with_broken_link_file.csv',
230  [1],
231  __DIR__ . '/Fixtures/expected_output_content_with_broken_link_none.csv',
232  ],
233  ];
234  }
235 
236  #[DataProvider('findFindOnlyExternalBrokenLinksDataProvider')]
237  #[Test]
238  public function ‪getLinkStatisticsFindOnlyExternalBrokenLinksInBodytext(string $inputFile, array $pidList, string $expectedOutputFile): void
239  {
240  $tsConfig = [
241  'searchFields' => [
242  'tt_content' => ['bodytext'],
243  ],
244  'linktypes' => 'external',
245  'checkhidden' => '0',
246  ];
247  $linkTypes = explode(',', $tsConfig['linktypes']);
248 
249  $searchFields = $tsConfig['searchFields'];
250 
251  $this->importCSVDataSet($inputFile);
252 
253  $linkAnalyzer = $this->get(LinkAnalyzer::class);
254  $linkAnalyzer->init($searchFields, $pidList, $tsConfig);
255  $linkAnalyzer->getLinkStatistics($linkTypes);
256 
257  $this->assertCSVDataSet($expectedOutputFile);
258  }
259 
261  {
262  $lagePageUidList = range(1, 200000, 1);
263  return [
264  // Tests with one broken link
265  'Test with one broken external link' =>
266  [
267  __DIR__ . '/Fixtures/input_content_with_broken_link_external.csv',
268  $lagePageUidList,
269  __DIR__ . '/Fixtures/expected_output_content_with_broken_link_external.csv',
270  ],
271  ];
272  }
273 
274  #[DataProvider('getLinkStatisticsFindOnlyExternalBrokenLinksInBodytextWithHugeListOfPageIdsDataProvider')]
275  #[Test]
276  public function ‪getLinkStatisticsFindOnlyExternalBrokenLinksInBodytextWithHugeListOfPageIds(string $inputFile, array $pidList, string $expectedOutputFile): void
277  {
278  $tsConfig = [
279  'searchFields' => [
280  'tt_content' => ['bodytext'],
281  ],
282  'linktypes' => 'external',
283  'checkhidden' => '0',
284  ];
285  $linkTypes = explode(',', $tsConfig['linktypes']);
286 
287  $searchFields = $tsConfig['searchFields'];
288 
289  $this->importCSVDataSet($inputFile);
290 
291  $linkAnalyzer = $this->get(LinkAnalyzer::class);
292  $linkAnalyzer->init($searchFields, $pidList, $tsConfig);
293  $linkAnalyzer->getLinkStatistics($linkTypes);
294 
295  $this->assertCSVDataSet($expectedOutputFile);
296  }
297 }
‪TYPO3\CMS\Core\Localization\LanguageServiceFactory
Definition: LanguageServiceFactory.php:25
‪TYPO3\CMS\Linkvalidator\Tests\Functional
‪$GLOBALS
‪$GLOBALS['TYPO3_CONF_VARS']['EXTCONF']['adminpanel']['modules']
Definition: ext_localconf.php:25