Commit 79d21e74 authored by alexpott's avatar alexpott
Browse files

Issue #1847174 by TravisCarden, Jody Lynn, boromino, Mariano, iaine: Path...

Issue #1847174 by TravisCarden, Jody Lynn, boromino, Mariano, iaine: Path alias validation should test for relative path, no trailing slash requirements
parent 7c37dac7
......@@ -104,7 +104,7 @@ public function buildForm(array $form, FormStateInterface $form_state, $pid = NU
'#default_value' => $this->path['alias'],
'#maxlength' => 255,
'#size' => 45,
'#description' => $this->t('Specify an alternative path by which this data can be accessed. For example, type "about" when writing an about page. Use a relative path and don\'t add a trailing slash or the URL alias won\'t work.'),
'#description' => $this->t('Specify an alternative path by which this data can be accessed. For example, type "about" when writing an about page. Use a relative path.'),
'#field_prefix' => $this->url('<none>', [], ['absolute' => TRUE]),
'#required' => TRUE,
);
......@@ -151,6 +151,8 @@ public function validateForm(array &$form, FormStateInterface $form_state) {
$source = &$form_state->getValue('source');
$source = $this->aliasManager->getPathByAlias($source);
$alias = $form_state->getValue('alias');
// Trim the submitted value of whitespace and slashes.
$alias = trim(trim($alias), " \\/");
// Language is only set if language.module is enabled, otherwise save for all
// languages.
$langcode = $form_state->getValue('langcode', LanguageInterface::LANGCODE_NOT_SPECIFIED);
......@@ -174,6 +176,8 @@ public function submitForm(array &$form, FormStateInterface $form_state) {
$source = &$form_state->getValue('source');
$source = $this->aliasManager->getPathByAlias($source);
$alias = $form_state->getValue('alias');
// Trim the submitted value of whitespace and slashes.
$alias = trim(trim($alias), " \\/");
// Language is only set if language.module is enabled, otherwise save for all
// languages.
$langcode = $form_state->getValue('langcode', LanguageInterface::LANGCODE_NOT_SPECIFIED);
......
......@@ -58,7 +58,7 @@ public function formElement(FieldItemListInterface $items, $delta, array $elemen
'#default_value' => $path['alias'],
'#required' => $element['#required'],
'#maxlength' => 255,
'#description' => $this->t('The alternative URL for this content. Use a relative path without a trailing slash. For example, enter "about" for the about page.'),
'#description' => $this->t('The alternative URL for this content. Use a relative path. For example, enter "about" for the about page.'),
);
$element['pid'] = array(
'#type' => 'value',
......@@ -84,8 +84,8 @@ public function formElement(FieldItemListInterface $items, $delta, array $elemen
* The form state.
*/
public static function validateFormElement(array &$element, FormStateInterface $form_state) {
// Trim the submitted value.
$alias = trim($element['alias']['#value']);
// Trim the submitted value of whitespace and slashes.
$alias = trim(trim($element['alias']['#value']), " \\/");
if (!empty($alias)) {
$form_state->setValueForElement($element['alias'], $alias);
......
......@@ -130,6 +130,34 @@ function testAdminAlias() {
$this->assertNoText($alias, 'The untruncated alias was not found.');
// The 'truncated' alias will always be found.
$this->assertText($truncated_alias, 'The truncated alias was found.');
// Create third test node.
$node3 = $this->drupalCreateNode();
// Create absolute path alias.
$edit = array();
$edit['source'] = 'node/' . $node3->id();
$edit['alias'] = '/' . $this->randomMachineName(8);
$this->drupalPostForm('admin/config/search/path/add', $edit, t('Save'));
// Confirm that the alias was converted to a relative path.
$this->assertNoText($edit['alias'], 'The absolute alias was not found.');
// The 'relative' alias will always be found.
$this->assertText(trim($edit['alias'], '/'), 'The relative alias was found.');
// Create fourth test node.
$node4 = $this->drupalCreateNode();
// Create alias with trailing slash.
$edit = array();
$edit['source'] = 'node/' . $node4->id();
$edit['alias'] = $this->randomMachineName(8) . '/';
$this->drupalPostForm('admin/config/search/path/add', $edit, t('Save'));
// Confirm that the alias with trailing slash is not found.
$this->assertNoText($edit['alias'], 'The absolute alias was not found.');
// The alias without trailing flash is found.
$this->assertText(trim($edit['alias'], '/'), 'The alias without trailing slash was found.');
}
/**
......@@ -189,6 +217,30 @@ function testNodeAlias() {
$this->drupalGet($edit['path[0][alias]']);
$this->assertNoText($node1->label(), 'Alias was successfully deleted.');
$this->assertResponse(404);
// Create third test node.
$node3 = $this->drupalCreateNode();
// Set its path alias to an absolute path.
$edit = array('path[0][alias]' => '/' . $this->randomMachineName(8));
$this->drupalPostForm('node/' . $node3->id() . '/edit', $edit, t('Save'));
// Confirm that the alias was converted to a relative path.
$this->drupalGet(trim($edit['path[0][alias]'], '/'));
$this->assertText($node3->label(), 'Alias became relative.');
$this->assertResponse(200);
// Create fourth test node.
$node4 = $this->drupalCreateNode();
// Set its path alias to have a trailing slash.
$edit = array('path[0][alias]' => $this->randomMachineName(8) . '/');
$this->drupalPostForm('node/' . $node4->id() . '/edit', $edit, t('Save'));
// Confirm that the alias was converted to a relative path.
$this->drupalGet(trim($edit['path[0][alias]'], '/'));
$this->assertText($node4->label(), 'Alias trimmed trailing slash.');
$this->assertResponse(200);
}
/**
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment