be0f1c062305b567a32aff69653a4a37b8c70506
[yaffs-website] / src / Functional / NodeEntityViewModeAlterTest.php
1 <?php
2
3 namespace Drupal\Tests\node\Functional;
4
5 use Drupal\Core\Cache\Cache;
6 use Drupal\Tests\EntityViewTrait;
7
8 /**
9  * Tests changing view modes for nodes.
10  *
11  * @group node
12  */
13 class NodeEntityViewModeAlterTest extends NodeTestBase {
14
15   use EntityViewTrait;
16
17   /**
18    * Enable dummy module that implements hook_ENTITY_TYPE_view() for nodes.
19    */
20   public static $modules = ['node_test'];
21
22   /**
23    * Create a "Basic page" node and verify its consistency in the database.
24    */
25   public function testNodeViewModeChange() {
26     $web_user = $this->drupalCreateUser(['create page content', 'edit own page content']);
27     $this->drupalLogin($web_user);
28
29     // Create a node.
30     $edit = [];
31     $edit['title[0][value]'] = $this->randomMachineName(8);
32     $edit['body[0][value]'] = t('Data that should appear only in the body for the node.');
33     $edit['body[0][summary]'] = t('Extra data that should appear only in the teaser for the node.');
34     $this->drupalPostForm('node/add/page', $edit, t('Save'));
35
36     $node = $this->drupalGetNodeByTitle($edit['title[0][value]']);
37
38     // Set the flag to alter the view mode and view the node.
39     \Drupal::state()->set('node_test_change_view_mode', 'teaser');
40     Cache::invalidateTags(['rendered']);
41     $this->drupalGet('node/' . $node->id());
42
43     // Check that teaser mode is viewed.
44     $this->assertText('Extra data that should appear only in the teaser for the node.', 'Teaser text present');
45     // Make sure body text is not present.
46     $this->assertNoText('Data that should appear only in the body for the node.', 'Body text not present');
47
48     // Test that the correct build mode has been set.
49     $build = $this->buildEntityView($node);
50     $this->assertEqual($build['#view_mode'], 'teaser', 'The view mode has correctly been set to teaser.');
51   }
52
53 }