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. 8.9.x core/modules/node/tests/src/Functional/NodeCreationTest.php \Drupal\Tests\node\Functional\NodeCreationTest::testNodeCreation()
  3. 10 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 69

Class

NodeCreationTest
Create a node and test saving it.

Namespace

Drupal\Tests\node\Functional

Code

public function testNodeCreation() : void {
    $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->assertSession()
        ->addressEquals('node/add/page');
    // Create a node.
    $edit = [];
    $edit['title[0][value]'] = $this->randomMachineName(8);
    $edit['body[0][value]'] = $this->randomMachineName(16);
    $this->drupalGet('node/add/page');
    $this->submitForm($edit, 'Save');
    // Check that the Basic page has been created.
    $this->assertSession()
        ->pageTextContains('Basic page ' . $edit['title[0][value]'] . ' has been created.');
    // Verify that the creation message contains a link to a node.
    $this->assertSession()
        ->elementExists('xpath', '//div[@data-drupal-messages]//a[contains(@href, "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->assertSession()
        ->pageTextNotContains($node->getOwner()
        ->getAccountName());
    $this->assertSession()
        ->pageTextNotContains($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->assertSession()
        ->pageTextContains($node->getOwner()
        ->getAccountName());
    $this->assertSession()
        ->pageTextContains($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->assertSession()
        ->fieldNotExists('edit-revision', NULL);
    // Check that a user with administer content types permission is not
    // allowed to create content.
    $content_types_admin = $this->drupalCreateUser([
        'administer content types',
    ]);
    $this->drupalLogin($content_types_admin);
    $this->drupalGet('node/add/page');
    $this->assertSession()
        ->statusCodeEquals(403);
}

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