Merge pull request #13926 from pradpnayak/NoticeErrorProfile
[civicrm-core.git] / tests / phpunit / api / v3 / AttachmentTest.php
CommitLineData
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
56154d36
TO
28/**
29 * Test for the Attachment API
30 *
31 * @package CiviCRM_APIv3
32 * @subpackage API_Contact
acb109b7 33 * @group headless
56154d36
TO
34 */
35class api_v3_AttachmentTest extends CiviUnitTestCase {
36 protected static $filePrefix = NULL;
37
f0be539a
EM
38 /**
39 * @return string
40 */
56154d36
TO
41 public static function getFilePrefix() {
42 if (!self::$filePrefix) {
43 self::$filePrefix = "test_" . CRM_Utils_String::createRandom(5, CRM_Utils_String::ALPHANUMERIC) . '_';
44 }
45 return self::$filePrefix;
46 }
47
48
49 protected function setUp() {
50 parent::setUp();
51 $this->useTransaction(TRUE);
52
53 $this->cleanupFiles();
54 file_put_contents($this->tmpFile('mytest.txt'), 'This comes from a file');
55 }
56
57 protected function tearDown() {
58 parent::tearDown();
59 $this->cleanupFiles();
048222df 60 \Civi::reset();
56154d36
TO
61 }
62
f0be539a
EM
63 /**
64 * @return array
65 */
56154d36
TO
66 public function okCreateProvider() {
67 $cases = array(); // array($entityClass, $createParams, $expectedContent)
68
69 $cases[] = array(
70 'CRM_Activity_DAO_Activity',
71 array(
72 'name' => self::getFilePrefix() . 'exampleFromContent.txt',
73 'mime_type' => 'text/plain',
74 'description' => 'My test description',
75 'content' => 'My test content',
76 ),
77 'My test content',
78 );
79
80 $cases[] = array(
81 'CRM_Activity_DAO_Activity',
82 array(
83 'name' => self::getFilePrefix() . 'exampleWithEmptyContent.txt',
84 'mime_type' => 'text/plain',
85 'description' => 'My test description',
86 'content' => '',
87 ),
88 '',
89 );
90
91 $cases[] = array(
92 'CRM_Activity_DAO_Activity',
93 array(
94 'name' => self::getFilePrefix() . 'exampleFromMove.txt',
95 'mime_type' => 'text/plain',
96 'description' => 'My test description',
97 'options' => array(
98 'move-file' => $this->tmpFile('mytest.txt'),
21dfd5f5 99 ),
56154d36
TO
100 ),
101 'This comes from a file',
102 );
103
104 return $cases;
105 }
106
f0be539a
EM
107 /**
108 * @return array
109 */
56154d36
TO
110 public function badCreateProvider() {
111 $cases = array(); // array($entityClass, $createParams, $expectedError)
112
113 $cases[] = array(
114 'CRM_Activity_DAO_Activity',
115 array(
116 'id' => 12345,
117 'name' => self::getFilePrefix() . 'exampleFromContent.txt',
118 'mime_type' => 'text/plain',
119 'description' => 'My test description',
120 'content' => 'My test content',
121 ),
122 '/Invalid ID/',
123 );
124 $cases[] = array(
125 'CRM_Activity_DAO_Activity',
126 array(
127 'name' => self::getFilePrefix() . 'failedExample.txt',
128 'mime_type' => 'text/plain',
129 'description' => 'My test description',
130 ),
131 "/Mandatory key\\(s\\) missing from params array: 'id' or 'content' or 'options.move-file'/",
132 );
133 $cases[] = array(
134 'CRM_Activity_DAO_Activity',
135 array(
136 'name' => self::getFilePrefix() . 'failedExample.txt',
137 'mime_type' => 'text/plain',
138 'description' => 'My test description',
139 'content' => 'too much content',
140 'options' => array(
21dfd5f5 141 'move-file' => $this->tmpFile('too-much.txt'),
56154d36
TO
142 ),
143 ),
144 "/'content' and 'options.move-file' are mutually exclusive/",
145 );
146 $cases[] = array(
147 'CRM_Activity_DAO_Activity',
148 array(
149 'name' => 'inv/alid.txt',
150 'mime_type' => 'text/plain',
151 'description' => 'My test description',
152 'content' => 'My test content',
153 ),
154 "/Malformed name/",
155 );
156 $cases[] = array(
157 'CRM_Core_DAO_Domain',
158 array(
159 'name' => self::getFilePrefix() . 'exampleFromContent.txt',
160 'mime_type' => 'text/plain',
161 'description' => 'My test description',
162 'content' => 'My test content',
2ffa31a8 163 'check_permissions' => 1,
56154d36
TO
164 ),
165 "/Unrecognized target entity/",
166 );
167
168 return $cases;
169 }
170
f0be539a
EM
171 /**
172 * @return array
173 */
56154d36
TO
174 public function badUpdateProvider() {
175 $cases = array(); // array($entityClass, $createParams, $updateParams, $expectedError)
176
177 $readOnlyFields = array(
178 'name' => 'newname.txt',
179 'entity_table' => 'civicrm_domain',
180 'entity_id' => 5,
181 'upload_date' => '2010-11-12 13:14:15',
182 );
183 foreach ($readOnlyFields as $readOnlyField => $newValue) {
184 $cases[] = array(
185 'CRM_Activity_DAO_Activity',
186 array(
187 'name' => self::getFilePrefix() . 'exampleFromContent.txt',
188 'mime_type' => 'text/plain',
189 'description' => 'My test description',
190 'content' => 'My test content',
191 ),
192 array(
193 'check_permissions' => 1,
194 $readOnlyField => $newValue,
195 ),
21dfd5f5 196 "/Cannot modify $readOnlyField/",
56154d36
TO
197 );
198 }
199
200 return $cases;
201 }
202
f0be539a
EM
203 /**
204 * @return array
205 */
56154d36
TO
206 public function okGetProvider() {
207 $cases = array(); // array($getParams, $expectedNames)
208
209 // Each search runs in a DB which contains these attachments:
210 // Activity #123: example_123.txt (text/plain) and example_123.csv (text/csv)
211 // Activity #456: example_456.txt (text/plain) and example_456.csv (text/csv)
212
e6b62a18
TO
213 // NOTE: Searching across multiple records (w/o entity_id) is currently
214 // prohibited by DynamicFKAuthorization. The technique used to authorize requests
215 // does not adapt well to such searches.
216
217 //$cases[] = array(
218 // array('entity_table' => 'civicrm_activity'),
219 // array(
220 // self::getFilePrefix() . 'example_123.csv',
221 // self::getFilePrefix() . 'example_123.txt',
222 // self::getFilePrefix() . 'example_456.csv',
223 // self::getFilePrefix() . 'example_456.txt',
224 // ),
225 //);
226 //$cases[] = array(
227 // array('entity_table' => 'civicrm_activity', 'mime_type' => 'text/plain'),
228 // array(self::getFilePrefix() . 'example_123.txt', self::getFilePrefix() . 'example_456.txt'),
229 //);
230
56154d36
TO
231 $cases[] = array(
232 array('entity_table' => 'civicrm_activity', 'entity_id' => '123'),
233 array(self::getFilePrefix() . 'example_123.txt', self::getFilePrefix() . 'example_123.csv'),
234 );
235 $cases[] = array(
236 array('entity_table' => 'civicrm_activity', 'entity_id' => '456'),
237 array(self::getFilePrefix() . 'example_456.txt', self::getFilePrefix() . 'example_456.csv'),
238 );
239 $cases[] = array(
240 array('entity_table' => 'civicrm_activity', 'entity_id' => '456', 'mime_type' => 'text/csv'),
241 array(self::getFilePrefix() . 'example_456.csv'),
242 );
243 $cases[] = array(
244 array('entity_table' => 'civicrm_activity', 'entity_id' => '456', 'mime_type' => 'text/html'),
245 array(),
246 );
247 $cases[] = array(
248 array('entity_table' => 'civicrm_activity', 'entity_id' => '999'),
249 array(),
250 );
251
252 return $cases;
253 }
254
f0be539a
EM
255 /**
256 * @return array
257 */
56154d36
TO
258 public function badGetProvider() {
259 $cases = array(); // array($getParams, $expectedNames)
260
261 // Each search runs in a DB which contains these attachments:
262 // Activity #123: example_123.txt (text/plain) and example_123.csv (text/csv)
263 // Activity #456: example_456.txt (text/plain) and example_456.csv (text/csv)
264
265 $cases[] = array(
266 array('check_permissions' => 1, 'mime_type' => 'text/plain'),
267 "/Mandatory key\\(s\\) missing from params array: 'id' or 'entity_table'/",
268 );
269 $cases[] = array(
270 array('check_permissions' => 1, 'entity_id' => '123'),
271 "/Mandatory key\\(s\\) missing from params array: 'id' or 'entity_table'/",
272 );
273 $cases[] = array(
274 array('check_permissions' => 1),
92915c55 275 "/Mandatory key\\(s\\) missing from params array: 'id' or 'entity_table'/",
56154d36
TO
276 );
277 $cases[] = array(
e6b62a18 278 array('entity_table' => 'civicrm_activity', 'entity_id' => '123', 'name' => 'example_456.csv'),
56154d36
TO
279 "/Get by name is not currently supported/",
280 );
281 $cases[] = array(
e6b62a18 282 array('entity_table' => 'civicrm_activity', 'entity_id' => '123', 'content' => 'test'),
56154d36
TO
283 "/Get by content is not currently supported/",
284 );
285 $cases[] = array(
e6b62a18 286 array('entity_table' => 'civicrm_activity', 'entity_id' => '123', 'path' => '/home/foo'),
56154d36
TO
287 "/Get by path is not currently supported/",
288 );
289 $cases[] = array(
e6b62a18 290 array('entity_table' => 'civicrm_activity', 'entity_id' => '123', 'url' => '/index.php'),
56154d36
TO
291 "/Get by url is not currently supported/",
292 );
293
294 return $cases;
295 }
296
297 /**
fe482240 298 * Create an attachment using "content" and then "get" the attachment.
56154d36 299 *
e16033b4
TO
300 * @param string $testEntityClass
301 * E.g. "CRM_Core_DAO_Activity".
56154d36
TO
302 * @param array $createParams
303 * @param string $expectedContent
304 * @dataProvider okCreateProvider
305 */
306 public function testCreate($testEntityClass, $createParams, $expectedContent) {
307 $entity = CRM_Core_DAO::createTestObject($testEntityClass);
308 $entity_table = CRM_Core_DAO_AllCoreTables::getTableForClass($testEntityClass);
309 $this->assertTrue(is_numeric($entity->id));
310
311 $createResult = $this->callAPISuccess('Attachment', 'create', $createParams + array(
312 'entity_table' => $entity_table,
313 'entity_id' => $entity->id,
314 ));
315 $fileId = $createResult['id'];
316 $this->assertTrue(is_numeric($fileId));
317 $this->assertEquals($entity_table, $createResult['values'][$fileId]['entity_table']);
318 $this->assertEquals($entity->id, $createResult['values'][$fileId]['entity_id']);
319 $this->assertEquals('My test description', $createResult['values'][$fileId]['description']);
320 $this->assertRegExp('/\d\d\d\d-\d\d-\d\d \d\d:\d\d:\d\d/', $createResult['values'][$fileId]['upload_date']);
321 $this->assertTrue(!isset($createResult['values'][$fileId]['content']));
322 $this->assertTrue(!empty($createResult['values'][$fileId]['url']));
323 $this->assertAttachmentExistence(TRUE, $createResult);
324
325 $getResult = $this->callAPISuccess('Attachment', 'get', array(
326 'entity_table' => $entity_table,
327 'entity_id' => $entity->id,
328 ));
329 $this->assertEquals(1, $getResult['count']);
330 foreach (array('id', 'entity_table', 'entity_id', 'url') as $field) {
331 $this->assertEquals($createResult['values'][$fileId][$field], $getResult['values'][$fileId][$field], "Expect field $field to match");
332 }
333 $this->assertTrue(!isset($getResult['values'][$fileId]['content']));
334
335 $getResult2 = $this->callAPISuccess('Attachment', 'get', array(
336 'entity_table' => $entity_table,
337 'entity_id' => $entity->id,
338 'return' => array('content'),
339 ));
340 $this->assertEquals($expectedContent, $getResult2['values'][$fileId]['content']);
341 foreach (array('id', 'entity_table', 'entity_id', 'url') as $field) {
342 $this->assertEquals($createResult['values'][$fileId][$field], $getResult['values'][$fileId][$field], "Expect field $field to match");
343 }
344 }
345
346 /**
347 * @param $testEntityClass
348 * @param $createParams
349 * @param $expectedError
350 * @dataProvider badCreateProvider
351 */
352 public function testCreateFailure($testEntityClass, $createParams, $expectedError) {
353 $entity = CRM_Core_DAO::createTestObject($testEntityClass);
354 $entity_table = CRM_Core_DAO_AllCoreTables::getTableForClass($testEntityClass);
355 $this->assertTrue(is_numeric($entity->id));
356
357 $createResult = $this->callAPIFailure('Attachment', 'create', $createParams + array(
358 'entity_table' => $entity_table,
359 'entity_id' => $entity->id,
360 ));
361 $this->assertRegExp($expectedError, $createResult['error_message']);
362 }
363
364 /**
365 * @param $testEntityClass
366 * @param $createParams
367 * @param $updateParams
368 * @param $expectedError
369 * @dataProvider badUpdateProvider
370 */
371 public function testCreateWithBadUpdate($testEntityClass, $createParams, $updateParams, $expectedError) {
372 $entity = CRM_Core_DAO::createTestObject($testEntityClass);
373 $entity_table = CRM_Core_DAO_AllCoreTables::getTableForClass($testEntityClass);
374 $this->assertTrue(is_numeric($entity->id));
375
376 $createResult = $this->callAPISuccess('Attachment', 'create', $createParams + array(
377 'entity_table' => $entity_table,
378 'entity_id' => $entity->id,
379 ));
380 $fileId = $createResult['id'];
381 $this->assertTrue(is_numeric($fileId));
382
383 $updateResult = $this->callAPIFailure('Attachment', 'create', $updateParams + array(
384 'id' => $fileId,
385 ));
386 $this->assertRegExp($expectedError, $updateResult['error_message']);
387 }
388
389 /**
390 * If one submits a weird file name, it should be automatically converted
391 * to something safe.
392 */
393 public function testCreateWithWeirdName() {
394 $entity = CRM_Core_DAO::createTestObject('CRM_Activity_DAO_Activity');
395 $this->assertTrue(is_numeric($entity->id));
396
397 $createResult = $this->callAPISuccess('Attachment', 'create', array(
398 'name' => self::getFilePrefix() . 'weird:na"me.txt',
399 'mime_type' => 'text/plain',
400 'description' => 'My test description',
401 'content' => 'My test content',
402 'entity_table' => 'civicrm_activity',
403 'entity_id' => $entity->id,
404 ));
405 $fileId = $createResult['id'];
406 $this->assertTrue(is_numeric($fileId));
407 $this->assertEquals(self::getFilePrefix() . 'weird_na_me.txt', $createResult['values'][$fileId]['name']);
4994819e
CW
408 // Check for appropriate icon
409 $this->assertEquals('fa-file-text-o', $createResult['values'][$fileId]['icon']);
56154d36
TO
410 }
411
7aab0058
DA
412 public function testCreateShouldSetCreatedIdAsTheLoggedInUser() {
413 $loggedInUser = $this->createLoggedInUser();
414
415 $testEntityClass = 'CRM_Activity_DAO_Activity';
416 $entity = CRM_Core_DAO::createTestObject($testEntityClass);
417 $entity_table = CRM_Core_DAO_AllCoreTables::getTableForClass($testEntityClass);
418 $this->assertTrue(is_numeric($entity->id));
419
420 $createResult = $this->callAPISuccess('Attachment', 'create', array(
421 'name' => self::getFilePrefix() . 'exampleFromContent.txt',
422 'mime_type' => 'text/plain',
423 'content' => 'My test content',
424 'entity_table' => $entity_table,
425 'entity_id' => $entity->id,
426 ));
427
428 $fileId = $createResult['id'];
429 $this->assertEquals($loggedInUser, $createResult['values'][$fileId]['created_id']);
430 }
431
432 public function testCreateShouldKeepCreatedIdEmptyIfTheresNoLoggedInUser() {
433 $testEntityClass = 'CRM_Activity_DAO_Activity';
434 $entity = CRM_Core_DAO::createTestObject($testEntityClass);
435 $entity_table = CRM_Core_DAO_AllCoreTables::getTableForClass($testEntityClass);
436 $this->assertTrue(is_numeric($entity->id));
437
438 $createResult = $this->callAPISuccess('Attachment', 'create', array(
439 'name' => self::getFilePrefix() . 'exampleFromContent.txt',
440 'mime_type' => 'text/plain',
441 'content' => 'My test content',
442 'entity_table' => $entity_table,
443 'entity_id' => $entity->id,
444 ));
445
446 $fileId = $createResult['id'];
447 $this->assertEmpty($createResult['values'][$fileId]['created_id']);
448 }
449
450 public function testCreateShouldNotUpdateTheCreatedId() {
451 $testEntityClass = 'CRM_Activity_DAO_Activity';
452 $entity = CRM_Core_DAO::createTestObject($testEntityClass);
453 $entity_table = CRM_Core_DAO_AllCoreTables::getTableForClass($testEntityClass);
454 $this->assertTrue(is_numeric($entity->id));
455
456 $attachmentParams = array(
457 'name' => self::getFilePrefix() . 'exampleFromContent.txt',
458 'mime_type' => 'text/plain',
459 'description' => 'My test description',
460 'content' => 'My test content',
461 'entity_table' => $entity_table,
462 'entity_id' => $entity->id,
463 );
464
465 $createResult = $this->callAPISuccess('Attachment', 'create', $attachmentParams);
466
467 $fileId = $createResult['id'];
468 $this->assertEmpty($createResult['values'][$fileId]['created_id']);
469
470 $attachmentParams['id'] = $fileId;
471 $attachmentParams['description'] = 'My updated description';
472
473 $loggedInUser = $this->createLoggedInUser();
474
475 $this->callAPISuccess('Attachment', 'create', $attachmentParams);
476
477 $updatedAttachment = $this->callAPISuccess('Attachment', 'get', array(
478 'id' => $fileId,
479 'entity_id' => $attachmentParams['entity_id'],
ae2c7e00 480 'entity_table' => $attachmentParams['entity_table'],
7aab0058
DA
481 ));
482
483 $this->assertNotEmpty($loggedInUser);
484 $this->assertEmpty($updatedAttachment['values'][$fileId]['created_id']);
485 $this->assertEquals($attachmentParams['description'], $updatedAttachment['values'][$fileId]['description']);
486 }
487
56154d36
TO
488 /**
489 * @param $getParams
490 * @param $expectedNames
491 * @dataProvider okGetProvider
492 */
493 public function testGet($getParams, $expectedNames) {
494 foreach (array(123, 456) as $entity_id) {
495 foreach (array('text/plain' => '.txt', 'text/csv' => '.csv') as $mime => $ext) {
496 $this->callAPISuccess('Attachment', 'create', array(
497 'name' => self::getFilePrefix() . 'example_' . $entity_id . $ext,
498 'mime_type' => $mime,
499 'description' => 'My test description',
500 'content' => 'My test content',
501 'entity_table' => 'civicrm_activity',
502 'entity_id' => $entity_id,
503 ));
504 }
505 }
506
507 $getResult = $this->callAPISuccess('Attachment', 'get', $getParams);
508 $actualNames = array_values(CRM_Utils_Array::collect('name', $getResult['values']));
ff9aeadb
SL
509 // Verify the hash generated by the API is valid if we were to try and load the file.
510 foreach ($getResult['values'] as $result) {
511 $queryResult = [];
512 $parsedURl = parse_url($result['url']);
513 $parsedQuery = parse_str($parsedURl['query'], $queryResult);
c5c40df7 514 $this->assertTrue(CRM_Core_BAO_File::validateFileHash($queryResult['fcs'], $queryResult['eid'], $queryResult['id']));
ff9aeadb
SL
515 }
516
56154d36
TO
517 sort($actualNames);
518 sort($expectedNames);
519 $this->assertEquals($expectedNames, $actualNames);
520 }
521
522 /**
523 * @param $getParams
524 * @param $expectedError
525 * @dataProvider badGetProvider
526 */
527 public function testGetError($getParams, $expectedError) {
528 foreach (array(123, 456) as $entity_id) {
529 foreach (array('text/plain' => '.txt', 'text/csv' => '.csv') as $mime => $ext) {
530 $this->callAPISuccess('Attachment', 'create', array(
531 'name' => self::getFilePrefix() . 'example_' . $entity_id . $ext,
532 'mime_type' => $mime,
533 'description' => 'My test description',
534 'content' => 'My test content',
535 'entity_table' => 'civicrm_activity',
536 'entity_id' => $entity_id,
537 ));
538 }
539 }
540
541 $getResult = $this->callAPIFailure('Attachment', 'get', $getParams);
542 $this->assertRegExp($expectedError, $getResult['error_message']);
543 }
544
545 /**
546 * Take the values from a "get", make a small change, and then send
547 * the full thing back in as an update ("create"). This ensures some
548 * consistency in the acceptable formats.
549 */
550 public function testGetThenUpdate() {
551 $entity = CRM_Core_DAO::createTestObject('CRM_Activity_DAO_Activity');
552 $this->assertTrue(is_numeric($entity->id));
553
554 $createResult = $this->callAPISuccess('Attachment', 'create', array(
555 'name' => self::getFilePrefix() . 'getThenUpdate.txt',
556 'mime_type' => 'text/plain',
557 'description' => 'My test description',
558 'content' => 'My test content',
559 'entity_table' => 'civicrm_activity',
560 'entity_id' => $entity->id,
561 ));
562 $fileId = $createResult['id'];
563 $this->assertTrue(is_numeric($fileId));
564 $this->assertEquals(self::getFilePrefix() . 'getThenUpdate.txt', $createResult['values'][$fileId]['name']);
565 $this->assertAttachmentExistence(TRUE, $createResult);
566
567 $getResult = $this->callAPISuccess('Attachment', 'get', array(
568 'id' => $fileId,
569 ));
570 $this->assertTrue(is_array($getResult['values'][$fileId]));
571
572 $updateParams = $getResult['values'][$fileId];
573 $updateParams['description'] = 'new description';
574 $this->callAPISuccess('Attachment', 'create', $updateParams);
575 $this->assertAttachmentExistence(TRUE, $createResult);
576 }
577
578 /**
579 * Create an attachment and delete using its ID. Assert that the records are correctly created and destroyed
580 * in the DB and the filesystem.
581 */
582 public function testDeleteByID() {
583 $entity = CRM_Core_DAO::createTestObject('CRM_Activity_DAO_Activity');
584 $this->assertTrue(is_numeric($entity->id));
585
586 foreach (array('first', 'second') as $n) {
587 $createResults[$n] = $this->callAPISuccess('Attachment', 'create', array(
588 'name' => self::getFilePrefix() . 'testDeleteByID.txt',
589 'mime_type' => 'text/plain',
590 'content' => 'My test content',
591 'entity_table' => 'civicrm_activity',
592 'entity_id' => $entity->id,
593 ));
594 $this->assertTrue(is_numeric($createResults[$n]['id']));
595 $this->assertEquals(self::getFilePrefix() . 'testDeleteByID.txt', $createResults[$n]['values'][$createResults[$n]['id']]['name']);
596 }
597 $this->assertAttachmentExistence(TRUE, $createResults['first']);
598 $this->assertAttachmentExistence(TRUE, $createResults['second']);
599
600 $this->callAPISuccess('Attachment', 'delete', array(
601 'id' => $createResults['first']['id'],
602 ));
603 $this->assertAttachmentExistence(FALSE, $createResults['first']);
604 $this->assertAttachmentExistence(TRUE, $createResults['second']);
605 }
606
607 /**
608 * Create an attachment and delete using its ID. Assert that the records are correctly created and destroyed
609 * in the DB and the filesystem.
610 */
611 public function testDeleteByEntity() {
612 // create 2 entities (keepme,delme) -- each with 2 attachments (first,second)
613 foreach (array('keepme', 'delme') as $e) {
614 $entities[$e] = CRM_Core_DAO::createTestObject('CRM_Activity_DAO_Activity');
615 $this->assertTrue(is_numeric($entities[$e]->id));
616 foreach (array('first', 'second') as $n) {
617 $createResults[$e][$n] = $this->callAPISuccess('Attachment', 'create', array(
618 'name' => self::getFilePrefix() . 'testDeleteByEntity.txt',
619 'mime_type' => 'text/plain',
620 'content' => 'My test content',
621 'entity_table' => 'civicrm_activity',
622 'entity_id' => $entities[$e]->id,
623 ));
624 $this->assertTrue(is_numeric($createResults[$e][$n]['id']));
625 }
626 }
627 $this->assertAttachmentExistence(TRUE, $createResults['keepme']['first']);
628 $this->assertAttachmentExistence(TRUE, $createResults['keepme']['second']);
629 $this->assertAttachmentExistence(TRUE, $createResults['delme']['first']);
630 $this->assertAttachmentExistence(TRUE, $createResults['delme']['second']);
631
632 $this->callAPISuccess('Attachment', 'delete', array(
633 'entity_table' => 'civicrm_activity',
634 'entity_id' => $entities[$e]->id,
635 ));
636 $this->assertAttachmentExistence(TRUE, $createResults['keepme']['first']);
637 $this->assertAttachmentExistence(TRUE, $createResults['keepme']['second']);
638 $this->assertAttachmentExistence(FALSE, $createResults['delme']['first']);
639 $this->assertAttachmentExistence(FALSE, $createResults['delme']['second']);
640 }
641
4994819e
CW
642 /**
643 * Ensure mime type is converted to appropriate icon.
644 */
645 public function testGetIcon() {
646 $entity = CRM_Core_DAO::createTestObject('CRM_Activity_DAO_Activity');
647 $this->assertTrue(is_numeric($entity->id));
648
649 $createResult = $this->callAPISuccess('Attachment', 'create', array(
650 'name' => self::getFilePrefix() . 'hasIcon.docx',
651 'mime_type' => 'application/vnd.openxmlformats-officedocument.wordprocessingml.document',
652 'description' => 'My test description',
653 'content' => 'My test content',
654 'entity_table' => 'civicrm_activity',
655 'entity_id' => $entity->id,
656 ));
657 $fileId = $createResult['id'];
658 $this->assertEquals('fa-file-word-o', $createResult['values'][$fileId]['icon']);
892be376 659
4994819e
CW
660 $createResult = $this->callAPISuccess('Attachment', 'create', array(
661 'name' => self::getFilePrefix() . 'hasIcon.jpg',
662 'mime_type' => 'image/jpg',
663 'description' => 'My test description',
664 'content' => 'My test content',
665 'entity_table' => 'civicrm_activity',
666 'entity_id' => $entity->id,
667 ));
668 $fileId = $createResult['id'];
669 $this->assertEquals('fa-file-image-o', $createResult['values'][$fileId]['icon']);
670 }
671
f0be539a
EM
672 /**
673 * @param $name
674 * @return string
675 */
56154d36
TO
676 protected function tmpFile($name) {
677 $tmpDir = sys_get_temp_dir();
678 $this->assertTrue($tmpDir && is_dir($tmpDir), 'Tmp dir must exist: ' . $tmpDir);
679 return $tmpDir . '/' . self::getFilePrefix() . $name;
680 }
681
682 protected function cleanupFiles() {
683 $config = CRM_Core_Config::singleton();
684 $dirs = array(
685 sys_get_temp_dir(),
686 $config->customFileUploadDir,
687 );
688 foreach ($dirs as $dir) {
689 $files = (array) glob($dir . "/" . self::getFilePrefix() . "*");
690 foreach ($files as $file) {
691 unlink($file);
692 }
693
694 }
695 }
96025800 696
56154d36 697}