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