3 * api_v3_AttachmentTest
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 $
11 * This file is part of CiviCRM
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.
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.
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/>.
29 * Include class definitions
31 require_once 'CiviTest/CiviUnitTestCase.php';
35 * Test for the Attachment API
37 * @package CiviCRM_APIv3
38 * @subpackage API_Contact
40 class api_v3_AttachmentTest
extends CiviUnitTestCase
{
41 protected static $filePrefix = NULL;
46 public static function getFilePrefix() {
47 if (!self
::$filePrefix) {
48 self
::$filePrefix = "test_" . CRM_Utils_String
::createRandom(5, CRM_Utils_String
::ALPHANUMERIC
) . '_';
50 return self
::$filePrefix;
54 protected function setUp() {
56 $this->useTransaction(TRUE);
58 $this->cleanupFiles();
59 file_put_contents($this->tmpFile('mytest.txt'), 'This comes from a file');
62 protected function tearDown() {
64 $this->cleanupFiles();
71 public function okCreateProvider() {
72 $cases = array(); // array($entityClass, $createParams, $expectedContent)
75 'CRM_Activity_DAO_Activity',
77 'name' => self
::getFilePrefix() . 'exampleFromContent.txt',
78 'mime_type' => 'text/plain',
79 'description' => 'My test description',
80 'content' => 'My test content',
86 'CRM_Activity_DAO_Activity',
88 'name' => self
::getFilePrefix() . 'exampleWithEmptyContent.txt',
89 'mime_type' => 'text/plain',
90 'description' => 'My test description',
97 'CRM_Activity_DAO_Activity',
99 'name' => self
::getFilePrefix() . 'exampleFromMove.txt',
100 'mime_type' => 'text/plain',
101 'description' => 'My test description',
103 'move-file' => $this->tmpFile('mytest.txt'),
106 'This comes from a file',
115 public function badCreateProvider() {
116 $cases = array(); // array($entityClass, $createParams, $expectedError)
119 'CRM_Activity_DAO_Activity',
122 'name' => self
::getFilePrefix() . 'exampleFromContent.txt',
123 'mime_type' => 'text/plain',
124 'description' => 'My test description',
125 'content' => 'My test content',
130 'CRM_Activity_DAO_Activity',
132 'name' => self
::getFilePrefix() . 'failedExample.txt',
133 'mime_type' => 'text/plain',
134 'description' => 'My test description',
136 "/Mandatory key\\(s\\) missing from params array: 'id' or 'content' or 'options.move-file'/",
139 'CRM_Activity_DAO_Activity',
141 'name' => self
::getFilePrefix() . 'failedExample.txt',
142 'mime_type' => 'text/plain',
143 'description' => 'My test description',
144 'content' => 'too much content',
146 'move-file' => $this->tmpFile('too-much.txt'),
149 "/'content' and 'options.move-file' are mutually exclusive/",
152 'CRM_Activity_DAO_Activity',
154 'name' => 'inv/alid.txt',
155 'mime_type' => 'text/plain',
156 'description' => 'My test description',
157 'content' => 'My test content',
162 'CRM_Core_DAO_Domain',
164 'name' => self
::getFilePrefix() . 'exampleFromContent.txt',
165 'mime_type' => 'text/plain',
166 'description' => 'My test description',
167 'content' => 'My test content',
169 "/Unrecognized target entity/",
178 public function badUpdateProvider() {
179 $cases = array(); // array($entityClass, $createParams, $updateParams, $expectedError)
181 $readOnlyFields = array(
182 'name' => 'newname.txt',
183 'entity_table' => 'civicrm_domain',
185 'upload_date' => '2010-11-12 13:14:15',
187 foreach ($readOnlyFields as $readOnlyField => $newValue) {
189 'CRM_Activity_DAO_Activity',
191 'name' => self
::getFilePrefix() . 'exampleFromContent.txt',
192 'mime_type' => 'text/plain',
193 'description' => 'My test description',
194 'content' => 'My test content',
197 'check_permissions' => 1,
198 $readOnlyField => $newValue,
200 "/Cannot modify $readOnlyField/",
210 public function okGetProvider() {
211 $cases = array(); // array($getParams, $expectedNames)
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)
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.
222 // array('entity_table' => 'civicrm_activity'),
224 // self::getFilePrefix() . 'example_123.csv',
225 // self::getFilePrefix() . 'example_123.txt',
226 // self::getFilePrefix() . 'example_456.csv',
227 // self::getFilePrefix() . 'example_456.txt',
231 // array('entity_table' => 'civicrm_activity', 'mime_type' => 'text/plain'),
232 // array(self::getFilePrefix() . 'example_123.txt', self::getFilePrefix() . 'example_456.txt'),
236 array('entity_table' => 'civicrm_activity', 'entity_id' => '123'),
237 array(self
::getFilePrefix() . 'example_123.txt', self
::getFilePrefix() . 'example_123.csv'),
240 array('entity_table' => 'civicrm_activity', 'entity_id' => '456'),
241 array(self
::getFilePrefix() . 'example_456.txt', self
::getFilePrefix() . 'example_456.csv'),
244 array('entity_table' => 'civicrm_activity', 'entity_id' => '456', 'mime_type' => 'text/csv'),
245 array(self
::getFilePrefix() . 'example_456.csv'),
248 array('entity_table' => 'civicrm_activity', 'entity_id' => '456', 'mime_type' => 'text/html'),
252 array('entity_table' => 'civicrm_activity', 'entity_id' => '999'),
262 public function badGetProvider() {
263 $cases = array(); // array($getParams, $expectedNames)
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)
270 array('check_permissions' => 1, 'mime_type' => 'text/plain'),
271 "/Mandatory key\\(s\\) missing from params array: 'id' or 'entity_table'/",
274 array('check_permissions' => 1, 'entity_id' => '123'),
275 "/Mandatory key\\(s\\) missing from params array: 'id' or 'entity_table'/",
278 array('check_permissions' => 1),
279 "/Mandatory key\\(s\\) missing from params array: 'id' or 'entity_table'/",
282 array('entity_table' => 'civicrm_activity', 'entity_id' => '123', 'name' => 'example_456.csv'),
283 "/Get by name is not currently supported/",
286 array('entity_table' => 'civicrm_activity', 'entity_id' => '123', 'content' => 'test'),
287 "/Get by content is not currently supported/",
290 array('entity_table' => 'civicrm_activity', 'entity_id' => '123', 'path' => '/home/foo'),
291 "/Get by path is not currently supported/",
294 array('entity_table' => 'civicrm_activity', 'entity_id' => '123', 'url' => '/index.php'),
295 "/Get by url is not currently supported/",
302 * Create an attachment using "content" and then "get" the attachment.
304 * @param string $testEntityClass
305 * E.g. "CRM_Core_DAO_Activity".
306 * @param array $createParams
307 * @param string $expectedContent
308 * @dataProvider okCreateProvider
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
));
315 $createResult = $this->callAPISuccess('Attachment', 'create', $createParams +
array(
316 'entity_table' => $entity_table,
317 'entity_id' => $entity->id
,
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);
329 $getResult = $this->callAPISuccess('Attachment', 'get', array(
330 'entity_table' => $entity_table,
331 'entity_id' => $entity->id
,
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");
337 $this->assertTrue(!isset($getResult['values'][$fileId]['content']));
339 $getResult2 = $this->callAPISuccess('Attachment', 'get', array(
340 'entity_table' => $entity_table,
341 'entity_id' => $entity->id
,
342 'return' => array('content'),
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");
351 * @param $testEntityClass
352 * @param $createParams
353 * @param $expectedError
354 * @dataProvider badCreateProvider
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
));
361 $createResult = $this->callAPIFailure('Attachment', 'create', $createParams +
array(
362 'entity_table' => $entity_table,
363 'entity_id' => $entity->id
,
365 $this->assertRegExp($expectedError, $createResult['error_message']);
369 * @param $testEntityClass
370 * @param $createParams
371 * @param $updateParams
372 * @param $expectedError
373 * @dataProvider badUpdateProvider
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
));
380 $createResult = $this->callAPISuccess('Attachment', 'create', $createParams +
array(
381 'entity_table' => $entity_table,
382 'entity_id' => $entity->id
,
384 $fileId = $createResult['id'];
385 $this->assertTrue(is_numeric($fileId));
387 $updateResult = $this->callAPIFailure('Attachment', 'create', $updateParams +
array(
390 $this->assertRegExp($expectedError, $updateResult['error_message']);
394 * If one submits a weird file name, it should be automatically converted
397 public function testCreateWithWeirdName() {
398 $entity = CRM_Core_DAO
::createTestObject('CRM_Activity_DAO_Activity');
399 $this->assertTrue(is_numeric($entity->id
));
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
,
409 $fileId = $createResult['id'];
410 $this->assertTrue(is_numeric($fileId));
411 $this->assertEquals(self
::getFilePrefix() . 'weird_na_me.txt', $createResult['values'][$fileId]['name']);
416 * @param $expectedNames
417 * @dataProvider okGetProvider
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,
433 $getResult = $this->callAPISuccess('Attachment', 'get', $getParams);
434 $actualNames = array_values(CRM_Utils_Array
::collect('name', $getResult['values']));
436 sort($expectedNames);
437 $this->assertEquals($expectedNames, $actualNames);
442 * @param $expectedError
443 * @dataProvider badGetProvider
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,
459 $getResult = $this->callAPIFailure('Attachment', 'get', $getParams);
460 $this->assertRegExp($expectedError, $getResult['error_message']);
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.
468 public function testGetThenUpdate() {
469 $entity = CRM_Core_DAO
::createTestObject('CRM_Activity_DAO_Activity');
470 $this->assertTrue(is_numeric($entity->id
));
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
,
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);
485 $getResult = $this->callAPISuccess('Attachment', 'get', array(
488 $this->assertTrue(is_array($getResult['values'][$fileId]));
490 $updateParams = $getResult['values'][$fileId];
491 $updateParams['description'] = 'new description';
492 $this->callAPISuccess('Attachment', 'create', $updateParams);
493 $this->assertAttachmentExistence(TRUE, $createResult);
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.
500 public function testDeleteByID() {
501 $entity = CRM_Core_DAO
::createTestObject('CRM_Activity_DAO_Activity');
502 $this->assertTrue(is_numeric($entity->id
));
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
,
512 $this->assertTrue(is_numeric($createResults[$n]['id']));
513 $this->assertEquals(self
::getFilePrefix() . 'testDeleteByID.txt', $createResults[$n]['values'][$createResults[$n]['id']]['name']);
515 $this->assertAttachmentExistence(TRUE, $createResults['first']);
516 $this->assertAttachmentExistence(TRUE, $createResults['second']);
518 $this->callAPISuccess('Attachment', 'delete', array(
519 'id' => $createResults['first']['id'],
521 $this->assertAttachmentExistence(FALSE, $createResults['first']);
522 $this->assertAttachmentExistence(TRUE, $createResults['second']);
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.
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
,
542 $this->assertTrue(is_numeric($createResults[$e][$n]['id']));
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']);
550 $this->callAPISuccess('Attachment', 'delete', array(
551 'entity_table' => 'civicrm_activity',
552 'entity_id' => $entities[$e]->id
,
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']);
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;
570 protected function cleanupFiles() {
571 $config = CRM_Core_Config
::singleton();
574 $config->customFileUploadDir
,
576 foreach ($dirs as $dir) {
577 $files = (array) glob($dir . "/" . self
::getFilePrefix() . "*");
578 foreach ($files as $file) {