function NodeEntityViewModeAlterTest::testNodeViewModeChange

Same name in other branches
  1. 9 core/modules/node/tests/src/Functional/NodeEntityViewModeAlterTest.php \Drupal\Tests\node\Functional\NodeEntityViewModeAlterTest::testNodeViewModeChange()
  2. 8.9.x core/modules/node/tests/src/Functional/NodeEntityViewModeAlterTest.php \Drupal\Tests\node\Functional\NodeEntityViewModeAlterTest::testNodeViewModeChange()
  3. 10 core/modules/node/tests/src/Functional/NodeEntityViewModeAlterTest.php \Drupal\Tests\node\Functional\NodeEntityViewModeAlterTest::testNodeViewModeChange()
  4. 11.x core/modules/node/tests/src/Functional/NodeEntityViewModeAlterTest.php \Drupal\Tests\node\Functional\NodeEntityViewModeAlterTest::testNodeViewModeChange()

Create a "Basic page" node and verify its consistency in the database.

File

modules/node/node.test, line 2953

Class

NodeEntityViewModeAlterTest
Tests changing view modes for nodes.

Code

function testNodeViewModeChange() {
    $web_user = $this->drupalCreateUser(array(
        'create page content',
        'edit own page content',
    ));
    $this->drupalLogin($web_user);
    // Create a node.
    $edit = array();
    $langcode = LANGUAGE_NONE;
    $edit["title"] = $this->randomName(8);
    $edit["body[{$langcode}][0][value]"] = 'Data that should appear only in the body for the node.';
    $edit["body[{$langcode}][0][summary]"] = 'Extra data that should appear only in the teaser for the node.';
    $this->drupalPost('node/add/page', $edit, t('Save'));
    $node = $this->drupalGetNodeByTitle($edit["title"]);
    // Set the flag to alter the view mode and view the node.
    variable_set('node_test_change_view_mode', 'teaser');
    $this->drupalGet('node/' . $node->nid);
    // Check that teaser mode is viewed.
    $this->assertText('Extra data that should appear only in the teaser for the node.', 'Teaser text present');
    // Make sure body text is not present.
    $this->assertNoText('Data that should appear only in the body for the node.', 'Body text not present');
    // Test that the correct build mode has been set.
    $build = node_view($node);
    $this->assertEqual($build['#view_mode'], 'teaser', '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.