function StyleSerializerTest::testRestViewExposedFilter

Same name and namespace in other branches
  1. 9 core/modules/rest/tests/src/Functional/Views/StyleSerializerTest.php \Drupal\Tests\rest\Functional\Views\StyleSerializerTest::testRestViewExposedFilter()
  2. 8.9.x core/modules/rest/tests/src/Functional/Views/StyleSerializerTest.php \Drupal\Tests\rest\Functional\Views\StyleSerializerTest::testRestViewExposedFilter()
  3. 10 core/modules/rest/tests/src/Functional/Views/StyleSerializerTest.php \Drupal\Tests\rest\Functional\Views\StyleSerializerTest::testRestViewExposedFilter()

Tests the exposed filter works.

There is an exposed filter on the title field which takes a title query parameter. This is set to filter nodes by those whose title starts with the value provided.

File

core/modules/rest/tests/src/Functional/Views/StyleSerializerTest.php, line 238

Class

StyleSerializerTest
Tests the serializer style plugin.

Namespace

Drupal\Tests\rest\Functional\Views

Code

public function testRestViewExposedFilter() : void {
    $this->drupalCreateContentType([
        'type' => 'page',
    ]);
    $node0 = $this->drupalCreateNode([
        'title' => 'Node 1',
    ]);
    $node1 = $this->drupalCreateNode([
        'title' => 'Node 11',
    ]);
    $node2 = $this->drupalCreateNode([
        'title' => 'Node 111',
    ]);
    // Test that no filter brings back all three nodes.
    $result = Json::decode($this->drupalGet('test/serialize/node-exposed-filter', [
        'query' => [
            '_format' => 'json',
        ],
    ]));
    $expected = [
        0 => [
            'nid' => $node0->id(),
            'body' => (string) $node0->body->processed,
        ],
        1 => [
            'nid' => $node1->id(),
            'body' => (string) $node1->body->processed,
        ],
        2 => [
            'nid' => $node2->id(),
            'body' => (string) $node2->body->processed,
        ],
    ];
    $this->assertSame($expected, $result, 'Querying a view with no exposed filter returns all nodes.');
    // Test that title starts with 'Node 11' query finds 2 of the 3 nodes.
    $result = Json::decode($this->drupalGet('test/serialize/node-exposed-filter', [
        'query' => [
            '_format' => 'json',
            'title' => 'Node 11',
        ],
    ]));
    $expected = [
        0 => [
            'nid' => $node1->id(),
            'body' => (string) $node1->body->processed,
        ],
        1 => [
            'nid' => $node2->id(),
            'body' => (string) $node2->body->processed,
        ],
    ];
    $cache_contexts = [
        'languages:language_content',
        'languages:language_interface',
        'theme',
        'request_format',
        'user.node_grants:view',
        'url',
    ];
    $this->assertSame($expected, $result, 'Querying a view with a starts with exposed filter on the title returns nodes whose title starts with value provided.');
    $this->assertCacheContexts($cache_contexts);
}

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