View source
<?php
namespace Drupal\Tests\redirect\FunctionalJavascript;
use Drupal\Core\Url;
use Drupal\FunctionalJavascriptTests\WebDriverTestBase;
class RedirectJavascriptTest extends WebDriverTestBase {
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 = $this->container
->get('entity_type.manager')
->getStorage('redirect');
}
public function testRedirectUI() {
$this
->drupalLogin($this->adminUser);
$this
->drupalGet('admin/config/search/redirect/add', [
'query' => [
'source' => 'non-existing',
'source_query' => [
'key' => 'val',
'key1' => 'val1',
],
'redirect' => 'node',
'redirect_options' => [
'query' => [
'key' => 'val',
'key1' => 'val1',
],
],
],
]);
$this
->assertFieldByName('redirect_source[0][path]', 'non-existing?key=val&key1=val1');
$this
->assertFieldByName('redirect_redirect[0][uri]', '/node?key=val&key1=val1');
$this
->drupalGet('admin/config/search/redirect/add');
$page = $this
->getSession()
->getPage();
$page
->fillField('redirect_source[0][path]', 'non-existing');
$page
->fillField('redirect_redirect[0][uri]', '/node');
$this
->assertSession()
->assertWaitOnAjaxRequest();
$page
->pressButton('Save');
$redirect = $this->repository
->findMatchingRedirect('non-existing');
$this
->assertEqual($redirect
->getSourceUrl(), Url::fromUri('base:non-existing')
->toString());
$this
->assertEqual($redirect
->getRedirectUrl()
->toString(), Url::fromUri('base:node')
->toString());
$this
->assertUrl('admin/config/search/redirect');
$this
->assertSession()
->pageTextContains('non-existing');
$this
->assertLink(Url::fromUri('base:node')
->toString());
$this
->assertSession()
->pageTextContains(t('Not specified'));
$this
->clickLink(t('Edit'));
$this
->assertFieldByName('redirect_source[0][path]', 'non-existing');
$this
->assertFieldByName('redirect_redirect[0][uri]', '/node');
$this
->assertFieldByName('status_code', $redirect
->getStatusCode());
$this
->drupalPostForm(NULL, [
'redirect_source[0][path]' => 'non-existing?key=value',
], t('Save'));
$this
->assertUrl('admin/config/search/redirect');
$this
->assertSession()
->pageTextContains('non-existing?key=value');
$this->storage
->resetCache();
$redirects = $this->repository
->findBySourcePath('non-existing');
$redirect = array_shift($redirects);
$this
->assertEqual($redirect
->getSourceUrl(), Url::fromUri('base:non-existing', [
'query' => [
'key' => 'value',
],
])
->toString());
$this
->drupalGet('admin/config/search/redirect/add');
$page
->fillField('redirect_source[0][path]', 'non-existing?key=value');
$page
->fillField('redirect_redirect[0][uri]', '');
$this
->assertSession()
->assertWaitOnAjaxRequest();
$this
->assertRaw(t('The base source path %source is already being redirected. Do you want to <a href="@edit-page">edit the existing redirect</a>?', [
'%source' => 'non-existing?key=value',
'@edit-page' => $redirect
->toUrl('edit-form')
->toString(),
]));
$this
->drupalGet('admin/config/search/redirect/add');
$page
->fillField('redirect_source[0][path]', 'node');
$page
->fillField('redirect_redirect[0][uri]', '');
$this
->assertSession()
->assertWaitOnAjaxRequest();
$this
->assertRaw(t('The source path %path is likely a valid path. It is preferred to <a href="@url-alias">create URL aliases</a> for existing paths rather than redirects.', [
'%path' => 'node',
'@url-alias' => Url::fromRoute('entity.path_alias.add_form')
->toString(),
]));
$this
->drupalGet('admin/config/search/redirect/add');
$page = $this
->getSession()
->getPage();
$page
->fillField('redirect_source[0][path]', 'non-existing?key=value');
$page
->fillField('redirect_redirect[0][uri]', '/node');
$this
->assertSession()
->assertWaitOnAjaxRequest();
$page
->pressButton('Save');
$this
->assertRaw(t('The source path %source is already being redirected. Do you want to <a href="@edit-page">edit the existing redirect</a>?', [
'%source' => 'non-existing?key=value',
'@edit-page' => $redirect
->toUrl('edit-form')
->toString(),
]));
$this
->drupalGet('admin/config/search/redirect/add');
$page = $this
->getSession()
->getPage();
$page
->fillField('redirect_source[0][path]', 'node');
$page
->fillField('redirect_redirect[0][uri]', '/node');
$this
->assertSession()
->assertWaitOnAjaxRequest();
$page
->pressButton('Save');
$this
->assertRaw(t('You are attempting to redirect the page to itself. This will result in an infinite loop.'));
$this
->drupalGet('admin/config/search/redirect/add');
$page = $this
->getSession()
->getPage();
$page
->fillField('redirect_source[0][path]', '<front>');
$page
->fillField('redirect_redirect[0][uri]', '/node');
$this
->assertSession()
->assertWaitOnAjaxRequest();
$page
->pressButton('Save');
$this
->assertRaw(t('It is not allowed to create a redirect from the front page.'));
$this
->drupalGet('admin/config/search/redirect/add');
$page = $this
->getSession()
->getPage();
$page
->fillField('redirect_source[0][path]', 'page-to-redirect#content');
$page
->fillField('redirect_redirect[0][uri]', '/node');
$this
->assertSession()
->assertWaitOnAjaxRequest();
$page
->pressButton('Save');
$this
->assertRaw(t('The anchor fragments are not allowed.'));
$this
->drupalGet('admin/config/search/redirect/add');
$page = $this
->getSession()
->getPage();
$page
->fillField('redirect_source[0][path]', '/page-to-redirect');
$page
->fillField('redirect_redirect[0][uri]', '/node');
$this
->assertSession()
->assertWaitOnAjaxRequest();
$page
->pressButton('Save');
$this
->assertRaw(t('The url to redirect from should not start with a forward slash (/).'));
$this
->drupalGet('admin/config/search/redirect/add');
$page = $this
->getSession()
->getPage();
$page
->fillField('redirect_source[0][path]', 'test27');
$page
->fillField('redirect_redirect[0][uri]', '/node');
$this
->assertSession()
->assertWaitOnAjaxRequest();
$page
->pressButton('Save');
$this
->drupalGet('admin/config/search/redirect', [
'query' => [
'status_code' => '3',
],
]);
$rows = $this
->xpath('//tbody/tr');
$this
->assertTrue(count($rows) == 0);
$this
->drupalGet('admin/config/search/redirect', [
'query' => [
'redirect_source__path' => 'test',
'status_code' => '2',
],
]);
$rows = $this
->xpath('//tbody/tr');
$this
->assertTrue(count($rows) == 1);
$this
->drupalGet('admin/config/search/redirect', [
'query' => [
'redirect_redirect__uri' => 'nod',
],
]);
$rows = $this
->xpath('//tbody/tr');
$this
->assertTrue(count($rows) == 2);
$this
->drupalGet('admin/config/search/redirect');
$edit = [
'redirect_bulk_form[0]' => TRUE,
'redirect_bulk_form[1]' => TRUE,
];
$this
->drupalPostForm(NULL, $edit, t('Apply to selected items'));
$this
->assertSession()
->pageTextContains('Are you sure you want to delete these redirects?');
$this
->clickLink('Cancel');
$page
->find('css', '.dropbutton-toggle button')
->press();
$this
->clickLink(t('Delete'));
$this
->assertRaw(t('Are you sure you want to delete the URL redirect from %source to %redirect?', [
'%source' => Url::fromUri('base:non-existing', [
'query' => [
'key' => 'value',
],
])
->toString(),
'%redirect' => Url::fromUri('base:node')
->toString(),
]));
$this
->drupalPostForm(NULL, [], t('Delete'));
$this
->assertUrl('admin/config/search/redirect');
$this
->drupalPostForm(NULL, [
'redirect_bulk_form[0]' => TRUE,
], t('Apply to selected items'));
$this
->assertSession()
->pageTextContains('Are you sure you want to delete this redirect?');
$this
->assertSession()
->pageTextContains('test27');
$this
->drupalPostForm(NULL, [], t('Delete'));
$this
->assertSession()
->pageTextContains(t('There is no redirect yet.'));
}
}