function NodeCreationTest::testFailedPageCreation

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

Verifies that a transaction rolls back the failed creation.

File

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

Class

NodeCreationTest
Create a node and test saving it.

Namespace

Drupal\Tests\node\Functional

Code

public function testFailedPageCreation() : void {
    // Create a node.
    $edit = [
        'uid' => $this->loggedInUser
            ->id(),
        'name' => $this->loggedInUser->name,
        'type' => 'page',
        'langcode' => LanguageInterface::LANGCODE_NOT_SPECIFIED,
        'title' => 'testing_transaction_exception',
    ];
    try {
        // An exception is generated by node_test_exception_node_insert() if the
        // title is 'testing_transaction_exception'.
        Node::create($edit)->save();
        $this->fail('Expected exception has not been thrown.');
    } catch (\Exception $e) {
        // Expected exception; just continue testing.
    }
    // Check that the node does not exist in the database.
    $node = $this->drupalGetNodeByTitle($edit['title']);
    $this->assertFalse($node);
    // Check that the rollback error was logged.
    $records = static::getWatchdogIdsForTestExceptionRollback();
    // Verify that the rollback explanatory error was logged.
    $this->assertNotEmpty($records);
}

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