View source
<?php
namespace Drupal\Tests\redirect\Functional;
use Drupal\Component\Render\FormattableMarkup;
use Drupal\Core\Language\Language;
use Drupal\Core\Logger\RfcLogLevel;
use Drupal\Core\Url;
use Drupal\taxonomy\Entity\Term;
use Drupal\taxonomy\Entity\Vocabulary;
use Drupal\Tests\BrowserTestBase;
class RedirectUITest extends BrowserTestBase {
use AssertRedirectTrait;
protected $adminUser;
protected $repository;
protected $storage;
public static $modules = [
'redirect',
'node',
'path',
'dblog',
'views',
'taxonomy',
];
protected $defaultTheme = 'stark';
protected function setUp() {
parent::setUp();
$this
->drupalCreateContentType([
'type' => 'article',
'name' => 'Article',
]);
$this->adminUser = $this
->drupalCreateUser([
'administer redirects',
'administer redirect settings',
'access content',
'bypass node access',
'create url aliases',
'administer taxonomy',
'administer url aliases',
]);
$this->repository = \Drupal::service('redirect.repository');
$this->storage = \Drupal::entityTypeManager()
->getStorage('redirect');
}
public function testAutomaticRedirects() {
$this
->drupalLogin($this->adminUser);
$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());
$this
->assertRedirect('node_test_alias', 'node_test_alias_updated');
$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());
$this
->assertRedirect('node_test_alias_updated', 'node_test_alias');
$this
->drupalPostForm('node/' . $node
->id() . '/delete', [], t('Delete'));
$redirect = $this->repository
->findMatchingRedirect('node_test_alias_updated', [], Language::LANGCODE_NOT_SPECIFIED);
$this
->assertTrue(empty($redirect));
$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());
$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';
}
$this
->drupalPostForm('admin/config/search/path/add', [
$path_field => '/node',
$alias_field => '/aaa_path_alias',
], t('Save'));
$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());
$this
->assertRedirect('aaa_path_alias', 'aaa_path_alias_updated');
$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');
}
function testRedirectLoop() {
\Drupal::service('module_installer')
->uninstall([
'page_cache',
]);
$redirect1 = $this->storage
->create();
$redirect1
->setSource('node');
$redirect1
->setRedirect('admin');
$redirect1
->setStatusCode(301);
$redirect1
->save();
$redirect2 = $this->storage
->create();
$redirect2
->setSource('admin');
$redirect2
->setRedirect('node');
$redirect2
->setStatusCode(301);
$redirect2
->save();
$this->maximumRedirects = 10;
$this
->drupalGet('node');
$this
->assertText('Service unavailable');
$this
->assertResponse(503);
$log = \Drupal::database()
->select('watchdog')
->fields('watchdog')
->condition('type', 'redirect')
->execute()
->fetchAll();
if (count($log) == 0) {
$this
->fail('Redirect loop has not been logged');
}
else {
$log = reset($log);
$this
->assertEquals(RfcLogLevel::WARNING, $log->severity);
$this
->assertEquals('Redirect loop identified at %path for redirect %rid', $log->message);
$this
->assertEquals([
'%path' => '/node',
'%rid' => $redirect1
->id(),
], unserialize($log->variables));
}
}
function createVocabulary() {
$vocabulary = Vocabulary::create([
'name' => $this
->randomMachineName(),
'description' => $this
->randomMachineName(),
'vid' => mb_strtolower($this
->randomMachineName()),
'langcode' => Language::LANGCODE_NOT_SPECIFIED,
'weight' => mt_rand(0, 10),
]);
$vocabulary
->save();
return $vocabulary;
}
function createTerm($vocabulary) {
$filter_formats = filter_formats();
$format = array_pop($filter_formats);
$term = Term::create([
'name' => $this
->randomMachineName(),
'description' => [
'value' => $this
->randomMachineName(),
'format' => $format
->id(),
],
'vid' => $vocabulary
->id(),
'langcode' => Language::LANGCODE_NOT_SPECIFIED,
'path' => [
'alias' => '/term_test_alias',
],
]);
$term
->save();
return $term;
}
public function testCacheTags() {
$redirect1 = $this->storage
->create();
$redirect1
->setSource('test-redirect');
$redirect1
->setRedirect('node');
$redirect1
->setStatusCode(301);
$redirect1
->save();
$response = $this
->assertRedirect('test-redirect', 'node');
$expected = 'http_response ' . implode(' ', $redirect1
->getCacheTags());
$this
->assertEqual($expected, $response
->getHeader('x-drupal-cache-tags')[0], 'Redirect cache tags properly set.');
$this
->assertEqual($response
->getHeader('x-drupal-cache')[0], 'MISS', 'First request to the redirect was not cached.');
$response = $this
->assertRedirect('test-redirect', 'node');
$this
->assertEqual($response
->getHeader('x-drupal-cache')[0], 'HIT', 'The second request to the redirect was cached.');
$redirect1
->delete();
$this
->drupalGet('test-redirect');
$this
->assertResponse(404, 'Deleted redirect properly clears the internal page cache.');
}
public function testExternal() {
$redirect = $this->storage
->create();
$redirect
->setSource('a-path');
$redirect->redirect_redirect
->set(0, [
'uri' => 'https://www.example.org',
]);
$redirect
->setStatusCode(301);
$redirect
->save();
$this
->assertRedirect('a-path', 'https://www.example.org');
$this
->drupalLogin($this->adminUser);
}
}