You are here

public function ExampleFunctionalTest::testNewPageApiCreate in Examples for Developers 3.x

Same name and namespace in other branches
  1. 8 testing_example/tests/src/Functional/ExampleFunctionalTest.php \Drupal\Tests\testing_example\Functional\ExampleFunctionalTest::testNewPageApiCreate()

Demonstrate node creation via NodeCreationTrait::createNode.

File

modules/testing_example/tests/src/Functional/ExampleFunctionalTest.php, line 128

Class

ExampleFunctionalTest
Class ExampleFunctionalTest.

Namespace

Drupal\Tests\testing_example\Functional

Code

public function testNewPageApiCreate() {
  $assert = $this
    ->assertSession();
  $this
    ->drupalLogin($this->adminUser);
  $nodeTitle = 'Test node for testNewPageApiCreate';

  // Create new node using API.
  $node = $this
    ->drupalCreateNode([
    'type' => 'test_content_type',
    'title' => $nodeTitle,
    'body' => [
      [
        'format' => filter_default_format($this->adminUser),
        'value' => 'Body of test node',
      ],
    ],
  ]);
  $node
    ->save();
  $url = $node
    ->toUrl();

  // Confirm page creation.
  $this
    ->drupalGet($url);
  $assert
    ->statusCodeEquals(200);

  // Log in our normal user and navigate to the node.
  $this
    ->drupalLogin($this->authUser);
  $this
    ->drupalGet($url);
  $assert
    ->statusCodeEquals(200);

  // Look at the *page* title.
  $assert
    ->titleEquals("{$nodeTitle} | Drupal");

  // Find the title of the node itself.
  $nodeTitleElement = $this
    ->getSession()
    ->getPage()
    ->find('css', 'h1 span');
  $this
    ->assertEquals($nodeTitleElement
    ->getText(), $nodeTitle);
}