/** * Creates a pathauto pattern. * * @param string $entity_type_id * The entity type. * @param string $pattern * The path pattern. * @param int $weight * (optional) The pattern weight. * * @return \Drupal\pathauto\PathautoPatternInterface * The created pattern. */ protected function createPattern($entity_type_id, $pattern, $weight = 10) { $pattern = PathautoPattern::create(['id' => Unicode::strtolower($this->randomMachineName()), 'type' => 'canonical_entities:' . $entity_type_id, 'pattern' => $pattern, 'weight' => $weight]); $pattern->save(); return $pattern; }
/** * Tests that nodes without a Pathauto pattern can set custom aliases. */ public function testCustomAliasWithoutPattern() { // First, delete all patterns to be sure that there will be no match. $entity_ids = \Drupal::entityQuery('pathauto_pattern')->execute(); $entities = PathautoPattern::loadMultiple($entity_ids); foreach ($entities as $entity) { $entity->delete(); } // Next, create a node with a custom alias. $edit = ['title[0][value]' => 'Sample article', 'path[0][alias]' => '/sample-article']; $this->drupalPostForm('node/add/article', $edit, t('Save and publish')); $this->assertText(t('article Sample article has been created.')); // Test the alias. $this->assertAliasExists(array('alias' => '/sample-article')); $this->drupalGet('sample-article'); $this->assertResponse(200, 'A node without a pattern can have a custom alias.'); // Now create a node through the API. $node = Node::create(['type' => 'article', 'title' => 'Sample article API', 'path' => ['alias' => '/sample-article-api']]); $node->save(); // Test the alias. $this->assertAliasExists(['alias' => '/sample-article-api']); $this->drupalGet('sample-article-api'); $this->assertResponse(200); }
/** * Tests editing nodes with different settings. */ function testNodeEditing() { // Ensure that the Pathauto checkbox is checked by default on the node add form. $this->drupalGet('node/add/page'); $this->assertFieldChecked('edit-path-0-pathauto'); // Create a node by saving the node form. $title = ' Testing: node title ['; $automatic_alias = '/content/testing-node-title'; $this->drupalPostForm(NULL, array('title[0][value]' => $title), t('Save and publish')); $node = $this->drupalGetNodeByTitle($title); // Look for alias generated in the form. $this->drupalGet("node/{$node->id()}/edit"); $this->assertFieldChecked('edit-path-0-pathauto'); $this->assertFieldByName('path[0][alias]', $automatic_alias, 'Generated alias visible in the path alias field.'); // Check whether the alias actually works. $this->drupalGet($automatic_alias); $this->assertText($title, 'Node accessible through automatic alias.'); // Manually set the node's alias. $manual_alias = '/content/' . $node->id(); $edit = array('path[0][pathauto]' => FALSE, 'path[0][alias]' => $manual_alias); $this->drupalPostForm($node->urlInfo('edit-form'), $edit, t('Save and keep published')); $this->assertRaw(t('@type %title has been updated.', array('@type' => 'page', '%title' => $title))); // Check that the automatic alias checkbox is now unchecked by default. $this->drupalGet("node/{$node->id()}/edit"); $this->assertNoFieldChecked('edit-path-0-pathauto'); $this->assertFieldByName('path[0][alias]', $manual_alias); // Submit the node form with the default values. $this->drupalPostForm(NULL, array('path[0][pathauto]' => FALSE), t('Save and keep published')); $this->assertRaw(t('@type %title has been updated.', array('@type' => 'page', '%title' => $title))); // Test that the old (automatic) alias has been deleted and only accessible // through the new (manual) alias. $this->drupalGet($automatic_alias); $this->assertResponse(404, 'Node not accessible through automatic alias.'); $this->drupalGet($manual_alias); $this->assertText($title, 'Node accessible through manual alias.'); // Test that the manual alias is not kept for new nodes when the pathauto // checkbox is ticked. $title = 'Automatic Title'; $edit = array('title[0][value]' => $title, 'path[0][pathauto]' => TRUE, 'path[0][alias]' => '/should-not-get-created'); $this->drupalPostForm('node/add/page', $edit, t('Save and publish')); $this->assertNoAliasExists(array('alias' => 'should-not-get-created')); $node = $this->drupalGetNodeByTitle($title); $this->assertEntityAlias($node, '/content/automatic-title'); // Remove the pattern for nodes, the pathauto checkbox should not be // displayed. $ids = \Drupal::entityQuery('pathauto_pattern')->condition('type', 'canonical_entities:node')->execute(); foreach (PathautoPattern::loadMultiple($ids) as $pattern) { $pattern->delete(); } $this->drupalGet('node/add/article'); $this->assertNoFieldById('edit-path-0-pathauto'); $this->assertFieldByName('path[0][alias]', ''); $edit = array(); $edit['title'] = 'My test article'; $this->drupalCreateNode($edit); //$this->drupalPostForm(NULL, $edit, t('Save and keep published')); $node = $this->drupalGetNodeByTitle($edit['title']); // Pathauto checkbox should still not exist. $this->drupalGet($node->urlInfo('edit-form')); $this->assertNoFieldById('edit-path-0-pathauto'); $this->assertFieldByName('path[0][alias]', ''); $this->assertNoEntityAlias($node); }
function testPatternsWorkflow() { // Try to save an empty pattern, should not be allowed. $this->drupalGet('admin/config/search/path/patterns/add'); $edit = array('type' => 'canonical_entities:node'); $this->drupalPostAjaxForm(NULL, $edit, 'type'); $edit += array('bundles[page]' => TRUE, 'label' => 'Page pattern', 'id' => 'page_pattern'); $this->drupalPostForm(NULL, $edit, 'Save'); $this->assertText('Path pattern field is required.'); $this->assertNoText('The configuration options have been saved.'); // Try to save an invalid pattern. $edit += array('pattern' => '[node:title]/[user:name]/[term:name]'); $this->drupalPostForm(NULL, $edit, 'Save'); $this->assertText('Path pattern is using the following invalid tokens: [user:name], [term:name].'); $this->assertNoText('The configuration options have been saved.'); $edit['pattern'] = '#[node:title]'; $this->drupalPostForm(NULL, $edit, 'Save'); $this->assertText('The Path pattern is using the following invalid characters: #.'); $this->assertNoText('The configuration options have been saved.'); // Fix the pattern, then check that it gets saved successfully. $edit['pattern'] = '[node:title]'; $this->drupalPostForm(NULL, $edit, 'Save'); $this->assertText('Pattern Page pattern saved.'); \Drupal::service('pathauto.generator')->resetCaches(); // Create a node with pattern enabled and check if the pattern applies. $title = 'Page Pattern enabled'; $alias = '/page-pattern-enabled'; $node = $this->createNode(['title' => $title, 'type' => 'page']); $this->drupalGet($alias); $this->assertResponse(200); $this->assertEntityAlias($node, $alias); // Edit workflow, set a new label for the pattern. $this->drupalGet('/admin/config/search/path/patterns'); $this->clickLink(t('Edit')); $this->assertUrl('/admin/config/search/path/patterns/page_pattern'); $this->assertFieldByName('pattern', '[node:title]'); $this->assertFieldByName('label', 'Page pattern'); $this->assertFieldChecked('edit-status'); $this->assertLink(t('Delete')); $edit = array('label' => 'Test'); $this->drupalPostForm('/admin/config/search/path/patterns/page_pattern', $edit, t('Save')); $this->assertText('Pattern Test saved.'); // Disable workflow. $this->drupalGet('/admin/config/search/path/patterns'); $this->assertNoLink(t('Enable')); $this->clickLink(t('Disable')); $this->assertUrl('/admin/config/search/path/patterns/page_pattern/disable'); $this->drupalPostForm(NULL, [], t('Disable')); $this->assertText('Disabled pattern Test.'); // Load the pattern from storage and check if its disabled. $pattern = PathautoPattern::load('page_pattern'); $this->assertFalse($pattern->status()); \Drupal::service('pathauto.generator')->resetCaches(); // Create a node with pattern disabled and check that we have no new alias. $title = 'Page Pattern disabled'; $node = $this->createNode(['title' => $title, 'type' => 'page']); $this->assertNoEntityAlias($node); // Enable workflow. $this->drupalGet('/admin/config/search/path/patterns'); $this->assertNoLink(t('Disable')); $this->clickLink(t('Enable')); $this->assertUrl('/admin/config/search/path/patterns/page_pattern/enable'); $this->drupalPostForm(NULL, [], t('Enable')); $this->assertText('Enabled pattern Test.'); // Reload pattern from storage and check if its enabled. $pattern = PathautoPattern::load('page_pattern'); $this->assertTrue($pattern->status()); // Delete workflow. $this->drupalGet('/admin/config/search/path/patterns'); $this->clickLink(t('Delete')); $this->assertUrl('/admin/config/search/path/patterns/page_pattern/delete'); $this->assertText(t('This action cannot be undone.')); $this->drupalPostForm(NULL, [], t('Delete')); $this->assertText('The pathauto pattern Test has been deleted.'); $this->assertFalse(PathautoPattern::load('page_pattern')); }