1 node.test NodeCreationTestCase::testFailedPageCreation()

Verifies that a transaction rolls back the failed creation.

File

core/modules/node/tests/node.test, line 807
Tests for node.module.

Class

NodeCreationTestCase
Tests creating and saving a node.

Code

function testFailedPageCreation() {
  // Create a node.
  $edit = array(
    'uid' => $this->loggedInUser->uid,
    'name' => $this->loggedInUser->name,
    'type' => 'page',
    'langcode' => LANGUAGE_NONE,
    'title' => 'testing_transaction_exception',
  );

  try {
    // An exception is generated by node_test_exception_node_insert() if the
    // title is 'testing_transaction_exception'.
    entity_create('node', $edit)->save();
    $this->fail(t('Expected exception has not been thrown.'));
  }
  catch (Exception $e) {
    $this->pass(t('Expected exception has been thrown.'));
  }

  if (Database::getConnection()->supportsTransactions()) {
    // Check that the node does not exist in the database.
    $node = $this->backdropGetNodeByTitle($edit['title']);
    $this->assertFalse($node, 'Transactions supported, and node not found in database.');
  }
  else {
    // Check that the node exists in the database.
    $node = $this->backdropGetNodeByTitle($edit['title']);
    $this->assertTrue($node, 'Transactions not supported, and node found in database.');

    // Check that the failed rollback was logged.
    $records = db_query("SELECT wid FROM {watchdog} WHERE message LIKE 'Explicit rollback failed%'")->fetchAll();
    $this->assertTrue(count($records) > 0, 'Transactions not supported, and rollback error logged to watchdog.');
  }

  // Check that the rollback error was logged.
  // The variables column is a blob, and therefore can't be searched with LIKE
  $records = db_query("SELECT variables FROM {watchdog} WHERE type='node'")->fetchAll();
  $count = 0;
  foreach ($records as $row) {
    if (strpos($row->variables, "Test exception for rollback")) {
      $count++;
    }
  }
  $this->assertTrue($count > 0, 'Rollback explanatory error logged to watchdog.');
}