3 namespace Drupal\Tests\workspaces\Kernel;
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;
12 * Tests CRUD operations for workspaces.
16 class WorkspaceCRUDTest extends KernelTestBase {
18 use UserCreationTrait;
19 use NodeCreationTrait;
20 use ContentTypeCreationTrait;
23 * The entity type manager.
25 * @var \Drupal\Core\Entity\EntityTypeManagerInterface
27 protected $entityTypeManager;
32 * @var \Drupal\Core\State\StateInterface
37 * The workspace replication manager.
39 * @var \Drupal\workspaces\WorkspaceManagerInterface
41 protected $workspaceManager;
46 public static $modules = [
59 protected function setUp() {
62 $this->installSchema('system', ['key_value_expire', 'sequences']);
63 $this->installSchema('node', ['node_access']);
65 $this->installEntitySchema('workspace');
66 $this->installEntitySchema('workspace_association');
67 $this->installEntitySchema('node');
68 $this->installEntitySchema('user');
70 $this->installConfig(['filter', 'node', 'system']);
72 $this->createContentType(['type' => 'page']);
74 $this->entityTypeManager = \Drupal::entityTypeManager();
75 $this->state = \Drupal::state();
76 $this->workspaceManager = \Drupal::service('workspaces.manager');
80 * Tests the deletion of workspaces.
82 public function testDeletingWorkspaces() {
83 $admin = $this->createUser([
88 'delete any workspace',
90 $this->setCurrentUser($admin);
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');
97 // Create a workspace with a very small number of associated node revisions.
98 $workspace_1 = Workspace::create([
102 $workspace_1->save();
103 $this->workspaceManager->setActiveWorkspace($workspace_1);
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();
111 $workspace_1_node_2->setNewRevision(TRUE);
112 $workspace_1_node_2->save();
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']);
119 // Check that we are allowed to delete the workspace.
120 $this->assertTrue($workspace_1->access('delete', $admin));
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();
126 $associated_revisions = $workspace_association_storage->getTrackedEntities($workspace_1->id(), TRUE);
127 $this->assertCount(0, $associated_revisions);
128 $node_revision_count = $node_storage
133 $this->assertEquals(0, $node_revision_count);
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([
141 $workspace_2->save();
142 $this->workspaceManager->setActiveWorkspace($workspace_2);
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();
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']);
154 // Delete the workspace and check that we still have 10 revision left to
156 $workspace_2->delete();
158 $associated_revisions = $workspace_association_storage->getTrackedEntities($workspace_2->id(), TRUE);
159 $this->assertCount(10, $associated_revisions['node']);
161 $workspace_deleted = \Drupal::state()->get('workspace.deleted');
162 $this->assertCount(1, $workspace_deleted);
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([
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());
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();
178 $associated_revisions = $workspace_association_storage->getTrackedEntities($workspace_2->id(), TRUE);
179 $this->assertCount(0, $associated_revisions);
180 $node_revision_count = $node_storage
185 $this->assertEquals(0, $node_revision_count);
187 $workspace_deleted = \Drupal::state()->get('workspace.deleted');
188 $this->assertCount(0, $workspace_deleted);