function WorkspaceIntegrationTest::testEntityQueryRelationship

Same name in other branches
  1. 8.9.x core/modules/workspaces/tests/src/Kernel/WorkspaceIntegrationTest.php \Drupal\Tests\workspaces\Kernel\WorkspaceIntegrationTest::testEntityQueryRelationship()
  2. 10 core/modules/workspaces/tests/src/Kernel/WorkspaceIntegrationTest.php \Drupal\Tests\workspaces\Kernel\WorkspaceIntegrationTest::testEntityQueryRelationship()
  3. 11.x core/modules/workspaces/tests/src/Kernel/WorkspaceIntegrationTest.php \Drupal\Tests\workspaces\Kernel\WorkspaceIntegrationTest::testEntityQueryRelationship()

Tests the Entity Query relationship API with workspaces.

File

core/modules/workspaces/tests/src/Kernel/WorkspaceIntegrationTest.php, line 515

Class

WorkspaceIntegrationTest
Tests a complete publishing scenario across different workspaces.

Namespace

Drupal\Tests\workspaces\Kernel

Code

public function testEntityQueryRelationship() {
    $this->initializeWorkspacesModule();
    // Add an entity reference field that targets 'entity_test_mulrevpub'
    // entities.
    $this->createEntityReferenceField('node', 'page', 'field_test_entity', 'Test entity reference', 'entity_test_mulrevpub');
    // 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 = 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(),
    ]);
    // 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_mulrevpub';
    $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()
        ->accessCheck(FALSE);
    $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.
    // @todo Re-enable the two conditions below when we find a way to not join
    //   the workspace_association table for every duplicate entity base table
    //   join.
    // @see https://www.drupal.org/project/drupal/issues/2983639
    $query->condition('field_test_entity.entity.uuid', $entity_test->uuid());
    $result = $query->execute();
    $this->assertSame([
        $node_2->getRevisionId() => $node_2->id(),
    ], $result);
}

Buggy or inaccurate documentation? Please file an issue. Need support? Need help programming? Connect with the Drupal community.