NodeEntityViewModeAlterTest.php in Drupal 9
File
core/modules/node/tests/src/Functional/NodeEntityViewModeAlterTest.php
View source
<?php
namespace Drupal\Tests\node\Functional;
use Drupal\Core\Cache\Cache;
use Drupal\Tests\EntityViewTrait;
class NodeEntityViewModeAlterTest extends NodeTestBase {
use EntityViewTrait;
protected static $modules = [
'node_test',
];
protected $defaultTheme = 'stark';
public function testNodeViewModeChange() {
$web_user = $this
->drupalCreateUser([
'create page content',
'edit own page content',
]);
$this
->drupalLogin($web_user);
$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]']);
\Drupal::state()
->set('node_test_change_view_mode', 'teaser');
Cache::invalidateTags([
'rendered',
]);
$this
->drupalGet('node/' . $node
->id());
$this
->assertSession()
->pageTextContains('Extra data that should appear only in the teaser for the node.');
$this
->assertSession()
->pageTextNotContains('Data that should appear only in the body for the node.');
$build = $this
->buildEntityView($node);
$this
->assertEquals('teaser', $build['#view_mode'], 'The view mode has correctly been set to teaser.');
}
}