‪TYPO3CMS  10.4
BrokenLinkRepositoryTest.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 Psr\EventDispatcher\EventDispatcherInterface;
24 use TYPO3\TestingFramework\Core\Functional\FunctionalTestCase;
25 
26 class ‪BrokenLinkRepositoryTest extends FunctionalTestCase
27 {
31  protected ‪$coreExtensionsToLoad = [
32  'linkvalidator',
33  'seo'
34  ];
35 
40 
41  protected ‪$beusers = [
42  'admin' => [
43  'fixture' => 'PACKAGE:typo3/testing-framework/Resources/Core/Functional/Fixtures/be_users.xml',
44  'uid' => 1,
45  'groupFixture' => ''
46  ],
47  'no group' => [
48  'fixture' => 'EXT:linkvalidator/Tests/Functional/Repository/Fixtures/be_users.xml',
49  'uid' => 2,
50  'groupFixture' => ''
51  ],
52  // write access to pages, tt_content
53  'group 1' => [
54  'fixture' => 'EXT:linkvalidator/Tests/Functional/Repository/Fixtures/be_users.xml',
55  'uid' => 3,
56  'groupFixture' => 'EXT:linkvalidator/Tests/Functional/Repository/Fixtures/be_groups.xml'
57  ],
58  // write access to pages, tt_content, exclude field pages.header_link
59  'group 2' => [
60  'fixture' => 'EXT:linkvalidator/Tests/Functional/Repository/Fixtures/be_users.xml',
61  'uid' => 4,
62  'groupFixture' => 'EXT:linkvalidator/Tests/Functional/Repository/Fixtures/be_groups.xml'
63  ],
64  // write access to pages, tt_content (restricted to default language)
65  'group 3' => [
66  'fixture' => 'EXT:linkvalidator/Tests/Functional/Repository/Fixtures/be_users.xml',
67  'uid' => 5,
68  'groupFixture' => 'EXT:linkvalidator/Tests/Functional/Repository/Fixtures/be_groups.xml'
69  ],
70  // group 6: access to all, but restricted via explicit allow to CType=texmedia and text
71  'group 6' => [
72  'fixture' => 'EXT:linkvalidator/Tests/Functional/Repository/Fixtures/be_users.xml',
73  'uid' => 6,
74  'groupFixture' => 'EXT:linkvalidator/Tests/Functional/Repository/Fixtures/be_groups.xml'
75  ],
76 
77  ];
78 
79  protected function ‪setUp(): void
80  {
81  parent::setUp();
82 
84 
85  ‪$GLOBALS['TYPO3_CONF_VARS']['BE']['explicitADmode'] = 'explicitAllow';
86  $this->brokenLinksRepository = new ‪BrokenLinkRepository();
87  }
88 
90  {
91  yield 'Admin user should see all broken links' =>
92  [
93  // backendUser: 1=admin
94  $this->beusers['admin'],
95  // input file for DB
96  __DIR__ . '/Fixtures/input.xml',
97  //pids
98  [1],
99  // expected result:
100  [
101  'db' => 1,
102  'file' => 1,
103  'external' => 2,
104  'total' => 4,
105  ]
106  ];
107  yield 'User with no group should see none' =>
108  [
109  // backend user
110  $this->beusers['no group'],
111  // input file for DB
112  __DIR__ . '/Fixtures/input.xml',
113  //pids
114  [1],
115  // expected result:
116  [
117  'total' => 0,
118  ]
119  ];
120  yield 'User with permission to pages but not to specific tables should see none' =>
121  [
122  // backend user
123  $this->beusers['no group'],
124  // input file for DB
125  __DIR__ . '/Fixtures/input_permissions_user_2.xml',
126  //pids
127  [1],
128  // expected result:
129  [
130  'total' => 0,
131  ]
132  ];
133  yield 'User with permission to pages and to specific tables, but no exclude fields should see 3 of 4 broken links' =>
134  [
135  // backend user
136  $this->beusers['group 1'],
137  // input file for DB
138  __DIR__ . '/Fixtures/input_permissions_user_3.xml',
139  //pids
140  [1],
141  // expected result:
142  [
143  'db' => 1,
144  'file' => 1,
145  'external' => 1,
146  'total' => 3
147  ]
148  ];
149  yield 'User with permission to pages, specific tables and exclude fields should see all broken links' =>
150  [
151  // backend user
152  $this->beusers['group 2'],
153  // input file for DB
154  __DIR__ . '/Fixtures/input_permissions_user_4.xml',
155  //pids
156  [1],
157  // expected result:
158  [
159  'db' => 1,
160  'file' => 1,
161  'external' => 2,
162  'total' => 4
163  ]
164  ];
165  yield 'User has write permission only for Ctype textmedia and text, should see only broken links from textmedia records' =>
166  [
167  // backend user
168  $this->beusers['group 6'],
169  // input file for DB
170  __DIR__ . '/Fixtures/input_permissions_user_6_explicit_allow.xml',
171  //pids
172  [1],
173  // expected result:
174  [
175  'external' => 1,
176  'total' => 1
177  ]
178  ];
179 
180  yield 'User has write permission only for default language and should see only 1 of 2 broken links' =>
181  [
182  // backend user
183  $this->beusers['group 3'],
184  // input file for DB
185  __DIR__ . '/Fixtures/input_permissions_user_5.xml',
186  //pids
187  [1],
188  // expected result:
189  [
190  'external' => 1,
191  'total' => 1
192  ]
193  ];
194  }
195 
201  array $beuser,
202  string $inputFile,
203  array $pidList,
204  array $expectedOutput
205  ) {
206  $config = [
207  'db' => '1',
208  'file' => '1',
209  'external' => '1',
210  'linkhandler' => '1'
211 
212  ];
213 
214  $tsConfig = [
215  'searchFields.' => [
216  'pages' => 'media,url,canonical_link',
217  'tt_content' => 'bodytext,header_link,records'
218  ],
219  'linktypes' => 'db,file,external,linkhandler',
220  'checkhidden' => '0',
221  'linkhandler' => [
222  'reportHiddenRecords' => '0'
223  ]
224  ];
225 
226  $searchFields = $tsConfig['searchFields.'];
227  foreach ($searchFields as $table => ‪$fields) {
228  $searchFields[$table] = explode(',', ‪$fields);
229  }
230 
231  $this->‪setupBackendUserAndGroup($beuser['uid'], $beuser['fixture'], $beuser['groupFixture']);
232 
233  $this->importDataSet($inputFile);
234 
235  $linkAnalyzer = new ‪LinkAnalyzer(
236  $this->prophesize(EventDispatcherInterface::class)->reveal(),
237  $this->brokenLinksRepository
238  );
239  $linkAnalyzer->init($searchFields, implode(',', $pidList), $tsConfig);
240  $linkAnalyzer->getLinkStatistics($config);
241  $result = $this->brokenLinksRepository->getNumberOfBrokenLinksForRecordsOnPages(
242  $pidList,
243  $searchFields
244  );
245 
246  self::assertEquals($expectedOutput, $result);
247  }
248 
250  {
251  yield 'Admin user should see all broken links' =>
252  [
253  // backendUser: 1=admin
254  $this->beusers['admin'],
255  // input file for DB
256  __DIR__ . '/Fixtures/input.xml',
257  //pids
258  [1],
259  // count
260  4
261  ];
262 
263  yield 'User with no group should see none' =>
264  [
265  // backend user
266  $this->beusers['no group'],
267  // input file for DB
268  __DIR__ . '/Fixtures/input.xml',
269  //pids
270  [1],
271  // count
272  0
273  ];
274  yield 'User with permission to pages but not to specific tables should see none' =>
275  [
276  // backend user
277  $this->beusers['no group'],
278  // input file for DB
279  __DIR__ . '/Fixtures/input_permissions_user_2.xml',
280  //pids
281  [1],
282  // count
283  0
284  ];
285  yield 'User with permission to pages and to specific tables, but no exclude fields should see 3 of 4 broken links' =>
286  [
287  // backend user
288  $this->beusers['group 1'],
289  // input file for DB
290  __DIR__ . '/Fixtures/input_permissions_user_3.xml',
291  //pids
292  [1],
293  // count
294  3
295  ];
296  yield 'User with permission to pages, specific tables and exclude fields should see all broken links' =>
297  [
298  // backend user
299  $this->beusers['group 2'],
300  // input file for DB
301  __DIR__ . '/Fixtures/input_permissions_user_4.xml',
302  //pids
303  [1],
304  // count
305  4
306  ];
307  yield 'User has write permission only for Ctype textmedia and text, should see only broken links from textmedia records' =>
308  [
309  // backend user
310  $this->beusers['group 6'],
311  // input file for DB
312  __DIR__ . '/Fixtures/input_permissions_user_6_explicit_allow.xml',
313  //pids
314  [1],
315  // count
316  1
317  ];
318 
319  yield 'User has write permission only for default language and should see only 1 of 2 broken links' =>
320  [
321  // backend user
322  $this->beusers['group 3'],
323  // input file for DB
324  __DIR__ . '/Fixtures/input_permissions_user_5.xml',
325  //pids
326  [1],
327  // count
328  1
329  ];
330  }
331 
337  array $beuser,
338  string $inputFile,
339  array $pidList,
340  int $expectedCount
341  ) {
342  $config = [
343  'db' => '1',
344  'file' => '1',
345  'external' => '1',
346  'linkhandler' => '1'
347 
348  ];
349 
350  $linkTypes = [
351  'db',
352  'file',
353  'external'
354  ];
355 
356  $tsConfig = [
357  'searchFields.' => [
358  'pages' => 'media,url,canonical_link',
359  'tt_content' => 'bodytext,header_link,records'
360  ],
361  'linktypes' => 'db,file,external,linkhandler',
362  'checkhidden' => '0',
363  'linkhandler' => [
364  'reportHiddenRecords' => '0'
365  ]
366  ];
367 
368  $searchFields = $tsConfig['searchFields.'];
369  foreach ($searchFields as $table => ‪$fields) {
370  $searchFields[$table] = explode(',', ‪$fields);
371  }
372 
373  $this->‪setupBackendUserAndGroup($beuser['uid'], $beuser['fixture'], $beuser['groupFixture']);
374 
375  $this->importDataSet($inputFile);
376 
377  $linkAnalyzer = new ‪LinkAnalyzer(
378  $this->prophesize(EventDispatcherInterface::class)->reveal(),
379  $this->brokenLinksRepository
380  );
381  $linkAnalyzer->init($searchFields, implode(',', $pidList), $tsConfig);
382  $linkAnalyzer->getLinkStatistics($config);
383 
384  $results = $this->brokenLinksRepository->getAllBrokenLinksForPages(
385  $pidList,
386  $linkTypes,
387  $searchFields
388  );
389 
390  self::assertEquals($expectedCount, count($results));
391  }
392 
394  {
395  yield 'Admin user should see all broken links' =>
396  [
397  // backendUser: 1=admin
398  $this->beusers['admin'],
399  // input file for DB
400  __DIR__ . '/Fixtures/input.xml',
401  //pids
402  [1],
403  // expected result:
404  [
405  [
406  'record_uid' => 1,
407  'record_pid' => 1,
408  'language' => 0,
409  'headline' => 'link',
410  'field' => 'bodytext',
411  'table_name' => 'tt_content',
412  'element_type' => 'textmedia',
413  'link_title' => 'link',
414  'url' => 'https://sfsfsfsfdfsfsdfsf/sfdsfsds',
415  'link_type' => 'external',
416  'needs_recheck' => 0
417  ],
418  [
419  'record_uid' => 2,
420  'record_pid' => 1,
421  'language' => 0,
422  'headline' => '[No title]',
423  'field' => 'header_link',
424  'table_name' => 'tt_content',
425  'element_type' => 'textmedia',
426  'link_title' => null,
427  'url' => 'https://sfsfsfsfdfsfsdfsf/sfdsfsds',
428  'link_type' => 'external',
429  'needs_recheck' => 0
430  ],
431  [
432  'record_uid' => 3,
433  'record_pid' => 1,
434  'language' => 0,
435  'headline' => 'broken link',
436  'field' => 'bodytext',
437  'table_name' => 'tt_content',
438  'element_type' => 'textmedia',
439  'link_title' => 'broken link',
440  'url' => '85',
441  'link_type' => 'db',
442  'needs_recheck' => 0
443  ],
444  [
445  'record_uid' => 5,
446  'record_pid' => 1,
447  'language' => 0,
448  'headline' => 'broken link',
449  'field' => 'bodytext',
450  'table_name' => 'tt_content',
451  'element_type' => 'textmedia',
452  'link_title' => 'broken link',
453  'url' => 'file:88',
454  'link_type' => 'file',
455  'needs_recheck' => 0
456  ],
457  ]
458  ];
459 
460  yield 'User with no group should see none' =>
461  [
462  // backend user
463  $this->beusers['no group'],
464  // input file for DB
465  __DIR__ . '/Fixtures/input.xml',
466  //pids
467  [1],
468  // expected result:
469  []
470  ];
471  yield 'User with permission to pages but not to specific tables should see none' =>
472  [
473  // backend user
474  $this->beusers['no group'],
475  // input file for DB
476  __DIR__ . '/Fixtures/input_permissions_user_2.xml',
477  //pids
478  [1],
479  // expected result:
480  []
481  ];
482  yield 'User with permission to pages and to specific tables, but no exclude fields should see 3 of 4 broken links' =>
483  [
484  // backend user
485  $this->beusers['group 1'],
486  // input file for DB
487  __DIR__ . '/Fixtures/input_permissions_user_3.xml',
488  //pids
489  [1],
490  // expected result:
491  [
492  [
493  'record_uid' => 1,
494  'record_pid' => 1,
495  'language' => 0,
496  'headline' => 'link',
497  'field' => 'bodytext',
498  'table_name' => 'tt_content',
499  'element_type' => 'textmedia',
500  'link_title' => 'link',
501  'url' => 'https://sfsfsfsfdfsfsdfsf/sfdsfsds',
502  'link_type' => 'external',
503  'needs_recheck' => 0
504  ],
505  [
506  'record_uid' => 3,
507  'record_pid' => 1,
508  'language' => 0,
509  'headline' => 'broken link',
510  'field' => 'bodytext',
511  'table_name' => 'tt_content',
512  'element_type' => 'textmedia',
513  'link_title' => 'broken link',
514  'url' => '85',
515  'link_type' => 'db',
516  'needs_recheck' => 0
517  ],
518  [
519  'record_uid' => 5,
520  'record_pid' => 1,
521  'language' => 0,
522  'headline' => 'broken link',
523  'field' => 'bodytext',
524  'table_name' => 'tt_content',
525  'element_type' => 'textmedia',
526  'link_title' => 'broken link',
527  'url' => 'file:88',
528  'link_type' => 'file',
529  'needs_recheck' => 0
530  ],
531  ]
532  ];
533  yield 'User with permission to pages, specific tables and exclude fields should see all broken links' =>
534  [
535  // backend user
536  $this->beusers['group 2'],
537  // input file for DB
538  __DIR__ . '/Fixtures/input_permissions_user_4.xml',
539  //pids
540  [1],
541  // expected result:
542  [
543  [
544  'record_uid' => 1,
545  'record_pid' => 1,
546  'language' => 0,
547  'headline' => 'link',
548  'field' => 'bodytext',
549  'table_name' => 'tt_content',
550  'element_type' => 'textmedia',
551  'link_title' => 'link',
552  'url' => 'https://sfsfsfsfdfsfsdfsf/sfdsfsds',
553  'link_type' => 'external',
554  'needs_recheck' => 0
555  ],
556  [
557  'record_uid' => 2,
558  'record_pid' => 1,
559  'language' => 0,
560  'headline' => '[No title]',
561  'field' => 'header_link',
562  'table_name' => 'tt_content',
563  'element_type' => 'textmedia',
564  'link_title' => null,
565  'url' => 'https://sfsfsfsfdfsfsdfsf/sfdsfsds',
566  'link_type' => 'external',
567  'needs_recheck' => 0
568  ],
569  [
570  'record_uid' => 3,
571  'record_pid' => 1,
572  'language' => 0,
573  'headline' => 'broken link',
574  'field' => 'bodytext',
575  'table_name' => 'tt_content',
576  'element_type' => 'textmedia',
577  'link_title' => 'broken link',
578  'url' => '85',
579  'link_type' => 'db',
580  'needs_recheck' => 0
581  ],
582  [
583  'record_uid' => 5,
584  'record_pid' => 1,
585  'language' => 0,
586  'headline' => 'broken link',
587  'field' => 'bodytext',
588  'table_name' => 'tt_content',
589  'element_type' => 'textmedia',
590  'link_title' => 'broken link',
591  'url' => 'file:88',
592  'link_type' => 'file',
593  'needs_recheck' => 0
594  ],
595  ]
596  ];
597  yield 'User has write permission only for Ctype textmedia and text, should see only broken links from textmedia records' =>
598  [
599  // backend user
600  $this->beusers['group 6'],
601  // input file for DB
602  __DIR__ . '/Fixtures/input_permissions_user_6_explicit_allow.xml',
603  //pids
604  [1],
605  // expected result:
606  [
607  [
608  'record_uid' => 1,
609  'record_pid' => 1,
610  'language' => 0,
611  'headline' => 'link',
612  'field' => 'bodytext',
613  'table_name' => 'tt_content',
614  'element_type' => 'textmedia',
615  'link_title' => 'link',
616  'url' => 'https://sfsfsfsfdfsfsdfsf/sfdsfsds',
617  'link_type' => 'external',
618  'needs_recheck' => 0
619  ],
620  ]
621  ];
622 
623  yield 'User has write permission only for default language and should see only 1 of 2 broken links' =>
624  [
625  // backend user
626  $this->beusers['group 3'],
627  // input file for DB
628  __DIR__ . '/Fixtures/input_permissions_user_5.xml',
629  //pids
630  [1],
631  // expected result:
632  [
633  [
634  'record_uid' => 1,
635  'record_pid' => 1,
636  'language' => 0,
637  'headline' => 'link',
638  'field' => 'bodytext',
639  'table_name' => 'tt_content',
640  'element_type' => 'textmedia',
641  'link_title' => 'link',
642  'url' => 'https://sfsfsfsfdfsfsdfsf/sfdsfsds',
643  'link_type' => 'external',
644  'needs_recheck' => 0
645  ],
646  ]
647  ];
648  }
649 
655  array $beuser,
656  string $inputFile,
657  array $pidList,
658  array $expectedResult
659  ) {
660  $config = [
661  'db' => '1',
662  'file' => '1',
663  'external' => '1',
664  'linkhandler' => '1'
665 
666  ];
667 
668  $linkTypes = [
669  'db',
670  'file',
671  'external'
672  ];
673 
674  $tsConfig = [
675  'searchFields.' => [
676  'pages' => 'media,url,canonical_link',
677  'tt_content' => 'bodytext,header_link,records'
678  ],
679  'linktypes' => 'db,file,external,linkhandler',
680  'checkhidden' => '0',
681  'linkhandler' => [
682  'reportHiddenRecords' => '0'
683  ]
684  ];
685 
686  $searchFields = $tsConfig['searchFields.'];
687  foreach ($searchFields as $table => ‪$fields) {
688  $searchFields[$table] = explode(',', ‪$fields);
689  }
690 
691  $this->‪setupBackendUserAndGroup($beuser['uid'], $beuser['fixture'], $beuser['groupFixture']);
692 
693  $this->importDataSet($inputFile);
694 
695  $linkAnalyzer = new ‪LinkAnalyzer(
696  $this->prophesize(EventDispatcherInterface::class)->reveal(),
697  $this->brokenLinksRepository
698  );
699  $linkAnalyzer->init($searchFields, implode(',', $pidList), $tsConfig);
700  $linkAnalyzer->getLinkStatistics($config);
701 
702  $results = $this->brokenLinksRepository->getAllBrokenLinksForPages(
703  $pidList,
704  $linkTypes,
705  $searchFields
706  );
707 
708  foreach ($results as &$result) {
709  unset($result['url_response']);
710  unset($result['uid']);
711  unset($result['last_check']);
712  }
713  self::assertEquals($expectedResult, $results);
714  }
715 
716  protected function ‪setupBackendUserAndGroup(int $uid, string $fixtureFile, string $groupFixtureFile)
717  {
718  if ($groupFixtureFile) {
719  $this->importDataSet($groupFixtureFile);
720  }
721  $this->backendUserFixture = $fixtureFile;
722  $this->setUpBackendUserFromFixture($uid);
723  }
724 }
‪$fields
‪$fields
Definition: pages.php:5
‪TYPO3\CMS\Linkvalidator\Tests\Functional\Repository
Definition: BrokenLinkRepositoryTest.php:18
‪TYPO3\CMS\Core\Core\Bootstrap\initializeLanguageObject
‪static initializeLanguageObject()
Definition: Bootstrap.php:617
‪$GLOBALS
‪$GLOBALS['TYPO3_CONF_VARS']['EXTCONF']['adminpanel']['modules']
Definition: ext_localconf.php:5
‪TYPO3\CMS\Core\Core\Bootstrap
Definition: Bootstrap.php:66