You are here

public function WorkspaceIntegrationTest::testEntityQueryRelationship in Workspace 8.2

Tests the Entity Query relationship API with workspaces.

File

tests/src/Kernel/WorkspaceIntegrationTest.php, line 372

Class

WorkspaceIntegrationTest
Tests a complete deployment scenario across different workspaces.

Namespace

Drupal\Tests\workspace\Kernel

Code

public function testEntityQueryRelationship() {
  $this
    ->initializeWorkspaceModule();

  // Add an entity reference field that targets 'entity_test_mulrev' entities.
  $this
    ->createEntityReferenceField('node', 'page', 'field_test_entity', 'Test entity reference', 'entity_test_mulrev');

  // Add an entity reference field that targets 'node' entities so we can test
  // references to the same base tables.
  $this
    ->createEntityReferenceField('node', 'page', 'field_test_node', 'Test node reference', 'node');
  $this
    ->switchToWorkspace('live');
  $node_1 = $this
    ->createNode([
    'title' => 'live node 1',
  ]);
  $entity_test = EntityTestMulRev::create([
    'name' => 'live entity_test_mulrev',
    '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(),
  ]);

  // Switch to the 'stage' workspace and change some values for the referenced
  // entities.
  $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_mulrev';
  $entity_test->non_rev_field->value = 'stage non-revisionable value';
  $entity_test
    ->save();

  // Make sure that we're requesting the default revision.
  $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());

  // Add conditions for a reference to the same entity type.
  $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());

  // Add conditions for a reference to a different entity type.
  $query
    ->condition('field_test_entity.entity.name', 'stage entity_test_mulrev')
    ->condition('field_test_entity.entity.non_rev_field', 'stage non-revisionable value')
    ->condition('field_test_entity.entity.uuid', $entity_test
    ->uuid());
  $result = $query
    ->execute();
  $this
    ->assertSame([
    $node_2
      ->getRevisionId() => $node_2
      ->id(),
  ], $result);
}