3 namespace Drupal\Tests\workspaces\Functional;
5 use Drupal\Tests\BrowserTestBase;
8 * Test the workspace entity.
12 class WorkspaceTest extends BrowserTestBase {
14 use WorkspaceTestUtilities;
19 public static $modules = ['workspaces', 'toolbar'];
24 * @var \Drupal\user\Entity\User
31 * @var \Drupal\user\Entity\User
38 public function setUp() {
41 'access administration pages',
42 'administer site configuration',
50 $this->editor1 = $this->drupalCreateUser($permissions);
51 $this->editor2 = $this->drupalCreateUser($permissions);
55 * Test creating a workspace with special characters.
57 public function testSpecialCharacters() {
58 $this->drupalLogin($this->editor1);
60 // Test a valid workspace name.
61 $this->createWorkspaceThroughUi('Workspace 1', 'a0_$()+-/');
63 // Test and invalid workspace name.
64 $this->drupalGet('/admin/config/workflow/workspaces/add');
65 $this->assertSession()->statusCodeEquals(200);
67 $page = $this->getSession()->getPage();
68 $page->fillField('label', 'workspace2');
69 $page->fillField('id', 'A!"£%^&*{}#~@?');
70 $page->findButton('Save')->click();
71 $page->hasContent("This value is not valid");
75 * Test that the toolbar correctly shows the active workspace.
77 public function testWorkspaceToolbar() {
78 $this->drupalLogin($this->editor1);
80 $this->drupalPostForm('/admin/config/workflow/workspaces/add', [
81 'id' => 'test_workspace',
82 'label' => 'Test workspace',
85 // Activate the test workspace.
86 $this->drupalPostForm('/admin/config/workflow/workspaces/manage/test_workspace/activate', [], 'Confirm');
88 $this->drupalGet('<front>');
89 $page = $this->getSession()->getPage();
90 // Toolbar should show the correct label.
91 $this->assertTrue($page->hasLink('Test workspace'));
93 // Change the workspace label.
94 $this->drupalPostForm('/admin/config/workflow/workspaces/manage/test_workspace/edit', [
95 'label' => 'New name',
98 $this->drupalGet('<front>');
99 $page = $this->getSession()->getPage();
100 // Toolbar should show the new label.
101 $this->assertTrue($page->hasLink('New name'));
105 * Test changing the owner of a workspace.
107 public function testWorkspaceOwner() {
108 $this->drupalLogin($this->editor1);
110 $this->drupalPostForm('/admin/config/workflow/workspaces/add', [
111 'id' => 'test_workspace',
112 'label' => 'Test workspace',
115 $storage = \Drupal::entityTypeManager()->getStorage('workspace');
116 $test_workspace = $storage->load('test_workspace');
117 $this->assertEquals($this->editor1->id(), $test_workspace->getOwnerId());
119 $this->drupalPostForm('/admin/config/workflow/workspaces/manage/test_workspace/edit', [
120 'uid[0][target_id]' => $this->editor2->getUsername(),
123 $test_workspace = $storage->loadUnchanged('test_workspace');
124 $this->assertEquals($this->editor2->id(), $test_workspace->getOwnerId());
128 * Tests that editing a workspace creates a new revision.
130 public function testWorkspaceFormRevisions() {
131 $this->drupalLogin($this->editor1);
132 $storage = \Drupal::entityTypeManager()->getStorage('workspace');
134 // The current live workspace entity should be revision 1.
135 $live_workspace = $storage->load('live');
136 $this->assertEquals('1', $live_workspace->getRevisionId());
138 // Re-save the live workspace via the UI to create revision 3.
139 $this->drupalPostForm($live_workspace->url('edit-form'), [], 'Save');
140 $live_workspace = $storage->loadUnchanged('live');
141 $this->assertEquals('3', $live_workspace->getRevisionId());