class NodeEntityViewModeAlterTest
Same name and namespace in other branches
- 11.x core/modules/node/tests/src/Functional/NodeEntityViewModeAlterTest.php \Drupal\Tests\node\Functional\NodeEntityViewModeAlterTest
- 10 core/modules/node/tests/src/Functional/NodeEntityViewModeAlterTest.php \Drupal\Tests\node\Functional\NodeEntityViewModeAlterTest
- 8.9.x core/modules/node/tests/src/Functional/NodeEntityViewModeAlterTest.php \Drupal\Tests\node\Functional\NodeEntityViewModeAlterTest
- 7.x modules/node/node.test \NodeEntityViewModeAlterTest
Tests changing view modes for nodes.
@group node
Hierarchy
- class \Drupal\Tests\BrowserTestBase uses \Drupal\Core\Test\FunctionalTestSetupTrait, \Drupal\Tests\UiHelperTrait, \Drupal\Core\Test\TestSetupTrait, \Drupal\Tests\block\Traits\BlockCreationTrait, \Drupal\FunctionalTests\AssertLegacyTrait, \Drupal\Tests\RandomGeneratorTrait, \Drupal\Tests\node\Traits\NodeCreationTrait, \Drupal\Tests\node\Traits\ContentTypeCreationTrait, \Drupal\Tests\ConfigTestTrait, \Drupal\Tests\TestRequirementsTrait, \Drupal\Tests\user\Traits\UserCreationTrait, \Drupal\Tests\XdebugRequestTrait, \Drupal\Tests\Traits\PhpUnitWarnings, \Drupal\Tests\PhpUnitCompatibilityTrait, \Symfony\Bridge\PhpUnit\ExpectDeprecationTrait, \Drupal\Tests\ExtensionListTestTrait extends \PHPUnit\Framework\TestCase
- class \Drupal\Tests\node\Functional\NodeTestBase extends \Drupal\Tests\BrowserTestBase
- class \Drupal\Tests\node\Functional\NodeEntityViewModeAlterTest uses \Drupal\Tests\EntityViewTrait extends \Drupal\Tests\node\Functional\NodeTestBase
- class \Drupal\Tests\node\Functional\NodeTestBase extends \Drupal\Tests\BrowserTestBase
Expanded class hierarchy of NodeEntityViewModeAlterTest
File
-
core/
modules/ node/ tests/ src/ Functional/ NodeEntityViewModeAlterTest.php, line 13
Namespace
Drupal\Tests\node\FunctionalView source
class NodeEntityViewModeAlterTest extends NodeTestBase {
use EntityViewTrait;
/**
* Enable dummy module that implements hook_ENTITY_TYPE_view() for nodes.
*/
protected static $modules = [
'node_test',
];
/**
* {@inheritdoc}
*/
protected $defaultTheme = 'stark';
/**
* Create a "Basic page" node and verify its consistency in the database.
*/
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]'] = 'Data that should appear only in the body for the node.';
$edit['body[0][summary]'] = '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.');
}
}
Buggy or inaccurate documentation? Please file an issue. Need support? Need help programming? Connect with the Drupal community.