| 1 | <?php |
| 2 | |
| 3 | /** |
| 4 | * Class CRM_Core_TransactionTest |
| 5 | * @group headless |
| 6 | */ |
| 7 | class CRM_Core_TransactionTest extends CiviUnitTestCase { |
| 8 | |
| 9 | /** |
| 10 | * @var array |
| 11 | */ |
| 12 | private $callbackLog; |
| 13 | |
| 14 | /** |
| 15 | * @var array (int $idx => int $contactId) list of contact IDs that have been created (in order of creation) |
| 16 | * |
| 17 | * Note that ID this is all IDs created by the test-case -- even if the creation was subsequently rolled back |
| 18 | */ |
| 19 | private $cids; |
| 20 | |
| 21 | public function setUp() { |
| 22 | parent::setUp(); |
| 23 | $this->quickCleanup(array('civicrm_contact', 'civicrm_activity')); |
| 24 | $this->callbackLog = array(); |
| 25 | $this->cids = array(); |
| 26 | } |
| 27 | |
| 28 | /** |
| 29 | * @return array |
| 30 | */ |
| 31 | public function dataCreateStyle() { |
| 32 | return array( |
| 33 | array('sql-insert'), |
| 34 | array('bao-create'), |
| 35 | ); |
| 36 | } |
| 37 | |
| 38 | /** |
| 39 | * @return array |
| 40 | */ |
| 41 | public function dataCreateAndCommitStyles() { |
| 42 | return array( |
| 43 | array('sql-insert', 'implicit-commit'), |
| 44 | array('sql-insert', 'explicit-commit'), |
| 45 | array('bao-create', 'implicit-commit'), |
| 46 | array('bao-create', 'explicit-commit'), |
| 47 | ); |
| 48 | } |
| 49 | |
| 50 | /** |
| 51 | * @param string $createStyle |
| 52 | * 'sql-insert'|'bao-create'. |
| 53 | * @param string $commitStyle |
| 54 | * 'implicit-commit'|'explicit-commit'. |
| 55 | * @dataProvider dataCreateAndCommitStyles |
| 56 | */ |
| 57 | public function testBasicCommit($createStyle, $commitStyle) { |
| 58 | $this->createContactWithTransaction('reuse-tx', $createStyle, $commitStyle); |
| 59 | $this->assertCount(1, $this->cids); |
| 60 | $this->assertContactsExistByOffset(array(0 => TRUE)); |
| 61 | } |
| 62 | |
| 63 | /** |
| 64 | * @dataProvider dataCreateStyle |
| 65 | * @param $createStyle |
| 66 | */ |
| 67 | public function testBasicRollback($createStyle) { |
| 68 | $this->createContactWithTransaction('reuse-tx', $createStyle, 'rollback'); |
| 69 | $this->assertCount(1, $this->cids); |
| 70 | $this->assertContactsExistByOffset(array(0 => FALSE)); |
| 71 | } |
| 72 | |
| 73 | /** |
| 74 | * Test in which an outer function makes multiple calls to inner functions. |
| 75 | * but then rolls back the entire set. |
| 76 | * |
| 77 | * @param string $createStyle |
| 78 | * 'sql-insert'|'bao-create'. |
| 79 | * @param string $commitStyle |
| 80 | * 'implicit-commit'|'explicit-commit'. |
| 81 | * @dataProvider dataCreateAndCommitStyles |
| 82 | */ |
| 83 | public function testBatchRollback($createStyle, $commitStyle) { |
| 84 | $this->runBatch( |
| 85 | 'reuse-tx', |
| 86 | array( |
| 87 | array('reuse-tx', $createStyle, $commitStyle), // cid 0 |
| 88 | array('reuse-tx', $createStyle, $commitStyle), // cid 1 |
| 89 | ), |
| 90 | array(0 => TRUE, 1 => TRUE), |
| 91 | 'rollback' |
| 92 | ); |
| 93 | $this->assertCount(2, $this->cids); |
| 94 | $this->assertContactsExistByOffset(array(0 => FALSE, 1 => FALSE)); |
| 95 | } |
| 96 | |
| 97 | /** |
| 98 | * Test in which runBatch makes multiple calls to |
| 99 | * createContactWithTransaction using a mix of rollback/commit. |
| 100 | * createContactWithTransaction use nesting (savepoints), so the |
| 101 | * batch is able to commit. |
| 102 | * |
| 103 | * @param string $createStyle |
| 104 | * 'sql-insert'|'bao-create'. |
| 105 | * @param string $commitStyle |
| 106 | * 'implicit-commit'|'explicit-commit'. |
| 107 | * @dataProvider dataCreateAndCommitStyles |
| 108 | */ |
| 109 | public function testMixedBatchCommit_nesting($createStyle, $commitStyle) { |
| 110 | $this->runBatch( |
| 111 | 'reuse-tx', |
| 112 | array( |
| 113 | array('nest-tx', $createStyle, $commitStyle), // cid 0 |
| 114 | array('nest-tx', $createStyle, 'rollback'), // cid 1 |
| 115 | array('nest-tx', $createStyle, $commitStyle), // cid 2 |
| 116 | ), |
| 117 | array(0 => TRUE, 1 => FALSE, 2 => TRUE), |
| 118 | $commitStyle |
| 119 | ); |
| 120 | $this->assertCount(3, $this->cids); |
| 121 | $this->assertContactsExistByOffset(array(0 => TRUE, 1 => FALSE, 2 => TRUE)); |
| 122 | } |
| 123 | |
| 124 | /** |
| 125 | * Test in which runBatch makes multiple calls to |
| 126 | * createContactWithTransaction using a mix of rollback/commit. |
| 127 | * createContactWithTransaction reuses the main transaction, |
| 128 | * so the overall batch must rollback. |
| 129 | * |
| 130 | * @param string $createStyle |
| 131 | * 'sql-insert'|'bao-create'. |
| 132 | * @param string $commitStyle |
| 133 | * 'implicit-commit'|'explicit-commit'. |
| 134 | * @dataProvider dataCreateAndCommitStyles |
| 135 | */ |
| 136 | public function testMixedBatchCommit_reuse($createStyle, $commitStyle) { |
| 137 | $this->runBatch( |
| 138 | 'reuse-tx', |
| 139 | array( |
| 140 | array('reuse-tx', $createStyle, $commitStyle), // cid 0 |
| 141 | array('reuse-tx', $createStyle, 'rollback'), // cid 1 |
| 142 | array('reuse-tx', $createStyle, $commitStyle), // cid 2 |
| 143 | ), |
| 144 | array(0 => TRUE, 1 => TRUE, 2 => TRUE), |
| 145 | $commitStyle |
| 146 | ); |
| 147 | $this->assertCount(3, $this->cids); |
| 148 | $this->assertContactsExistByOffset(array(0 => FALSE, 1 => FALSE, 2 => FALSE)); |
| 149 | } |
| 150 | |
| 151 | /** |
| 152 | * Test in which runBatch makes multiple calls to |
| 153 | * createContactWithTransaction using a mix of rollback/commit. |
| 154 | * The overall batch is rolled back. |
| 155 | * |
| 156 | * @param string $createStyle |
| 157 | * 'sql-insert'|'bao-create'. |
| 158 | * @param string $commitStyle |
| 159 | * 'implicit-commit'|'explicit-commit'. |
| 160 | * @dataProvider dataCreateAndCommitStyles |
| 161 | */ |
| 162 | public function testMixedBatchRollback_nesting($createStyle, $commitStyle) { |
| 163 | $this->assertFalse(CRM_Core_Transaction::isActive()); |
| 164 | $this->runBatch( |
| 165 | 'reuse-tx', |
| 166 | array( |
| 167 | array('nest-tx', $createStyle, $commitStyle), // cid 0 |
| 168 | array('nest-tx', $createStyle, 'rollback'), // cid 1 |
| 169 | array('nest-tx', $createStyle, $commitStyle), // cid 2 |
| 170 | ), |
| 171 | array(0 => TRUE, 1 => FALSE, 2 => TRUE), |
| 172 | 'rollback' |
| 173 | ); |
| 174 | $this->assertFalse(CRM_Core_Transaction::isActive()); |
| 175 | $this->assertCount(3, $this->cids); |
| 176 | $this->assertContactsExistByOffset(array(0 => FALSE, 1 => FALSE, 2 => FALSE)); |
| 177 | } |
| 178 | |
| 179 | public function testIsActive() { |
| 180 | $this->assertEquals(FALSE, CRM_Core_Transaction::isActive()); |
| 181 | $this->assertEquals(TRUE, CRM_Core_Transaction::willCommit()); |
| 182 | $tx = new CRM_Core_Transaction(); |
| 183 | $this->assertEquals(TRUE, CRM_Core_Transaction::isActive()); |
| 184 | $this->assertEquals(TRUE, CRM_Core_Transaction::willCommit()); |
| 185 | $tx = NULL; |
| 186 | $this->assertEquals(FALSE, CRM_Core_Transaction::isActive()); |
| 187 | $this->assertEquals(TRUE, CRM_Core_Transaction::willCommit()); |
| 188 | } |
| 189 | |
| 190 | public function testIsActive_rollback() { |
| 191 | $this->assertEquals(FALSE, CRM_Core_Transaction::isActive()); |
| 192 | $this->assertEquals(TRUE, CRM_Core_Transaction::willCommit()); |
| 193 | |
| 194 | $tx = new CRM_Core_Transaction(); |
| 195 | $this->assertEquals(TRUE, CRM_Core_Transaction::isActive()); |
| 196 | $this->assertEquals(TRUE, CRM_Core_Transaction::willCommit()); |
| 197 | |
| 198 | $tx->rollback(); |
| 199 | $this->assertEquals(TRUE, CRM_Core_Transaction::isActive()); |
| 200 | $this->assertEquals(FALSE, CRM_Core_Transaction::willCommit()); |
| 201 | |
| 202 | $tx = NULL; |
| 203 | $this->assertEquals(FALSE, CRM_Core_Transaction::isActive()); |
| 204 | $this->assertEquals(TRUE, CRM_Core_Transaction::willCommit()); |
| 205 | } |
| 206 | |
| 207 | public function testCallback_commit() { |
| 208 | $tx = new CRM_Core_Transaction(); |
| 209 | |
| 210 | CRM_Core_Transaction::addCallback(CRM_Core_Transaction::PHASE_PRE_COMMIT, array($this, '_preCommit'), array( |
| 211 | 'qwe', |
| 212 | 'rty', |
| 213 | )); |
| 214 | CRM_Core_Transaction::addCallback(CRM_Core_Transaction::PHASE_POST_COMMIT, array($this, '_postCommit'), array( |
| 215 | 'uio', |
| 216 | 'p[]', |
| 217 | )); |
| 218 | CRM_Core_Transaction::addCallback(CRM_Core_Transaction::PHASE_PRE_ROLLBACK, array( |
| 219 | $this, |
| 220 | '_preRollback', |
| 221 | ), array('asd', 'fgh')); |
| 222 | CRM_Core_Transaction::addCallback(CRM_Core_Transaction::PHASE_POST_ROLLBACK, array( |
| 223 | $this, |
| 224 | '_postRollback', |
| 225 | ), array('jkl', ';')); |
| 226 | |
| 227 | CRM_Core_DAO::executeQuery('UPDATE civicrm_contact SET id = 100 WHERE id = 100'); |
| 228 | |
| 229 | $this->assertEquals(array(), $this->callbackLog); |
| 230 | $tx = NULL; |
| 231 | $this->assertEquals(array('_preCommit', 'qwe', 'rty'), $this->callbackLog[0]); |
| 232 | $this->assertEquals(array('_postCommit', 'uio', 'p[]'), $this->callbackLog[1]); |
| 233 | } |
| 234 | |
| 235 | public function testCallback_rollback() { |
| 236 | $tx = new CRM_Core_Transaction(); |
| 237 | |
| 238 | CRM_Core_Transaction::addCallback(CRM_Core_Transaction::PHASE_PRE_COMMIT, array($this, '_preCommit'), array( |
| 239 | 'ewq', |
| 240 | 'ytr', |
| 241 | )); |
| 242 | CRM_Core_Transaction::addCallback(CRM_Core_Transaction::PHASE_POST_COMMIT, array($this, '_postCommit'), array( |
| 243 | 'oiu', |
| 244 | '][p', |
| 245 | )); |
| 246 | CRM_Core_Transaction::addCallback(CRM_Core_Transaction::PHASE_PRE_ROLLBACK, array( |
| 247 | $this, |
| 248 | '_preRollback', |
| 249 | ), array('dsa', 'hgf')); |
| 250 | CRM_Core_Transaction::addCallback(CRM_Core_Transaction::PHASE_POST_ROLLBACK, array( |
| 251 | $this, |
| 252 | '_postRollback', |
| 253 | ), array('lkj', ';')); |
| 254 | |
| 255 | CRM_Core_DAO::executeQuery('UPDATE civicrm_contact SET id = 100 WHERE id = 100'); |
| 256 | $tx->rollback(); |
| 257 | |
| 258 | $this->assertEquals(array(), $this->callbackLog); |
| 259 | $tx = NULL; |
| 260 | $this->assertEquals(array('_preRollback', 'dsa', 'hgf'), $this->callbackLog[0]); |
| 261 | $this->assertEquals(array('_postRollback', 'lkj', ';'), $this->callbackLog[1]); |
| 262 | } |
| 263 | |
| 264 | /** |
| 265 | * @param string $createStyle |
| 266 | * 'sql-insert'|'bao-create'. |
| 267 | * @param string $commitStyle |
| 268 | * 'implicit-commit'|'explicit-commit'. |
| 269 | * @dataProvider dataCreateAndCommitStyles |
| 270 | */ |
| 271 | public function testRun_ok($createStyle, $commitStyle) { |
| 272 | $test = $this; |
| 273 | CRM_Core_Transaction::create(TRUE)->run(function ($tx) use (&$test, $createStyle, $commitStyle) { |
| 274 | $test->createContactWithTransaction('nest-tx', $createStyle, $commitStyle); |
| 275 | $test->assertContactsExistByOffset(array(0 => TRUE)); |
| 276 | }); |
| 277 | $this->assertContactsExistByOffset(array(0 => TRUE)); |
| 278 | } |
| 279 | |
| 280 | /** |
| 281 | * @param string $createStyle |
| 282 | * 'sql-insert'|'bao-create'. |
| 283 | * @param string $commitStyle |
| 284 | * 'implicit-commit'|'explicit-commit'. |
| 285 | * @dataProvider dataCreateAndCommitStyles |
| 286 | */ |
| 287 | public function testRun_exception($createStyle, $commitStyle) { |
| 288 | $tx = new CRM_Core_Transaction(); |
| 289 | $test = $this; |
| 290 | $e = NULL; // Exception |
| 291 | try { |
| 292 | CRM_Core_Transaction::create(TRUE)->run(function ($tx) use (&$test, $createStyle, $commitStyle) { |
| 293 | $test->createContactWithTransaction('nest-tx', $createStyle, $commitStyle); |
| 294 | $test->assertContactsExistByOffset(array(0 => TRUE)); |
| 295 | throw new Exception("Ruh-roh"); |
| 296 | }); |
| 297 | } |
| 298 | catch (Exception $ex) { |
| 299 | $e = $ex; |
| 300 | if (get_class($e) != 'Exception' || $e->getMessage() != 'Ruh-roh') { |
| 301 | throw $e; |
| 302 | } |
| 303 | } |
| 304 | $this->assertTrue($e instanceof Exception); |
| 305 | $this->assertContactsExistByOffset(array(0 => FALSE)); |
| 306 | } |
| 307 | |
| 308 | /** |
| 309 | * @param $cids |
| 310 | * @param bool $exist |
| 311 | */ |
| 312 | public function assertContactsExist($cids, $exist = TRUE) { |
| 313 | foreach ($cids as $cid) { |
| 314 | $this->assertTrue(is_numeric($cid)); |
| 315 | $this->assertDBQuery($exist ? 1 : 0, 'SELECT count(*) FROM civicrm_contact WHERE id = %1', array( |
| 316 | 1 => array($cid, 'Integer'), |
| 317 | )); |
| 318 | } |
| 319 | } |
| 320 | |
| 321 | /** |
| 322 | * @param array $existsByOffset |
| 323 | * Array(int $cidOffset => bool $expectExists). |
| 324 | * @param int $generalOffset |
| 325 | */ |
| 326 | public function assertContactsExistByOffset($existsByOffset, $generalOffset = 0) { |
| 327 | foreach ($existsByOffset as $offset => $expectExists) { |
| 328 | $this->assertTrue(isset($this->cids[$generalOffset + $offset]), "Find cid at offset($generalOffset + $offset)"); |
| 329 | $cid = $this->cids[$generalOffset + $offset]; |
| 330 | $this->assertTrue(is_numeric($cid)); |
| 331 | $this->assertDBQuery($expectExists ? 1 : 0, 'SELECT count(*) FROM civicrm_contact WHERE id = %1', array( |
| 332 | 1 => array($cid, 'Integer'), |
| 333 | ), "Check contact at offset($generalOffset + $offset)"); |
| 334 | } |
| 335 | } |
| 336 | |
| 337 | /** |
| 338 | * Use SQL to INSERT a contact and assert success. Perform |
| 339 | * work within a transaction. |
| 340 | * |
| 341 | * @param string $nesting |
| 342 | * 'reuse-tx'|'nest-tx' how to construct transaction. |
| 343 | * @param string $insert |
| 344 | * 'sql-insert'|'bao-create' how to add the example record. |
| 345 | * @param string $outcome |
| 346 | * 'rollback'|'implicit-commit'|'explicit-commit' how to finish transaction. |
| 347 | * @return int |
| 348 | * cid |
| 349 | */ |
| 350 | public function createContactWithTransaction($nesting, $insert, $outcome) { |
| 351 | if ($nesting != 'reuse-tx' && $nesting != 'nest-tx') { |
| 352 | throw new RuntimeException('Bad test data: reuse=' . $nesting); |
| 353 | } |
| 354 | if ($insert != 'sql-insert' && $insert != 'bao-create') { |
| 355 | throw new RuntimeException('Bad test data: insert=' . $insert); |
| 356 | } |
| 357 | if ($outcome != 'rollback' && $outcome != 'implicit-commit' && $outcome != 'explicit-commit') { |
| 358 | throw new RuntimeException('Bad test data: outcome=' . $outcome); |
| 359 | } |
| 360 | |
| 361 | $tx = new CRM_Core_Transaction($nesting === 'nest-tx'); |
| 362 | |
| 363 | if ($insert == 'sql-insert') { |
| 364 | $r = CRM_Core_DAO::executeQuery("INSERT INTO civicrm_contact(first_name,last_name) VALUES ('ff', 'll')"); |
| 365 | $cid = mysql_insert_id(); |
| 366 | } |
| 367 | elseif ($insert == 'bao-create') { |
| 368 | $params = array( |
| 369 | 'contact_type' => 'Individual', |
| 370 | 'first_name' => 'FF', |
| 371 | 'last_name' => 'LL', |
| 372 | ); |
| 373 | $r = CRM_Contact_BAO_Contact::create($params); |
| 374 | $cid = $r->id; |
| 375 | } |
| 376 | |
| 377 | $this->cids[] = $cid; |
| 378 | |
| 379 | $this->assertContactsExist(array($cid), TRUE); |
| 380 | |
| 381 | if ($outcome == 'rollback') { |
| 382 | $tx->rollback(); |
| 383 | } |
| 384 | elseif ($outcome == 'explicit-commit') { |
| 385 | $tx->commit(); |
| 386 | } // else: implicit-commit |
| 387 | |
| 388 | return $cid; |
| 389 | } |
| 390 | |
| 391 | /** |
| 392 | * Perform a series of operations within smaller transactions. |
| 393 | * |
| 394 | * @param string $nesting |
| 395 | * 'reuse-tx'|'nest-tx' how to construct transaction. |
| 396 | * @param array $callbacks |
| 397 | * See createContactWithTransaction. |
| 398 | * @param array $existsByOffset |
| 399 | * See assertContactsMix. |
| 400 | * @param string $outcome |
| 401 | * 'rollback'|'implicit-commit'|'explicit-commit' how to finish transaction. |
| 402 | * @return void |
| 403 | */ |
| 404 | public function runBatch($nesting, $callbacks, $existsByOffset, $outcome) { |
| 405 | if ($nesting != 'reuse-tx' && $nesting != 'nest-tx') { |
| 406 | throw new RuntimeException('Bad test data: nesting=' . $nesting); |
| 407 | } |
| 408 | if ($outcome != 'rollback' && $outcome != 'implicit-commit' && $outcome != 'explicit-commit') { |
| 409 | throw new RuntimeException('Bad test data: outcome=' . $nesting); |
| 410 | } |
| 411 | |
| 412 | $tx = new CRM_Core_Transaction($nesting === 'reuse-tx'); |
| 413 | |
| 414 | $generalOffset = count($this->cids); |
| 415 | foreach ($callbacks as $callback) { |
| 416 | list ($cbNesting, $cbInsert, $cbOutcome) = $callback; |
| 417 | $this->createContactWithTransaction($cbNesting, $cbInsert, $cbOutcome); |
| 418 | } |
| 419 | |
| 420 | $this->assertContactsExistByOffset($existsByOffset, $generalOffset); |
| 421 | |
| 422 | if ($outcome == 'rollback') { |
| 423 | $tx->rollback(); |
| 424 | } |
| 425 | elseif ($outcome == 'explicit-commit') { |
| 426 | $tx->commit(); |
| 427 | } // else: implicit-commit |
| 428 | } |
| 429 | |
| 430 | /** |
| 431 | * @param $arg1 |
| 432 | * @param $arg2 |
| 433 | */ |
| 434 | public function _preCommit($arg1, $arg2) { |
| 435 | $this->callbackLog[] = array('_preCommit', $arg1, $arg2); |
| 436 | } |
| 437 | |
| 438 | /** |
| 439 | * @param $arg1 |
| 440 | * @param $arg2 |
| 441 | */ |
| 442 | public function _postCommit($arg1, $arg2) { |
| 443 | $this->callbackLog[] = array('_postCommit', $arg1, $arg2); |
| 444 | } |
| 445 | |
| 446 | /** |
| 447 | * @param $arg1 |
| 448 | * @param $arg2 |
| 449 | */ |
| 450 | public function _preRollback($arg1, $arg2) { |
| 451 | $this->callbackLog[] = array('_preRollback', $arg1, $arg2); |
| 452 | } |
| 453 | |
| 454 | /** |
| 455 | * @param $arg1 |
| 456 | * @param $arg2 |
| 457 | */ |
| 458 | public function _postRollback($arg1, $arg2) { |
| 459 | $this->callbackLog[] = array('_postRollback', $arg1, $arg2); |
| 460 | } |
| 461 | |
| 462 | } |