View source
<?php
namespace Drupal\Tests\workspaces\Kernel;
use Drupal\workspaces\Entity\Workspace;
trait WorkspaceTestTrait {
protected $workspaceManager;
protected $workspaces = [];
protected function initializeWorkspacesModule() {
$this
->enableModules([
'workspaces',
]);
$this->container = \Drupal::getContainer();
$this->entityTypeManager = \Drupal::entityTypeManager();
$this->workspaceManager = \Drupal::service('workspaces.manager');
$this
->installEntitySchema('workspace');
$this
->installSchema('workspaces', [
'workspace_association',
]);
$this->workspaces['live'] = Workspace::create([
'id' => 'live',
'label' => 'Live',
]);
$this->workspaces['live']
->save();
$this->workspaces['stage'] = Workspace::create([
'id' => 'stage',
'label' => 'Stage',
]);
$this->workspaces['stage']
->save();
$permissions = array_intersect([
'administer nodes',
'create workspace',
'edit any workspace',
'view any workspace',
], array_keys($this->container
->get('user.permissions')
->getPermissions()));
$this
->setCurrentUser($this
->createUser($permissions));
}
protected function switchToWorkspace($workspace_id) {
$workspace = $this->entityTypeManager
->getStorage('workspace')
->load($workspace_id);
\Drupal::service('workspaces.manager')
->setActiveWorkspace($workspace);
}
protected function createWorkspaceHierarchy() {
$this->workspaces['dev'] = Workspace::create([
'id' => 'dev',
'parent' => 'stage',
]);
$this->workspaces['dev']
->save();
$this->workspaces['local_1'] = Workspace::create([
'id' => 'local_1',
'parent' => 'dev',
]);
$this->workspaces['local_1']
->save();
$this->workspaces['local_2'] = Workspace::create([
'id' => 'local_2',
'parent' => 'dev',
]);
$this->workspaces['local_2']
->save();
$this->workspaces['qa'] = Workspace::create([
'id' => 'qa',
'parent' => 'live',
]);
$this->workspaces['qa']
->save();
}
protected function assertWorkspaceAssociation(array $expected, $entity_type_id) {
$workspace_association = \Drupal::service('workspaces.association');
foreach ($expected as $workspace_id => $expected_tracked_revision_ids) {
$tracked_entities = $workspace_association
->getTrackedEntities($workspace_id, $entity_type_id);
$tracked_revision_ids = isset($tracked_entities[$entity_type_id]) ? $tracked_entities[$entity_type_id] : [];
$this
->assertEquals($expected_tracked_revision_ids, array_keys($tracked_revision_ids));
}
}
protected function getUnassociatedRevisions($entity_type_id, $entity_ids = NULL) {
$entity_type = \Drupal::entityTypeManager()
->getDefinition($entity_type_id);
$query = \Drupal::entityTypeManager()
->getStorage($entity_type_id)
->getQuery()
->allRevisions()
->accessCheck(FALSE)
->notExists($entity_type
->get('revision_metadata_keys')['workspace']);
if ($entity_ids) {
$query
->condition($entity_type
->getKey('id'), $entity_ids, 'IN');
}
return $query
->execute();
}
}