You are here

public function SimpleTestExampleTest::testSimpleTestExampleCreate in Examples for Developers 8

Test node creation through the user interface.

Creates a node using the node/add form and verifies its consistency in the database.

File

testing_example/src/Tests/SimpleTestExampleTest.php, line 79

Class

SimpleTestExampleTest
Ensure that the simpletest_example content type provided functions properly.

Namespace

Drupal\testing_example\Tests

Code

public function testSimpleTestExampleCreate() {

  // Create a user with the ability to create our content type. This
  // permission is generated by the node module.
  $user = $this
    ->createUser([
    'create testing_example content',
  ]);

  // Log in our user.
  $this
    ->drupalLogin($user);

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

  // Check that our testing_example node has been created.
  $this
    ->assertText((string) new FormattableMarkup('@post @title has been created.', [
    '@post' => 'Testing Example Node Type',
    '@title' => $edit['title[0][value]'],
  ]));

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

  // Verify 'submitted by' information. Drupal adds a newline in there, so
  // we have to check for that.
  $username = $this->loggedInUser
    ->getAccountName();
  $datetime = $this->container
    ->get('date.formatter')
    ->format($node
    ->getCreatedTime());
  $submitted_by = "Submitted by {$username}\n on {$datetime}";
  $this
    ->drupalGet('node/' . $node
    ->id());
  $this
    ->assertText($submitted_by);
}