function WorkspaceViewsIntegrationTest::testViewsQueryAlter

Same name and namespace in other branches
  1. 10 core/modules/workspaces/tests/src/Kernel/WorkspaceViewsIntegrationTest.php \Drupal\Tests\workspaces\Kernel\WorkspaceViewsIntegrationTest::testViewsQueryAlter()

Tests workspace query alter for views.

@covers \Drupal\workspaces\ViewsQueryAlter::alterQueryForEntityType @covers \Drupal\workspaces\ViewsQueryAlter::getRevisionTableJoin

File

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

Class

WorkspaceViewsIntegrationTest
Tests the views integration for workspaces.

Namespace

Drupal\Tests\workspaces\Kernel

Code

public function testViewsQueryAlter() : void {
    // Create a test entity and two nodes.
    $test_entity = \Drupal::entityTypeManager()->getStorage('entity_test_mulrevpub')
        ->create([
        'name' => 'test entity - live',
    ]);
    $test_entity->save();
    $node_1 = $this->createNode([
        'title' => 'node - live - 1',
        'body' => 'node 1',
        'created' => $this->createdTimestamp++,
        'field_reference' => $test_entity->id(),
    ]);
    $node_2 = $this->createNode([
        'title' => 'node - live - 2',
        'body' => 'node 2',
        'created' => $this->createdTimestamp++,
    ]);
    // Create a new workspace and activate it.
    Workspace::create([
        'id' => 'stage',
        'label' => 'Stage',
    ])->save();
    $this->switchToWorkspace('stage');
    $view = Views::getView('frontpage');
    // Add a filter on a field that is stored in a dedicated table in order to
    // test field joins with extra conditions (e.g. 'deleted' and 'langcode').
    $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();
    $expected = [
        [
            'nid' => $node_2->id(),
        ],
        [
            'nid' => $node_1->id(),
        ],
    ];
    $this->assertIdenticalResultset($view, $expected, [
        'nid' => 'nid',
    ]);
    // Add a filter on a field from a relationship, in order to test field
    // joins with extra conditions (e.g. 'deleted' and 'langcode').
    $view->destroy();
    $view->setDisplay('page_1');
    $view->displayHandlers
        ->get('page_1')
        ->overrideOption('relationships', [
        'field_reference' => [
            'id' => 'field_reference',
            'table' => 'node__field_reference',
            'field' => 'field_reference',
            'required' => FALSE,
        ],
    ]);
    $view->displayHandlers
        ->get('page_1')
        ->overrideOption('filters', $filters + [
        'name' => [
            'id' => 'name',
            'table' => 'entity_test_mulrevpub_property_data',
            'field' => 'name',
            'operator' => 'not empty',
            'relationship' => 'field_reference',
        ],
    ]);
    $view->execute();
    $expected = [
        [
            'nid' => $node_1->id(),
        ],
    ];
    $this->assertIdenticalResultset($view, $expected, [
        'nid' => 'nid',
    ]);
}

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