View source
<?php
namespace Drupal\Tests\pathauto\Functional;
use Drupal\pathauto\Entity\PathautoPattern;
use Drupal\node\Entity\Node;
use Drupal\pathauto\PathautoState;
use Drupal\Tests\BrowserTestBase;
class PathautoNodeWebTest extends BrowserTestBase {
use PathautoTestHelperTrait;
protected $defaultTheme = 'stable';
public static $modules = [
'node',
'pathauto',
'views',
'taxonomy',
'pathauto_views_test',
];
protected $adminUser;
protected function setUp() {
parent::setUp();
$this
->drupalCreateContentType([
'type' => 'page',
'name' => 'Basic page',
]);
$this
->drupalCreateContentType([
'type' => 'article',
]);
$permissions = [
'administer pathauto',
'administer url aliases',
'create url aliases',
'bypass node access',
'access content overview',
];
$this->adminUser = $this
->drupalCreateUser($permissions);
$this
->drupalLogin($this->adminUser);
$this
->createPattern('node', '/content/[node:title]');
}
public function testNodeEditing() {
$this
->drupalGet('node/add/page');
$this
->assertFieldChecked('edit-path-0-pathauto');
$title = ' Testing: node title [';
$automatic_alias = '/content/testing-node-title';
$this
->drupalPostForm(NULL, [
'title[0][value]' => $title,
], t('Save'));
$node = $this
->drupalGetNodeByTitle($title);
$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.');
$this
->drupalGet($automatic_alias);
$this
->assertText($title, 'Node accessible through automatic 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,
]));
$this
->drupalGet("node/{$node->id()}/edit");
$this
->assertNoFieldChecked('edit-path-0-pathauto');
$this
->assertFieldByName('path[0][alias]', $manual_alias);
$this
->drupalPostForm(NULL, [
'path[0][pathauto]' => FALSE,
], t('Save'));
$this
->assertText(t('@type @title has been updated.', [
'@type' => 'page',
'@title' => $title,
]));
$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.');
$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');
$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);
$node = $this
->drupalGetNodeByTitle($edit['title']);
$this
->drupalGet($node
->toUrl('edit-form'));
$this
->assertNoFieldById('edit-path-0-pathauto');
$this
->assertFieldByName('path[0][alias]', '');
$this
->assertNoEntityAlias($node);
}
public function testNodeOperations() {
$node1 = $this
->drupalCreateNode([
'title' => 'node1',
]);
$node2 = $this
->drupalCreateNode([
'title' => 'node2',
]);
$this
->deleteAllAliases();
$this
->drupalGet('admin/content');
if (strpos($this
->getTextContent(), 'node1') < strpos($this
->getTextContent(), 'node2')) {
$index = 0;
}
else {
$index = 1;
}
$edit = [
'action' => 'pathauto_update_alias_node',
'node_bulk_form[' . $index . ']' => TRUE,
];
$this
->drupalPostForm(NULL, $edit, t('Apply to selected items'));
$this
->assertText('Update URL alias was applied to 1 item.');
$this
->assertEntityAlias($node1, '/content/' . $node1
->getTitle());
$this
->assertEntityAlias($node2, '/node/' . $node2
->id());
}
public function testNodeState() {
$nodeNoAliasUser = $this
->drupalCreateUser([
'bypass node access',
]);
$nodeAliasUser = $this
->drupalCreateUser([
'bypass node access',
'create url aliases',
]);
$node = $this
->drupalCreateNode([
'title' => 'Node version one',
'type' => 'page',
'path' => [
'pathauto' => PathautoState::SKIP,
],
]);
$this
->assertNoEntityAlias($node);
$node->path->alias = '/test-alias';
$node
->save();
\Drupal::entityTypeManager()
->getStorage('node')
->resetCache();
$node = Node::load($node
->id());
$this
->assertSame(PathautoState::SKIP, $node->path->pathauto);
$this
->assertEntityAlias($node, '/test-alias');
$this
->assertNoEntityAliasExists($node, '/content/node-version-one');
$this
->drupalLogin($nodeNoAliasUser);
$this
->drupalGet('node/' . $node
->id() . '/edit');
$this
->assertNoFieldByName('path[0][pathauto]');
$edit = [
'title[0][value]' => 'Node version two',
];
$this
->drupalPostForm(NULL, $edit, 'Save');
$this
->assertText('Basic page Node version two has been updated.');
$this
->assertEntityAlias($node, '/test-alias');
$this
->assertNoEntityAliasExists($node, '/content/node-version-one');
$this
->assertNoEntityAliasExists($node, '/content/node-version-two');
$this
->drupalLogin($nodeAliasUser);
$this
->drupalGet('node/' . $node
->id() . '/edit');
$this
->assertNoFieldChecked('edit-path-0-pathauto');
$edit = [
'title[0][value]' => 'Node version three',
'path[0][alias]' => '/manually-edited-alias',
];
$this
->drupalPostForm(NULL, $edit, 'Save');
$this
->assertText('Basic page Node version three has been updated.');
$this
->assertEntityAlias($node, '/manually-edited-alias');
$this
->assertNoEntityAliasExists($node, '/test-alias');
$this
->assertNoEntityAliasExists($node, '/content/node-version-one');
$this
->assertNoEntityAliasExists($node, '/content/node-version-two');
$this
->assertNoEntityAliasExists($node, '/content/node-version-three');
\Drupal::entityTypeManager()
->getStorage('node')
->resetCache();
$node = Node::load($node
->id());
$node->path->pathauto = PathautoState::CREATE;
$node
->save();
\Drupal::entityTypeManager()
->getStorage('node')
->resetCache();
$node = Node::load($node
->id());
$this
->assertSame(PathautoState::CREATE, $node->path->pathauto);
$this
->assertEntityAlias($node, '/content/node-version-three');
$this
->assertNoEntityAliasExists($node, '/manually-edited-alias');
$this
->assertNoEntityAliasExists($node, '/test-alias');
$this
->assertNoEntityAliasExists($node, '/content/node-version-one');
$this
->assertNoEntityAliasExists($node, '/content/node-version-two');
$node
->delete();
$this
->assertNull(\Drupal::keyValue('pathauto_state.node')
->get($node
->id()), 'Pathauto state was deleted');
}
public function testCustomAliasWithoutPattern() {
$entity_ids = \Drupal::entityQuery('pathauto_pattern')
->execute();
$entities = PathautoPattern::loadMultiple($entity_ids);
foreach ($entities as $entity) {
$entity
->delete();
}
$edit = [
'title[0][value]' => 'Sample article',
'path[0][alias]' => '/sample-article',
];
$this
->drupalPostForm('node/add/article', $edit, t('Save'));
$this
->assertText(t('article Sample article has been created.'));
$this
->assertAliasExists([
'alias' => '/sample-article',
]);
$this
->drupalGet('sample-article');
$this
->assertResponse(200, 'A node without a pattern can have a custom alias.');
$node = Node::create([
'type' => 'article',
'title' => 'Sample article API',
'path' => [
'alias' => '/sample-article-api',
],
]);
$node
->save();
$this
->assertAliasExists([
'alias' => '/sample-article-api',
]);
$this
->drupalGet('sample-article-api');
$this
->assertResponse(200);
}
public function testCustomAliasAfterAutomaticAlias() {
$this
->createPattern('node', '/content/[node:title]');
$edit = [
'title[0][value]' => 'Sample article',
];
$this
->drupalPostForm('node/add/article', $edit, 'Save');
$this
->assertSession()
->pageTextContains('article Sample article has been created.');
$this
->assertAliasExists([
'alias' => '/content/sample-article',
]);
$this
->drupalGet('/content/sample-article');
$this
->assertSession()
->statusCodeEquals(200);
$edit = [
'path[0][pathauto]' => 0,
'path[0][alias]' => '/sample-pattern-for-article',
];
$this
->drupalPostForm('node/1/edit', $edit, 'Save');
$this
->assertAliasExists([
'alias' => '/sample-pattern-for-article',
]);
$this
->assertNoAliasExists([
'alias' => '/content/sample-article',
]);
$this
->drupalGet('sample-pattern-for-article');
$this
->assertSession()
->statusCodeEquals(200);
}
public function testCustomAliasAfterRemovingPattern() {
$this
->createPattern('node', '/content/[node:title]');
$edit = [
'title[0][value]' => 'Sample article',
];
$this
->drupalPostForm('node/add/article', $edit, 'Save');
$this
->assertSession()
->pageTextContains('article Sample article has been created.');
$this
->assertAliasExists([
'alias' => '/content/sample-article',
]);
$this
->drupalGet('/content/sample-article');
$this
->assertSession()
->statusCodeEquals(200);
$this
->drupalGet('node/1/edit');
$this
->assertSession()
->elementExists('css', '#edit-path-0-pathauto');
$entity_ids = \Drupal::entityQuery('pathauto_pattern')
->execute();
$entities = PathautoPattern::loadMultiple($entity_ids);
foreach ($entities as $entity) {
$entity
->delete();
}
$this
->drupalGet('node/1/edit');
$this
->assertSession()
->elementNotExists('css', '#edit-path-0-pathauto');
$edit = [
'path[0][alias]' => '/sample-alias-for-article',
];
$this
->submitForm($edit, 'Save');
$this
->assertAliasExists([
'alias' => '/sample-alias-for-article',
]);
$this
->assertNoAliasExists([
'alias' => '/content/sample-article',
]);
$this
->drupalGet('sample-alias-for-article');
$this
->assertSession()
->statusCodeEquals(200);
}
}