You are here

public function Fix404RedirectUITest::testFix404Pages in Redirect 8

Tests the fix 404 pages workflow.

File

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

Class

Fix404RedirectUITest
UI tests for redirect_404 module.

Namespace

Drupal\Tests\redirect_404\Functional

Code

public function testFix404Pages() {

  // Visit a non existing page to have the 404 redirect_error entry.
  $this
    ->drupalGet('non-existing0');

  // Go to the "fix 404" page and check the listing.
  $this
    ->drupalGet('admin/config/search/redirect/404');
  $this
    ->assertText('non-existing0');
  $this
    ->clickLink(t('Add redirect'));

  // Check if we generate correct Add redirect url and if the form is
  // pre-filled.
  $destination = Url::fromRoute('redirect_404.fix_404')
    ->getInternalPath();
  $expected_query = [
    'destination' => $destination,
    'language' => 'en',
    'source' => 'non-existing0',
  ];
  $parsed_url = UrlHelper::parse($this
    ->getUrl());
  $this
    ->assertEqual(Url::fromRoute('redirect.add')
    ->setAbsolute()
    ->toString(), $parsed_url['path']);
  $this
    ->assertEqual($expected_query, $parsed_url['query']);
  $this
    ->assertFieldByName('redirect_source[0][path]', 'non-existing0');

  // Save the redirect.
  $edit = [
    'redirect_redirect[0][uri]' => '/node',
  ];
  $this
    ->drupalPostForm(NULL, $edit, t('Save'));
  $this
    ->assertUrl('admin/config/search/redirect/404');
  $this
    ->assertText('There are no 404 errors to fix.');

  // Check if the redirect works as expected.
  $this
    ->drupalGet('non-existing0');
  $this
    ->assertUrl('node');

  // Test removing a redirect assignment, visit again the non existing page.
  $this
    ->drupalGet('admin/config/search/redirect');
  $this
    ->assertText('non-existing0');
  $this
    ->clickLink('Delete', 0);
  $this
    ->drupalPostForm(NULL, [], 'Delete');
  $this
    ->assertUrl('admin/config/search/redirect');
  $this
    ->assertText('There is no redirect yet.');
  $this
    ->drupalGet('admin/config/search/redirect/404');
  $this
    ->assertText('There are no 404 errors to fix.');

  // Should be listed again in the 404 overview.
  $this
    ->drupalGet('non-existing0');
  $this
    ->drupalGet('admin/config/search/redirect/404');
  $this
    ->assertText('non-existing0');

  // Visit multiple non existing pages to test the Redirect 404 View.
  $this
    ->drupalGet('non-existing0?test=1');
  $this
    ->drupalGet('non-existing0?test=2');
  $this
    ->drupalGet('non-existing1');
  $this
    ->drupalGet('non-existing2');
  $this
    ->drupalGet('admin/config/search/redirect/404');
  $this
    ->assertText('non-existing0?test=1');
  $this
    ->assertText('non-existing0?test=2');
  $this
    ->assertText('non-existing0');
  $this
    ->assertText('non-existing1');
  $this
    ->assertText('non-existing2');

  // Test the Path view filter.
  $this
    ->drupalGet('admin/config/search/redirect/404', [
    'query' => [
      'path' => 'test=',
    ],
  ]);
  $this
    ->assertText('non-existing0?test=1');
  $this
    ->assertText('non-existing0?test=2');
  $this
    ->assertNoText('non-existing1');
  $this
    ->assertNoText('non-existing2');
  $this
    ->drupalGet('admin/config/search/redirect/404', [
    'query' => [
      'path' => 'existing1',
    ],
  ]);
  $this
    ->assertNoText('non-existing0?test=1');
  $this
    ->assertNoText('non-existing0?test=2');
  $this
    ->assertNoText('non-existing0');
  $this
    ->assertText('non-existing1');
  $this
    ->assertNoText('non-existing2');
  $this
    ->drupalGet('admin/config/search/redirect/404');
  $this
    ->assertText('non-existing0?test=1');
  $this
    ->assertText('non-existing0?test=2');
  $this
    ->assertText('non-existing0');
  $this
    ->assertText('non-existing1');
  $this
    ->assertText('non-existing2');
  $this
    ->drupalGet('admin/config/search/redirect/404', [
    'query' => [
      'path' => 'g2',
    ],
  ]);
  $this
    ->assertNoText('non-existing0?test=1');
  $this
    ->assertNoText('non-existing0?test=2');
  $this
    ->assertNoText('non-existing0');
  $this
    ->assertNoText('non-existing1');
  $this
    ->assertText('non-existing2');

  // Assign a redirect to 'non-existing2'.
  $this
    ->clickLink('Add redirect');
  $expected_query = [
    'source' => 'non-existing2',
    'language' => 'en',
    'destination' => $destination,
  ];
  $parsed_url = UrlHelper::parse($this
    ->getUrl());
  $this
    ->assertEqual(Url::fromRoute('redirect.add')
    ->setAbsolute()
    ->toString(), $parsed_url['path']);
  $this
    ->assertEqual($expected_query, $parsed_url['query']);
  $this
    ->assertFieldByName('redirect_source[0][path]', 'non-existing2');
  $this
    ->drupalPostForm(NULL, $edit, t('Save'));
  $this
    ->assertUrl('admin/config/search/redirect/404');
  $this
    ->assertText('non-existing0?test=1');
  $this
    ->assertText('non-existing0?test=2');
  $this
    ->assertText('non-existing0');
  $this
    ->assertText('non-existing1');
  $this
    ->assertNoText('non-existing2');

  // Check if the redirect works as expected.
  $this
    ->drupalGet('admin/config/search/redirect');
  $this
    ->assertText('non-existing2');
}