function StyleSerializerTest::testRestViewExposedFilter
Same name in other branches
- 8.9.x core/modules/rest/tests/src/Functional/Views/StyleSerializerTest.php \Drupal\Tests\rest\Functional\Views\StyleSerializerTest::testRestViewExposedFilter()
- 10 core/modules/rest/tests/src/Functional/Views/StyleSerializerTest.php \Drupal\Tests\rest\Functional\Views\StyleSerializerTest::testRestViewExposedFilter()
- 11.x 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 798
Class
- StyleSerializerTest
- Tests the serializer style plugin.
Namespace
Drupal\Tests\rest\Functional\ViewsCode
public function testRestViewExposedFilter() {
$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' => $node0->body->processed,
],
1 => [
'nid' => $node1->id(),
'body' => $node1->body->processed,
],
2 => [
'nid' => $node2->id(),
'body' => $node2->body->processed,
],
];
$this->assertEquals($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' => $node1->body->processed,
],
1 => [
'nid' => $node2->id(),
'body' => $node2->body->processed,
],
];
$cache_contexts = [
'languages:language_content',
'languages:language_interface',
'theme',
'request_format',
'user.node_grants:view',
'url',
];
$this->assertEquals($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.