function WorkspaceViewsIntegrationTest::testViewsQueryAlterWithoutDataTable

Tests the views query alter for an entity type without a data table.

File

core/modules/workspaces/tests/src/Kernel/WorkspaceViewsIntegrationTest.php, line 200

Class

WorkspaceViewsIntegrationTest
Tests the views integration for workspaces.

Namespace

Drupal\Tests\workspaces\Kernel

Code

public function testViewsQueryAlterWithoutDataTable() : void {
  // This entity type has no data table, so the query reads the base table,
  // which holds revisionable and non-revisionable fields alike. Only the
  // revisionable ones (e.g. 'name') should be affected by the query alter.
  $storage = $this->entityTypeManager
    ->getStorage('entity_test_revpub');
  $entity = $storage->create([
    'name' => 'live',
    'non_rev_field' => 'shared',
  ]);
  $entity->save();
  Workspace::create([
    'id' => 'stage',
    'label' => 'Stage',
  ])->save();
  $this->assertSame([
    'live',
  ], $this->getRevPubViewResults('name', 'live'));
  $this->assertSame([], $this->getRevPubViewResults('name', 'stage'));
  $this->switchToWorkspace('stage');
  $entity->set('name', 'stage');
  $entity->save();
  // The pending revision holds the new name, so that is what the filter has
  // to match on, and what the field has to return.
  $this->assertSame([
    'stage',
  ], $this->getRevPubViewResults('name', 'stage'));
  $this->assertSame([], $this->getRevPubViewResults('name', 'live'));
  // A non-revisionable field is shared by every revision and only exists on
  // the base table. Redirecting it to the revision table would query a column
  // that doesn't exist. The name still comes from the pending revision.
  $this->assertSame([
    'stage',
  ], $this->getRevPubViewResults('non_rev_field', 'shared'));
  // Live still sees the default revision.
  $this->switchToLive();
  $this->assertSame([
    'live',
  ], $this->getRevPubViewResults('name', 'live'));
  $this->assertSame([], $this->getRevPubViewResults('name', 'stage'));
}

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