0d878ac08388b4dd918fcf89d9bf707ebdc96379
[yaffs-website] / web / core / modules / workspaces / tests / src / Kernel / WorkspaceCRUDTest.php
1 <?php
2
3 namespace Drupal\Tests\workspaces\Kernel;
4
5 use Drupal\KernelTests\KernelTestBase;
6 use Drupal\Tests\node\Traits\ContentTypeCreationTrait;
7 use Drupal\Tests\node\Traits\NodeCreationTrait;
8 use Drupal\Tests\user\Traits\UserCreationTrait;
9 use Drupal\workspaces\Entity\Workspace;
10
11 /**
12  * Tests CRUD operations for workspaces.
13  *
14  * @group workspaces
15  */
16 class WorkspaceCRUDTest extends KernelTestBase {
17
18   use UserCreationTrait;
19   use NodeCreationTrait;
20   use ContentTypeCreationTrait;
21
22   /**
23    * The entity type manager.
24    *
25    * @var \Drupal\Core\Entity\EntityTypeManagerInterface
26    */
27   protected $entityTypeManager;
28
29   /**
30    * The state service.
31    *
32    * @var \Drupal\Core\State\StateInterface
33    */
34   protected $state;
35
36   /**
37    * The workspace replication manager.
38    *
39    * @var \Drupal\workspaces\WorkspaceManagerInterface
40    */
41   protected $workspaceManager;
42
43   /**
44    * {@inheritdoc}
45    */
46   public static $modules = [
47     'user',
48     'system',
49     'workspaces',
50     'field',
51     'filter',
52     'node',
53     'text',
54   ];
55
56   /**
57    * {@inheritdoc}
58    */
59   protected function setUp() {
60     parent::setUp();
61
62     $this->installSchema('system', ['key_value_expire', 'sequences']);
63     $this->installSchema('node', ['node_access']);
64
65     $this->installEntitySchema('workspace');
66     $this->installEntitySchema('workspace_association');
67     $this->installEntitySchema('node');
68     $this->installEntitySchema('user');
69
70     $this->installConfig(['filter', 'node', 'system']);
71
72     $this->createContentType(['type' => 'page']);
73
74     $this->entityTypeManager = \Drupal::entityTypeManager();
75     $this->state = \Drupal::state();
76     $this->workspaceManager = \Drupal::service('workspaces.manager');
77   }
78
79   /**
80    * Tests the deletion of workspaces.
81    */
82   public function testDeletingWorkspaces() {
83     $admin = $this->createUser([
84       'administer nodes',
85       'create workspace',
86       'view any workspace',
87       'edit any workspace',
88       'delete any workspace',
89     ]);
90     $this->setCurrentUser($admin);
91
92     /** @var \Drupal\workspaces\WorkspaceAssociationStorageInterface $workspace_association_storage */
93     $workspace_association_storage = $this->entityTypeManager->getStorage('workspace_association');
94     /** @var \Drupal\node\NodeStorageInterface $node_storage */
95     $node_storage = $this->entityTypeManager->getStorage('node');
96
97     // Create a workspace with a very small number of associated node revisions.
98     $workspace_1 = Workspace::create([
99       'id' => 'gibbon',
100       'label' => 'Gibbon',
101     ]);
102     $workspace_1->save();
103     $this->workspaceManager->setActiveWorkspace($workspace_1);
104
105     $workspace_1_node_1 = $this->createNode(['status' => FALSE]);
106     $workspace_1_node_2 = $this->createNode(['status' => FALSE]);
107     for ($i = 0; $i < 4; $i++) {
108       $workspace_1_node_1->setNewRevision(TRUE);
109       $workspace_1_node_1->save();
110
111       $workspace_1_node_2->setNewRevision(TRUE);
112       $workspace_1_node_2->save();
113     }
114
115     // The workspace should have 10 associated node revisions, 5 for each node.
116     $associated_revisions = $workspace_association_storage->getTrackedEntities($workspace_1->id(), TRUE);
117     $this->assertCount(10, $associated_revisions['node']);
118
119     // Check that we are allowed to delete the workspace.
120     $this->assertTrue($workspace_1->access('delete', $admin));
121
122     // Delete the workspace and check that all the workspace_association
123     // entities and all the node revisions have been deleted as well.
124     $workspace_1->delete();
125
126     $associated_revisions = $workspace_association_storage->getTrackedEntities($workspace_1->id(), TRUE);
127     $this->assertCount(0, $associated_revisions);
128     $node_revision_count = $node_storage
129       ->getQuery()
130       ->allRevisions()
131       ->count()
132       ->execute();
133     $this->assertEquals(0, $node_revision_count);
134
135     // Create another workspace, this time with a larger number of associated
136     // node revisions so we can test the batch purge process.
137     $workspace_2 = Workspace::create([
138       'id' => 'baboon',
139       'label' => 'Baboon',
140     ]);
141     $workspace_2->save();
142     $this->workspaceManager->setActiveWorkspace($workspace_2);
143
144     $workspace_2_node_1 = $this->createNode(['status' => FALSE]);
145     for ($i = 0; $i < 59; $i++) {
146       $workspace_2_node_1->setNewRevision(TRUE);
147       $workspace_2_node_1->save();
148     }
149
150     // The workspace should have 60 associated node revisions.
151     $associated_revisions = $workspace_association_storage->getTrackedEntities($workspace_2->id(), TRUE);
152     $this->assertCount(60, $associated_revisions['node']);
153
154     // Delete the workspace and check that we still have 10 revision left to
155     // delete.
156     $workspace_2->delete();
157
158     $associated_revisions = $workspace_association_storage->getTrackedEntities($workspace_2->id(), TRUE);
159     $this->assertCount(10, $associated_revisions['node']);
160
161     $workspace_deleted = \Drupal::state()->get('workspace.deleted');
162     $this->assertCount(1, $workspace_deleted);
163
164     // Check that we can not create another workspace with the same ID while its
165     // data purging is not finished.
166     $workspace_3 = Workspace::create([
167       'id' => 'baboon',
168       'label' => 'Baboon',
169     ]);
170     $violations = $workspace_3->validate();
171     $this->assertCount(1, $violations);
172     $this->assertEquals('A workspace with this ID has been deleted but data still exists for it.', $violations[0]->getMessage());
173
174     // Running cron should delete the remaining data as well as the workspace ID
175     // from the "workspace.delete" state entry.
176     \Drupal::service('cron')->run();
177
178     $associated_revisions = $workspace_association_storage->getTrackedEntities($workspace_2->id(), TRUE);
179     $this->assertCount(0, $associated_revisions);
180     $node_revision_count = $node_storage
181       ->getQuery()
182       ->allRevisions()
183       ->count()
184       ->execute();
185     $this->assertEquals(0, $node_revision_count);
186
187     $workspace_deleted = \Drupal::state()->get('workspace.deleted');
188     $this->assertCount(0, $workspace_deleted);
189   }
190
191 }