You are here

public function ViewModeTest::testDefaultViewModeAndOverrides in Search API Pages 8

Tests whether the default view mode and the overrides can be used for a Search API Page.

File

tests/src/Functional/ViewModeTest.php, line 39

Class

ViewModeTest
Provides web tests for Search API Pages.

Namespace

Drupal\Tests\search_api_page\Functional

Code

public function testDefaultViewModeAndOverrides() {
  $this
    ->drupalLogin($this->adminUser);
  $assert_session = $this
    ->assertSession();

  // Setup Search API server and index.
  $this
    ->setupSearchApi();

  // The body field of a Document is not visible in the teaser view mode.
  $this
    ->drupalGet('admin/structure/types/manage/document/display/teaser');
  $assert_session
    ->statusCodeEquals(200);
  $edit = [
    'fields[body][region]' => 'hidden',
  ];
  $this
    ->drupalPostForm(NULL, $edit, 'Save');

  // Create a Search API Page and verify that it exists.
  $step1 = [
    'label' => 'Search Page Test',
    'id' => 'search_page_test',
    'index' => $this->index
      ->id(),
  ];
  $this
    ->drupalPostForm('admin/config/search/search-api-pages/add', $step1, 'Next');
  $step2 = [
    'path' => 'search-page-test',
  ];
  $this
    ->drupalPostForm(NULL, $step2, 'Save');
  $this
    ->drupalGet('admin/config/search/search-api-pages');
  $assert_session
    ->statusCodeEquals(200);
  $assert_session
    ->pageTextContains('Search Page Test');

  // The default view mode is chosen as default.
  // For the content type Document, the teaser view mode is chosen.
  $this
    ->drupalGet('admin/config/search/search-api-pages/search_page_test');
  $assert_session
    ->statusCodeEquals(200);
  $assert_session
    ->pageTextContains('Default View mode all Content bundles');
  $assert_session
    ->pageTextContains('Content View mode overrides');
  $edit = [
    'view_mode_configuration[entity:node][default]' => 'default',
    'view_mode_configuration[entity:node][overrides][document]' => 'teaser',
  ];
  $this
    ->drupalPostForm(NULL, $edit, 'Save');

  // Perform a few searches and check whether the outcome is as expected.
  $this
    ->drupalGet('search-page-test');
  $assert_session
    ->statusCodeEquals(200);
  $this
    ->drupalPostForm(NULL, [
    'keys' => 'blog number 1',
  ], 'Search');
  $assert_session
    ->pageTextContains('Title blog number 1');
  $assert_session
    ->pageTextContains('This is the body text for blog number 1.');
  $this
    ->drupalPostForm(NULL, [
    'keys' => 'document number 1',
  ], 'Search');
  $assert_session
    ->pageTextContains('Title document number 1');
  $assert_session
    ->pageTextNotContains('This is the body text for document number 1.');
}