View source
<?php
namespace Drupal\Tests\linkchecker\Kernel;
use Drupal\Component\Render\FormattableMarkup;
use Drupal\KernelTests\KernelTestBase;
use Drupal\linkchecker\Entity\LinkCheckerLink;
use GuzzleHttp\Client;
use GuzzleHttp\Handler\MockHandler;
use GuzzleHttp\HandlerStack;
use GuzzleHttp\Psr7\Response;
class LinkcheckerLinkCheckerServiceTest extends KernelTestBase {
public static $modules = [
'system',
'dynamic_entity_reference',
'linkchecker',
];
protected $checkerService;
protected $httpClient;
protected $time;
protected $linkcheckerSetting;
public function setUp() {
parent::setUp();
$this
->installSchema('system', 'sequences');
$this
->installEntitySchema('linkcheckerlink');
$this
->installConfig('linkchecker');
$mock = new MockHandler([
new Response(200, []),
new Response(200, [
'Content-Type' => 'text/html',
'Link' => [
'bar',
'foo',
],
], '<div id="foo"></div>'),
new Response(200, [
'Content-Type' => 'text/html',
'Link' => [
'foo',
'baz',
],
], '<div id="foo"></div>'),
new Response(200, [
'Content-type' => 'text/html',
'Link' => '<https://drupal.org>; rel="my-rel", <https://drupal.org>; rel=shortlink',
], '<div id="bar">This is bar</div>'),
new Response(301, []),
new Response(404, []),
new Response(405, []),
new Response(500, []),
new Response(100, []),
]);
$handler = HandlerStack::create($mock);
$client = new Client([
'handler' => $handler,
]);
$handler2 = HandlerStack::create(clone $mock);
$client2 = new Client([
'handler' => $handler2,
]);
$this->container
->set('http_client', $client);
$this->checkerService = $this->container
->get('linkchecker.checker');
$this->httpClient = $client2;
$this->time = $this->container
->get('datetime.time');
$this->linkcheckerSetting = $this->container
->get('config.factory')
->getEditable('linkchecker.settings');
}
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);
}
$duplicateLink = $this
->createDummyLink(reset($urls));
foreach ($links as $link) {
$linkBeforeUpdate = clone $link;
$this->checkerService
->check($link)
->wait();
$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(),
]));
}
}
$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(),
]));
}
protected function createDummyLink($url) {
$link = LinkCheckerLink::create([
'url' => $url,
'entity_id' => [
'target_id' => 1,
'target_type' => 'dummy_type',
],
'entity_field' => 'dummy_field',
'entity_langcode' => 'en',
]);
$link
->save();
return $link;
}
}