You are here

public function StyleSerializerTest::testRestViewExposedFilter in Drupal 8

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()

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 790

Class

StyleSerializerTest
Tests the serializer style plugin.

Namespace

Drupal\Tests\rest\Functional\Views

Code

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
    ->assertEqual($result, $expected, '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
    ->assertEqual($result, $expected, 'Querying a view with a starts with exposed filter on the title returns nodes whose title starts with value provided.');
  $this
    ->assertCacheContexts($cache_contexts);
}