View source
<?php
namespace Drupal\Tests\workspace\Kernel;
use Drupal\KernelTests\KernelTestBase;
use Drupal\Tests\node\Traits\ContentTypeCreationTrait;
use Drupal\Tests\node\Traits\NodeCreationTrait;
use Drupal\Tests\user\Traits\UserCreationTrait;
use Drupal\workspace\Entity\Workspace;
class WorkspaceCRUDTest extends KernelTestBase {
use UserCreationTrait;
use NodeCreationTrait;
use ContentTypeCreationTrait;
protected $entityTypeManager;
protected $state;
protected $workspaceManager;
public static $modules = [
'user',
'system',
'workspace',
'field',
'filter',
'node',
'text',
];
protected function setUp() {
parent::setUp();
$this
->installSchema('system', [
'key_value_expire',
'sequences',
]);
$this
->installSchema('node', [
'node_access',
]);
$this
->installEntitySchema('workspace');
$this
->installEntitySchema('workspace_association');
$this
->installEntitySchema('node');
$this
->installEntitySchema('user');
$this
->installConfig([
'filter',
'node',
'system',
]);
$this
->createContentType([
'type' => 'page',
]);
$this->entityTypeManager = \Drupal::entityTypeManager();
$this->state = \Drupal::state();
$this->workspaceManager = \Drupal::service('workspace.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_storage = $this->entityTypeManager
->getStorage('workspace_association');
$node_storage = $this->entityTypeManager
->getStorage('node');
$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,
]);
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();
}
$associated_revisions = $workspace_association_storage
->getTrackedEntities($workspace_1
->id(), TRUE);
$this
->assertCount(10, $associated_revisions['node']);
$this
->assertTrue($workspace_1
->access('delete', $admin));
$workspace_1
->delete();
$associated_revisions = $workspace_association_storage
->getTrackedEntities($workspace_1
->id(), TRUE);
$this
->assertCount(0, $associated_revisions);
$node_revision_count = $node_storage
->getQuery()
->allRevisions()
->count()
->execute();
$this
->assertEquals(0, $node_revision_count);
$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();
}
$associated_revisions = $workspace_association_storage
->getTrackedEntities($workspace_2
->id(), TRUE);
$this
->assertCount(60, $associated_revisions['node']);
$workspace_2
->delete();
$associated_revisions = $workspace_association_storage
->getTrackedEntities($workspace_2
->id(), TRUE);
$this
->assertCount(10, $associated_revisions['node']);
$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_storage
->getTrackedEntities($workspace_2
->id(), TRUE);
$this
->assertCount(0, $associated_revisions);
$node_revision_count = $node_storage
->getQuery()
->allRevisions()
->count()
->execute();
$this
->assertEquals(0, $node_revision_count);
$workspace_deleted = \Drupal::state()
->get('workspace.deleted');
$this
->assertCount(0, $workspace_deleted);
}
}