Pull merge.
[yaffs-website] / web / core / modules / workspaces / tests / src / Functional / WorkspaceConcurrentEditingTest.php
1 <?php
2
3 namespace Drupal\Tests\workspaces\Functional;
4
5 use Drupal\Tests\BrowserTestBase;
6 use Drupal\workspaces\Entity\Workspace;
7
8 /**
9  * Tests concurrent edits in different workspaces.
10  *
11  * @group workspaces
12  */
13 class WorkspaceConcurrentEditingTest extends BrowserTestBase {
14
15   use WorkspaceTestUtilities;
16
17   /**
18    * {@inheritdoc}
19    */
20   public static $modules = ['block', 'node', 'workspaces'];
21
22   /**
23    * Test switching workspace via the switcher block and admin page.
24    */
25   public function testSwitchingWorkspaces() {
26     $permissions = [
27       'create workspace',
28       'edit own workspace',
29       'view own workspace',
30       'bypass entity access own workspace',
31     ];
32
33     $mayer = $this->drupalCreateUser($permissions);
34     $this->drupalLogin($mayer);
35     $this->setupWorkspaceSwitcherBlock();
36
37     // Create a test node.
38     $this->createContentType(['type' => 'test', 'label' => 'Test']);
39     $test_node = $this->createNodeThroughUi('Test node', 'test');
40
41     // Check that the user can edit the node.
42     $page = $this->getSession()->getPage();
43     $page->hasField('title[0][value]');
44
45     // Create two workspaces.
46     $vultures = $this->createWorkspaceThroughUi('Vultures', 'vultures');
47     $gravity = $this->createWorkspaceThroughUi('Gravity', 'gravity');
48
49     // Edit the node in workspace 'vultures'.
50     $this->switchToWorkspace($vultures);
51     $this->drupalGet('/node/' . $test_node->id() . '/edit');
52     $page = $this->getSession()->getPage();
53     $page->fillField('Title', 'Test node - override');
54     $page->findButton('Save')->click();
55
56     // Check that the user can still edit the node in the same workspace.
57     $this->drupalGet('/node/' . $test_node->id() . '/edit');
58     $page = $this->getSession()->getPage();
59     $this->assertTrue($page->hasField('title[0][value]'));
60
61     // Switch to a different workspace and check that the user can not edit the
62     // node anymore.
63     $this->switchToWorkspace($gravity);
64     $this->drupalGet('/node/' . $test_node->id() . '/edit');
65     $page = $this->getSession()->getPage();
66     $this->assertFalse($page->hasField('title[0][value]'));
67     $page->hasContent('The content is being edited in the Vultures workspace.');
68
69     // Check that the node fails validation for API calls.
70     $violations = $test_node->validate();
71     $this->assertCount(1, $violations);
72     $this->assertEquals('The content is being edited in the <em class="placeholder">Vultures</em> workspace. As a result, your changes cannot be saved.', $violations->get(0)->getMessage());
73
74     // Switch to the Live workspace and check that the user still can not edit
75     // the node.
76     $live = Workspace::load('live');
77     $this->switchToWorkspace($live);
78     $this->drupalGet('/node/' . $test_node->id() . '/edit');
79     $page = $this->getSession()->getPage();
80     $this->assertFalse($page->hasField('title[0][value]'));
81     $page->hasContent('The content is being edited in the Vultures workspace.');
82
83     // Check that the node fails validation for API calls.
84     $violations = $test_node->validate();
85     $this->assertCount(1, $violations);
86     $this->assertEquals('The content is being edited in the <em class="placeholder">Vultures</em> workspace. As a result, your changes cannot be saved.', $violations->get(0)->getMessage());
87
88     // Deploy the changes from the 'Vultures' workspace and check that the node
89     // can be edited again in other workspaces.
90     $vultures->publish();
91     $this->switchToWorkspace($gravity);
92     $this->drupalGet('/node/' . $test_node->id() . '/edit');
93     $page = $this->getSession()->getPage();
94     $this->assertTrue($page->hasField('title[0][value]'));
95   }
96
97 }