1 path_pattern.test | PathPatternFunctionalTestCase::testNodeEditing() |
Basic functional testing of node Path patterns.
File
- core/
modules/ path/ tests/ path_pattern.test, line 511 - Functionality tests for automatic path generation.
Class
- PathPatternFunctionalTestCase
- Test basic Path automatic URL alias functionality.
Code
function testNodeEditing() {
$config = config('path.settings');
// Delete the default node pattern. Only the page content type will have a pattern.
$config->set('node_pattern', '');
$config->save();
// Ensure the Path auto checkbox is checked by default on the node add form.
$this->backdropGet('node/add/page');
$this->assertFieldChecked('edit-path-auto');
// Create node for testing by saving the node form.
$title = ' Testing: node title [';
$automatic_alias = 'content/testing-node-title';
$this->backdropPost(NULL, array('title' => $title), 'Save');
$node = $this->backdropGetNodeByTitle($title);
// Look for URL alias generated in the form.
$this->backdropGet("node/{$node->nid}/edit");
$this->assertFieldChecked('edit-path-auto');
$this->assertFieldByName('path[alias]', $automatic_alias, 'Generated URL alias visible in the path URL alias field.');
// Check whether the URL alias actually works.
$this->backdropGet($automatic_alias);
$this->assertText($title, 'Node accessible through automatic alias.');
// Disable the update action. The checkbox should not be visible.
$config->set('update_action', PATH_UPDATE_ACTION_NO_NEW);
$config->save();
$this->backdropGet("node/{$node->nid}/edit");
$this->assertNoFieldById('edit-path-auto');
// Reset the update action back to default. The checkbox should be visible.
$config->set('update_action', PATH_UPDATE_ACTION_LEAVE);
$config->save();
$this->backdropGet("node/{$node->nid}/edit");
$this->assertFieldChecked('edit-path-auto');
// Manually set the node's alias.
$manual_alias = 'content/' . $node->nid;
$edit = array(
'path[auto]' => FALSE,
'path[alias]' => $manual_alias,
);
$this->backdropPost(NULL, $edit, t('Save'));
$this->assertText("Page $title has been updated.");
// Check that the automatic URL alias checkbox is now unchecked by default.
$this->backdropGet("node/{$node->nid}/edit");
$this->assertNoFieldChecked('edit-path-auto');
$this->assertFieldByName('path[alias]', $manual_alias);
// Submit the node form with the default values.
$this->backdropPost(NULL, array(), t('Save'));
$this->assertText("Page $title has been updated.");
// Test that the old (automatic) URL alias has been deleted and only accessible
// through the new (manual) alias.
$this->backdropGet($automatic_alias);
$this->assertResponse(404, 'Node not accessible through automatic alias.');
$this->backdropGet($manual_alias);
$this->assertText($title, 'Node accessible through manual alias.');
// Now attempt to create a node that has no pattern (post content type).
// The automatic URL alias checkbox should not exist.
$this->backdropGet('node/add/post');
$this->assertNoFieldById('edit-path-auto');
$this->assertFieldByName('path[alias]', '');
$edit = array();
$edit['title'] = 'My test post';
$this->backdropPost(NULL, $edit, t('Save'));
$node = $this->backdropGetNodeByTitle($edit['title']);
// Automatic URL alias checkbox should still not exist.
$this->backdropGet('node/' . $node->nid . '/edit');
$this->assertNoFieldById('edit-path-auto');
$this->assertFieldByName('path[alias]', '');
// Set the page pattern to use only tokens so we can test the checkbox
// behavior if none of the tokens have a value currently.
$config->set('node_page_pattern', '[node:title]');
$config->save();
// Create a node with an empty title. The auto checkbox should still be
// visible but unchecked.
$node = $this->backdropCreateNode(array('type' => 'page', 'title' => ''));
$this->backdropGet('node/' . $node->nid . '/edit');
$this->assertNoFieldChecked('edit-path-auto');
$this->assertFieldByName('path[alias]', '');
$this->assertNoEntityAlias('node', $node);
$edit = array();
$edit['title'] = 'Valid title';
$edit['path[auto]'] = TRUE;
$this->backdropPost(NULL, $edit, t('Save'));
$this->backdropGet('node/' . $node->nid . '/edit');
$this->assertFieldChecked('edit-path-auto');
$this->assertFieldByName('path[alias]', 'valid-title');
}