CRM-13890 - Test fixes (unit tests were coded against the buggy behavior where real...
[civicrm-core.git] / tests / phpunit / CRM / Core / TransactionTest.php
CommitLineData
792ad554
TO
1<?php
2require_once 'CiviTest/CiviUnitTestCase.php';
3require_once 'CiviTest/Contact.php';
4require_once 'CiviTest/Custom.php';
5class CRM_Core_TransactionTest extends CiviUnitTestCase {
6
7 function setUp() {
8 parent::setUp();
9 $this->quickCleanup(array('civicrm_contact', 'civicrm_activity'));
10 }
11
12 function testDefaultCommit_RawInsert_1xOuter() {
13 $cid = NULL;
14 $test = $this;
15
16 $transactionalFunction_outermost = function () use (&$cid, $test) {
17 $tx = new CRM_Core_Transaction();
18 $r = CRM_Core_DAO::executeQuery("INSERT INTO civicrm_contact(first_name,last_name) VALUES ('ff', 'll')");
19 $cid = mysql_insert_id();
20 $test->assertContactsExist(array($cid), TRUE);
21
22 // End of outermost $tx; COMMIT will execute ASAP
23 };
24
25 $transactionalFunction_outermost();
26 $test->assertContactsExist(array($cid), TRUE);
27 }
28
29 function testDefaultCommit_BaoCreate_1xOuter() {
30 $cid = NULL;
31 $test = $this;
32
33 $transactionalFunction_outermost = function () use (&$cid, $test) {
34 $tx = new CRM_Core_Transaction();
35 $params = array(
36 'contact_type' => 'Individual',
37 'first_name' => 'FF',
38 'last_name' => 'LL',
39 );
40 $r = CRM_Contact_BAO_Contact::create($params);
41 $cid = $r->id;
42 $test->assertContactsExist(array($cid), TRUE);
43
44 // End of outermost $tx; COMMIT will execute ASAP
45 };
46
47 $transactionalFunction_outermost();
48 $test->assertContactsExist(array($cid), TRUE);
49 }
50
51 function testRollback_RawInsert_1xOuter() {
52 $cid = NULL;
53 $test = $this;
54
55 $transactionalFunction_outermost = function() use (&$cid, $test) {
56 $tx = new CRM_Core_Transaction();
57
58 $r = CRM_Core_DAO::executeQuery("INSERT INTO civicrm_contact(first_name,last_name) VALUES ('ff', 'll')");
59 $cid = mysql_insert_id();
60
61 $test->assertContactsExist(array($cid), TRUE);
62
63 $tx->rollback(); // Mark ROLLBACK, but don't execute yet
64
65 // End of outermost $tx; ROLLBACK will execute ASAP
66 };
67
68 $transactionalFunction_outermost();
69 $test->assertContactsExist(array($cid), FALSE);
70 }
71
72 /**
73 * Test in which an outer function ($transactionalFunction_outermost) makes multiple calls
74 * to inner functions ($transactionalFunction_inner) but then rollsback the entire set.
75 */
76 function testRollback_RawInsert_2xInner() {
77 $cids = array();
78 $test = $this;
79
80 $transactionalFunction_inner = function() use (&$cids, $test) {
81 $tx = new CRM_Core_Transaction();
82
83 $r = CRM_Core_DAO::executeQuery("INSERT INTO civicrm_contact(first_name,last_name) VALUES ('ff', 'll')");
84 $cid = mysql_insert_id();
85 $cids[] = $cid;
86
87 $test->assertContactsExist($cids, TRUE);
88
89 // End of inner $tx; neither COMMIT nor ROLLBACK b/c another $tx remains
90 };
91
92 $transactionalFunction_outermost = function() use (&$cids, $test, $transactionalFunction_inner) {
93 $tx = new CRM_Core_Transaction();
94
95 $transactionalFunction_inner();
96 $transactionalFunction_inner();
97
98 $tx->rollback(); // Mark ROLLBACK, but don't execute yet
99
100 $test->assertContactsExist($cids, TRUE); // not yet rolled back
101
102 // End of outermost $tx; ROLLBACK will execute ASAP
103 };
104
105 $transactionalFunction_outermost();
106 $test->assertContactsExist($cids, FALSE);
107 }
108
109 function testRollback_BaoCreate_1xOuter() {
110 $cid = NULL;
111 $test = $this;
112
113 $transactionalFunction_outermost = function() use (&$cid, $test) {
114 $tx = new CRM_Core_Transaction();
115
116 $params = array(
117 'contact_type' => 'Individual',
118 'first_name' => 'F',
119 'last_name' => 'L',
120 );
121 $r = CRM_Contact_BAO_Contact::create($params);
122 $cid = $r->id;
123
124 $test->assertContactsExist(array($cid), TRUE);
125
126 $tx->rollback(); // Mark ROLLBACK, but don't execute yet
127
128 // End of outermost $tx; ROLLBACK will execute ASAP
129 };
130
131 $transactionalFunction_outermost();
132
133 // No outstanding $tx -- ROLLBACK should be done
134 $test->assertContactsExist(array($cid), FALSE);
135 }
136
137 /**
138 * Test in which an outer function ($transactionalFunction_outermost) makes multiple calls
139 * to inner functions ($transactionalFunction_inner) but then rollsback the entire set.
140 */
141 function testRollback_BaoCreate_2xInner() {
142 $cids = array();
143 $test = $this;
144
145 $transactionalFunction_inner = function() use (&$cids, $test) {
146 $tx = new CRM_Core_Transaction();
147
148 $params = array(
149 'contact_type' => 'Individual',
150 'first_name' => 'F',
151 'last_name' => 'L',
152 );
153 $r = CRM_Contact_BAO_Contact::create($params);
154 $cid = $r->id;
155 $cids[] = $cid;
156
157 $test->assertContactsExist($cids, TRUE);
158
159 // end of inner $tx; neither COMMIT nor ROLLBACK b/c it's inner
160 };
161
162 $transactionalFunction_outermost = function() use (&$cids, $test, $transactionalFunction_inner) {
163 $tx = new CRM_Core_Transaction();
164
165 $transactionalFunction_inner();
166 $transactionalFunction_inner();
167
168 $tx->rollback(); // Mark ROLLBACK, but don't execute yet
169
170 $test->assertContactsExist($cids, TRUE); // not yet rolled back
171
172 // End of outermost $tx; ROLLBACK will execute ASAP
173 };
174
175 $transactionalFunction_outermost();
176
177 // No outstanding $tx -- ROLLBACK should be done
178 $test->assertContactsExist($cids, FALSE);
179 }
180
181 public function assertContactsExist($cids, $exist = TRUE) {
182 foreach ($cids as $cid) {
183 $this->assertTrue(is_numeric($cid));
184 $this->assertDBQuery($exist ? 1 : 0, 'SELECT count(*) FROM civicrm_contact WHERE id = %1', array(
185 1 => array($cid, 'Integer'),
186 ));
187 }
188 }
189}