NodeCreationTestCase::testFailedPageCreation

7 node.test NodeCreationTestCase::testFailedPageCreation()
8 node.test NodeCreationTestCase::testFailedPageCreation()

Create a page node and verify that a transaction rolls back the failed creation

File

modules/node/node.test, line 507
Tests for node.module.

Code

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

  try {
    node_save((object) $edit);
    $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->drupalGetNodeByTitle($edit['title']);
    $this->assertFalse($node, t('Transactions supported, and node not found in database.'));
  }
  else {
    // Check that the node exists in the database.
    $node = $this->drupalGetNodeByTitle($edit['title']);
    $this->assertTrue($node, t('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, t('Transactions not supported, and rollback error logged to watchdog.'));
  }

  // Check that the rollback error was logged.
  $records = db_query("SELECT wid FROM {watchdog} WHERE variables LIKE '%Test exception for rollback.%'")->fetchAll();
  $this->assertTrue(count($records) > 0, t('Rollback explanatory error logged to watchdog.'));
}
Login or register to post comments