View source
<?php
namespace Drupal\Tests\workspaces\Kernel;
use Drupal\Core\Entity\EntityStorageException;
use Drupal\Core\Form\FormState;
use Drupal\entity_test\Entity\EntityTestMulRevPub;
use Drupal\KernelTests\KernelTestBase;
use Drupal\language\Entity\ConfigurableLanguage;
use Drupal\system\Form\SiteInformationForm;
use Drupal\Tests\field\Traits\EntityReferenceTestTrait;
use Drupal\Tests\node\Traits\ContentTypeCreationTrait;
use Drupal\Tests\node\Traits\NodeCreationTrait;
use Drupal\Tests\user\Traits\UserCreationTrait;
use Drupal\views\Tests\ViewResultAssertionTrait;
use Drupal\views\Views;
use Drupal\workspaces\Entity\Workspace;
use Drupal\workspaces\WorkspaceAccessException;
class WorkspaceIntegrationTest extends KernelTestBase {
use ContentTypeCreationTrait;
use EntityReferenceTestTrait;
use NodeCreationTrait;
use UserCreationTrait;
use ViewResultAssertionTrait;
use WorkspaceTestTrait;
protected $entityTypeManager;
protected $createdTimestamp;
protected $nodes = [];
protected static $modules = [
'entity_test',
'field',
'filter',
'node',
'text',
'user',
'system',
'views',
'language',
'content_translation',
'path_alias',
];
protected function setUp() {
parent::setUp();
$this->entityTypeManager = \Drupal::entityTypeManager();
$this
->installEntitySchema('entity_test_mulrev');
$this
->installEntitySchema('entity_test_mulrevpub');
$this
->installEntitySchema('entity_test_no_label');
$this
->installEntitySchema('node');
$this
->installEntitySchema('user');
$this
->installConfig([
'filter',
'node',
'system',
'language',
'content_translation',
]);
$this
->installSchema('system', [
'key_value_expire',
'sequences',
]);
$this
->installSchema('node', [
'node_access',
]);
$language = ConfigurableLanguage::createFromLangcode('de');
$language
->save();
$this
->createContentType([
'type' => 'page',
]);
$this
->setCurrentUser($this
->createUser([
'administer nodes',
]));
$this->container
->get('content_translation.manager')
->setEnabled('node', 'page', TRUE);
$this->createdTimestamp = \Drupal::time()
->getRequestTime();
$this->nodes[] = $this
->createNode([
'title' => 'live - 1 - r1 - published',
'body' => 'node 1',
'created' => $this->createdTimestamp++,
'status' => TRUE,
]);
$this->nodes[] = $this
->createNode([
'title' => 'live - 2 - r2 - unpublished',
'body' => 'node 2',
'created' => $this->createdTimestamp++,
'status' => FALSE,
]);
$translation = $this->nodes[0]
->addTranslation('de');
$translation
->setTitle('live - 1 - r1 - published - de');
$translation
->save();
}
public function testWorkspaces() {
$this
->initializeWorkspacesModule();
$test_scenarios = [];
$expected_workspace_association = [];
$revision_state = [
'live' => [
1 => [
1 => [
'title' => 'live - 1 - r1 - published',
'status' => TRUE,
'default_revision' => TRUE,
],
],
2 => [
2 => [
'title' => 'live - 2 - r2 - unpublished',
'status' => FALSE,
'default_revision' => TRUE,
],
],
],
'stage' => [
1 => [
1 => [
'title' => 'live - 1 - r1 - published',
'status' => TRUE,
'default_revision' => TRUE,
],
],
2 => [
2 => [
'title' => 'live - 2 - r2 - unpublished',
'status' => FALSE,
'default_revision' => TRUE,
],
],
],
];
$test_scenarios['initial_state'] = $revision_state;
$expected_workspace_association['initial_state'] = [
'stage' => [],
];
$revision_state = array_replace_recursive($revision_state, [
'live' => [
1 => [
3 => [
'title' => 'stage - 1 - r3 - unpublished',
'status' => FALSE,
'default_revision' => FALSE,
],
],
],
'stage' => [
1 => [
1 => [
'default_revision' => FALSE,
],
3 => [
'title' => 'stage - 1 - r3 - unpublished',
'status' => FALSE,
'default_revision' => TRUE,
],
],
],
]);
$test_scenarios['unpublish_node_1_in_stage'] = $revision_state;
$expected_workspace_association['unpublish_node_1_in_stage'] = [
'stage' => [
3,
],
];
$revision_state = array_replace_recursive($revision_state, [
'live' => [
2 => [
4 => [
'title' => 'stage - 2 - r4 - published',
'status' => TRUE,
'default_revision' => FALSE,
],
],
],
'stage' => [
2 => [
2 => [
'default_revision' => FALSE,
],
4 => [
'title' => 'stage - 2 - r4 - published',
'status' => TRUE,
'default_revision' => TRUE,
],
],
],
]);
$test_scenarios['publish_node_2_in_stage'] = $revision_state;
$expected_workspace_association['publish_node_2_in_stage'] = [
'stage' => [
3,
4,
],
];
$revision_state = array_replace_recursive($revision_state, [
'live' => [
3 => [
5 => [
'title' => 'stage - 3 - r5 - unpublished',
'status' => FALSE,
'default_revision' => TRUE,
],
],
],
'stage' => [
3 => [
5 => [
'title' => 'stage - 3 - r5 - unpublished',
'status' => FALSE,
'default_revision' => TRUE,
],
],
],
]);
$test_scenarios['add_unpublished_node_in_stage'] = $revision_state;
$expected_workspace_association['add_unpublished_node_in_stage'] = [
'stage' => [
3,
4,
5,
],
];
$revision_state = array_replace_recursive($revision_state, [
'live' => [
4 => [
6 => [
'title' => 'stage - 4 - r6 - published',
'status' => FALSE,
'default_revision' => TRUE,
],
7 => [
'title' => 'stage - 4 - r6 - published',
'status' => TRUE,
'default_revision' => FALSE,
],
],
],
'stage' => [
4 => [
6 => [
'title' => 'stage - 4 - r6 - published',
'status' => FALSE,
'default_revision' => FALSE,
],
7 => [
'title' => 'stage - 4 - r6 - published',
'status' => TRUE,
'default_revision' => TRUE,
],
],
],
]);
$test_scenarios['add_published_node_in_stage'] = $revision_state;
$expected_workspace_association['add_published_node_in_stage'] = [
'stage' => [
3,
4,
5,
7,
],
];
$revision_state = array_replace_recursive($revision_state, [
'live' => [
1 => [
1 => [
'default_revision' => FALSE,
],
3 => [
'default_revision' => TRUE,
],
],
2 => [
2 => [
'default_revision' => FALSE,
],
4 => [
'default_revision' => TRUE,
],
],
4 => [
6 => [
'default_revision' => FALSE,
],
7 => [
'default_revision' => TRUE,
],
],
],
]);
$test_scenarios['push_stage_to_live'] = $revision_state;
$expected_workspace_association['push_stage_to_live'] = [
'stage' => [],
];
$this
->assertWorkspaceStatus($test_scenarios['initial_state'], 'node');
$this
->assertWorkspaceAssociation($expected_workspace_association['initial_state'], 'node');
$this
->switchToWorkspace('stage');
$node = $this->entityTypeManager
->getStorage('node')
->load(1);
$node
->setTitle('stage - 1 - r3 - unpublished');
$node
->setUnpublished();
$node
->save();
$this
->assertWorkspaceStatus($test_scenarios['unpublish_node_1_in_stage'], 'node');
$this
->assertWorkspaceAssociation($expected_workspace_association['unpublish_node_1_in_stage'], 'node');
$this
->switchToWorkspace('stage');
$node = $this->entityTypeManager
->getStorage('node')
->load(2);
$node
->setTitle('stage - 2 - r4 - published');
$node
->setPublished();
$node
->save();
$this
->assertWorkspaceStatus($test_scenarios['publish_node_2_in_stage'], 'node');
$this
->assertWorkspaceAssociation($expected_workspace_association['publish_node_2_in_stage'], 'node');
$this
->switchToWorkspace('stage');
$this
->createNode([
'title' => 'stage - 3 - r5 - unpublished',
'created' => $this->createdTimestamp++,
'status' => FALSE,
]);
$this
->assertWorkspaceStatus($test_scenarios['add_unpublished_node_in_stage'], 'node');
$this
->assertWorkspaceAssociation($expected_workspace_association['add_unpublished_node_in_stage'], 'node');
$this
->switchToWorkspace('stage');
$this
->createNode([
'title' => 'stage - 4 - r6 - published',
'created' => $this->createdTimestamp++,
'status' => TRUE,
]);
$this
->assertWorkspaceStatus($test_scenarios['add_published_node_in_stage'], 'node');
$this
->assertWorkspaceAssociation($expected_workspace_association['add_published_node_in_stage'], 'node');
$workspace_publisher = \Drupal::service('workspaces.operation_factory')
->getPublisher($this->workspaces['stage']);
$expected = [
'node' => [
3 => 1,
4 => 2,
5 => 3,
7 => 4,
],
];
$this
->assertEquals($expected, $workspace_publisher
->getDifferringRevisionIdsOnSource());
$this->workspaces['stage']
->publish();
$this
->assertWorkspaceStatus($test_scenarios['push_stage_to_live'], 'node');
$this
->assertWorkspaceAssociation($expected_workspace_association['push_stage_to_live'], 'node');
$this
->assertEmpty($workspace_publisher
->getDifferringRevisionIdsOnSource());
}
public function testWorkspaceHierarchy() {
$this
->initializeWorkspacesModule();
$this
->createWorkspaceHierarchy();
$expected_workspace_association = [
'stage' => [],
'dev' => [],
'local_1' => [],
'local_2' => [],
'qa' => [],
];
$this
->assertWorkspaceAssociation($expected_workspace_association, 'node');
$this
->switchToWorkspace('stage');
$node = $this->entityTypeManager
->getStorage('node')
->load(1);
$node
->setTitle('stage - 1 - r3');
$node
->save();
$expected_workspace_association = [
'stage' => [
3,
],
'dev' => [
3,
],
'local_1' => [
3,
],
'local_2' => [
3,
],
'qa' => [],
];
$this
->assertWorkspaceAssociation($expected_workspace_association, 'node');
$this
->switchToWorkspace('stage');
$this
->createNode([
'title' => 'stage - 3 - r5 - published',
'created' => $this->createdTimestamp++,
'status' => TRUE,
]);
$expected_workspace_association = [
'stage' => [
3,
5,
],
'dev' => [
3,
5,
],
'local_1' => [
3,
5,
],
'local_2' => [
3,
5,
],
'qa' => [],
];
$this
->assertWorkspaceAssociation($expected_workspace_association, 'node');
$this
->switchToWorkspace('dev');
$node = $this->entityTypeManager
->getStorage('node')
->load(1);
$node
->setTitle('dev - 1 - r6');
$node
->save();
$expected_workspace_association = [
'stage' => [
3,
5,
],
'dev' => [
5,
6,
],
'local_1' => [
5,
6,
],
'local_2' => [
5,
6,
],
'qa' => [],
];
$this
->assertWorkspaceAssociation($expected_workspace_association, 'node');
$this
->switchToWorkspace('local_1');
$node = $this->entityTypeManager
->getStorage('node')
->load(1);
$node
->setTitle('local_1 - 1 - r7');
$node
->save();
$expected_workspace_association = [
'stage' => [
3,
5,
],
'dev' => [
5,
6,
],
'local_1' => [
5,
7,
],
'local_2' => [
5,
6,
],
'qa' => [],
];
$this
->assertWorkspaceAssociation($expected_workspace_association, 'node');
$this
->switchToWorkspace('dev');
$node = $this->entityTypeManager
->getStorage('node')
->load(1);
$node
->setTitle('dev - 1 - r8');
$node
->save();
$expected_workspace_association = [
'stage' => [
3,
5,
],
'dev' => [
5,
8,
],
'local_1' => [
5,
7,
],
'local_2' => [
5,
8,
],
'qa' => [],
];
$this
->assertWorkspaceAssociation($expected_workspace_association, 'node');
$this->workspaces['local_3'] = Workspace::create([
'id' => 'local_3',
'parent' => 'dev',
]);
$this->workspaces['local_3']
->save();
$expected_workspace_association = [
'stage' => [
3,
5,
],
'dev' => [
5,
8,
],
'local_1' => [
5,
7,
],
'local_2' => [
5,
8,
],
'local_3' => [
5,
8,
],
'qa' => [],
];
$this
->assertWorkspaceAssociation($expected_workspace_association, 'node');
$this
->expectException(WorkspaceAccessException::class);
$this
->expectExceptionMessage('Only top-level workspaces can be published.');
$this->workspaces['dev']
->publish();
}
public function testEntityQueryWithoutConditions() {
$this
->initializeWorkspacesModule();
$this
->switchToWorkspace('stage');
$node = $this->entityTypeManager
->getStorage('node')
->load(2);
$node->title->value = 'stage - 2 - r3 - published';
$node
->save();
$query = $this->entityTypeManager
->getStorage('node')
->getQuery();
$query
->sort('nid');
$query
->pager(1);
$result = $query
->execute();
$this
->assertSame([
1 => '1',
], $result);
$query = $this->entityTypeManager
->getStorage('node')
->getQuery();
$query
->sort('nid', 'DESC');
$query
->pager(10);
$result = $query
->execute();
$this
->assertSame([
3 => '2',
1 => '1',
], $result);
}
public function testEntityQueryRelationship() {
$this
->initializeWorkspacesModule();
$this
->createEntityReferenceField('node', 'page', 'field_test_entity', 'Test entity reference', 'entity_test_mulrevpub');
$this
->createEntityReferenceField('node', 'page', 'field_test_node', 'Test node reference', 'node');
$this
->switchToWorkspace('live');
$node_1 = $this
->createNode([
'title' => 'live node 1',
]);
$entity_test = EntityTestMulRevPub::create([
'name' => 'live entity_test_mulrevpub',
'non_rev_field' => 'live non-revisionable value',
]);
$entity_test
->save();
$node_2 = $this
->createNode([
'title' => 'live node 2',
'field_test_entity' => $entity_test
->id(),
'field_test_node' => $node_1
->id(),
]);
$this
->switchToWorkspace('stage');
$node_1->title->value = 'stage node 1';
$node_1
->save();
$node_2->title->value = 'stage node 2';
$node_2
->save();
$entity_test->name->value = 'stage entity_test_mulrevpub';
$entity_test->non_rev_field->value = 'stage non-revisionable value';
$entity_test
->save();
$query = $this->entityTypeManager
->getStorage('node')
->getQuery();
$query
->currentRevision();
$query
->condition('title', 'stage node 2')
->condition('revision_uid', $node_2
->getRevisionUserId())
->condition('type', $node_2
->bundle())
->condition('uuid', $node_2
->uuid());
$query
->condition('field_test_node.entity.title', 'stage node 1')
->condition('field_test_node.entity.revision_uid', $node_1
->getRevisionUserId())
->condition('field_test_node.entity.type', $node_1
->bundle())
->condition('field_test_node.entity.uuid', $node_1
->uuid());
$query
->condition('field_test_entity.entity.uuid', $entity_test
->uuid());
$result = $query
->execute();
$this
->assertSame([
$node_2
->getRevisionId() => $node_2
->id(),
], $result);
}
public function testDisallowedEntityCreateInNonDefaultWorkspace($entity_type_id, $allowed) {
$this
->initializeWorkspacesModule();
$storage = $this->entityTypeManager
->getStorage($entity_type_id);
$this
->switchToWorkspace('stage');
$entity = $storage
->createWithSampleValues($entity_type_id);
if ($entity_type_id === 'workspace') {
$entity->id = 'test';
}
if (!$allowed) {
$this
->expectException(EntityStorageException::class);
$this
->expectExceptionMessage('This entity can only be saved in the default workspace.');
}
$entity
->save();
}
public function testDisallowedEntityUpdateInNonDefaultWorkspace($entity_type_id, $allowed) {
$this
->initializeWorkspacesModule();
$storage = $this->entityTypeManager
->getStorage($entity_type_id);
$this->workspaceManager
->switchToLive();
$entity = $storage
->createWithSampleValues($entity_type_id);
if ($entity_type_id === 'workspace') {
$entity->id = 'test';
}
$entity
->save();
$this
->switchToWorkspace('stage');
$entity->created->value = 1;
if (!$allowed) {
$this
->expectException(EntityStorageException::class);
$this
->expectExceptionMessage('This entity can only be saved in the default workspace.');
}
$entity
->save();
}
public function testDisallowedEntityDeleteInNonDefaultWorkspace($entity_type_id, $allowed) {
$this
->initializeWorkspacesModule();
$storage = $this->entityTypeManager
->getStorage($entity_type_id);
$this->workspaceManager
->switchToLive();
$entity = $storage
->createWithSampleValues($entity_type_id);
if ($entity_type_id === 'workspace') {
$entity->id = 'test';
}
$entity
->save();
$this
->switchToWorkspace('stage');
if (!$allowed) {
$this
->expectException(EntityStorageException::class);
$this
->expectExceptionMessage('This entity can only be deleted in the default workspace.');
}
$entity
->delete();
}
public function providerTestAllowedEntityCrudInNonDefaultWorkspace() {
return [
'workspace-provided non-internal entity type' => [
'entity_type_id' => 'workspace',
'allowed' => TRUE,
],
'internal entity type' => [
'entity_type_id' => 'entity_test_no_label',
'allowed' => TRUE,
],
'non-internal entity type' => [
'entity_type_id' => 'entity_test_mulrev',
'allowed' => FALSE,
],
];
}
public function testExecuteInWorkspaceContext() {
$this
->initializeWorkspacesModule();
$this->workspaceManager
->switchToLive();
$node = $this
->createNode([
'title' => 'live node 1',
]);
$node
->save();
$this
->switchToWorkspace('stage');
$node->title->value = 'stage node 1';
$node
->save();
$this->workspaceManager
->switchToLive();
$storage = $this->entityTypeManager
->getStorage('node');
$this
->assertFalse($this->workspaceManager
->hasActiveWorkspace());
$live_node = $storage
->load($node
->id());
$this
->assertEquals('live node 1', $live_node->title->value);
$result = $storage
->getQuery()
->condition('title', 'live node 1')
->execute();
$this
->assertEquals([
$live_node
->getRevisionId() => $node
->id(),
], $result);
$this->workspaceManager
->executeInWorkspace('stage', function () use ($node, $storage) {
$this
->assertEquals('stage', $this->workspaceManager
->getActiveWorkspace()
->id());
$stage_node = $storage
->load($node
->id());
$this
->assertEquals('stage node 1', $stage_node->title->value);
$result = $storage
->getQuery()
->condition('title', 'stage node 1')
->execute();
$this
->assertEquals([
$stage_node
->getRevisionId() => $stage_node
->id(),
], $result);
});
$this
->assertFalse($this->workspaceManager
->getActiveWorkspace());
}
protected function assertWorkspaceStatus(array $expected, $entity_type_id) {
$expected = $this
->flattenExpectedValues($expected, $entity_type_id);
$entity_keys = $this->entityTypeManager
->getDefinition($entity_type_id)
->getKeys();
foreach ($expected as $workspace_id => $expected_values) {
$this
->switchToWorkspace($workspace_id);
$this
->assertEntityLoad($expected_values, $entity_type_id);
$this
->assertEntityRevisionLoad($expected_values, $entity_type_id);
$this
->assertEntityQuery($expected_values, $entity_type_id);
$expected_frontpage = array_filter($expected_values, function ($expected_value) {
return $expected_value['status'] === TRUE && $expected_value['default_revision'] === TRUE;
});
usort($expected_frontpage, function ($a, $b) {
return $b['nid'] - $a['nid'];
});
$view = Views::getView('frontpage');
$view
->execute();
$this
->assertIdenticalResultset($view, $expected_frontpage, [
'nid' => 'nid',
]);
$rendered_view = $view
->render('page_1');
$output = \Drupal::service('renderer')
->renderRoot($rendered_view);
$this
->setRawContent($output);
foreach ($expected_values as $expected_entity_values) {
if ($expected_entity_values[$entity_keys['published']] === TRUE && $expected_entity_values['default_revision'] === TRUE) {
$this
->assertRaw($expected_entity_values[$entity_keys['label']]);
}
elseif ($workspace_id != 'stage' && $expected_entity_values[$entity_keys['id']] != 4) {
$this
->assertNoRaw($expected_entity_values[$entity_keys['label']]);
}
}
$view
->destroy();
$view
->setDisplay('page_1');
$filters = $view->displayHandlers
->get('page_1')
->getOption('filters');
$view->displayHandlers
->get('page_1')
->overrideOption('filters', $filters + [
'body_value' => [
'id' => 'body_value',
'table' => 'node__body',
'field' => 'body_value',
'operator' => 'not empty',
'plugin_id' => 'string',
],
]);
$view
->execute();
$this
->assertIdenticalResultset($view, $expected_frontpage, [
'nid' => 'nid',
]);
}
}
protected function assertEntityLoad(array $expected_values, $entity_type_id) {
$expected_default_revisions = array_filter($expected_values, function ($expected_value) {
return $expected_value['default_revision'] === TRUE;
});
$entity_keys = $this->entityTypeManager
->getDefinition($entity_type_id)
->getKeys();
$id_key = $entity_keys['id'];
$revision_key = $entity_keys['revision'];
$label_key = $entity_keys['label'];
$published_key = $entity_keys['published'];
$entities = $this->entityTypeManager
->getStorage($entity_type_id)
->loadMultiple(array_column($expected_default_revisions, $id_key));
foreach ($expected_default_revisions as $expected_default_revision) {
$entity_id = $expected_default_revision[$id_key];
$this
->assertEquals($expected_default_revision[$revision_key], $entities[$entity_id]
->getRevisionId());
$this
->assertEquals($expected_default_revision[$label_key], $entities[$entity_id]
->label());
$this
->assertEquals($expected_default_revision[$published_key], $entities[$entity_id]
->isPublished());
}
foreach ($expected_default_revisions as $expected_default_revision) {
$entity_id = $expected_default_revision[$id_key];
$entities = $this->entityTypeManager
->getStorage($entity_type_id)
->loadMultiple([
$entity_id,
]);
$this
->assertCount(1, $entities);
$this
->assertEquals($expected_default_revision[$revision_key], $entities[$entity_id]
->getRevisionId());
$this
->assertEquals($expected_default_revision[$label_key], $entities[$entity_id]
->label());
$this
->assertEquals($expected_default_revision[$published_key], $entities[$entity_id]
->isPublished());
}
foreach ($expected_default_revisions as $expected_default_revision) {
$entity = $this->entityTypeManager
->getStorage($entity_type_id)
->loadUnchanged($expected_default_revision[$id_key]);
$this
->assertEquals($expected_default_revision[$revision_key], $entity
->getRevisionId());
$this
->assertEquals($expected_default_revision[$label_key], $entity
->label());
$this
->assertEquals($expected_default_revision[$published_key], $entity
->isPublished());
}
}
protected function assertEntityRevisionLoad(array $expected_values, $entity_type_id) {
$entity_keys = $this->entityTypeManager
->getDefinition($entity_type_id)
->getKeys();
$id_key = $entity_keys['id'];
$revision_key = $entity_keys['revision'];
$label_key = $entity_keys['label'];
$published_key = $entity_keys['published'];
$entities = $this->entityTypeManager
->getStorage($entity_type_id)
->loadMultipleRevisions(array_column($expected_values, $revision_key));
foreach ($expected_values as $expected_revision) {
$revision_id = $expected_revision[$revision_key];
$this
->assertEquals($expected_revision[$id_key], $entities[$revision_id]
->id());
$this
->assertEquals($expected_revision[$revision_key], $entities[$revision_id]
->getRevisionId());
$this
->assertEquals($expected_revision[$label_key], $entities[$revision_id]
->label());
$this
->assertEquals($expected_revision[$published_key], $entities[$revision_id]
->isPublished());
}
}
protected function assertEntityQuery(array $expected_values, $entity_type_id) {
$storage = $this->entityTypeManager
->getStorage($entity_type_id);
$entity_keys = $this->entityTypeManager
->getDefinition($entity_type_id)
->getKeys();
$id_key = $entity_keys['id'];
$revision_key = $entity_keys['revision'];
$label_key = $entity_keys['label'];
$published_key = $entity_keys['published'];
$expected_default_revisions = array_filter($expected_values, function ($expected_value) {
return $expected_value['default_revision'] === TRUE;
});
$result = $storage
->getQuery()
->count()
->execute();
$this
->assertEquals(count($expected_default_revisions), $result);
$result = $storage
->getAggregateQuery()
->count()
->execute();
$this
->assertEquals(count($expected_default_revisions), $result);
$result = $storage
->getQuery()
->execute();
$expected_result = array_combine(array_column($expected_default_revisions, $revision_key), array_column($expected_default_revisions, $id_key));
$this
->assertEquals($expected_result, $result);
foreach ($expected_values as $expected_value) {
$query = $storage
->getQuery();
$query
->condition($entity_keys['id'], $expected_value[$id_key])
->condition($entity_keys['label'], $expected_value[$label_key])
->condition($entity_keys['published'], (int) $expected_value[$published_key]);
if (!$expected_value['default_revision']) {
$query
->allRevisions();
}
$result = $query
->execute();
$this
->assertEquals([
$expected_value[$revision_key] => $expected_value[$id_key],
], $result);
}
}
protected function flattenExpectedValues(array $expected, $entity_type_id) {
$flattened = [];
$entity_keys = $this->entityTypeManager
->getDefinition($entity_type_id)
->getKeys();
foreach ($expected as $workspace_id => $workspace_values) {
foreach ($workspace_values as $entity_id => $entity_revisions) {
foreach ($entity_revisions as $revision_id => $revision_values) {
$flattened[$workspace_id][] = [
$entity_keys['id'] => $entity_id,
$entity_keys['revision'] => $revision_id,
] + $revision_values;
}
}
}
return $flattened;
}
public function testFormCacheForEntityForms() {
$this
->initializeWorkspacesModule();
$this
->switchToWorkspace('stage');
$form_builder = $this->container
->get('form_builder');
$form = $this->entityTypeManager
->getFormObject('entity_test_mulrevpub', 'default');
$form
->setEntity(EntityTestMulRevPub::create([]));
$form_state = new FormState();
$built_form = $form_builder
->buildForm($form, $form_state);
$form_builder
->setCache($built_form['#build_id'], $built_form, $form_state);
}
public function testFormCacheForRegularForms() {
$this
->initializeWorkspacesModule();
$this
->switchToWorkspace('stage');
$form_builder = $this->container
->get('form_builder');
$form_state = new FormState();
$built_form = $form_builder
->getForm(SiteInformationForm::class, $form_state);
$form_builder
->setCache($built_form['#build_id'], $built_form, $form_state);
}
public function testPublishWorkspaceDedicatedTableStorage() {
$this
->initializeWorkspacesModule();
$node_storage = $this->entityTypeManager
->getStorage('node');
$this->workspaceManager
->switchToLive();
$node = $node_storage
->create([
'title' => 'Foo title',
'body' => 'Foo body',
'type' => 'page',
]);
$node
->save();
$this
->switchToWorkspace('stage');
$node->title = 'Bar title';
$node->body = 'Bar body';
$node
->save();
$this->workspaces['stage']
->publish();
$this->workspaceManager
->switchToLive();
$reloaded = $node_storage
->load($node
->id());
$this
->assertEquals('Bar title', $reloaded->title->value);
$this
->assertEquals('Bar body', $reloaded->body->value);
}
}