View source  
  <?php
namespace Drupal\Tests\search_api_page\Functional;
class ViewModeTest extends FunctionalTestBase {
  
  public function setUp() {
    parent::setUp();
    
    $this
      ->drupalCreateContentType([
      'type' => 'blog',
    ]);
    $this
      ->drupalCreateNode([
      'title' => 'Title blog number 1',
      'type' => 'blog',
      'body' => [
        [
          'value' => 'This is the body text for blog number 1.',
        ],
      ],
    ]);
    
    $this
      ->drupalCreateContentType([
      'type' => 'document',
    ]);
    $this
      ->drupalCreateNode([
      'title' => 'Title document number 1',
      'type' => 'document',
      'body' => [
        [
          'value' => 'This is the body text for document number 1.',
        ],
      ],
    ]);
  }
  
  public function testDefaultViewModeAndOverrides() {
    $this
      ->drupalLogin($this->adminUser);
    $assert_session = $this
      ->assertSession();
    
    $this
      ->setupSearchApi();
    
    $this
      ->drupalGet('admin/structure/types/manage/document/display/teaser');
    $assert_session
      ->statusCodeEquals(200);
    $edit = [
      'fields[body][region]' => 'hidden',
    ];
    $this
      ->drupalPostForm(NULL, $edit, 'Save');
    
    $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');
    
    $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');
    
    $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.');
  }
}