Commit | Line | Data |
---|---|---|
56154d36 TO |
1 | <?php |
2 | /** | |
3 | * api_v3_AttachmentTest | |
4 | * | |
5 | * @copyright Copyright CiviCRM LLC (C) 2014 | |
6 | * @license http://www.fsf.org/licensing/licenses/agpl-3.0.html | |
7 | * GNU Affero General Public License version 3 | |
8 | * @version $Id: ContactTest.php 31254 2010-12-15 10:09:29Z eileen $ | |
9 | * @package CiviCRM | |
10 | * | |
11 | * This file is part of CiviCRM | |
12 | * | |
13 | * CiviCRM is free software; you can redistribute it and/or | |
14 | * modify it under the terms of the GNU Affero General Public License | |
15 | * as published by the Free Software Foundation; either version 3 of | |
16 | * the License, or (at your option) any later version. | |
17 | * | |
18 | * CiviCRM is distributed in the hope that it will be useful, | |
19 | * but WITHOUT ANY WARRANTY; without even the implied warranty of | |
20 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
21 | * GNU Affero General Public License for more details. | |
22 | * | |
23 | * You should have received a copy of the GNU Affero General Public | |
24 | * License along with this program. If not, see | |
25 | * <http://www.gnu.org/licenses/>. | |
26 | */ | |
27 | ||
28 | /** | |
29 | * Include class definitions | |
30 | */ | |
31 | require_once 'CiviTest/CiviUnitTestCase.php'; | |
32 | ||
33 | ||
34 | /** | |
35 | * Test for the Attachment API | |
36 | * | |
37 | * @package CiviCRM_APIv3 | |
38 | * @subpackage API_Contact | |
39 | */ | |
40 | class api_v3_AttachmentTest extends CiviUnitTestCase { | |
41 | protected static $filePrefix = NULL; | |
42 | ||
f0be539a EM |
43 | /** |
44 | * @return string | |
45 | */ | |
56154d36 TO |
46 | public static function getFilePrefix() { |
47 | if (!self::$filePrefix) { | |
48 | self::$filePrefix = "test_" . CRM_Utils_String::createRandom(5, CRM_Utils_String::ALPHANUMERIC) . '_'; | |
49 | } | |
50 | return self::$filePrefix; | |
51 | } | |
52 | ||
53 | ||
54 | protected function setUp() { | |
55 | parent::setUp(); | |
56 | $this->useTransaction(TRUE); | |
57 | ||
58 | $this->cleanupFiles(); | |
59 | file_put_contents($this->tmpFile('mytest.txt'), 'This comes from a file'); | |
60 | } | |
61 | ||
62 | protected function tearDown() { | |
63 | parent::tearDown(); | |
64 | $this->cleanupFiles(); | |
048222df | 65 | \Civi::reset(); |
56154d36 TO |
66 | } |
67 | ||
f0be539a EM |
68 | /** |
69 | * @return array | |
70 | */ | |
56154d36 TO |
71 | public function okCreateProvider() { |
72 | $cases = array(); // array($entityClass, $createParams, $expectedContent) | |
73 | ||
74 | $cases[] = array( | |
75 | 'CRM_Activity_DAO_Activity', | |
76 | array( | |
77 | 'name' => self::getFilePrefix() . 'exampleFromContent.txt', | |
78 | 'mime_type' => 'text/plain', | |
79 | 'description' => 'My test description', | |
80 | 'content' => 'My test content', | |
81 | ), | |
82 | 'My test content', | |
83 | ); | |
84 | ||
85 | $cases[] = array( | |
86 | 'CRM_Activity_DAO_Activity', | |
87 | array( | |
88 | 'name' => self::getFilePrefix() . 'exampleWithEmptyContent.txt', | |
89 | 'mime_type' => 'text/plain', | |
90 | 'description' => 'My test description', | |
91 | 'content' => '', | |
92 | ), | |
93 | '', | |
94 | ); | |
95 | ||
96 | $cases[] = array( | |
97 | 'CRM_Activity_DAO_Activity', | |
98 | array( | |
99 | 'name' => self::getFilePrefix() . 'exampleFromMove.txt', | |
100 | 'mime_type' => 'text/plain', | |
101 | 'description' => 'My test description', | |
102 | 'options' => array( | |
103 | 'move-file' => $this->tmpFile('mytest.txt'), | |
21dfd5f5 | 104 | ), |
56154d36 TO |
105 | ), |
106 | 'This comes from a file', | |
107 | ); | |
108 | ||
109 | return $cases; | |
110 | } | |
111 | ||
f0be539a EM |
112 | /** |
113 | * @return array | |
114 | */ | |
56154d36 TO |
115 | public function badCreateProvider() { |
116 | $cases = array(); // array($entityClass, $createParams, $expectedError) | |
117 | ||
118 | $cases[] = array( | |
119 | 'CRM_Activity_DAO_Activity', | |
120 | array( | |
121 | 'id' => 12345, | |
122 | 'name' => self::getFilePrefix() . 'exampleFromContent.txt', | |
123 | 'mime_type' => 'text/plain', | |
124 | 'description' => 'My test description', | |
125 | 'content' => 'My test content', | |
126 | ), | |
127 | '/Invalid ID/', | |
128 | ); | |
129 | $cases[] = array( | |
130 | 'CRM_Activity_DAO_Activity', | |
131 | array( | |
132 | 'name' => self::getFilePrefix() . 'failedExample.txt', | |
133 | 'mime_type' => 'text/plain', | |
134 | 'description' => 'My test description', | |
135 | ), | |
136 | "/Mandatory key\\(s\\) missing from params array: 'id' or 'content' or 'options.move-file'/", | |
137 | ); | |
138 | $cases[] = array( | |
139 | 'CRM_Activity_DAO_Activity', | |
140 | array( | |
141 | 'name' => self::getFilePrefix() . 'failedExample.txt', | |
142 | 'mime_type' => 'text/plain', | |
143 | 'description' => 'My test description', | |
144 | 'content' => 'too much content', | |
145 | 'options' => array( | |
21dfd5f5 | 146 | 'move-file' => $this->tmpFile('too-much.txt'), |
56154d36 TO |
147 | ), |
148 | ), | |
149 | "/'content' and 'options.move-file' are mutually exclusive/", | |
150 | ); | |
151 | $cases[] = array( | |
152 | 'CRM_Activity_DAO_Activity', | |
153 | array( | |
154 | 'name' => 'inv/alid.txt', | |
155 | 'mime_type' => 'text/plain', | |
156 | 'description' => 'My test description', | |
157 | 'content' => 'My test content', | |
158 | ), | |
159 | "/Malformed name/", | |
160 | ); | |
161 | $cases[] = array( | |
162 | 'CRM_Core_DAO_Domain', | |
163 | array( | |
164 | 'name' => self::getFilePrefix() . 'exampleFromContent.txt', | |
165 | 'mime_type' => 'text/plain', | |
166 | 'description' => 'My test description', | |
167 | 'content' => 'My test content', | |
168 | ), | |
169 | "/Unrecognized target entity/", | |
170 | ); | |
171 | ||
172 | return $cases; | |
173 | } | |
174 | ||
f0be539a EM |
175 | /** |
176 | * @return array | |
177 | */ | |
56154d36 TO |
178 | public function badUpdateProvider() { |
179 | $cases = array(); // array($entityClass, $createParams, $updateParams, $expectedError) | |
180 | ||
181 | $readOnlyFields = array( | |
182 | 'name' => 'newname.txt', | |
183 | 'entity_table' => 'civicrm_domain', | |
184 | 'entity_id' => 5, | |
185 | 'upload_date' => '2010-11-12 13:14:15', | |
186 | ); | |
187 | foreach ($readOnlyFields as $readOnlyField => $newValue) { | |
188 | $cases[] = array( | |
189 | 'CRM_Activity_DAO_Activity', | |
190 | array( | |
191 | 'name' => self::getFilePrefix() . 'exampleFromContent.txt', | |
192 | 'mime_type' => 'text/plain', | |
193 | 'description' => 'My test description', | |
194 | 'content' => 'My test content', | |
195 | ), | |
196 | array( | |
197 | 'check_permissions' => 1, | |
198 | $readOnlyField => $newValue, | |
199 | ), | |
21dfd5f5 | 200 | "/Cannot modify $readOnlyField/", |
56154d36 TO |
201 | ); |
202 | } | |
203 | ||
204 | return $cases; | |
205 | } | |
206 | ||
f0be539a EM |
207 | /** |
208 | * @return array | |
209 | */ | |
56154d36 TO |
210 | public function okGetProvider() { |
211 | $cases = array(); // array($getParams, $expectedNames) | |
212 | ||
213 | // Each search runs in a DB which contains these attachments: | |
214 | // Activity #123: example_123.txt (text/plain) and example_123.csv (text/csv) | |
215 | // Activity #456: example_456.txt (text/plain) and example_456.csv (text/csv) | |
216 | ||
e6b62a18 TO |
217 | // NOTE: Searching across multiple records (w/o entity_id) is currently |
218 | // prohibited by DynamicFKAuthorization. The technique used to authorize requests | |
219 | // does not adapt well to such searches. | |
220 | ||
221 | //$cases[] = array( | |
222 | // array('entity_table' => 'civicrm_activity'), | |
223 | // array( | |
224 | // self::getFilePrefix() . 'example_123.csv', | |
225 | // self::getFilePrefix() . 'example_123.txt', | |
226 | // self::getFilePrefix() . 'example_456.csv', | |
227 | // self::getFilePrefix() . 'example_456.txt', | |
228 | // ), | |
229 | //); | |
230 | //$cases[] = array( | |
231 | // array('entity_table' => 'civicrm_activity', 'mime_type' => 'text/plain'), | |
232 | // array(self::getFilePrefix() . 'example_123.txt', self::getFilePrefix() . 'example_456.txt'), | |
233 | //); | |
234 | ||
56154d36 TO |
235 | $cases[] = array( |
236 | array('entity_table' => 'civicrm_activity', 'entity_id' => '123'), | |
237 | array(self::getFilePrefix() . 'example_123.txt', self::getFilePrefix() . 'example_123.csv'), | |
238 | ); | |
239 | $cases[] = array( | |
240 | array('entity_table' => 'civicrm_activity', 'entity_id' => '456'), | |
241 | array(self::getFilePrefix() . 'example_456.txt', self::getFilePrefix() . 'example_456.csv'), | |
242 | ); | |
243 | $cases[] = array( | |
244 | array('entity_table' => 'civicrm_activity', 'entity_id' => '456', 'mime_type' => 'text/csv'), | |
245 | array(self::getFilePrefix() . 'example_456.csv'), | |
246 | ); | |
247 | $cases[] = array( | |
248 | array('entity_table' => 'civicrm_activity', 'entity_id' => '456', 'mime_type' => 'text/html'), | |
249 | array(), | |
250 | ); | |
251 | $cases[] = array( | |
252 | array('entity_table' => 'civicrm_activity', 'entity_id' => '999'), | |
253 | array(), | |
254 | ); | |
255 | ||
256 | return $cases; | |
257 | } | |
258 | ||
f0be539a EM |
259 | /** |
260 | * @return array | |
261 | */ | |
56154d36 TO |
262 | public function badGetProvider() { |
263 | $cases = array(); // array($getParams, $expectedNames) | |
264 | ||
265 | // Each search runs in a DB which contains these attachments: | |
266 | // Activity #123: example_123.txt (text/plain) and example_123.csv (text/csv) | |
267 | // Activity #456: example_456.txt (text/plain) and example_456.csv (text/csv) | |
268 | ||
269 | $cases[] = array( | |
270 | array('check_permissions' => 1, 'mime_type' => 'text/plain'), | |
271 | "/Mandatory key\\(s\\) missing from params array: 'id' or 'entity_table'/", | |
272 | ); | |
273 | $cases[] = array( | |
274 | array('check_permissions' => 1, 'entity_id' => '123'), | |
275 | "/Mandatory key\\(s\\) missing from params array: 'id' or 'entity_table'/", | |
276 | ); | |
277 | $cases[] = array( | |
278 | array('check_permissions' => 1), | |
92915c55 | 279 | "/Mandatory key\\(s\\) missing from params array: 'id' or 'entity_table'/", |
56154d36 TO |
280 | ); |
281 | $cases[] = array( | |
e6b62a18 | 282 | array('entity_table' => 'civicrm_activity', 'entity_id' => '123', 'name' => 'example_456.csv'), |
56154d36 TO |
283 | "/Get by name is not currently supported/", |
284 | ); | |
285 | $cases[] = array( | |
e6b62a18 | 286 | array('entity_table' => 'civicrm_activity', 'entity_id' => '123', 'content' => 'test'), |
56154d36 TO |
287 | "/Get by content is not currently supported/", |
288 | ); | |
289 | $cases[] = array( | |
e6b62a18 | 290 | array('entity_table' => 'civicrm_activity', 'entity_id' => '123', 'path' => '/home/foo'), |
56154d36 TO |
291 | "/Get by path is not currently supported/", |
292 | ); | |
293 | $cases[] = array( | |
e6b62a18 | 294 | array('entity_table' => 'civicrm_activity', 'entity_id' => '123', 'url' => '/index.php'), |
56154d36 TO |
295 | "/Get by url is not currently supported/", |
296 | ); | |
297 | ||
298 | return $cases; | |
299 | } | |
300 | ||
301 | /** | |
fe482240 | 302 | * Create an attachment using "content" and then "get" the attachment. |
56154d36 | 303 | * |
e16033b4 TO |
304 | * @param string $testEntityClass |
305 | * E.g. "CRM_Core_DAO_Activity". | |
56154d36 TO |
306 | * @param array $createParams |
307 | * @param string $expectedContent | |
308 | * @dataProvider okCreateProvider | |
309 | */ | |
310 | public function testCreate($testEntityClass, $createParams, $expectedContent) { | |
311 | $entity = CRM_Core_DAO::createTestObject($testEntityClass); | |
312 | $entity_table = CRM_Core_DAO_AllCoreTables::getTableForClass($testEntityClass); | |
313 | $this->assertTrue(is_numeric($entity->id)); | |
314 | ||
315 | $createResult = $this->callAPISuccess('Attachment', 'create', $createParams + array( | |
316 | 'entity_table' => $entity_table, | |
317 | 'entity_id' => $entity->id, | |
318 | )); | |
319 | $fileId = $createResult['id']; | |
320 | $this->assertTrue(is_numeric($fileId)); | |
321 | $this->assertEquals($entity_table, $createResult['values'][$fileId]['entity_table']); | |
322 | $this->assertEquals($entity->id, $createResult['values'][$fileId]['entity_id']); | |
323 | $this->assertEquals('My test description', $createResult['values'][$fileId]['description']); | |
324 | $this->assertRegExp('/\d\d\d\d-\d\d-\d\d \d\d:\d\d:\d\d/', $createResult['values'][$fileId]['upload_date']); | |
325 | $this->assertTrue(!isset($createResult['values'][$fileId]['content'])); | |
326 | $this->assertTrue(!empty($createResult['values'][$fileId]['url'])); | |
327 | $this->assertAttachmentExistence(TRUE, $createResult); | |
328 | ||
329 | $getResult = $this->callAPISuccess('Attachment', 'get', array( | |
330 | 'entity_table' => $entity_table, | |
331 | 'entity_id' => $entity->id, | |
332 | )); | |
333 | $this->assertEquals(1, $getResult['count']); | |
334 | foreach (array('id', 'entity_table', 'entity_id', 'url') as $field) { | |
335 | $this->assertEquals($createResult['values'][$fileId][$field], $getResult['values'][$fileId][$field], "Expect field $field to match"); | |
336 | } | |
337 | $this->assertTrue(!isset($getResult['values'][$fileId]['content'])); | |
338 | ||
339 | $getResult2 = $this->callAPISuccess('Attachment', 'get', array( | |
340 | 'entity_table' => $entity_table, | |
341 | 'entity_id' => $entity->id, | |
342 | 'return' => array('content'), | |
343 | )); | |
344 | $this->assertEquals($expectedContent, $getResult2['values'][$fileId]['content']); | |
345 | foreach (array('id', 'entity_table', 'entity_id', 'url') as $field) { | |
346 | $this->assertEquals($createResult['values'][$fileId][$field], $getResult['values'][$fileId][$field], "Expect field $field to match"); | |
347 | } | |
348 | } | |
349 | ||
350 | /** | |
351 | * @param $testEntityClass | |
352 | * @param $createParams | |
353 | * @param $expectedError | |
354 | * @dataProvider badCreateProvider | |
355 | */ | |
356 | public function testCreateFailure($testEntityClass, $createParams, $expectedError) { | |
357 | $entity = CRM_Core_DAO::createTestObject($testEntityClass); | |
358 | $entity_table = CRM_Core_DAO_AllCoreTables::getTableForClass($testEntityClass); | |
359 | $this->assertTrue(is_numeric($entity->id)); | |
360 | ||
361 | $createResult = $this->callAPIFailure('Attachment', 'create', $createParams + array( | |
362 | 'entity_table' => $entity_table, | |
363 | 'entity_id' => $entity->id, | |
364 | )); | |
365 | $this->assertRegExp($expectedError, $createResult['error_message']); | |
366 | } | |
367 | ||
368 | /** | |
369 | * @param $testEntityClass | |
370 | * @param $createParams | |
371 | * @param $updateParams | |
372 | * @param $expectedError | |
373 | * @dataProvider badUpdateProvider | |
374 | */ | |
375 | public function testCreateWithBadUpdate($testEntityClass, $createParams, $updateParams, $expectedError) { | |
376 | $entity = CRM_Core_DAO::createTestObject($testEntityClass); | |
377 | $entity_table = CRM_Core_DAO_AllCoreTables::getTableForClass($testEntityClass); | |
378 | $this->assertTrue(is_numeric($entity->id)); | |
379 | ||
380 | $createResult = $this->callAPISuccess('Attachment', 'create', $createParams + array( | |
381 | 'entity_table' => $entity_table, | |
382 | 'entity_id' => $entity->id, | |
383 | )); | |
384 | $fileId = $createResult['id']; | |
385 | $this->assertTrue(is_numeric($fileId)); | |
386 | ||
387 | $updateResult = $this->callAPIFailure('Attachment', 'create', $updateParams + array( | |
388 | 'id' => $fileId, | |
389 | )); | |
390 | $this->assertRegExp($expectedError, $updateResult['error_message']); | |
391 | } | |
392 | ||
393 | /** | |
394 | * If one submits a weird file name, it should be automatically converted | |
395 | * to something safe. | |
396 | */ | |
397 | public function testCreateWithWeirdName() { | |
398 | $entity = CRM_Core_DAO::createTestObject('CRM_Activity_DAO_Activity'); | |
399 | $this->assertTrue(is_numeric($entity->id)); | |
400 | ||
401 | $createResult = $this->callAPISuccess('Attachment', 'create', array( | |
402 | 'name' => self::getFilePrefix() . 'weird:na"me.txt', | |
403 | 'mime_type' => 'text/plain', | |
404 | 'description' => 'My test description', | |
405 | 'content' => 'My test content', | |
406 | 'entity_table' => 'civicrm_activity', | |
407 | 'entity_id' => $entity->id, | |
408 | )); | |
409 | $fileId = $createResult['id']; | |
410 | $this->assertTrue(is_numeric($fileId)); | |
411 | $this->assertEquals(self::getFilePrefix() . 'weird_na_me.txt', $createResult['values'][$fileId]['name']); | |
412 | } | |
413 | ||
414 | /** | |
415 | * @param $getParams | |
416 | * @param $expectedNames | |
417 | * @dataProvider okGetProvider | |
418 | */ | |
419 | public function testGet($getParams, $expectedNames) { | |
420 | foreach (array(123, 456) as $entity_id) { | |
421 | foreach (array('text/plain' => '.txt', 'text/csv' => '.csv') as $mime => $ext) { | |
422 | $this->callAPISuccess('Attachment', 'create', array( | |
423 | 'name' => self::getFilePrefix() . 'example_' . $entity_id . $ext, | |
424 | 'mime_type' => $mime, | |
425 | 'description' => 'My test description', | |
426 | 'content' => 'My test content', | |
427 | 'entity_table' => 'civicrm_activity', | |
428 | 'entity_id' => $entity_id, | |
429 | )); | |
430 | } | |
431 | } | |
432 | ||
433 | $getResult = $this->callAPISuccess('Attachment', 'get', $getParams); | |
434 | $actualNames = array_values(CRM_Utils_Array::collect('name', $getResult['values'])); | |
435 | sort($actualNames); | |
436 | sort($expectedNames); | |
437 | $this->assertEquals($expectedNames, $actualNames); | |
438 | } | |
439 | ||
440 | /** | |
441 | * @param $getParams | |
442 | * @param $expectedError | |
443 | * @dataProvider badGetProvider | |
444 | */ | |
445 | public function testGetError($getParams, $expectedError) { | |
446 | foreach (array(123, 456) as $entity_id) { | |
447 | foreach (array('text/plain' => '.txt', 'text/csv' => '.csv') as $mime => $ext) { | |
448 | $this->callAPISuccess('Attachment', 'create', array( | |
449 | 'name' => self::getFilePrefix() . 'example_' . $entity_id . $ext, | |
450 | 'mime_type' => $mime, | |
451 | 'description' => 'My test description', | |
452 | 'content' => 'My test content', | |
453 | 'entity_table' => 'civicrm_activity', | |
454 | 'entity_id' => $entity_id, | |
455 | )); | |
456 | } | |
457 | } | |
458 | ||
459 | $getResult = $this->callAPIFailure('Attachment', 'get', $getParams); | |
460 | $this->assertRegExp($expectedError, $getResult['error_message']); | |
461 | } | |
462 | ||
463 | /** | |
464 | * Take the values from a "get", make a small change, and then send | |
465 | * the full thing back in as an update ("create"). This ensures some | |
466 | * consistency in the acceptable formats. | |
467 | */ | |
468 | public function testGetThenUpdate() { | |
469 | $entity = CRM_Core_DAO::createTestObject('CRM_Activity_DAO_Activity'); | |
470 | $this->assertTrue(is_numeric($entity->id)); | |
471 | ||
472 | $createResult = $this->callAPISuccess('Attachment', 'create', array( | |
473 | 'name' => self::getFilePrefix() . 'getThenUpdate.txt', | |
474 | 'mime_type' => 'text/plain', | |
475 | 'description' => 'My test description', | |
476 | 'content' => 'My test content', | |
477 | 'entity_table' => 'civicrm_activity', | |
478 | 'entity_id' => $entity->id, | |
479 | )); | |
480 | $fileId = $createResult['id']; | |
481 | $this->assertTrue(is_numeric($fileId)); | |
482 | $this->assertEquals(self::getFilePrefix() . 'getThenUpdate.txt', $createResult['values'][$fileId]['name']); | |
483 | $this->assertAttachmentExistence(TRUE, $createResult); | |
484 | ||
485 | $getResult = $this->callAPISuccess('Attachment', 'get', array( | |
486 | 'id' => $fileId, | |
487 | )); | |
488 | $this->assertTrue(is_array($getResult['values'][$fileId])); | |
489 | ||
490 | $updateParams = $getResult['values'][$fileId]; | |
491 | $updateParams['description'] = 'new description'; | |
492 | $this->callAPISuccess('Attachment', 'create', $updateParams); | |
493 | $this->assertAttachmentExistence(TRUE, $createResult); | |
494 | } | |
495 | ||
496 | /** | |
497 | * Create an attachment and delete using its ID. Assert that the records are correctly created and destroyed | |
498 | * in the DB and the filesystem. | |
499 | */ | |
500 | public function testDeleteByID() { | |
501 | $entity = CRM_Core_DAO::createTestObject('CRM_Activity_DAO_Activity'); | |
502 | $this->assertTrue(is_numeric($entity->id)); | |
503 | ||
504 | foreach (array('first', 'second') as $n) { | |
505 | $createResults[$n] = $this->callAPISuccess('Attachment', 'create', array( | |
506 | 'name' => self::getFilePrefix() . 'testDeleteByID.txt', | |
507 | 'mime_type' => 'text/plain', | |
508 | 'content' => 'My test content', | |
509 | 'entity_table' => 'civicrm_activity', | |
510 | 'entity_id' => $entity->id, | |
511 | )); | |
512 | $this->assertTrue(is_numeric($createResults[$n]['id'])); | |
513 | $this->assertEquals(self::getFilePrefix() . 'testDeleteByID.txt', $createResults[$n]['values'][$createResults[$n]['id']]['name']); | |
514 | } | |
515 | $this->assertAttachmentExistence(TRUE, $createResults['first']); | |
516 | $this->assertAttachmentExistence(TRUE, $createResults['second']); | |
517 | ||
518 | $this->callAPISuccess('Attachment', 'delete', array( | |
519 | 'id' => $createResults['first']['id'], | |
520 | )); | |
521 | $this->assertAttachmentExistence(FALSE, $createResults['first']); | |
522 | $this->assertAttachmentExistence(TRUE, $createResults['second']); | |
523 | } | |
524 | ||
525 | /** | |
526 | * Create an attachment and delete using its ID. Assert that the records are correctly created and destroyed | |
527 | * in the DB and the filesystem. | |
528 | */ | |
529 | public function testDeleteByEntity() { | |
530 | // create 2 entities (keepme,delme) -- each with 2 attachments (first,second) | |
531 | foreach (array('keepme', 'delme') as $e) { | |
532 | $entities[$e] = CRM_Core_DAO::createTestObject('CRM_Activity_DAO_Activity'); | |
533 | $this->assertTrue(is_numeric($entities[$e]->id)); | |
534 | foreach (array('first', 'second') as $n) { | |
535 | $createResults[$e][$n] = $this->callAPISuccess('Attachment', 'create', array( | |
536 | 'name' => self::getFilePrefix() . 'testDeleteByEntity.txt', | |
537 | 'mime_type' => 'text/plain', | |
538 | 'content' => 'My test content', | |
539 | 'entity_table' => 'civicrm_activity', | |
540 | 'entity_id' => $entities[$e]->id, | |
541 | )); | |
542 | $this->assertTrue(is_numeric($createResults[$e][$n]['id'])); | |
543 | } | |
544 | } | |
545 | $this->assertAttachmentExistence(TRUE, $createResults['keepme']['first']); | |
546 | $this->assertAttachmentExistence(TRUE, $createResults['keepme']['second']); | |
547 | $this->assertAttachmentExistence(TRUE, $createResults['delme']['first']); | |
548 | $this->assertAttachmentExistence(TRUE, $createResults['delme']['second']); | |
549 | ||
550 | $this->callAPISuccess('Attachment', 'delete', array( | |
551 | 'entity_table' => 'civicrm_activity', | |
552 | 'entity_id' => $entities[$e]->id, | |
553 | )); | |
554 | $this->assertAttachmentExistence(TRUE, $createResults['keepme']['first']); | |
555 | $this->assertAttachmentExistence(TRUE, $createResults['keepme']['second']); | |
556 | $this->assertAttachmentExistence(FALSE, $createResults['delme']['first']); | |
557 | $this->assertAttachmentExistence(FALSE, $createResults['delme']['second']); | |
558 | } | |
559 | ||
f0be539a EM |
560 | /** |
561 | * @param $name | |
562 | * @return string | |
563 | */ | |
56154d36 TO |
564 | protected function tmpFile($name) { |
565 | $tmpDir = sys_get_temp_dir(); | |
566 | $this->assertTrue($tmpDir && is_dir($tmpDir), 'Tmp dir must exist: ' . $tmpDir); | |
567 | return $tmpDir . '/' . self::getFilePrefix() . $name; | |
568 | } | |
569 | ||
570 | protected function cleanupFiles() { | |
571 | $config = CRM_Core_Config::singleton(); | |
572 | $dirs = array( | |
573 | sys_get_temp_dir(), | |
574 | $config->customFileUploadDir, | |
575 | ); | |
576 | foreach ($dirs as $dir) { | |
577 | $files = (array) glob($dir . "/" . self::getFilePrefix() . "*"); | |
578 | foreach ($files as $file) { | |
579 | unlink($file); | |
580 | } | |
581 | ||
582 | } | |
583 | } | |
96025800 | 584 | |
56154d36 | 585 | } |