function NodeCreationTest::testNodeCreation

Same name and namespace in other branches
  1. 9 core/modules/node/tests/src/Functional/NodeCreationTest.php \Drupal\Tests\node\Functional\NodeCreationTest::testNodeCreation()
  2. 10 core/modules/node/tests/src/Functional/NodeCreationTest.php \Drupal\Tests\node\Functional\NodeCreationTest::testNodeCreation()
  3. 11.x core/modules/node/tests/src/Functional/NodeCreationTest.php \Drupal\Tests\node\Functional\NodeCreationTest::testNodeCreation()

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

File

core/modules/node/tests/src/Functional/NodeCreationTest.php, line 44

Class

NodeCreationTest
Create a node and test saving it.

Namespace

Drupal\Tests\node\Functional

Code

public function testNodeCreation() {
    $node_type_storage = \Drupal::entityTypeManager()->getStorage('node_type');
    // Test /node/add page with only one content type.
    $node_type_storage->load('article')
        ->delete();
    $this->drupalGet('node/add');
    $this->assertSession()
        ->statusCodeEquals(200);
    $this->assertUrl('node/add/page');
    // Create a node.
    $edit = [];
    $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->assertText(t('@post @title has been created.', [
        '@post' => 'Basic page',
        '@title' => $edit['title[0][value]'],
    ]), 'Basic page created.');
    // Verify that the creation message contains a link to a node.
    $view_link = $this->xpath('//div[@class="messages"]//a[contains(@href, :href)]', [
        ':href' => 'node/',
    ]);
    $this->assert(isset($view_link), 'The message area contains a link to a node');
    // Check that the node exists in the database.
    $node = $this->drupalGetNodeByTitle($edit['title[0][value]']);
    $this->assertNotEmpty($node, 'Node found in database.');
    // Verify that pages do not show submitted information by default.
    $this->drupalGet('node/' . $node->id());
    $this->assertNoText($node->getOwner()
        ->getAccountName());
    $this->assertNoText($this->container
        ->get('date.formatter')
        ->format($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()
        ->getAccountName());
    $this->assertText($this->container
        ->get('date.formatter')
        ->format($node->getCreatedTime()));
    // Check if the node revision checkbox is not rendered on node creation form.
    $admin_user = $this->drupalCreateUser([
        'administer nodes',
        'create page content',
    ]);
    $this->drupalLogin($admin_user);
    $this->drupalGet('node/add/page');
    $this->assertNoFieldById('edit-revision', NULL, 'The revision checkbox is not present.');
}

Buggy or inaccurate documentation? Please file an issue. Need support? Need help programming? Connect with the Drupal community.