Exemplo n.º 1
0
 /**
  * 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);
 }
Exemplo n.º 2
0
 /**
  * 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);
 }