View source
<?php
namespace Drupal\Tests\workspaces\Kernel;
use Drupal\Core\Entity\EntityStorageException;
use Drupal\KernelTests\KernelTestBase;
use Drupal\Tests\node\Traits\ContentTypeCreationTrait;
use Drupal\Tests\node\Traits\NodeCreationTrait;
use Drupal\Tests\user\Traits\UserCreationTrait;
use Drupal\workspaces\Entity\Workspace;
class WorkspaceCRUDTest extends KernelTestBase {
use UserCreationTrait;
use NodeCreationTrait;
use ContentTypeCreationTrait;
use WorkspaceTestTrait;
protected $entityTypeManager;
protected $state;
protected $workspaceManager;
protected static $modules = [
'user',
'system',
'workspaces',
'field',
'filter',
'node',
'text',
'path_alias',
];
protected function setUp() : void {
parent::setUp();
$this
->setUpCurrentUser();
$this
->installSchema('node', [
'node_access',
]);
$this
->installEntitySchema('workspace');
$this
->installSchema('workspaces', [
'workspace_association',
]);
$this
->installEntitySchema('node');
$this
->installConfig([
'filter',
'node',
'system',
]);
$this
->createContentType([
'type' => 'page',
]);
$this->entityTypeManager = \Drupal::entityTypeManager();
$this->state = \Drupal::state();
$this->workspaceManager = \Drupal::service('workspaces.manager');
}
public function testDeletingWorkspaces() {
$admin = $this
->createUser([
'administer nodes',
'create workspace',
'view any workspace',
'edit any workspace',
'delete any workspace',
]);
$this
->setCurrentUser($admin);
$workspace_association = \Drupal::service('workspaces.association');
$workspace_1 = Workspace::create([
'id' => 'gibbon',
'label' => 'Gibbon',
]);
$workspace_1
->save();
$this->workspaceManager
->setActiveWorkspace($workspace_1);
$workspace_1_node_1 = $this
->createNode([
'status' => FALSE,
]);
$workspace_1_node_2 = $this
->createNode([
'status' => FALSE,
]);
$live_revisions = $this
->getUnassociatedRevisions('node');
$this
->assertCount(2, $live_revisions);
for ($i = 0; $i < 4; $i++) {
$workspace_1_node_1
->setNewRevision(TRUE);
$workspace_1_node_1
->save();
$workspace_1_node_2
->setNewRevision(TRUE);
$workspace_1_node_2
->save();
}
$tracked_entities = $workspace_association
->getTrackedEntities($workspace_1
->id());
$this
->assertCount(2, $tracked_entities['node']);
$live_revisions = $this
->getUnassociatedRevisions('node');
$this
->assertCount(2, $live_revisions);
$associated_revisions = $workspace_association
->getAssociatedRevisions($workspace_1
->id(), 'node');
$this
->assertCount(8, $associated_revisions);
$this
->assertTrue($workspace_1
->access('delete', $admin));
$workspace_1
->delete();
$tracked_entities = $workspace_association
->getTrackedEntities($workspace_1
->id());
$this
->assertEmpty($tracked_entities);
$associated_revisions = $workspace_association
->getAssociatedRevisions($workspace_1
->id(), 'node');
$this
->assertCount(0, $associated_revisions);
$live_revisions = $this
->getUnassociatedRevisions('node');
$this
->assertCount(2, $live_revisions);
$workspace_2 = Workspace::create([
'id' => 'baboon',
'label' => 'Baboon',
]);
$workspace_2
->save();
$this->workspaceManager
->setActiveWorkspace($workspace_2);
$workspace_2_node_1 = $this
->createNode([
'status' => FALSE,
]);
for ($i = 0; $i < 59; $i++) {
$workspace_2_node_1
->setNewRevision(TRUE);
$workspace_2_node_1
->save();
}
$tracked_entities = $workspace_association
->getTrackedEntities($workspace_2
->id());
$this
->assertCount(1, $tracked_entities['node']);
$live_revisions = $this
->getUnassociatedRevisions('node', [
$workspace_2_node_1
->id(),
]);
$this
->assertCount(1, $live_revisions);
$associated_revisions = $workspace_association
->getAssociatedRevisions($workspace_2
->id(), 'node', [
$workspace_2_node_1
->id(),
]);
$this
->assertCount(59, $associated_revisions);
$workspace_2
->delete();
$associated_revisions = $workspace_association
->getAssociatedRevisions($workspace_2
->id(), 'node', [
$workspace_2_node_1
->id(),
]);
$this
->assertCount(9, $associated_revisions);
$live_revisions = $this
->getUnassociatedRevisions('node', [
$workspace_2_node_1
->id(),
]);
$this
->assertCount(1, $live_revisions);
$workspace_deleted = \Drupal::state()
->get('workspace.deleted');
$this
->assertCount(1, $workspace_deleted);
$workspace_3 = Workspace::create([
'id' => 'baboon',
'label' => 'Baboon',
]);
$violations = $workspace_3
->validate();
$this
->assertCount(1, $violations);
$this
->assertEquals('A workspace with this ID has been deleted but data still exists for it.', $violations[0]
->getMessage());
\Drupal::service('cron')
->run();
$associated_revisions = $workspace_association
->getTrackedEntities($workspace_2
->id());
$this
->assertCount(0, $associated_revisions);
$associated_revisions = $workspace_association
->getAssociatedRevisions($workspace_2
->id(), 'node', [
$workspace_2_node_1
->id(),
]);
$this
->assertCount(0, $associated_revisions);
$tracked_entities = $workspace_association
->getTrackedEntities($workspace_2
->id());
$this
->assertEmpty($tracked_entities);
$live_revisions = $this
->getUnassociatedRevisions('node');
$this
->assertCount(3, $live_revisions);
$workspace_deleted = \Drupal::state()
->get('workspace.deleted');
$this
->assertCount(0, $workspace_deleted);
}
public function testDeletingPublishedWorkspace() {
$admin = $this
->createUser([
'administer nodes',
'create workspace',
'view own workspace',
'edit own workspace',
'delete own workspace',
]);
$this
->setCurrentUser($admin);
$live_workspace = Workspace::create([
'id' => 'live',
'label' => 'Live',
]);
$live_workspace
->save();
$workspace = Workspace::create([
'id' => 'stage',
'label' => 'Stage',
]);
$workspace
->save();
$this->workspaceManager
->setActiveWorkspace($workspace);
$node = $this
->createNode([
'status' => TRUE,
]);
$node
->setNewRevision(TRUE);
$node
->save();
$revisions = $this->entityTypeManager
->getStorage('node')
->loadMultipleRevisions([
1,
2,
3,
]);
$this
->assertCount(3, $revisions);
$this
->assertTrue($revisions[1]
->isDefaultRevision());
$this
->assertFalse($revisions[2]
->isDefaultRevision());
$this
->assertFalse($revisions[3]
->isDefaultRevision());
$workspace
->publish();
$revisions = $this->entityTypeManager
->getStorage('node')
->loadMultipleRevisions([
1,
2,
3,
]);
$this
->assertFalse($revisions[1]
->isDefaultRevision());
$this
->assertFalse($revisions[2]
->isDefaultRevision());
$this
->assertTrue($revisions[3]
->isDefaultRevision());
$node
->setNewRevision(TRUE);
$node
->save();
$node
->setNewRevision(TRUE);
$node
->save();
$revisions = $this->entityTypeManager
->getStorage('node')
->loadMultipleRevisions([
1,
2,
3,
4,
5,
]);
$this
->assertFalse($revisions[1]
->isDefaultRevision());
$this
->assertFalse($revisions[2]
->isDefaultRevision());
$this
->assertTrue($revisions[3]
->isDefaultRevision());
$this
->assertFalse($revisions[4]
->isDefaultRevision());
$this
->assertFalse($revisions[5]
->isDefaultRevision());
$workspace
->delete();
$revisions = $this->entityTypeManager
->getStorage('node')
->loadMultipleRevisions([
1,
2,
3,
4,
5,
]);
$this
->assertCount(3, $revisions);
$this
->assertFalse($revisions[1]
->isDefaultRevision());
$this
->assertFalse($revisions[2]
->isDefaultRevision());
$this
->assertTrue($revisions[3]
->isDefaultRevision());
$this
->assertFalse(isset($revisions[4]));
$this
->assertFalse(isset($revisions[5]));
}
public function testDeletingWorkspaceWithChildren() {
$stage = Workspace::create([
'id' => 'stage',
'label' => 'Stage',
]);
$stage
->save();
$dev = Workspace::create([
'id' => 'dev',
'label' => 'Dev',
'parent' => 'stage',
]);
$dev
->save();
try {
$stage
->delete();
$this
->fail('The Stage workspace has children and should not be deletable.');
} catch (EntityStorageException $e) {
$this
->assertEquals('The Stage workspace can not be deleted because it has child workspaces.', $e
->getMessage());
$this
->assertNotNull(Workspace::load('stage'));
}
$dev
->delete();
$stage
->delete();
$this
->assertNull(Workspace::load('dev'));
$this
->assertNull(Workspace::load('stage'));
}
}