// 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.'
);
}}