function NodeCreationTest::testUnpublishedNodeCreation

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

Creates an unpublished node and confirms correct redirect behavior.

File

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

Class

NodeCreationTest
Create a node and test saving it.

Namespace

Drupal\Tests\node\Functional

Code

public function testUnpublishedNodeCreation() {
    // Set the front page to the test page.
    $this->config('system.site')
        ->set('page.front', '/test-page')
        ->save();
    // Set "Basic page" content type to be unpublished by default.
    $fields = \Drupal::service('entity_field.manager')->getFieldDefinitions('node', 'page');
    $fields['status']->getConfig('page')
        ->setDefaultValue(FALSE)
        ->save();
    // 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 user was redirected to the home page.
    $this->assertSession()
        ->addressEquals('');
    $this->assertSession()
        ->pageTextContains('Test page text');
    // Confirm that the node was 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/")]');
}

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