Pull merge.
[yaffs-website] / web / core / modules / workspaces / tests / src / Functional / WorkspaceTest.php
1 <?php
2
3 namespace Drupal\Tests\workspaces\Functional;
4
5 use Drupal\Tests\BrowserTestBase;
6
7 /**
8  * Test the workspace entity.
9  *
10  * @group workspaces
11  */
12 class WorkspaceTest extends BrowserTestBase {
13
14   use WorkspaceTestUtilities;
15
16   /**
17    * {@inheritdoc}
18    */
19   public static $modules = ['workspaces', 'toolbar'];
20
21   /**
22    * A test user.
23    *
24    * @var \Drupal\user\Entity\User
25    */
26   protected $editor1;
27
28   /**
29    * A test user.
30    *
31    * @var \Drupal\user\Entity\User
32    */
33   protected $editor2;
34
35   /**
36    * {@inheritdoc}
37    */
38   public function setUp() {
39     parent::setUp();
40     $permissions = [
41       'access administration pages',
42       'administer site configuration',
43       'create workspace',
44       'edit own workspace',
45       'edit any workspace',
46       'view own workspace',
47       'access toolbar',
48     ];
49
50     $this->editor1 = $this->drupalCreateUser($permissions);
51     $this->editor2 = $this->drupalCreateUser($permissions);
52   }
53
54   /**
55    * Test creating a workspace with special characters.
56    */
57   public function testSpecialCharacters() {
58     $this->drupalLogin($this->editor1);
59
60     // Test a valid workspace name.
61     $this->createWorkspaceThroughUi('Workspace 1', 'a0_$()+-/');
62
63     // Test and invalid workspace name.
64     $this->drupalGet('/admin/config/workflow/workspaces/add');
65     $this->assertSession()->statusCodeEquals(200);
66
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");
72   }
73
74   /**
75    * Test that the toolbar correctly shows the active workspace.
76    */
77   public function testWorkspaceToolbar() {
78     $this->drupalLogin($this->editor1);
79
80     $this->drupalPostForm('/admin/config/workflow/workspaces/add', [
81       'id' => 'test_workspace',
82       'label' => 'Test workspace',
83     ], 'Save');
84
85     // Activate the test workspace.
86     $this->drupalPostForm('/admin/config/workflow/workspaces/manage/test_workspace/activate', [], 'Confirm');
87
88     $this->drupalGet('<front>');
89     $page = $this->getSession()->getPage();
90     // Toolbar should show the correct label.
91     $this->assertTrue($page->hasLink('Test workspace'));
92
93     // Change the workspace label.
94     $this->drupalPostForm('/admin/config/workflow/workspaces/manage/test_workspace/edit', [
95       'label' => 'New name',
96     ], 'Save');
97
98     $this->drupalGet('<front>');
99     $page = $this->getSession()->getPage();
100     // Toolbar should show the new label.
101     $this->assertTrue($page->hasLink('New name'));
102   }
103
104   /**
105    * Test changing the owner of a workspace.
106    */
107   public function testWorkspaceOwner() {
108     $this->drupalLogin($this->editor1);
109
110     $this->drupalPostForm('/admin/config/workflow/workspaces/add', [
111       'id' => 'test_workspace',
112       'label' => 'Test workspace',
113     ], 'Save');
114
115     $storage = \Drupal::entityTypeManager()->getStorage('workspace');
116     $test_workspace = $storage->load('test_workspace');
117     $this->assertEquals($this->editor1->id(), $test_workspace->getOwnerId());
118
119     $this->drupalPostForm('/admin/config/workflow/workspaces/manage/test_workspace/edit', [
120       'uid[0][target_id]' => $this->editor2->getUsername(),
121     ], 'Save');
122
123     $test_workspace = $storage->loadUnchanged('test_workspace');
124     $this->assertEquals($this->editor2->id(), $test_workspace->getOwnerId());
125   }
126
127   /**
128    * Tests that editing a workspace creates a new revision.
129    */
130   public function testWorkspaceFormRevisions() {
131     $this->drupalLogin($this->editor1);
132     $storage = \Drupal::entityTypeManager()->getStorage('workspace');
133
134     // The current live workspace entity should be revision 1.
135     $live_workspace = $storage->load('live');
136     $this->assertEquals('1', $live_workspace->getRevisionId());
137
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());
142   }
143
144 }