You are here

public function RedirectUITest::testAutomaticRedirects in Redirect 8

Tests redirects being automatically created upon path alias change.

File

tests/src/Functional/RedirectUITest.php, line 72

Class

RedirectUITest
UI tests for redirect module.

Namespace

Drupal\Tests\redirect\Functional

Code

public function testAutomaticRedirects() {
  $this
    ->drupalLogin($this->adminUser);

  // Create a node and update its path alias which should result in a redirect
  // being automatically created from the old alias to the new one.
  $node = $this
    ->drupalCreateNode([
    'type' => 'article',
    'langcode' => Language::LANGCODE_NOT_SPECIFIED,
    'path' => [
      'alias' => '/node_test_alias',
    ],
  ]);
  $this
    ->drupalGet('node/' . $node
    ->id() . '/edit');
  $this
    ->assertText(t('No URL redirects available.'));
  $this
    ->drupalPostForm('node/' . $node
    ->id() . '/edit', [
    'path[0][alias]' => '/node_test_alias_updated',
  ], t('Save'));
  $redirect = $this->repository
    ->findMatchingRedirect('node_test_alias', [], Language::LANGCODE_NOT_SPECIFIED);
  $this
    ->assertEqual($redirect
    ->getRedirectUrl()
    ->toString(), Url::fromUri('base:node_test_alias_updated')
    ->toString());

  // Test if the automatically created redirect works.
  $this
    ->assertRedirect('node_test_alias', 'node_test_alias_updated');

  // Test that changing the path back deletes the first redirect, creates
  // a new one and does not result in a loop.
  $this
    ->drupalPostForm('node/' . $node
    ->id() . '/edit', [
    'path[0][alias]' => '/node_test_alias',
  ], t('Save'));
  $redirect = $this->repository
    ->findMatchingRedirect('node_test_alias', [], Language::LANGCODE_NOT_SPECIFIED);
  $this
    ->assertTrue(empty($redirect));
  \Drupal::service('path_alias.manager')
    ->cacheClear();
  $redirect = $this->repository
    ->findMatchingRedirect('node_test_alias_updated', [], Language::LANGCODE_NOT_SPECIFIED);
  $this
    ->drupalGet('node/' . $node
    ->id() . '/edit');
  $this
    ->assertText($redirect
    ->getSourcePathWithQuery());
  $this
    ->assertLinkByHref(Url::fromRoute('entity.redirect.edit_form', [
    'redirect' => $redirect
      ->id(),
  ])
    ->toString());
  $this
    ->assertLinkByHref(Url::fromRoute('entity.redirect.delete_form', [
    'redirect' => $redirect
      ->id(),
  ])
    ->toString());
  $this
    ->assertEqual($redirect
    ->getRedirectUrl()
    ->toString(), Url::fromUri('base:node_test_alias')
    ->toString());

  // Test if the automatically created redirect works.
  $this
    ->assertRedirect('node_test_alias_updated', 'node_test_alias');

  // Test that the redirect will be deleted upon node deletion.
  $this
    ->drupalPostForm('node/' . $node
    ->id() . '/delete', [], t('Delete'));
  $redirect = $this->repository
    ->findMatchingRedirect('node_test_alias_updated', [], Language::LANGCODE_NOT_SPECIFIED);
  $this
    ->assertTrue(empty($redirect));

  // Create a term and update its path alias and check if we have a redirect
  // from the previous path alias to the new one.
  $term = $this
    ->createTerm($this
    ->createVocabulary());
  $this
    ->drupalPostForm('taxonomy/term/' . $term
    ->id() . '/edit', [
    'path[0][alias]' => '/term_test_alias_updated',
  ], t('Save'));
  $redirect = $this->repository
    ->findMatchingRedirect('term_test_alias');
  $this
    ->assertEqual($redirect
    ->getRedirectUrl()
    ->toString(), Url::fromUri('base:term_test_alias_updated')
    ->toString());

  // Test if the automatically created redirect works.
  $this
    ->assertRedirect('term_test_alias', 'term_test_alias_updated');
  if (version_compare(\Drupal::VERSION, '8.8', '>=')) {
    $path_field = 'path[0][value]';
    $alias_field = 'alias[0][value]';
  }
  else {
    $path_field = 'source';
    $alias_field = 'alias';
  }

  // Test the path alias update via the admin path form.
  $this
    ->drupalPostForm('admin/config/search/path/add', [
    $path_field => '/node',
    $alias_field => '/aaa_path_alias',
  ], t('Save'));

  // Note that here we rely on fact that we land on the path alias list page
  // and the default sort is by the alias, which implies that the first edit
  // link leads to the edit page of the aaa_path_alias.
  $this
    ->clickLink(t('Edit'));
  $this
    ->drupalPostForm(NULL, [
    $alias_field => '/aaa_path_alias_updated',
  ], t('Save'));
  $redirect = $this->repository
    ->findMatchingRedirect('aaa_path_alias', [], 'en');
  $this
    ->assertEqual($redirect
    ->getRedirectUrl()
    ->toString(), Url::fromUri('base:aaa_path_alias_updated')
    ->toString());

  // Test if the automatically created redirect works.
  $this
    ->assertRedirect('aaa_path_alias', 'aaa_path_alias_updated');

  // Test the automatically created redirect shows up in the form correctly.
  $this
    ->drupalGet('admin/config/search/redirect/edit/' . $redirect
    ->id());
  $this
    ->assertFieldByName('redirect_source[0][path]', 'aaa_path_alias');
  $this
    ->assertFieldByName('redirect_redirect[0][uri]', '/node');
}