You are here

public function NodeEntityViewModeAlterTest::testNodeViewModeChange in Drupal 9

Same name and namespace in other branches
  1. 8 core/modules/node/tests/src/Functional/NodeEntityViewModeAlterTest.php \Drupal\Tests\node\Functional\NodeEntityViewModeAlterTest::testNodeViewModeChange()

Create a "Basic page" node and verify its consistency in the database.

File

core/modules/node/tests/src/Functional/NodeEntityViewModeAlterTest.php, line 30

Class

NodeEntityViewModeAlterTest
Tests changing view modes for nodes.

Namespace

Drupal\Tests\node\Functional

Code

public function testNodeViewModeChange() {
  $web_user = $this
    ->drupalCreateUser([
    'create page content',
    'edit own page content',
  ]);
  $this
    ->drupalLogin($web_user);

  // Create a node.
  $edit = [];
  $edit['title[0][value]'] = $this
    ->randomMachineName(8);
  $edit['body[0][value]'] = t('Data that should appear only in the body for the node.');
  $edit['body[0][summary]'] = t('Extra data that should appear only in the teaser for the node.');
  $this
    ->drupalGet('node/add/page');
  $this
    ->submitForm($edit, 'Save');
  $node = $this
    ->drupalGetNodeByTitle($edit['title[0][value]']);

  // Set the flag to alter the view mode and view the node.
  \Drupal::state()
    ->set('node_test_change_view_mode', 'teaser');
  Cache::invalidateTags([
    'rendered',
  ]);
  $this
    ->drupalGet('node/' . $node
    ->id());

  // Check that teaser mode is viewed.
  $this
    ->assertSession()
    ->pageTextContains('Extra data that should appear only in the teaser for the node.');

  // Make sure body text is not present.
  $this
    ->assertSession()
    ->pageTextNotContains('Data that should appear only in the body for the node.');

  // Test that the correct build mode has been set.
  $build = $this
    ->buildEntityView($node);
  $this
    ->assertEquals('teaser', $build['#view_mode'], 'The view mode has correctly been set to teaser.');
}