View source
<?php
namespace Drupal\Tests\redirect\Functional;
use Behat\Mink\Driver\GoutteDriver;
use Drupal\Core\Cache\Cache;
use Drupal\Core\Language\Language;
use Drupal\language\Entity\ConfigurableLanguage;
use Drupal\taxonomy\Entity\Term;
use Drupal\taxonomy\Entity\Vocabulary;
use Drupal\Tests\BrowserTestBase;
use Drupal\Tests\Traits\Core\PathAliasTestTrait;
class GlobalRedirectTest extends BrowserTestBase {
use PathAliasTestTrait;
public static $modules = [
'path',
'node',
'redirect',
'taxonomy',
'forum',
'views',
'language',
'content_translation',
];
protected $normalUser;
protected $adminUser;
protected $config;
protected $forumTerm;
protected $term;
protected $node;
protected $defaultTheme = 'stark';
protected $minkDefaultDriverClass = GoutteDriver::class;
protected function initMink() {
$session = parent::initMink();
$driver = $session
->getDriver();
$driver
->getClient()
->followRedirects(FALSE);
return $session;
}
protected function setUp() {
parent::setUp();
$this->config = $this
->config('redirect.settings');
$this
->drupalCreateContentType([
'type' => 'page',
'name' => 'Page',
]);
$this
->drupalCreateContentType([
'type' => 'article',
'name' => 'Article',
]);
$this->normalUser = $this
->drupalCreateUser([
'access content',
'create page content',
'create url aliases',
'access administration pages',
]);
$this->adminUser = $this
->drupalCreateUser([
'administer site configuration',
'access administration pages',
'administer languages',
'administer content types',
'administer content translation',
'create page content',
'edit own page content',
'create content translations',
]);
$this->node = $this
->drupalCreateNode([
'type' => 'page',
'title' => 'Test Page Node',
'path' => [
'alias' => '/test-node',
],
'language' => Language::LANGCODE_NOT_SPECIFIED,
]);
$this
->createPathAlias('/admin/config/system/site-information', '/site-info');
$term = Term::create([
'name' => 'Test Forum Term',
'vid' => 'forums',
'langcode' => Language::LANGCODE_NOT_SPECIFIED,
]);
$term
->save();
$this->forumTerm = $term;
$vocab = Vocabulary::create([
'name' => 'test vocab',
'vid' => 'test-vocab',
'langcode' => Language::LANGCODE_NOT_SPECIFIED,
]);
$vocab
->save();
$term = Term::create([
'name' => 'Test Term',
'vid' => $vocab
->id(),
'langcode' => Language::LANGCODE_NOT_SPECIFIED,
'path' => [
'alias' => '/test-term',
],
]);
$term
->save();
$this->term = $term;
}
public function testRedirects() {
$this->config
->set('route_normalizer_enabled', FALSE)
->save();
$this
->assertNoRedirect('index.php/node/' . $this->node
->id());
$this
->assertNoRedirect('index.php/test-node');
$this
->assertNoRedirect('test-node/');
$this
->assertNoRedirect('Test-node/');
$this->config
->set('route_normalizer_enabled', TRUE)
->save();
$this
->assertRedirect('node/' . $this->node
->id(), 'test-node');
$this
->assertRedirect('Test-node', 'test-node');
$this
->assertRedirect('index.php/node/' . $this->node
->id(), 'test-node');
$this
->assertRedirect('index.php/test-node', 'test-node');
$this
->assertRedirect('test-node/', 'test-node');
$this
->config('system.site')
->set('page.front', '/node')
->save();
$this
->assertRedirect('node', '/');
$this
->createPathAlias('/node', '/node-alias');
$this
->assertRedirect('node-alias', '/');
$driver = $this
->getSession()
->getDriver();
$response = $driver
->getClient()
->getClient()
->post($this
->getAbsoluteUrl('Test-node'), [
'allow_redirects' => FALSE,
'headers' => [
'Accept' => 'application/json',
],
]);
$this
->assertSame(200, $response
->getStatusCode());
$this
->assertEmpty($response
->getHeader('Location'));
$this
->assertStringNotContainsString('http-equiv="refresh', (string) $response
->getBody());
$this->config
->set('access_check', TRUE)
->save();
$this
->assertNoRedirect('admin/config/system/site-information', 403);
$this->config
->set('access_check', FALSE)
->save();
$this
->assertRedirect('Test-node?&foo&.bar=baz', 'test-node?&foo&.bar=baz');
$this
->assertNoRedirect('test-node?');
$this
->assertRedirect('Test-node?', 'test-node');
$this
->assertNoRedirect('test-node');
$this
->assertRedirect('Test-node', 'test-node');
$this
->drupalLogin($this->adminUser);
$this->config
->set('ignore_admin_path', FALSE)
->save();
$this
->assertRedirect('admin/config/system/site-information', 'site-info');
$this
->assertRedirect('Test-node', 'test-node');
$this->config
->set('ignore_admin_path', TRUE)
->save();
$this
->assertNoRedirect('admin/config/system/site-information');
$this
->assertRedirect('Test-node', 'test-node');
}
public function testLanguageRedirects() {
$this
->drupalLogin($this->adminUser);
ConfigurableLanguage::createFromLangcode('es')
->save();
$edit = [
'language_interface[enabled][language-url]' => '1',
];
$this
->drupalPostForm('admin/config/regional/language/detection', $edit, t('Save settings'));
$edit = [
'language_configuration[language_alterable]' => TRUE,
'language_configuration[content_translation]' => TRUE,
];
$this
->drupalPostForm('admin/structure/types/manage/page', $edit, t('Save content type'));
$this
->assertRaw(t('The content type %type has been updated.', [
'%type' => 'Page',
]));
$spanish_node = $this
->drupalCreateNode([
'type' => 'page',
'title' => 'Spanish Test Page Node',
'path' => [
'alias' => '/spanish-test-node',
],
'langcode' => 'es',
]);
$this
->assertRedirect('es/node/' . $spanish_node
->id(), 'es/spanish-test-node');
}
public function assertRedirect($path, $expected_destination, $status_code = 301) {
$url = $this
->getAbsoluteUrl($path);
$this
->getSession()
->visit($url);
$this
->refreshVariables();
$assert_session = $this
->assertSession();
$assert_session
->responseHeaderEquals('Location', $this
->getAbsoluteUrl($expected_destination));
$assert_session
->statusCodeEquals($status_code);
}
protected function assertNoRedirect($path, $status_code = 200) {
$url = $this
->getAbsoluteUrl($path);
$this
->getSession()
->visit($url);
$assert_session = $this
->assertSession();
$assert_session
->statusCodeEquals($status_code);
$assert_session
->responseHeaderEquals('Location', NULL);
$assert_session
->responseNotContains('http-equiv="refresh');
$assert_session
->addressEquals($path);
$this
->refreshVariables();
}
}