function NodeCreationTest::testUnpublishedNodeCreation

Same name in other branches
  1. 9 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 140

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->drupalPostForm('node/add/page', $edit, t('Save'));
    // Check that the user was redirected to the home page.
    $this->assertUrl('');
    $this->assertText(t('Test page text'));
    // Confirm that the node was created.
    $this->assertText(t('@post @title has been created.', [
        '@post' => 'Basic page',
        '@title' => $edit['title[0][value]'],
    ]));
    // 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');
}

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