You are here

public function Fix404RedirectUITest::testIgnorePages in Redirect 8

Tests the redirect ignore pages.

File

modules/redirect_404/tests/src/Functional/Fix404RedirectUITest.php, line 125

Class

Fix404RedirectUITest
UI tests for redirect_404 module.

Namespace

Drupal\Tests\redirect_404\Functional

Code

public function testIgnorePages() {

  // Create two nodes.
  $node1 = $this
    ->drupalCreateNode([
    'type' => 'page',
  ]);
  $node2 = $this
    ->drupalCreateNode([
    'type' => 'page',
  ]);

  // Set some pages to be ignored just for the test.
  $node_to_ignore = '/node/' . $node1
    ->id() . '/test';
  $terms_to_ignore = '/term/*';
  $pages = $node_to_ignore . "\r\n" . $terms_to_ignore . "\n";
  \Drupal::configFactory()
    ->getEditable('redirect_404.settings')
    ->set('pages', $pages)
    ->save();

  // Visit ignored or non existing pages.
  $this
    ->drupalGet('node/' . $node1
    ->id() . '/test');
  $this
    ->drupalGet('term/foo');
  $this
    ->drupalGet('term/1');

  // Go to the "fix 404" page and check there are no 404 entries.
  $this
    ->drupalGet('admin/config/search/redirect/404');
  $this
    ->assertNoText('node/' . $node1
    ->id() . '/test');
  $this
    ->assertNoText('term/foo');
  $this
    ->assertNoText('term/1');

  // Visit non existing but 'unignored' page.
  $this
    ->drupalGet('node/' . $node2
    ->id() . '/test');

  // Go to the "fix 404" page and check there is a 404 entry.
  $this
    ->drupalGet('admin/config/search/redirect/404');
  $this
    ->assertText('node/' . $node2
    ->id() . '/test');

  // Add this 404 entry to the 'ignore path' list, assert it works properly.
  $path_to_ignore = '/node/' . $node2
    ->id() . '/test';
  $destination = '&destination=admin/config/search/redirect/404';
  $this
    ->clickLink('Ignore');
  $this
    ->assertUrl('admin/config/search/redirect/settings?ignore=' . $path_to_ignore . $destination);
  $this
    ->assertText('Resolved the path ' . $path_to_ignore . ' in the database. Please check the ignored list and save the settings.');
  $this
    ->assertSession()
    ->fieldValueEquals('ignore_pages', $node_to_ignore . "\r\n/term/*\n/node/2/test");
  $this
    ->assertSession()
    ->elementContains('css', '#edit-ignore-pages', $node_to_ignore);
  $this
    ->assertSession()
    ->elementContains('css', '#edit-ignore-pages', $terms_to_ignore);
  $this
    ->assertSession()
    ->elementContains('css', '#edit-ignore-pages', $path_to_ignore);

  // Save the path with wildcard, but omitting the leading slash.
  $nodes_to_ignore = 'node/*';
  $edit = [
    'ignore_pages' => $nodes_to_ignore . "\r\n" . $terms_to_ignore,
  ];
  $this
    ->drupalPostForm(NULL, $edit, 'Save configuration');

  // Should redirect to 'Fix 404'. Check the 404 entry is not shown anymore.
  $this
    ->assertUrl('admin/config/search/redirect/404');
  $this
    ->assertText('The configuration options have been saved.');
  $this
    ->assertNoText('node/' . $node2
    ->id() . '/test');
  $this
    ->assertText('There are no 404 errors to fix.');

  // Go back to the settings to check the 'Path to ignore' configurations.
  $this
    ->drupalGet('admin/config/search/redirect/settings');
  $xpath = $this
    ->xpath('//*[@id="edit-ignore-pages"]')[0]
    ->getHtml();

  // Check that the new page to ignore has been saved with leading slash.
  $this
    ->assertSession()
    ->elementContains('css', '#edit-ignore-pages', '/' . $nodes_to_ignore);
  $this
    ->assertSession()
    ->elementContains('css', '#edit-ignore-pages', $terms_to_ignore);
  $this
    ->assertSession()
    ->elementNotContains('css', '#edit-ignore-pages', $node_to_ignore);
  $this
    ->assertSession()
    ->elementNotContains('css', '#edit-ignore-pages', $path_to_ignore);

  // Testing whitelines.
  $this
    ->drupalGet('llama_page');
  $this
    ->drupalGet('admin/config/search/redirect/404');
  $this
    ->assertText('llama_page');
  $this
    ->clickLink('Ignore');
  $this
    ->assertSession()
    ->fieldValueEquals('ignore_pages', "/node/*\r\n/term/*\n/llama_page");
  $this
    ->getSession()
    ->getPage()
    ->pressButton('Save configuration');
  $this
    ->drupalGet('admin/config/search/redirect/settings');
  $this
    ->assertSession()
    ->fieldValueEquals('ignore_pages', "/node/*\r\n/term/*\n/llama_page");
}