Skip to content
Snippets Groups Projects
Commit a0417b5d authored by C_Logemann's avatar C_Logemann
Browse files

Issue #3058014 by Deepak Goyal, JeroenT, C_Logemann, Tomefa: Add support for Redirect module

parent b4ee47a6
No related branches found
No related tags found
No related merge requests found
......@@ -22,6 +22,9 @@
"drupal/core": "^8.8.0 || ^9",
"drupal/dynamic_entity_reference": "^1.0 || ^2.0@alpha"
},
"require-dev": {
"drupal/redirect": "^1.6"
},
"extra": {
"drush": {
"services": {
......
......@@ -6,3 +6,5 @@ configure: linkchecker.admin_settings_form
dependencies:
- dynamic_entity_reference:dynamic_entity_reference
- drupal:system (>= 8.6)
test_dependencies:
- redirect:redirect
......@@ -18,15 +18,29 @@ function linkchecker_redirect_insert($redirect) {
* Implements hook_redirect_update().
*/
function linkchecker_redirect_update($redirect) {
// Get Source URL.
$source = $redirect->getSourceUrl();
// Get host domain.
$host = \Drupal::request()->getSchemeAndHttpHost();
// It's unknown if this is a redirect for HTTP/HTTPS or the encoded urls.
$url_http = Url::fromUri('internal:' . $redirect->source)->toString();
$url_https = Url::fromUri('internal:' . $redirect->source, ['https' => TRUE])->toString();
$url_http = Url::fromUri('internal:' . $source)->toString();
$url_https = Url::fromUri('internal:' . $source, ['https' => TRUE])->toString();
$full_url_http = Url::fromUri($host . $source, ['https' => FALSE])->toString();
$full_url_https = Url::fromUri($host . $source, ['https' => TRUE])->toString();
$urls = [
$source,
$url_http,
$url_https,
$full_url_http,
$full_url_https,
rawurldecode($source),
rawurldecode($url_http),
rawurldecode($url_https),
rawurldecode($full_url_http),
rawurldecode($full_url_https),
];
_linkchecker_redirect_reset($urls);
......@@ -40,12 +54,26 @@ function linkchecker_redirect_update($redirect) {
*/
function _linkchecker_redirect_reset(array $urls = []) {
$urls = array_unique($urls);
$num_updated = \Drupal::database()->update('linkchecker_link')
->condition('urlhash', array_map('\Drupal\Component\Utility\Crypt::hashBase64', $urls))
->condition('fail_count', 0, '>')
->condition('status', 1)
->fields(['last_checked' => 0])
->execute();
$linkCheckerLinkStorage = \Drupal::entityTypeManager()->getStorage('linkcheckerlink');
$query = $linkCheckerLinkStorage->getQuery();
$query->condition('urlhash', array_map('\Drupal\Component\Utility\Crypt::hashBase64', $urls), 'IN');
$query->condition('fail_count', 0, '>');
$query->condition('status', 1);
$linkcheckerLinkIds = $query->execute();
if (empty($linkcheckerLinkIds)) {
return;
}
$num_updated = 0;
$linkcheckerLinks = $linkCheckerLinkStorage->loadMultiple($linkcheckerLinkIds);
foreach ($linkcheckerLinks as $linkcheckerLink) {
$linkcheckerLink->set('last_check', 0);
$linkcheckerLink->save();
$num_updated++;
}
if ($num_updated) {
\Drupal::messenger()->addMessage(t('The link %url will be checked again on the next cron run.', ['%url' => $urls[0]]));
......
<?php
namespace Drupal\Tests\linkchecker\Kernel;
use Drupal\KernelTests\KernelTestBase;
use Drupal\node\Entity\NodeType;
use Drupal\Tests\node\Traits\NodeCreationTrait;
/**
* Test linkchecker with the redirect module.
*
* @group linkchecker
*/
class LinkcheckerRedirectTest extends KernelTestBase {
use NodeCreationTrait;
/**
* The redirect storage.
*
* @var \Drupal\Core\Entity\EntityStorageInterface
*/
protected $redirectStorage;
/**
* The linkcheckerlink storage.
*
* @var \Drupal\Core\Entity\EntityStorageInterface
*/
protected $linkCheckerLinkStorage;
/**
* {@inheritdoc}
*/
public static $modules = [
'user',
'node',
'filter',
'field',
'text',
'system',
'path_alias',
'link',
'views',
'dynamic_entity_reference',
'linkchecker',
'redirect',
];
/**
* {@inheritdoc}
*/
public function setUp() {
parent::setUp();
$this->installSchema('system', 'sequences');
$this->installSchema('linkchecker', 'linkchecker_index');
$this->installEntitySchema('user');
$this->installEntitySchema('node');
$this->installEntitySchema('redirect');
$this->installEntitySchema('linkcheckerlink');
$this->installConfig([
'field',
'node',
'filter',
'linkchecker',
'redirect',
]);
$this->cron = $this->container->get('cron');
$this->redirectStorage = $this->container->get('entity_type.manager')
->getStorage('redirect');
$this->linkCheckerLinkStorage = $this->container->get('entity_type.manager')
->getStorage('linkcheckerlink');
NodeType::create(['name' => 'Links', 'type' => 'links']);
}
/**
* Test the linkchecker module with redirect integration.
*/
public function testLinkcheckerRedirect() {
$node = $this->createNode(['type' => 'links']);
$this->linkCheckerLinkStorage->create([
'url' => '/unexisting-url',
'entity_id' => [
'target_id' => $node->id(),
'target_type' => $node->getEntityTypeId(),
],
'entity_field' => 'body',
'entity_langcode' => $node->language()->getId(),
'last_check' => 680356800,
'fail_count' => 3,
'status' => 1,
])->save();
$redirect = $this->redirectStorage->create();
$redirect->setSource('unexisting-url');
$redirect->setRedirect('<front>');
$redirect->setStatusCode(301);
$redirect->save();
$links = $this->linkCheckerLinkStorage->loadByProperties(['url' => '/unexisting-url']);
$this->assertNotEmpty($links);
$link = current($links);
// Make sure the last_check value is reset when a redirect is created.
$this->assertEquals(0, $link->get('last_check')->value);
}
}
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment