You are here

public function PathautoNodeWebTest::testNodeEditing in Pathauto 8

Tests editing nodes with different settings.

File

tests/src/Functional/PathautoNodeWebTest.php, line 64

Class

PathautoNodeWebTest
Tests pathauto node UI integration.

Namespace

Drupal\Tests\pathauto\Functional

Code

public 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, [
    'title[0][value]' => $title,
  ], t('Save'));
  $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 = [
    'path[0][pathauto]' => FALSE,
    'path[0][alias]' => $manual_alias,
  ];
  $this
    ->drupalPostForm($node
    ->toUrl('edit-form'), $edit, t('Save'));
  $this
    ->assertText(t('@type @title has been updated.', [
    '@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, [
    'path[0][pathauto]' => FALSE,
  ], t('Save'));
  $this
    ->assertText(t('@type @title has been updated.', [
    '@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 = [
    'title[0][value]' => $title,
    'path[0][pathauto]' => TRUE,
    'path[0][alias]' => '/should-not-get-created',
  ];
  $this
    ->drupalPostForm('node/add/page', $edit, t('Save'));
  $this
    ->assertNoAliasExists([
    '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 = [];
  $edit['title'] = 'My test article';
  $this
    ->drupalCreateNode($edit);

  //$this->drupalPostForm(NULL, $edit, t('Save'));
  $node = $this
    ->drupalGetNodeByTitle($edit['title']);

  // Pathauto checkbox should still not exist.
  $this
    ->drupalGet($node
    ->toUrl('edit-form'));
  $this
    ->assertNoFieldById('edit-path-0-pathauto');
  $this
    ->assertFieldByName('path[0][alias]', '');
  $this
    ->assertNoEntityAlias($node);
}