View source
<?php
namespace Drupal\Tests\workspaces\Functional;
use Drupal\language\Entity\ConfigurableLanguage;
use Drupal\Tests\BrowserTestBase;
use Drupal\workspaces\Entity\Workspace;
class PathWorkspacesTest extends BrowserTestBase {
use WorkspaceTestUtilities;
protected static $modules = [
'block',
'content_translation',
'node',
'path',
'workspaces',
];
protected $defaultTheme = 'stark';
protected function setUp() : void {
parent::setUp();
ConfigurableLanguage::createFromLangcode('ro')
->save();
$this
->rebuildContainer();
$this
->drupalCreateContentType([
'name' => 'article',
'type' => 'article',
]);
$this
->drupalLogin($this->rootUser);
$edit = [
'language_interface[enabled][language-url]' => 1,
];
$this
->drupalGet('admin/config/regional/language/detection');
$this
->submitForm($edit, 'Save settings');
$edit = [
'entity_types[node]' => 1,
'settings[node][article][translatable]' => 1,
'settings[node][article][fields][path]' => 1,
'settings[node][article][fields][body]' => 1,
'settings[node][article][settings][language][language_alterable]' => 1,
];
$this
->drupalGet('admin/config/regional/content-language');
$this
->submitForm($edit, 'Save configuration');
\Drupal::entityTypeManager()
->clearCachedDefinitions();
$this
->setupWorkspaceSwitcherBlock();
}
public function testPathAliases() {
$node = $this
->drupalCreateNode([
'type' => 'article',
'status' => TRUE,
]);
$stage = Workspace::load('stage');
$this
->switchToWorkspace($stage);
$edit = [
'path[0][alias]' => '/' . $this
->randomMachineName(),
];
$this
->drupalGet('node/' . $node
->id() . '/edit');
$this
->submitForm($edit, 'Save');
$path = $edit['path[0][alias]'];
$this
->assertAccessiblePaths([
$path,
]);
$this
->assertNotEmpty(\Drupal::cache('data')
->get('preload-paths:stage:/node/1'));
$this
->assertFalse(\Drupal::cache('data')
->get('preload-paths:/node/1'));
$this
->switchToLive();
$this
->assertNotAccessiblePaths([
$path,
]);
$this
->assertFalse(\Drupal::cache('data')
->get('preload-paths:/node/1'));
$stage
->publish();
$this
->assertAccessiblePaths([
$path,
]);
$this
->assertNotEmpty(\Drupal::cache('data')
->get('preload-paths:/node/1'));
}
public function testPathAliasesUserSwitch() {
$node = $this
->drupalCreateNode([
'type' => 'article',
'status' => TRUE,
]);
$stage = Workspace::load('stage');
$this
->switchToWorkspace($stage);
$edit = [
'path[0][alias]' => '/' . $this
->randomMachineName(),
];
$this
->drupalGet('node/' . $node
->id() . '/edit');
$this
->submitForm($edit, 'Save');
$path = $edit['path[0][alias]'];
$this
->assertAccessiblePaths([
$path,
]);
$this
->assertNotEmpty(\Drupal::cache('data')
->get('preload-paths:stage:/node/1'));
$this
->assertFalse(\Drupal::cache('data')
->get('preload-paths:/node/1'));
$this
->drupalLogout();
$this
->assertNotAccessiblePaths([
$path,
]);
$this
->assertFalse(\Drupal::cache('data')
->get('preload-paths:/node/1'));
$this
->drupalLogin($this->rootUser);
$stage
->publish();
$this
->drupalLogout();
$this
->assertAccessiblePaths([
$path,
]);
$this
->assertNotEmpty(\Drupal::cache('data')
->get('preload-paths:/node/1'));
}
public function testPathAliasesWithTranslation() {
$stage = Workspace::load('stage');
$default_node = $this
->drupalCreateNode([
'type' => 'article',
'langcode' => 'en',
'status' => TRUE,
'path' => '/' . $this
->randomMachineName(),
]);
$this
->drupalGet('node/' . $default_node
->id());
$this
->drupalGet('node/' . $default_node
->id() . '/translations');
$this
->clickLink('Add');
$edit_translation = [
'body[0][value]' => $this
->randomMachineName(),
'status[value]' => TRUE,
'path[0][alias]' => '/' . $this
->randomMachineName(),
];
$this
->submitForm($edit_translation, 'Save (this translation)');
$this
->drupalGet('ro' . $edit_translation['path[0][alias]']);
$this
->assertSession()
->pageTextContains($edit_translation['body[0][value]']);
$default_path = $default_node->path->alias;
$translation_path = 'ro' . $edit_translation['path[0][alias]'];
$this
->assertAccessiblePaths([
$default_path,
$translation_path,
]);
$this
->switchToWorkspace($stage);
$this
->assertAccessiblePaths([
$default_path,
$translation_path,
]);
$edit_new_translation_draft_with_alias = [
'path[0][alias]' => '/' . $this
->randomMachineName(),
];
$this
->drupalGet('ro/node/' . $default_node
->id() . '/edit');
$this
->submitForm($edit_new_translation_draft_with_alias, 'Save (this translation)');
$stage_translation_path = 'ro' . $edit_new_translation_draft_with_alias['path[0][alias]'];
$this
->assertAccessiblePaths([
$default_path,
$stage_translation_path,
]);
$this
->assertNotAccessiblePaths([
$translation_path,
]);
$this
->switchToLive();
$this
->assertAccessiblePaths([
$default_path,
$translation_path,
]);
$this
->assertNotAccessiblePaths([
$stage_translation_path,
]);
$this
->switchToWorkspace($stage);
$edit_new_translation_draft = [
'body[0][value]' => $this
->randomMachineName(),
];
$this
->drupalGet('ro/node/' . $default_node
->id() . '/edit');
$this
->submitForm($edit_new_translation_draft, 'Save (this translation)');
$this
->assertSession()
->pageTextContains($edit_new_translation_draft['body[0][value]']);
$this
->switchToLive();
$this
->assertAccessiblePaths([
$default_path,
$translation_path,
]);
$this
->assertNotAccessiblePaths([
$stage_translation_path,
]);
$this
->switchToWorkspace($stage);
$this
->assertAccessiblePaths([
$default_path,
$stage_translation_path,
]);
$this
->assertNotAccessiblePaths([
$translation_path,
]);
$edit_new_translation_draft_with_defaults_alias = [
'path[0][alias]' => $default_node->path->alias,
];
$this
->drupalGet('ro/node/' . $default_node
->id() . '/edit');
$this
->submitForm($edit_new_translation_draft_with_defaults_alias, 'Save (this translation)');
$this
->switchToLive();
$this
->assertAccessiblePaths([
$default_path,
$translation_path,
]);
$this
->assertNotAccessiblePaths([
$stage_translation_path,
]);
$this
->switchToWorkspace($stage);
$this
->assertAccessiblePaths([
$default_path,
]);
$this
->assertNotAccessiblePaths([
$translation_path,
$stage_translation_path,
]);
$edit_new_translation_draft_empty_alias = [
'body[0][value]' => $this
->randomMachineName(),
'path[0][alias]' => '',
];
$this
->drupalGet('ro/node/' . $default_node
->id() . '/edit');
$this
->submitForm($edit_new_translation_draft_empty_alias, 'Save (this translation)');
$this
->switchToLive();
$this
->assertAccessiblePaths([
$default_path,
$translation_path,
]);
$this
->assertNotAccessiblePaths([
$stage_translation_path,
]);
$this
->switchToWorkspace($stage);
$this
->assertAccessiblePaths([
$default_path,
]);
$this
->assertNotAccessiblePaths([
$translation_path,
$stage_translation_path,
]);
$edit_new_translation = [
'body[0][value]' => $this
->randomMachineName(),
'path[0][alias]' => '/' . $this
->randomMachineName(),
];
$this
->drupalGet('ro/node/' . $default_node
->id() . '/edit');
$this
->submitForm($edit_new_translation, 'Save (this translation)');
$this
->assertSession()
->pageTextContains($edit_new_translation['body[0][value]']);
$this
->assertSession()
->addressEquals('ro' . $edit_new_translation['path[0][alias]']);
$new_stage_translation_path = 'ro' . $edit_new_translation['path[0][alias]'];
$this
->assertAccessiblePaths([
$default_path,
$new_stage_translation_path,
]);
$this
->assertNotAccessiblePaths([
$stage_translation_path,
]);
$this
->switchToLive();
$this
->assertAccessiblePaths([
$default_path,
$translation_path,
]);
$this
->assertNotAccessiblePaths([
$stage_translation_path,
$new_stage_translation_path,
]);
$stage
->publish();
$this
->assertAccessiblePaths([
$default_path,
$new_stage_translation_path,
]);
$this
->assertNotAccessiblePaths([
$stage_translation_path,
]);
}
protected function assertAccessiblePaths(array $paths) {
foreach ($paths as $path) {
$this
->drupalGet($path);
$this
->assertSession()
->statusCodeEquals(200);
}
}
protected function assertNotAccessiblePaths(array $paths) {
foreach ($paths as $path) {
$this
->drupalGet($path);
$this
->assertSession()
->statusCodeEquals(404);
}
}
}