View source
<?php
namespace Drupal\Tests\redirect_404\Functional;
use Drupal\Component\Utility\UrlHelper;
use Drupal\Core\Language\LanguageInterface;
use Drupal\Core\Url;
use Drupal\language\Entity\ConfigurableLanguage;
use Drupal\Tests\redirect\Functional\AssertRedirectTrait;
class Fix404RedirectUILanguageTest extends Redirect404TestBase {
use AssertRedirectTrait;
public static $modules = [
'language',
];
protected $adminPermissions = [
'administer redirects',
'administer redirect settings',
'access content',
'bypass node access',
'create url aliases',
'administer url aliases',
'administer languages',
];
public function setUp() {
parent::setUp();
$language = ConfigurableLanguage::createFromLangcode('de');
$language
->save();
$language = ConfigurableLanguage::createFromLangcode('es');
$language
->save();
$language = ConfigurableLanguage::createFromLangcode('fr');
$language
->save();
}
public function testFix404RedirectList() {
$this
->drupalGet('fr/testing');
$redirect = \Drupal::database()
->select('redirect_404')
->fields('redirect_404')
->condition('path', '/testing')
->execute()
->fetchAll();
if (count($redirect) == 0) {
$this
->fail('No record was added');
}
$this
->drupalGet('admin/config/search/redirect/404');
$this
->assertText('testing');
$this
->assertLanguageInTableBody('French');
$this
->assertOption('edit-langcode', 'All');
$this
->assertOption('edit-langcode', 'en');
$this
->assertOption('edit-langcode', 'de');
$this
->assertOption('edit-langcode', 'es');
$this
->assertOption('edit-langcode', 'fr');
$this
->assertOption('edit-langcode', LanguageInterface::LANGCODE_NOT_SPECIFIED);
$this
->clickLink(t('Add redirect'));
$destination = Url::fromRoute('redirect_404.fix_404')
->getInternalPath();
$expected_query = [
'destination' => $destination,
'language' => 'fr',
'source' => 'testing',
];
$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]', 'testing');
$this
->assertOptionSelected('edit-language-0-value', 'fr');
$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.');
$this
->assertRedirect('fr/testing', 'fr/node', 301);
$this
->drupalGet('admin/config/search/redirect');
$this
->assertText('testing');
$this
->assertLanguageInTableBody('French');
$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.');
$this
->drupalGet('fr/testing');
$this
->drupalGet('admin/config/search/redirect/404');
$this
->assertLanguageInTableBody('French');
$this
->assertFieldByXPath('//table/tbody/tr/td[2]', 2);
$this
->clickLink('Add redirect');
$this
->assertFieldByName('redirect_source[0][path]', 'testing');
$this
->assertOptionSelected('edit-language-0-value', 'fr');
$edit['language[0][value]'] = 'es';
$this
->drupalPostForm(NULL, $edit, 'Save');
$this
->assertUrl('admin/config/search/redirect/404');
$this
->assertLanguageInTableBody('French');
$this
->drupalGet('admin/config/search/redirect');
$this
->assertLanguageInTableBody('Spanish');
$this
->assertRedirect('es/testing', 'es/node', 301);
$this
->drupalGet('testing1');
$this
->drupalGet('de/testing2');
$this
->drupalGet('de/testing2?test=1');
$this
->drupalGet('de/testing2?test=2');
$this
->drupalGet('admin/config/search/redirect/404');
$this
->assertLanguageInTableBody('French');
$this
->assertLanguageInTableBody('English');
$this
->assertLanguageInTableBody('German');
$this
->assertText('testing1');
$this
->assertText('testing2');
$this
->assertText('testing2?test=1');
$this
->assertText('testing2?test=2');
$this
->drupalGet('admin/config/search/redirect/404', [
'query' => [
'langcode' => 'de',
],
]);
$this
->assertText('English');
$this
->assertNoLanguageInTableBody('English');
$this
->assertLanguageInTableBody('German');
$this
->assertNoText('testing1');
$this
->assertText('testing2');
$this
->assertText('testing2?test=1');
$this
->assertText('testing2?test=2');
$this
->drupalGet('admin/config/search/redirect/404');
$this
->assertLanguageInTableBody('English');
$this
->assertLanguageInTableBody('German');
$this
->assertText('testing1');
$this
->assertText('testing2');
$this
->assertText('testing2?test=1');
$this
->assertText('testing2?test=2');
$this
->drupalGet('admin/config/search/redirect/404', [
'query' => [
'langcode' => 'en',
],
]);
$this
->assertLanguageInTableBody('English');
$this
->assertNoLanguageInTableBody('German');
$this
->assertText('testing1');
$this
->assertNoText('testing2');
$this
->assertNoText('testing2?test=1');
$this
->assertNoText('testing2?test=2');
$this
->clickLink('Add redirect');
$expected_query = [
'destination' => $destination,
'language' => 'en',
'source' => 'testing1',
];
$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]', 'testing1');
$this
->assertOptionSelected('edit-language-0-value', 'en');
$edit = [
'redirect_redirect[0][uri]' => '/node',
];
$this
->drupalPostForm(NULL, $edit, t('Save'));
$this
->assertUrl('admin/config/search/redirect/404');
$this
->assertNoLanguageInTableBody('English');
$this
->assertLanguageInTableBody('German');
$this
->drupalGet('admin/config/search/redirect');
$this
->assertLanguageInTableBody('Spanish');
$this
->assertLanguageInTableBody('English');
$this
->assertRedirect('/testing1', '/node', 301);
}
}