Commit 528b3198 authored by Sascha Grossenbacher's avatar Sascha Grossenbacher Committed by Sascha Grossenbacher
Browse files

Issue #3332989 by Primsi, Berdir: Check 404 exclude patterns before adding record on ignore

parent cccc57ba
Loading
Loading
Loading
Loading
+18 −0
Original line number Diff line number Diff line
@@ -6,6 +6,7 @@
 */

use Drupal\Core\Form\FormStateInterface;
use Drupal\Core\Language\LanguageInterface;
use Drupal\Core\Url;
use Drupal\redirect\Entity\Redirect;

@@ -69,6 +70,12 @@ function redirect_404_form_redirect_settings_form_alter(&$form, FormStateInterfa
    ]),
  ];

  $form['clear_ignored'] = [
    '#type' => 'checkbox',
    '#title' => t('Clear ignored 404 log entries when saving this form'),
    '#default_value' => FALSE,
  ];

  $form['suppress_404'] = [
    '#type' => 'checkbox',
    '#title' => t("Suppress 'page not found' log messages"),
@@ -100,6 +107,13 @@ function redirect_404_logging_settings_submit($form, FormStateInterface $form_st
    ->set('pages', $pages)
    ->set('suppress_404', $form_state->getValue('suppress_404'))
    ->save();

  // Remove the filtered out items.
  /** @var \Drupal\redirect_404\RedirectNotFoundStorageInterface; $redirect_storage */
  $redirect_storage = \Drupal::service('redirect.not_found_storage');
  foreach ($ignore_pages as $ignore_page) {
    $redirect_storage->resolveLogRequest(trim($ignore_page));
  }
}

/**
@@ -109,6 +123,10 @@ function redirect_404_redirect_presave(Redirect $redirect) {
  $path = $redirect->getSourcePathWithQuery();
  $langcode = $redirect->get('language')->value;

  if ($langcode == LanguageInterface::LANGCODE_NOT_SPECIFIED) {
    $langcode = NULL;
  }

  // Mark a potentially existing log entry for this path as resolved.
  \Drupal::service('redirect.not_found_storage')->resolveLogRequest($path, $langcode);
}
+1 −1
Original line number Diff line number Diff line
@@ -66,7 +66,7 @@ class Fix404IgnoreController extends ControllerBase {
    $langcode = $request->query->get('langcode');

    if (empty($existing_config_raw) || !empty($path) || !strpos($path, $existing_config_raw)) {
      $this->redirectStorage->resolveLogRequest($path, $langcode);
      $this->redirectStorage->resolveLogRequest($path);

      // Users without 'administer redirect settings' and 'ignore 4040 request'
      // permission can also ignore pages.
+3 −3
Original line number Diff line number Diff line
@@ -18,14 +18,14 @@ interface RedirectNotFoundStorageInterface {
  public function logRequest($path, $langcode);

  /**
   * Marks a 404 request log as resolved.
   * Marks a 404 request log as resolved (supports wildcards).
   *
   * @param string $path
   *   The path of the current request.
   * @param string $langcode
   *   The ID of the language code.
   *   (optional) The ID of the language code.
   */
  public function resolveLogRequest($path, $langcode);
  public function resolveLogRequest($path, $langcode = NULL);

  /**
   * Returns the 404 request data.
+10 −5
Original line number Diff line number Diff line
@@ -80,12 +80,17 @@ class SqlRedirectNotFoundStorage implements RedirectNotFoundStorageInterface {
  /**
   * {@inheritdoc}
   */
  public function resolveLogRequest($path, $langcode) {
    $this->database->update('redirect_404')
  public function resolveLogRequest($path, $langcode = NULL) {
    $path = str_replace('*', '%', $path);
    $update = $this->database->update('redirect_404')
      ->fields(['resolved' => 1])
      ->condition('path', $path)
      ->condition('langcode', $langcode)
      ->execute();
      ->condition('path', $path, 'LIKE');

    if ($langcode) {
      $update->condition('langcode', $langcode);
    }

    $update->execute();
  }

  /**
+32 −0
Original line number Diff line number Diff line
@@ -191,6 +191,38 @@ class Fix404RedirectUITest extends Redirect404TestBase {
    $this->getSession()->getPage()->pressButton('Save configuration');
    $this->drupalGet('admin/config/search/redirect/settings');
    $this->assertSession()->fieldValueEquals('ignore_pages', "/node/*\n/term/*\n/llama_page");

    // Test clearing of ignored pages.
    $this->drupalGet('vicuna_page');
    $this->drupalGet('vicuna_page/subpage');
    $this->drupalGet('prefix/vicuna_page/subpage');
    $this->drupalGet('alpaca_page');
    $this->drupalGet('admin/config/search/redirect/404');
    $this->assertSession()->pageTextContains('vicuna_page');
    $this->assertSession()->pageTextContains('alpaca_page');
    $this->drupalGet('admin/config/search/redirect/settings');
    $edit = [
      'ignore_pages' => '*vicuna*',
      'clear_ignored' => TRUE,
    ];
    $this->submitForm($edit, 'Save configuration');
    $this->drupalGet('admin/config/search/redirect/404');
    $this->assertSession()->pageTextNotContains('vicuna');

    $this->drupalGet('prefix/jaguar_page/subpage');
    $this->drupalGet('prefix/tucan_page/subpage');
    $this->drupalGet('admin/config/search/redirect/404');
    $this->assertSession()->pageTextContains('jaguar_page');
    $this->assertSession()->pageTextContains('tucan_page');
    $this->drupalGet('admin/config/search/redirect/settings');
    $edit = [
      'ignore_pages' => '*/tucan_page/*',
      'clear_ignored' => TRUE,
    ];
    $this->submitForm($edit, 'Save configuration');
    $this->drupalGet('admin/config/search/redirect/404');
    $this->assertSession()->pageTextContains('jaguar_page');
    $this->assertSession()->pageTextNotContains('tucan_page');
  }

  /**