You are here

function NodeCreationTest::testNodeCreation in Zircon Profile 8

Same name and namespace in other branches
  1. 8.0 core/modules/node/src/Tests/NodeCreationTest.php \Drupal\node\Tests\NodeCreationTest::testNodeCreation()

Creates a "Basic page" node and verifies its consistency in the database.

File

core/modules/node/src/Tests/NodeCreationTest.php, line 40
Contains \Drupal\node\Tests\NodeCreationTest.

Class

NodeCreationTest
Create a node and test saving it.

Namespace

Drupal\node\Tests

Code

function testNodeCreation() {
  $node_type_storage = \Drupal::entityManager()
    ->getStorage('node_type');

  // Test /node/add page with only one content type.
  $node_type_storage
    ->load('article')
    ->delete();
  $this
    ->drupalGet('node/add');
  $this
    ->assertResponse(200);
  $this
    ->assertUrl('node/add/page');

  // Create a node.
  $edit = array();
  $edit['title[0][value]'] = $this
    ->randomMachineName(8);
  $edit['body[0][value]'] = $this
    ->randomMachineName(16);
  $this
    ->drupalPostForm('node/add/page', $edit, t('Save'));

  // Check that the Basic page has been created.
  $this
    ->assertRaw(t('@post %title has been created.', array(
    '@post' => 'Basic page',
    '%title' => $edit['title[0][value]'],
  )), 'Basic page created.');

  // Check that the node exists in the database.
  $node = $this
    ->drupalGetNodeByTitle($edit['title[0][value]']);
  $this
    ->assertTrue($node, 'Node found in database.');

  // Verify that pages do not show submitted information by default.
  $this
    ->drupalGet('node/' . $node
    ->id());
  $this
    ->assertNoText($node
    ->getOwner()
    ->getUsername());
  $this
    ->assertNoText(format_date($node
    ->getCreatedTime()));

  // Change the node type setting to show submitted by information.

  /** @var \Drupal\node\NodeTypeInterface $node_type */
  $node_type = $node_type_storage
    ->load('page');
  $node_type
    ->setDisplaySubmitted(TRUE);
  $node_type
    ->save();
  $this
    ->drupalGet('node/' . $node
    ->id());
  $this
    ->assertText($node
    ->getOwner()
    ->getUsername());
  $this
    ->assertText(format_date($node
    ->getCreatedTime()));
}