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