You are here

protected function ViewsCacheInvalidationTest::assertViewsResult in Search API 8

Checks that the view for the given user contains the expected results.

Parameters

string $user_key: The key of the user to check.

string[] $node_titles: The titles of the nodes that are expected to be present in the results.

1 call to ViewsCacheInvalidationTest::assertViewsResult()
ViewsCacheInvalidationTest::testQueryCacheInvalidation in tests/src/Kernel/Views/ViewsCacheInvalidationTest.php
Tests that a cached views query result is invalidated at the right moments.

File

tests/src/Kernel/Views/ViewsCacheInvalidationTest.php, line 386

Class

ViewsCacheInvalidationTest
Tests that cached Search API views get invalidated at the right occasions.

Namespace

Drupal\Tests\search_api\Kernel\Views

Code

protected function assertViewsResult($user_key, array $node_titles) {

  // Clear the static caches of the cache tags invalidators. The invalidators
  // will only invalidate cache tags once per request to improve performance.
  // Unfortunately they cannot distinguish between an actual Drupal page
  // request and a PHPUnit test that simulates visiting multiple pages.
  // We are pretending that every time this method is called a new page has
  // been requested, and the static caches are empty.
  $this->cacheTagsInvalidator
    ->resetChecksums();
  $this
    ->setCurrentUser($user_key);
  $render_array = $this
    ->getRenderableView();
  $html = (string) $this->renderer
    ->renderRoot($render_array);

  // Check that exactly the titles of the expected results are present.
  $node_titles = array_flip($node_titles);
  foreach ($this->nodes as $node_title => $node) {
    if (isset($node_titles[$node_title])) {
      $this
        ->assertStringContainsString($node_title, $html);
    }
    else {
      $this
        ->assertStringNotContainsString($node_title, $html);
    }
  }
}