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 * Test for the Attachment API
31 * @package CiviCRM_APIv3
32 * @subpackage API_Contact
34 class api_v3_AttachmentTest
extends CiviUnitTestCase
{
35 protected static $filePrefix = NULL;
40 public static function getFilePrefix() {
41 if (!self
::$filePrefix) {
42 self
::$filePrefix = "test_" . CRM_Utils_String
::createRandom(5, CRM_Utils_String
::ALPHANUMERIC
) . '_';
44 return self
::$filePrefix;
48 protected function setUp() {
50 $this->useTransaction(TRUE);
52 $this->cleanupFiles();
53 file_put_contents($this->tmpFile('mytest.txt'), 'This comes from a file');
56 protected function tearDown() {
58 $this->cleanupFiles();
65 public function okCreateProvider() {
66 $cases = array(); // array($entityClass, $createParams, $expectedContent)
69 'CRM_Activity_DAO_Activity',
71 'name' => self
::getFilePrefix() . 'exampleFromContent.txt',
72 'mime_type' => 'text/plain',
73 'description' => 'My test description',
74 'content' => 'My test content',
80 'CRM_Activity_DAO_Activity',
82 'name' => self
::getFilePrefix() . 'exampleWithEmptyContent.txt',
83 'mime_type' => 'text/plain',
84 'description' => 'My test description',
91 'CRM_Activity_DAO_Activity',
93 'name' => self
::getFilePrefix() . 'exampleFromMove.txt',
94 'mime_type' => 'text/plain',
95 'description' => 'My test description',
97 'move-file' => $this->tmpFile('mytest.txt'),
100 'This comes from a file',
109 public function badCreateProvider() {
110 $cases = array(); // array($entityClass, $createParams, $expectedError)
113 'CRM_Activity_DAO_Activity',
116 'name' => self
::getFilePrefix() . 'exampleFromContent.txt',
117 'mime_type' => 'text/plain',
118 'description' => 'My test description',
119 'content' => 'My test content',
124 'CRM_Activity_DAO_Activity',
126 'name' => self
::getFilePrefix() . 'failedExample.txt',
127 'mime_type' => 'text/plain',
128 'description' => 'My test description',
130 "/Mandatory key\\(s\\) missing from params array: 'id' or 'content' or 'options.move-file'/",
133 'CRM_Activity_DAO_Activity',
135 'name' => self
::getFilePrefix() . 'failedExample.txt',
136 'mime_type' => 'text/plain',
137 'description' => 'My test description',
138 'content' => 'too much content',
140 'move-file' => $this->tmpFile('too-much.txt'),
143 "/'content' and 'options.move-file' are mutually exclusive/",
146 'CRM_Activity_DAO_Activity',
148 'name' => 'inv/alid.txt',
149 'mime_type' => 'text/plain',
150 'description' => 'My test description',
151 'content' => 'My test content',
156 'CRM_Core_DAO_Domain',
158 'name' => self
::getFilePrefix() . 'exampleFromContent.txt',
159 'mime_type' => 'text/plain',
160 'description' => 'My test description',
161 'content' => 'My test content',
163 "/Unrecognized target entity/",
172 public function badUpdateProvider() {
173 $cases = array(); // array($entityClass, $createParams, $updateParams, $expectedError)
175 $readOnlyFields = array(
176 'name' => 'newname.txt',
177 'entity_table' => 'civicrm_domain',
179 'upload_date' => '2010-11-12 13:14:15',
181 foreach ($readOnlyFields as $readOnlyField => $newValue) {
183 'CRM_Activity_DAO_Activity',
185 'name' => self
::getFilePrefix() . 'exampleFromContent.txt',
186 'mime_type' => 'text/plain',
187 'description' => 'My test description',
188 'content' => 'My test content',
191 'check_permissions' => 1,
192 $readOnlyField => $newValue,
194 "/Cannot modify $readOnlyField/",
204 public function okGetProvider() {
205 $cases = array(); // array($getParams, $expectedNames)
207 // Each search runs in a DB which contains these attachments:
208 // Activity #123: example_123.txt (text/plain) and example_123.csv (text/csv)
209 // Activity #456: example_456.txt (text/plain) and example_456.csv (text/csv)
211 // NOTE: Searching across multiple records (w/o entity_id) is currently
212 // prohibited by DynamicFKAuthorization. The technique used to authorize requests
213 // does not adapt well to such searches.
216 // array('entity_table' => 'civicrm_activity'),
218 // self::getFilePrefix() . 'example_123.csv',
219 // self::getFilePrefix() . 'example_123.txt',
220 // self::getFilePrefix() . 'example_456.csv',
221 // self::getFilePrefix() . 'example_456.txt',
225 // array('entity_table' => 'civicrm_activity', 'mime_type' => 'text/plain'),
226 // array(self::getFilePrefix() . 'example_123.txt', self::getFilePrefix() . 'example_456.txt'),
230 array('entity_table' => 'civicrm_activity', 'entity_id' => '123'),
231 array(self
::getFilePrefix() . 'example_123.txt', self
::getFilePrefix() . 'example_123.csv'),
234 array('entity_table' => 'civicrm_activity', 'entity_id' => '456'),
235 array(self
::getFilePrefix() . 'example_456.txt', self
::getFilePrefix() . 'example_456.csv'),
238 array('entity_table' => 'civicrm_activity', 'entity_id' => '456', 'mime_type' => 'text/csv'),
239 array(self
::getFilePrefix() . 'example_456.csv'),
242 array('entity_table' => 'civicrm_activity', 'entity_id' => '456', 'mime_type' => 'text/html'),
246 array('entity_table' => 'civicrm_activity', 'entity_id' => '999'),
256 public function badGetProvider() {
257 $cases = array(); // array($getParams, $expectedNames)
259 // Each search runs in a DB which contains these attachments:
260 // Activity #123: example_123.txt (text/plain) and example_123.csv (text/csv)
261 // Activity #456: example_456.txt (text/plain) and example_456.csv (text/csv)
264 array('check_permissions' => 1, 'mime_type' => 'text/plain'),
265 "/Mandatory key\\(s\\) missing from params array: 'id' or 'entity_table'/",
268 array('check_permissions' => 1, 'entity_id' => '123'),
269 "/Mandatory key\\(s\\) missing from params array: 'id' or 'entity_table'/",
272 array('check_permissions' => 1),
273 "/Mandatory key\\(s\\) missing from params array: 'id' or 'entity_table'/",
276 array('entity_table' => 'civicrm_activity', 'entity_id' => '123', 'name' => 'example_456.csv'),
277 "/Get by name is not currently supported/",
280 array('entity_table' => 'civicrm_activity', 'entity_id' => '123', 'content' => 'test'),
281 "/Get by content is not currently supported/",
284 array('entity_table' => 'civicrm_activity', 'entity_id' => '123', 'path' => '/home/foo'),
285 "/Get by path is not currently supported/",
288 array('entity_table' => 'civicrm_activity', 'entity_id' => '123', 'url' => '/index.php'),
289 "/Get by url is not currently supported/",
296 * Create an attachment using "content" and then "get" the attachment.
298 * @param string $testEntityClass
299 * E.g. "CRM_Core_DAO_Activity".
300 * @param array $createParams
301 * @param string $expectedContent
302 * @dataProvider okCreateProvider
304 public function testCreate($testEntityClass, $createParams, $expectedContent) {
305 $entity = CRM_Core_DAO
::createTestObject($testEntityClass);
306 $entity_table = CRM_Core_DAO_AllCoreTables
::getTableForClass($testEntityClass);
307 $this->assertTrue(is_numeric($entity->id
));
309 $createResult = $this->callAPISuccess('Attachment', 'create', $createParams +
array(
310 'entity_table' => $entity_table,
311 'entity_id' => $entity->id
,
313 $fileId = $createResult['id'];
314 $this->assertTrue(is_numeric($fileId));
315 $this->assertEquals($entity_table, $createResult['values'][$fileId]['entity_table']);
316 $this->assertEquals($entity->id
, $createResult['values'][$fileId]['entity_id']);
317 $this->assertEquals('My test description', $createResult['values'][$fileId]['description']);
318 $this->assertRegExp('/\d\d\d\d-\d\d-\d\d \d\d:\d\d:\d\d/', $createResult['values'][$fileId]['upload_date']);
319 $this->assertTrue(!isset($createResult['values'][$fileId]['content']));
320 $this->assertTrue(!empty($createResult['values'][$fileId]['url']));
321 $this->assertAttachmentExistence(TRUE, $createResult);
323 $getResult = $this->callAPISuccess('Attachment', 'get', array(
324 'entity_table' => $entity_table,
325 'entity_id' => $entity->id
,
327 $this->assertEquals(1, $getResult['count']);
328 foreach (array('id', 'entity_table', 'entity_id', 'url') as $field) {
329 $this->assertEquals($createResult['values'][$fileId][$field], $getResult['values'][$fileId][$field], "Expect field $field to match");
331 $this->assertTrue(!isset($getResult['values'][$fileId]['content']));
333 $getResult2 = $this->callAPISuccess('Attachment', 'get', array(
334 'entity_table' => $entity_table,
335 'entity_id' => $entity->id
,
336 'return' => array('content'),
338 $this->assertEquals($expectedContent, $getResult2['values'][$fileId]['content']);
339 foreach (array('id', 'entity_table', 'entity_id', 'url') as $field) {
340 $this->assertEquals($createResult['values'][$fileId][$field], $getResult['values'][$fileId][$field], "Expect field $field to match");
345 * @param $testEntityClass
346 * @param $createParams
347 * @param $expectedError
348 * @dataProvider badCreateProvider
350 public function testCreateFailure($testEntityClass, $createParams, $expectedError) {
351 $entity = CRM_Core_DAO
::createTestObject($testEntityClass);
352 $entity_table = CRM_Core_DAO_AllCoreTables
::getTableForClass($testEntityClass);
353 $this->assertTrue(is_numeric($entity->id
));
355 $createResult = $this->callAPIFailure('Attachment', 'create', $createParams +
array(
356 'entity_table' => $entity_table,
357 'entity_id' => $entity->id
,
359 $this->assertRegExp($expectedError, $createResult['error_message']);
363 * @param $testEntityClass
364 * @param $createParams
365 * @param $updateParams
366 * @param $expectedError
367 * @dataProvider badUpdateProvider
369 public function testCreateWithBadUpdate($testEntityClass, $createParams, $updateParams, $expectedError) {
370 $entity = CRM_Core_DAO
::createTestObject($testEntityClass);
371 $entity_table = CRM_Core_DAO_AllCoreTables
::getTableForClass($testEntityClass);
372 $this->assertTrue(is_numeric($entity->id
));
374 $createResult = $this->callAPISuccess('Attachment', 'create', $createParams +
array(
375 'entity_table' => $entity_table,
376 'entity_id' => $entity->id
,
378 $fileId = $createResult['id'];
379 $this->assertTrue(is_numeric($fileId));
381 $updateResult = $this->callAPIFailure('Attachment', 'create', $updateParams +
array(
384 $this->assertRegExp($expectedError, $updateResult['error_message']);
388 * If one submits a weird file name, it should be automatically converted
391 public function testCreateWithWeirdName() {
392 $entity = CRM_Core_DAO
::createTestObject('CRM_Activity_DAO_Activity');
393 $this->assertTrue(is_numeric($entity->id
));
395 $createResult = $this->callAPISuccess('Attachment', 'create', array(
396 'name' => self
::getFilePrefix() . 'weird:na"me.txt',
397 'mime_type' => 'text/plain',
398 'description' => 'My test description',
399 'content' => 'My test content',
400 'entity_table' => 'civicrm_activity',
401 'entity_id' => $entity->id
,
403 $fileId = $createResult['id'];
404 $this->assertTrue(is_numeric($fileId));
405 $this->assertEquals(self
::getFilePrefix() . 'weird_na_me.txt', $createResult['values'][$fileId]['name']);
410 * @param $expectedNames
411 * @dataProvider okGetProvider
413 public function testGet($getParams, $expectedNames) {
414 foreach (array(123, 456) as $entity_id) {
415 foreach (array('text/plain' => '.txt', 'text/csv' => '.csv') as $mime => $ext) {
416 $this->callAPISuccess('Attachment', 'create', array(
417 'name' => self
::getFilePrefix() . 'example_' . $entity_id . $ext,
418 'mime_type' => $mime,
419 'description' => 'My test description',
420 'content' => 'My test content',
421 'entity_table' => 'civicrm_activity',
422 'entity_id' => $entity_id,
427 $getResult = $this->callAPISuccess('Attachment', 'get', $getParams);
428 $actualNames = array_values(CRM_Utils_Array
::collect('name', $getResult['values']));
430 sort($expectedNames);
431 $this->assertEquals($expectedNames, $actualNames);
436 * @param $expectedError
437 * @dataProvider badGetProvider
439 public function testGetError($getParams, $expectedError) {
440 foreach (array(123, 456) as $entity_id) {
441 foreach (array('text/plain' => '.txt', 'text/csv' => '.csv') as $mime => $ext) {
442 $this->callAPISuccess('Attachment', 'create', array(
443 'name' => self
::getFilePrefix() . 'example_' . $entity_id . $ext,
444 'mime_type' => $mime,
445 'description' => 'My test description',
446 'content' => 'My test content',
447 'entity_table' => 'civicrm_activity',
448 'entity_id' => $entity_id,
453 $getResult = $this->callAPIFailure('Attachment', 'get', $getParams);
454 $this->assertRegExp($expectedError, $getResult['error_message']);
458 * Take the values from a "get", make a small change, and then send
459 * the full thing back in as an update ("create"). This ensures some
460 * consistency in the acceptable formats.
462 public function testGetThenUpdate() {
463 $entity = CRM_Core_DAO
::createTestObject('CRM_Activity_DAO_Activity');
464 $this->assertTrue(is_numeric($entity->id
));
466 $createResult = $this->callAPISuccess('Attachment', 'create', array(
467 'name' => self
::getFilePrefix() . 'getThenUpdate.txt',
468 'mime_type' => 'text/plain',
469 'description' => 'My test description',
470 'content' => 'My test content',
471 'entity_table' => 'civicrm_activity',
472 'entity_id' => $entity->id
,
474 $fileId = $createResult['id'];
475 $this->assertTrue(is_numeric($fileId));
476 $this->assertEquals(self
::getFilePrefix() . 'getThenUpdate.txt', $createResult['values'][$fileId]['name']);
477 $this->assertAttachmentExistence(TRUE, $createResult);
479 $getResult = $this->callAPISuccess('Attachment', 'get', array(
482 $this->assertTrue(is_array($getResult['values'][$fileId]));
484 $updateParams = $getResult['values'][$fileId];
485 $updateParams['description'] = 'new description';
486 $this->callAPISuccess('Attachment', 'create', $updateParams);
487 $this->assertAttachmentExistence(TRUE, $createResult);
491 * Create an attachment and delete using its ID. Assert that the records are correctly created and destroyed
492 * in the DB and the filesystem.
494 public function testDeleteByID() {
495 $entity = CRM_Core_DAO
::createTestObject('CRM_Activity_DAO_Activity');
496 $this->assertTrue(is_numeric($entity->id
));
498 foreach (array('first', 'second') as $n) {
499 $createResults[$n] = $this->callAPISuccess('Attachment', 'create', array(
500 'name' => self
::getFilePrefix() . 'testDeleteByID.txt',
501 'mime_type' => 'text/plain',
502 'content' => 'My test content',
503 'entity_table' => 'civicrm_activity',
504 'entity_id' => $entity->id
,
506 $this->assertTrue(is_numeric($createResults[$n]['id']));
507 $this->assertEquals(self
::getFilePrefix() . 'testDeleteByID.txt', $createResults[$n]['values'][$createResults[$n]['id']]['name']);
509 $this->assertAttachmentExistence(TRUE, $createResults['first']);
510 $this->assertAttachmentExistence(TRUE, $createResults['second']);
512 $this->callAPISuccess('Attachment', 'delete', array(
513 'id' => $createResults['first']['id'],
515 $this->assertAttachmentExistence(FALSE, $createResults['first']);
516 $this->assertAttachmentExistence(TRUE, $createResults['second']);
520 * Create an attachment and delete using its ID. Assert that the records are correctly created and destroyed
521 * in the DB and the filesystem.
523 public function testDeleteByEntity() {
524 // create 2 entities (keepme,delme) -- each with 2 attachments (first,second)
525 foreach (array('keepme', 'delme') as $e) {
526 $entities[$e] = CRM_Core_DAO
::createTestObject('CRM_Activity_DAO_Activity');
527 $this->assertTrue(is_numeric($entities[$e]->id
));
528 foreach (array('first', 'second') as $n) {
529 $createResults[$e][$n] = $this->callAPISuccess('Attachment', 'create', array(
530 'name' => self
::getFilePrefix() . 'testDeleteByEntity.txt',
531 'mime_type' => 'text/plain',
532 'content' => 'My test content',
533 'entity_table' => 'civicrm_activity',
534 'entity_id' => $entities[$e]->id
,
536 $this->assertTrue(is_numeric($createResults[$e][$n]['id']));
539 $this->assertAttachmentExistence(TRUE, $createResults['keepme']['first']);
540 $this->assertAttachmentExistence(TRUE, $createResults['keepme']['second']);
541 $this->assertAttachmentExistence(TRUE, $createResults['delme']['first']);
542 $this->assertAttachmentExistence(TRUE, $createResults['delme']['second']);
544 $this->callAPISuccess('Attachment', 'delete', array(
545 'entity_table' => 'civicrm_activity',
546 'entity_id' => $entities[$e]->id
,
548 $this->assertAttachmentExistence(TRUE, $createResults['keepme']['first']);
549 $this->assertAttachmentExistence(TRUE, $createResults['keepme']['second']);
550 $this->assertAttachmentExistence(FALSE, $createResults['delme']['first']);
551 $this->assertAttachmentExistence(FALSE, $createResults['delme']['second']);
558 protected function tmpFile($name) {
559 $tmpDir = sys_get_temp_dir();
560 $this->assertTrue($tmpDir && is_dir($tmpDir), 'Tmp dir must exist: ' . $tmpDir);
561 return $tmpDir . '/' . self
::getFilePrefix() . $name;
564 protected function cleanupFiles() {
565 $config = CRM_Core_Config
::singleton();
568 $config->customFileUploadDir
,
570 foreach ($dirs as $dir) {
571 $files = (array) glob($dir . "/" . self
::getFilePrefix() . "*");
572 foreach ($files as $file) {