View source
<?php
namespace Drupal\Tests\path\Functional;
use Drupal\Core\Cache\Cache;
use Drupal\Core\Database\Database;
use Drupal\Core\Url;
class PathAliasTest extends PathTestBase {
public static $modules = [
'path',
];
protected $defaultTheme = 'stark';
protected function setUp() {
parent::setUp();
$web_user = $this
->drupalCreateUser([
'create page content',
'edit own page content',
'administer url aliases',
'create url aliases',
'access content overview',
]);
$this
->drupalLogin($web_user);
}
public function testPathCache() {
$node1 = $this
->drupalCreateNode();
$edit = [];
$edit['path[0][value]'] = '/node/' . $node1
->id();
$edit['alias[0][value]'] = '/' . $this
->randomMachineName(8);
$this
->drupalPostForm('admin/config/search/path/add', $edit, t('Save'));
$whitelist = \Drupal::cache('bootstrap')
->get('path_alias_whitelist');
$this
->assertTrue($whitelist->data['node']);
$this
->assertFalse($whitelist->data['admin']);
\Drupal::cache('data')
->deleteAll();
$this
->drupalGet(trim($edit['path[0][value]'], '/'), [
'alias' => TRUE,
]);
$this
->assertNotEmpty(\Drupal::cache('data')
->get('preload-paths:' . $edit['path[0][value]']), 'Cache entry was created.');
\Drupal::cache('data')
->deleteAll();
Cache::invalidateTags([
'rendered',
]);
$this
->drupalGet(trim($edit['alias[0][value]'], '/'));
$this
->assertNotEmpty(\Drupal::cache('data')
->get('preload-paths:' . $edit['path[0][value]']), 'Cache entry was created.');
}
public function testAdminAlias() {
$node1 = $this
->drupalCreateNode();
$edit = [];
$edit['path[0][value]'] = '/node/' . $node1
->id();
$edit['alias[0][value]'] = '/' . $this
->getRandomGenerator()
->word(8);
$this
->drupalPostForm('admin/config/search/path/add', $edit, t('Save'));
$this
->drupalGet($edit['alias[0][value]']);
$this
->assertText($node1
->label(), 'Alias works.');
$this
->assertSession()
->statusCodeEquals(200);
$this
->assertTrue(ctype_lower(ltrim($edit['alias[0][value]'], '/')));
$this
->drupalGet($edit['alias[0][value]']);
$this
->assertText($node1
->label(), 'Alias works lower case.');
$this
->assertSession()
->statusCodeEquals(200);
$this
->drupalGet(mb_strtoupper($edit['alias[0][value]']));
$this
->assertText($node1
->label(), 'Alias works upper case.');
$this
->assertSession()
->statusCodeEquals(200);
$pid = $this
->getPID($edit['alias[0][value]']);
$previous = $edit['alias[0][value]'];
$edit['alias[0][value]'] = '/alias' . "- ._~!\$'\"()*@[]?&+%#,;=:" . "%23%25%26%2B%2F%3F" . "中國書۞";
$connection = Database::getConnection();
if ($connection
->databaseType() != 'sqlite') {
$edit['alias[0][value]'] .= "ïвβéø";
}
$this
->drupalPostForm('admin/config/search/path/edit/' . $pid, $edit, t('Save'));
$this
->drupalGet(mb_strtoupper($edit['alias[0][value]']));
$this
->assertText($node1
->label(), 'Changed alias works.');
$this
->assertSession()
->statusCodeEquals(200);
$this->container
->get('path_alias.manager')
->cacheClear();
$this
->drupalGet($previous);
$this
->assertNoText($node1
->label(), 'Previous alias no longer works.');
$this
->assertSession()
->statusCodeEquals(404);
$node2 = $this
->drupalCreateNode();
$edit['path[0][value]'] = '/node/' . $node2
->id();
$this
->drupalPostForm('admin/config/search/path/add', $edit, t('Save'));
$this
->assertRaw(t('The alias %alias is already in use in this language.', [
'%alias' => $edit['alias[0][value]'],
]), 'Attempt to move alias was rejected.');
$edit_upper = $edit;
$edit_upper['alias[0][value]'] = mb_strtoupper($edit['alias[0][value]']);
$this
->drupalPostForm('admin/config/search/path/add', $edit_upper, t('Save'));
$this
->assertRaw(t('The alias %alias could not be added because it is already in use in this language with different capitalization: %stored_alias.', [
'%alias' => $edit_upper['alias[0][value]'],
'%stored_alias' => $edit['alias[0][value]'],
]), 'Attempt to move upper-case alias was rejected.');
$this
->drupalGet('admin/config/search/path/edit/' . $pid);
$this
->clickLink(t('Delete'));
$this
->assertRaw(t('Are you sure you want to delete the URL alias %name?', [
'%name' => $edit['alias[0][value]'],
]));
$this
->drupalPostForm(NULL, [], t('Delete'));
$this
->drupalGet($edit['alias[0][value]']);
$this
->assertNoText($node1
->label(), 'Alias was successfully deleted.');
$this
->assertSession()
->statusCodeEquals(404);
$edit = [];
$edit['path[0][value]'] = '/node/' . $node1
->id();
$alias = '/' . $this
->randomMachineName(128);
$edit['alias[0][value]'] = $alias;
$truncated_alias = substr($alias, 0, 47);
$this
->drupalPostForm('admin/config/search/path/add', $edit, t('Save'));
$this
->assertNoText($alias, 'The untruncated alias was not found.');
$this
->assertText($truncated_alias, 'The truncated alias was found.');
$node3 = $this
->drupalCreateNode();
$edit = [];
$edit['path[0][value]'] = '/node/' . $node3
->id();
$node3_alias = '/' . $this
->randomMachineName(8);
$edit['alias[0][value]'] = $node3_alias;
$this
->drupalPostForm('admin/config/search/path/add', $edit, t('Save'));
$node4 = $this
->drupalCreateNode();
$edit = [];
$edit['path[0][value]'] = '/node/' . $node4
->id();
$node4_alias = '/' . $this
->randomMachineName(8);
$edit['alias[0][value]'] = $node4_alias . '/';
$this
->drupalPostForm('admin/config/search/path/add', $edit, t('Save'));
$this
->assertNoText($edit['alias[0][value]'], 'The absolute alias was not found.');
$this
->assertText(trim($edit['alias[0][value]'], '/'), 'The alias without trailing slash was found.');
$pid = $this
->getPID($node4_alias);
$edit = [];
$edit['alias[0][value]'] = $node4_alias;
$edit['path[0][value]'] = '/node/' . $node2
->id();
$this
->drupalPostForm('admin/config/search/path/edit/' . $pid, $edit, t('Save'));
$this
->assertText('The alias has been saved.');
$this
->drupalGet($edit['alias[0][value]']);
$this
->assertNoText($node4
->label(), 'Previous alias no longer works.');
$this
->assertText($node2
->label(), 'Alias works.');
$this
->assertSession()
->statusCodeEquals(200);
$pid = $this
->getPID($node3_alias);
$edit = [];
$edit['alias[0][value]'] = $node4_alias;
$edit['path[0][value]'] = '/node/' . $node3
->id();
$this
->drupalPostForm('admin/config/search/path/edit/' . $pid, $edit, t('Save'));
$this
->assertRaw(t('The alias %alias is already in use in this language.', [
'%alias' => $edit['alias[0][value]'],
]));
$node5 = $this
->drupalCreateNode();
$edit = [];
$edit['path[0][value]'] = 'node/' . $node5
->id();
$node5_alias = $this
->randomMachineName(8);
$edit['alias[0][value]'] = $node5_alias . '/';
$this
->drupalPostForm('admin/config/search/path/add', $edit, t('Save'));
$this
->assertUrl('admin/config/search/path/add');
$this
->assertText('The source path has to start with a slash.');
$this
->assertText('The alias path has to start with a slash.');
}
public function testNodeAlias() {
$node1 = $this
->drupalCreateNode();
$edit = [];
$edit['path[0][alias]'] = '/' . $this
->randomMachineName(8);
$this
->drupalPostForm('node/' . $node1
->id() . '/edit', $edit, t('Save'));
$this
->drupalGet($edit['path[0][alias]']);
$this
->assertText($node1
->label(), 'Alias works.');
$this
->assertSession()
->statusCodeEquals(200);
$elements = $this
->xpath("//link[contains(@rel, 'canonical') and contains(@href, '" . $edit['path[0][alias]'] . "')]");
$this
->assertTrue(!empty($elements), 'Page contains canonical link URL.');
$elements = $this
->xpath("//link[contains(@rel, 'shortlink') and contains(@href, 'node/" . $node1
->id() . "')]");
$this
->assertTrue(!empty($elements), 'Page contains shortlink URL.');
$previous = $edit['path[0][alias]'];
$edit['path[0][alias]'] = '/alias' . "- ._~!\$'\"()*@[]?&+%#,;=:" . "%23%25%26%2B%2F%3F" . "中國書۞";
$connection = Database::getConnection();
if ($connection
->databaseType() != 'sqlite') {
$edit['path[0][alias]'] .= "ïвβéø";
}
$this
->drupalPostForm('node/' . $node1
->id() . '/edit', $edit, t('Save'));
$this
->drupalGet(mb_strtoupper($edit['path[0][alias]']));
$this
->assertText($node1
->label(), 'Changed alias works.');
$this
->assertSession()
->statusCodeEquals(200);
$this
->drupalGet($previous);
$this
->assertNoText($node1
->label(), 'Previous alias no longer works.');
$this
->assertSession()
->statusCodeEquals(404);
$node2 = $this
->drupalCreateNode();
$this
->drupalPostForm('node/' . $node2
->id() . '/edit', $edit, t('Save'));
$this
->assertSession()
->pageTextContains("The alias {$edit['path[0][alias]']} is already in use in this language.");
$this
->drupalPostForm('node/' . $node1
->id() . '/edit', [
'path[0][alias]' => '',
], t('Save'));
$this
->drupalGet($edit['path[0][alias]']);
$this
->assertNoText($node1
->label(), 'Alias was successfully deleted.');
$this
->assertSession()
->statusCodeEquals(404);
$node3 = $this
->drupalCreateNode();
$edit = [
'path[0][alias]' => '/' . $this
->randomMachineName(8),
];
$this
->drupalPostForm('node/' . $node3
->id() . '/edit', $edit, t('Save'));
$this
->drupalGet(trim($edit['path[0][alias]'], '/'));
$this
->assertText($node3
->label(), 'Alias became relative.');
$this
->assertSession()
->statusCodeEquals(200);
$node4 = $this
->drupalCreateNode();
$edit = [
'path[0][alias]' => '/' . $this
->randomMachineName(8) . '/',
];
$this
->drupalPostForm('node/' . $node4
->id() . '/edit', $edit, t('Save'));
$this
->drupalGet(trim($edit['path[0][alias]'], '/'));
$this
->assertText($node4
->label(), 'Alias trimmed trailing slash.');
$this
->assertSession()
->statusCodeEquals(200);
$node5 = $this
->drupalCreateNode();
$edit = [
'path[0][alias]' => '/' . $this
->randomMachineName(8),
];
$this
->drupalPostForm('node/' . $node5
->id() . '/edit', $edit, t('Save'));
$node5
->delete();
$path_alias = \Drupal::service('path_alias.repository')
->lookUpBySystemPath('/node/' . $node5
->id(), $node5
->language()
->getId());
$this
->assertNull($path_alias, 'Alias was successfully deleted when the referenced node was deleted.');
$node6 = $this
->drupalCreateNode();
$edit = [
'path[0][alias]' => '0',
];
$this
->drupalGet($node6
->toUrl('edit-form'));
$this
->submitForm($edit, 'Save');
$this
->assertSession()
->pageTextContains('The alias path has to start with a slash.');
$alias = $this
->randomMachineName(8);
$edit = [
'path[0][alias]' => '//' . $alias,
];
$this
->drupalPostForm($node6
->toUrl('edit-form'), $edit, t('Save'));
$this
->drupalGet(Url::fromRoute('system.admin_content'));
$link_xpath = $this
->xpath('//a[normalize-space(text())=:label]', [
':label' => $node6
->getTitle(),
]);
$link_href = $link_xpath[0]
->getAttribute('href');
$this
->assertEquals($link_href, base_path() . $alias);
$this
->clickLink($node6
->getTitle());
$this
->assertSession()
->statusCodeEquals(404);
}
public function getPID($alias) {
$result = \Drupal::entityTypeManager()
->getStorage('path_alias')
->getQuery()
->condition('alias', $alias, '=')
->accessCheck(FALSE)
->execute();
return reset($result);
}
public function testDuplicateNodeAlias() {
$node_one = $this
->drupalCreateNode();
$edit = [];
$edit['path[0][alias]'] = '/' . $this
->randomMachineName();
$this
->drupalPostForm('node/' . $node_one
->id() . '/edit', $edit, t('Save'));
$node_two = $this
->drupalCreateNode();
$this
->drupalPostForm('node/' . $node_two
->id() . '/edit', $edit, t('Save'));
$this
->assertSession()
->pageTextContains("The alias {$edit['path[0][alias]']} is already in use in this language.");
$this
->assertFieldByXPath("//input[@name='path[0][alias]' and contains(@class, 'error')]", $edit['path[0][alias]'], 'Textfield exists and has the error class.');
$this
->assertTrue($this->container
->get('module_installer')
->install([
'inline_form_errors',
], TRUE), 'Installed inline_form_errors.');
$this
->drupalPostForm('node/' . $node_two
->id() . '/edit', $edit, t('Preview'));
$this
->assertSession()
->pageTextContains("The alias {$edit['path[0][alias]']} is already in use in this language.");
$this
->assertSession()
->pageTextContains(t('1 error has been found: URL alias'), 'Form error found with expected text.');
$this
->assertSession()
->linkExists('URL alias');
$this
->assertSession()
->linkByHrefExists('#edit-path-0-alias');
}
}