View source
<?php
namespace Drupal\Tests\multiversion\Functional;
use Drupal\multiversion\Entity\Workspace;
use Drupal\multiversion\Entity\WorkspaceType;
use Drupal\Tests\BrowserTestBase;
use Drupal\Tests\user\Traits\UserCreationTrait;
class PathAliasTest extends BrowserTestBase {
use UserCreationTrait;
protected static $modules = [
'system',
'path',
'multiversion',
'key_value',
'serialization',
'user',
'node',
];
protected function setUp() {
parent::setUp();
if ($this->profile != 'standard') {
$this
->drupalCreateContentType([
'type' => 'article',
'name' => 'Article',
]);
$this
->drupalCreateContentType([
'type' => 'page',
'name' => 'Page',
]);
}
$web_user = $this
->drupalCreateUser([
'administer url aliases',
'create url aliases',
'administer workspaces',
]);
$this
->drupalLogin($web_user);
}
public function testPathAlias() {
$alias_storage = \Drupal::service('path.alias_storage');
WorkspaceType::create([
'id' => 'test',
'label' => 'Test',
])
->save();
$live = Workspace::create([
'type' => 'test',
'machine_name' => 'live',
'label' => 'Live',
]);
$live
->save();
$stage = Workspace::create([
'type' => 'test',
'machine_name' => 'stage',
'label' => 'Stage',
]);
$stage
->save();
\Drupal::service('workspace.manager')
->setActiveWorkspace($live);
$alias = '/foo';
$node1 = $this
->drupalCreateNode();
$node1
->get('path')->alias = $alias;
$node1
->save();
$this
->assertEquals($alias, $node1
->get('path')->alias);
$stored_alias = $alias_storage
->lookupPathAlias('/' . $node1
->toUrl()
->getInternalPath(), $node1
->language()
->getId());
$this
->assertEquals($alias, $stored_alias);
\Drupal::service('workspace.manager')
->setActiveWorkspace($stage);
$stored_alias = $alias_storage
->lookupPathAlias('/' . $node1
->toUrl()
->getInternalPath(), $node1
->language()
->getId());
$this
->assertFalse($stored_alias);
$node2 = $this
->drupalCreateNode();
$node2
->get('path')->alias = $alias;
$node2
->save();
$this
->assertEquals($alias, $node2
->get('path')->alias);
$stored_alias = $alias_storage
->lookupPathAlias('/' . $node2
->toUrl()
->getInternalPath(), $node2
->language()
->getId());
$this
->assertEquals($alias, $stored_alias);
$this
->drupalGet($alias);
$web_assert = $this
->assertSession();
$web_assert
->statusCodeEquals(200);
$web_assert
->pageTextNotContains($node1
->label());
$web_assert
->pageTextContains($node2
->label());
$this
->drupalGet('admin/config/search/path');
$web_assert
->statusCodeEquals(200);
$web_assert
->pageTextNotContains('/node/' . $node1
->id());
$web_assert
->pageTextContains('/node/' . $node2
->id());
\Drupal::service('workspace.manager')
->setActiveWorkspace($live);
$this
->drupalGet($alias);
$web_assert
->statusCodeEquals(200);
$web_assert
->pageTextNotContains($node2
->label());
$web_assert
->pageTextContains($node1
->label());
$this
->drupalGet('admin/config/search/path');
$web_assert
->statusCodeEquals(200);
$web_assert
->pageTextNotContains('/node/' . $node2
->id());
$web_assert
->pageTextContains('/node/' . $node1
->id());
$node1
->delete();
$this
->drupalGet($alias);
$web_assert
->statusCodeEquals(404);
$this
->drupalGet('admin/config/search/path');
$web_assert
->statusCodeEquals(200);
$web_assert
->pageTextNotContains('/node/' . $node2
->id());
$web_assert
->pageTextNotContains('/node/' . $node1
->id());
\Drupal::service('workspace.manager')
->setActiveWorkspace($stage);
$this
->drupalGet($alias);
$web_assert = $this
->assertSession();
$web_assert
->statusCodeEquals(200);
$web_assert
->pageTextNotContains($node1
->label());
$web_assert
->pageTextContains($node2
->label());
$this
->drupalGet('admin/config/search/path');
$web_assert
->statusCodeEquals(200);
$web_assert
->pageTextNotContains('/node/' . $node1
->id());
$web_assert
->pageTextContains('/node/' . $node2
->id());
$alias2 = '/bar';
$node2
->get('path')->alias = $alias2;
$node2
->save();
$this
->assertEquals($alias2, $node2
->get('path')->alias);
$stored_alias = $alias_storage
->lookupPathAlias('/' . $node2
->toUrl()
->getInternalPath(), $node2
->language()
->getId());
$this
->assertEquals($alias2, $stored_alias);
$this
->drupalGet('admin/config/search/path');
$web_assert
->statusCodeEquals(200);
$web_assert
->pageTextNotContains('/node/' . $node1
->id());
$web_assert
->pageTextContains('/node/' . $node2
->id());
$web_assert
->pageTextContains($alias2);
$session = $this
->getSession();
$page = $session
->getPage();
$page
->clickLink('Delete');
$web_assert
->pageTextContains('Are you sure you want to delete path alias ' . $alias2 . '?');
$page
->pressButton('Confirm');
$this
->drupalGet($alias2);
$web_assert = $this
->assertSession();
$web_assert
->statusCodeEquals(404);
$this
->drupalGet('admin/config/search/path');
$web_assert
->statusCodeEquals(200);
$web_assert
->pageTextNotContains('/node/' . $node2
->id());
$web_assert
->pageTextNotContains('/node/' . $node1
->id());
$stored_alias = $alias_storage
->lookupPathAlias('/' . $node2
->toUrl()
->getInternalPath(), $node2
->language()
->getId());
$this
->assertFalse($stored_alias);
\Drupal::service('workspace.manager')
->setActiveWorkspace($live);
$alias3 = '/aliases';
$edit = [
'source' => '/admin/config/search/path',
'alias' => $alias3,
];
$this
->drupalPostForm('admin/config/search/path/add', $edit, t('Save'));
$this
->drupalGet('admin/config/search/path');
$web_assert
->statusCodeEquals(200);
$web_assert
->pageTextContains($alias3);
\Drupal::service('workspace.manager')
->setActiveWorkspace($stage);
$this
->drupalGet('admin/config/search/path');
$web_assert
->statusCodeEquals(200);
$web_assert
->pageTextContains($alias3);
$session = $this
->getSession();
$page = $session
->getPage();
$page
->clickLink('Delete');
$web_assert
->pageTextContains('Are you sure you want to delete path alias ' . $alias3 . '?');
$page
->pressButton('Confirm');
$this
->drupalGet('admin/config/search/path');
$web_assert
->statusCodeEquals(200);
$web_assert
->pageTextNotContains($alias3);
\Drupal::service('workspace.manager')
->setActiveWorkspace($live);
$this
->drupalGet('admin/config/search/path');
$web_assert
->statusCodeEquals(200);
$web_assert
->pageTextNotContains($alias3);
}
}