function NodeTypeExampleTest::testNodeTypes

Same name and namespace in other branches
  1. 3.x modules/node_type_example/tests/src/Functional/NodeTypeExampleTest.php \Drupal\Tests\node_type_example\Functional\NodeTypeExampleTest::testNodeTypes()
  2. 8.x-1.x node_type_example/tests/src/Functional/NodeTypeExampleTest.php \Drupal\Tests\node_type_example\Functional\NodeTypeExampleTest::testNodeTypes()

Test our new content types.

Tests for the following:

  • That our content types appear in the user interface.
  • That our unlocked content type is unlocked.
  • That our locked content type is locked.
  • That we can create content using the user interface.
  • That our created content does appear in the database.

File

modules/node_type_example/tests/src/Functional/NodeTypeExampleTest.php, line 85

Class

NodeTypeExampleTest
Test that our content types are successfully created.

Namespace

Drupal\Tests\node_type_example\Functional

Code

public function testNodeTypes() {
  $assert = $this->assertSession();
  // Log in an admin user.
  $admin_user = $this->drupalCreateUser([
    'administer content types',
  ]);
  $this->drupalLogin($admin_user);
  // Get a list of content types.
  $this->drupalGet('/admin/structure/types');
  // Verify that these content types show up in the user interface.
  // Basic content type found.
  $assert->pageTextContains('Example: Basic Content Type');
  // Locked content type found.
  $assert->pageTextContains('Example: Locked Content Type');
  // Check for the locked status of our content types.
  // $nodeType will be of type Drupal\node\NodeTypeInterface.
  $node_type = NodeType::load('basic_content_type');
  $this->assertInstanceOf(NodeTypeInterface::class, $node_type, 'basic_content_type exists.');
  if ($node_type) {
    $this->assertFalse($node_type->isLocked(), 'basic_content_type is not locked.');
  }
  $node_type = NodeType::load('locked_content_type');
  $this->assertInstanceOf(NodeTypeInterface::class, $node_type, 'locked_content_type exists.');
  if ($node_type) {
    $this->assertEquals('locked_content_type', $node_type->isLocked());
  }
  // Log in a content creator.
  $creator_user = $this->drupalCreateUser([
    'create basic_content_type content',
  ]);
  $this->drupalLogin($creator_user);
  // Create a node.
  $edit = [];
  $edit['title[0][value]'] = $this->randomMachineName(8);
  $edit['body[0][value]'] = $this->randomMachineName(16);
  $this->drupalGet('/node/add/basic_content_type');
  $this->submitForm($edit, 'Save');
  // Check that the Basic page has been created.
  $assert->pageTextContains((string) new FormattableMarkup('@post @title has been created.', [
    '@post' => 'Example: Basic Content Type',
    '@title' => $edit['title[0][value]'],
  ]));
  // Check that the node exists in the database.
  $node = $this->drupalGetNodeByTitle($edit['title[0][value]']);
  $this->assertInstanceOf(NodeInterface::class, $node, 'Node found in database.');
}