View source
<?php
namespace Drupal\Tests\workspaces\Functional;
use Drupal\Tests\BrowserTestBase;
use Drupal\Tests\node\Traits\ContentTypeCreationTrait;
use Drupal\Tests\taxonomy\Traits\TaxonomyTestTrait;
class WorkspaceTest extends BrowserTestBase {
use WorkspaceTestUtilities;
use ContentTypeCreationTrait;
use TaxonomyTestTrait;
protected static $modules = [
'block',
'field_ui',
'node',
'taxonomy',
'toolbar',
'user',
'workspaces',
];
protected $defaultTheme = 'stark';
protected $editor1;
protected $editor2;
public function setUp() : void {
parent::setUp();
$permissions = [
'access administration pages',
'administer site configuration',
'create workspace',
'edit own workspace',
'edit any workspace',
'view own workspace',
'access toolbar',
];
$this->editor1 = $this
->drupalCreateUser($permissions);
$this->editor2 = $this
->drupalCreateUser($permissions);
$this
->drupalPlaceBlock('local_actions_block');
}
public function testSpecialCharacters() {
$this
->drupalLogin($this->editor1);
$this
->createWorkspaceThroughUi('Workspace 1', 'a0_$()+-/');
$this
->drupalGet('/admin/config/workflow/workspaces/add');
$this
->assertSession()
->statusCodeEquals(200);
$page = $this
->getSession()
->getPage();
$page
->fillField('label', 'workspace2');
$page
->fillField('id', 'A!"£%^&*{}#~@?');
$page
->findButton('Save')
->click();
$page
->hasContent("This value is not valid");
}
public function testWorkspaceToolbar() {
$this
->drupalLogin($this->editor1);
$this
->drupalGet('/admin/config/workflow/workspaces/add');
$this
->submitForm([
'id' => 'test_workspace',
'label' => 'Test workspace',
], 'Save');
$this
->drupalGet('/admin/config/workflow/workspaces/manage/test_workspace/activate');
$this
->submitForm([], 'Confirm');
$this
->drupalGet('<front>');
$page = $this
->getSession()
->getPage();
$this
->assertTrue($page
->hasLink('Test workspace'));
$this
->drupalGet('/admin/config/workflow/workspaces/manage/test_workspace/edit');
$this
->submitForm([
'label' => 'New name',
], 'Save');
$this
->drupalGet('<front>');
$page = $this
->getSession()
->getPage();
$this
->assertTrue($page
->hasLink('New name'));
}
public function testWorkspaceOwner() {
$this
->drupalLogin($this->editor1);
$this
->drupalGet('/admin/config/workflow/workspaces/add');
$this
->submitForm([
'id' => 'test_workspace',
'label' => 'Test workspace',
], 'Save');
$storage = \Drupal::entityTypeManager()
->getStorage('workspace');
$test_workspace = $storage
->load('test_workspace');
$this
->assertEquals($this->editor1
->id(), $test_workspace
->getOwnerId());
$this
->drupalGet('/admin/config/workflow/workspaces/manage/test_workspace/edit');
$this
->submitForm([
'uid[0][target_id]' => $this->editor2
->getAccountName(),
], 'Save');
$test_workspace = $storage
->loadUnchanged('test_workspace');
$this
->assertEquals($this->editor2
->id(), $test_workspace
->getOwnerId());
}
public function testWorkspaceFormRevisions() {
$this
->drupalLogin($this->editor1);
$storage = \Drupal::entityTypeManager()
->getStorage('workspace');
$stage_workspace = $storage
->load('stage');
$this
->assertEquals('1', $stage_workspace
->getRevisionId());
$this
->drupalGet($stage_workspace
->toUrl('edit-form')
->toString());
$this
->submitForm([], 'Save');
$stage_workspace = $storage
->loadUnchanged('stage');
$this
->assertEquals('2', $stage_workspace
->getRevisionId());
}
public function testWorkspaceManagePage() {
$this
->drupalLogin($this->rootUser);
$this
->setupWorkspaceSwitcherBlock();
$assert_session = $this
->assertSession();
$test_1 = $this
->createWorkspaceThroughUi('Test 1', 'test_1');
$test_2 = $this
->createWorkspaceThroughUi('Test 2', 'test_2');
$this
->switchToWorkspace($test_1);
$this
->drupalGet($test_1
->toUrl()
->toString());
$assert_session
->pageTextContains('This workspace has no changes.');
$assert_session
->linkNotExists('Switch to this workspace');
$this
->drupalGet($test_2
->toUrl()
->toString());
$assert_session
->linkExists('Switch to this workspace');
$this
->drupalCreateContentType([
'type' => 'test',
'label' => 'Test',
]);
$this
->createNodeThroughUi('Node 1', 'test');
$this
->createNodeThroughUi('Node 2', 'test');
$vocabulary = $this
->createVocabulary();
$edit = [
'name[0][value]' => 'Term 1',
];
$this
->drupalGet('admin/structure/taxonomy/manage/' . $vocabulary
->id() . '/add');
$this
->submitForm($edit, 'Save');
$this
->drupalGet($test_1
->toUrl()
->toString());
$assert_session
->pageTextContains('2 content items, 1 taxonomy term');
$assert_session
->linkExists('Node 1');
$assert_session
->linkExists('Node 2');
$assert_session
->linkExists('Term 1');
}
public function testWorkspaceFieldUi() {
$user = $this
->drupalCreateUser([
'administer workspaces',
'access administration pages',
'administer site configuration',
'administer workspace fields',
'administer workspace display',
'administer workspace form display',
]);
$this
->drupalLogin($user);
$this
->drupalGet('admin/config/workflow/workspaces/fields');
$this
->assertSession()
->statusCodeEquals(200);
$field_name = mb_strtolower($this
->randomMachineName());
$field_label = $this
->randomMachineName();
$edit = [
'new_storage_type' => 'string',
'label' => $field_label,
'field_name' => $field_name,
];
$this
->drupalGet("admin/config/workflow/workspaces/fields/add-field");
$this
->submitForm($edit, 'Save and continue');
$page = $this
->getSession()
->getPage();
$page
->pressButton('Save field settings');
$page
->pressButton('Save settings');
$this
->drupalGet('admin/config/workflow/workspaces/form-display');
$this
->assertSession()
->pageTextContains($field_label);
$this
->drupalGet('admin/config/workflow/workspaces/display');
$this
->assertSession()
->pageTextContains($field_label);
}
public function testDeleteWorkspaceWithExistingContent() {
$this
->createContentType([
'type' => 'test',
'label' => 'Test',
]);
$this
->setupWorkspaceSwitcherBlock();
$this
->drupalLogin($this->rootUser);
$may_4 = $this
->createWorkspaceThroughUi('May 4', 'may_4');
$this
->switchToWorkspace($may_4);
$node = $this
->createNodeThroughUi('A mayfly flies / In May or June', 'test');
$this
->drupalGet('/admin/config/workflow/workspaces/manage/may_4/delete');
$this
->assertSession()
->statusCodeEquals(200);
$page = $this
->getSession()
->getPage();
$page
->findButton('Delete')
->click();
$page
->hasContent('The workspace May 4 has been deleted.');
}
public function testWorkspaceList() {
$page = $this
->getSession()
->getPage();
$assert_session = $this
->assertSession();
$this
->drupalLogin($this->editor1);
$this
->createWorkspaceThroughUi('Summer event', 'summer_event');
$this
->drupalGet('/admin/config/workflow/workspaces');
$this
->assertSession()
->statusCodeEquals(200);
$active_workspace_row = $page
->find('css', '.active-workspace');
$this
->assertTrue($active_workspace_row
->hasClass('active-workspace--default'));
$this
->assertEquals('Live', $active_workspace_row
->find('css', 'td:first-of-type')
->getText());
$assert_session
->linkNotExists('Switch to Live');
$page
->clickLink('Switch to Summer event');
$page
->pressButton('Confirm');
$active_workspace_row = $page
->find('css', '.active-workspace');
$this
->assertTrue($active_workspace_row
->hasClass('active-workspace--not-default'));
$this
->assertEquals('Summer event', $active_workspace_row
->find('css', 'td:first-of-type')
->getText());
$assert_session
->linkExists('Switch to Live');
}
}