public function LinkcheckerLinkCheckerServiceTest::testStatusHandling in Link checker 8
Test link checker service status handling.
File
- tests/
src/ Kernel/ LinkcheckerLinkCheckerServiceTest.php, line 108
Class
- LinkcheckerLinkCheckerServiceTest
- Test for LinkCheckerService.
Namespace
Drupal\Tests\linkchecker\KernelCode
public function testStatusHandling() {
$ignoreResponseCodes = preg_split('/(\\r\\n?|\\n)/', $this->linkcheckerSetting
->get('error.ignore_response_codes'));
$urls = [
'200',
'200#foo',
'200#baz',
'200#bar',
'301',
'404',
'405',
'500',
'100',
];
$links = [];
foreach ($urls as $url) {
$links[] = $this
->createDummyLink($url);
}
// Create duplicate link.
$duplicateLink = $this
->createDummyLink(reset($urls));
/** @var \Drupal\linkchecker\LinkCheckerLinkInterface $link */
foreach ($links as $link) {
$linkBeforeUpdate = clone $link;
$this->checkerService
->check($link)
->wait();
// We use mocked http client where URL and status code are same.
$response = $this->httpClient
->request($linkBeforeUpdate
->getRequestMethod(), $linkBeforeUpdate
->getUrl(), [
'http_errors' => FALSE,
]);
$expectedStatusCode = $response
->getStatusCode();
$expectedErrorMessage = $response
->getReasonPhrase();
if ($link
->getUrl() == '200#baz') {
$expectedStatusCode = 404;
$expectedErrorMessage = 'URL fragment identifier not found in content';
}
$this
->assertEquals($expectedStatusCode, $link
->getStatusCode(), new FormattableMarkup('Expected status code is @expected. @actual is given', [
'@expected' => $expectedStatusCode,
'@actual' => $link
->getStatusCode(),
]));
$this
->assertEquals($expectedErrorMessage, $link
->getErrorMessage(), new FormattableMarkup('Expected error message is @expected. "@actual" is given', [
'@expected' => $expectedErrorMessage,
'@actual' => $link
->getErrorMessage(),
]));
$this
->assertGreaterThan(0, $link
->getLastCheckTime(), new FormattableMarkup('Expected last check time is greater than @expected. @actual is given', [
'@expected' => 0,
'@actual' => $link
->getLastCheckTime(),
]));
if (in_array($expectedStatusCode, $ignoreResponseCodes)) {
$this
->assertEquals(0, $link
->getFailCount(), new FormattableMarkup('Expected fail count is @expected. @actual is given', [
'@expected' => 0,
'@actual' => $link
->getFailCount(),
]));
}
else {
$this
->assertEquals($linkBeforeUpdate
->getFailCount() + 1, $link
->getFailCount(), new FormattableMarkup('Expected fail count is @expected. @actual is given', [
'@expected' => $linkBeforeUpdate
->getFailCount() + 1,
'@actual' => $link
->getFailCount(),
]));
}
}
// Check if duplicate link was updated.
$link = reset($links);
$duplicateLink = LinkCheckerLink::load($duplicateLink
->id());
$this
->assertEquals($duplicateLink
->getStatusCode(), $link
->getStatusCode(), new FormattableMarkup('Expected status code is @expected. @actual is given', [
'@expected' => $duplicateLink
->getStatusCode(),
'@actual' => $link
->getStatusCode(),
]));
$this
->assertEquals($duplicateLink
->getErrorMessage(), $link
->getErrorMessage(), new FormattableMarkup('Expected error message is @expected. "@actual" is given', [
'@expected' => $duplicateLink
->getErrorMessage(),
'@actual' => $link
->getErrorMessage(),
]));
$this
->assertEquals($duplicateLink
->getLastCheckTime(), $link
->getLastCheckTime(), new FormattableMarkup('Expected last check time is @expected. @actual is given', [
'@expected' => $duplicateLink
->getLastCheckTime(),
'@actual' => $link
->getLastCheckTime(),
]));
$this
->assertEquals($duplicateLink
->getFailCount(), $link
->getFailCount(), new FormattableMarkup('Expected fail count is @expected. @actual is given', [
'@expected' => $duplicateLink
->getFailCount(),
'@actual' => $link
->getFailCount(),
]));
}