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 | ||
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 | */ |
35 | class 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 | ||
56154d36 TO |
48 | protected function setUp() { |
49 | parent::setUp(); | |
50 | $this->useTransaction(TRUE); | |
51 | ||
52 | $this->cleanupFiles(); | |
53 | file_put_contents($this->tmpFile('mytest.txt'), 'This comes from a file'); | |
54 | } | |
55 | ||
56 | protected function tearDown() { | |
57 | parent::tearDown(); | |
58 | $this->cleanupFiles(); | |
048222df | 59 | \Civi::reset(); |
56154d36 TO |
60 | } |
61 | ||
f0be539a EM |
62 | /** |
63 | * @return array | |
64 | */ | |
56154d36 | 65 | public function okCreateProvider() { |
39b959db SL |
66 | // array($entityClass, $createParams, $expectedContent) |
67 | $cases = array(); | |
56154d36 TO |
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 | 110 | public function badCreateProvider() { |
39b959db SL |
111 | // array($entityClass, $createParams, $expectedError) |
112 | $cases = array(); | |
56154d36 TO |
113 | |
114 | $cases[] = array( | |
115 | 'CRM_Activity_DAO_Activity', | |
116 | array( | |
117 | 'id' => 12345, | |
118 | 'name' => self::getFilePrefix() . 'exampleFromContent.txt', | |
119 | 'mime_type' => 'text/plain', | |
120 | 'description' => 'My test description', | |
121 | 'content' => 'My test content', | |
122 | ), | |
123 | '/Invalid ID/', | |
124 | ); | |
125 | $cases[] = array( | |
126 | 'CRM_Activity_DAO_Activity', | |
127 | array( | |
128 | 'name' => self::getFilePrefix() . 'failedExample.txt', | |
129 | 'mime_type' => 'text/plain', | |
130 | 'description' => 'My test description', | |
131 | ), | |
132 | "/Mandatory key\\(s\\) missing from params array: 'id' or 'content' or 'options.move-file'/", | |
133 | ); | |
134 | $cases[] = array( | |
135 | 'CRM_Activity_DAO_Activity', | |
136 | array( | |
137 | 'name' => self::getFilePrefix() . 'failedExample.txt', | |
138 | 'mime_type' => 'text/plain', | |
139 | 'description' => 'My test description', | |
140 | 'content' => 'too much content', | |
141 | 'options' => array( | |
21dfd5f5 | 142 | 'move-file' => $this->tmpFile('too-much.txt'), |
56154d36 TO |
143 | ), |
144 | ), | |
145 | "/'content' and 'options.move-file' are mutually exclusive/", | |
146 | ); | |
147 | $cases[] = array( | |
148 | 'CRM_Activity_DAO_Activity', | |
149 | array( | |
150 | 'name' => 'inv/alid.txt', | |
151 | 'mime_type' => 'text/plain', | |
152 | 'description' => 'My test description', | |
153 | 'content' => 'My test content', | |
154 | ), | |
155 | "/Malformed name/", | |
156 | ); | |
157 | $cases[] = array( | |
158 | 'CRM_Core_DAO_Domain', | |
159 | array( | |
160 | 'name' => self::getFilePrefix() . 'exampleFromContent.txt', | |
161 | 'mime_type' => 'text/plain', | |
162 | 'description' => 'My test description', | |
163 | 'content' => 'My test content', | |
2ffa31a8 | 164 | 'check_permissions' => 1, |
56154d36 TO |
165 | ), |
166 | "/Unrecognized target entity/", | |
167 | ); | |
168 | ||
169 | return $cases; | |
170 | } | |
171 | ||
f0be539a EM |
172 | /** |
173 | * @return array | |
174 | */ | |
56154d36 | 175 | public function badUpdateProvider() { |
39b959db SL |
176 | // array($entityClass, $createParams, $updateParams, $expectedError) |
177 | $cases = array(); | |
56154d36 TO |
178 | |
179 | $readOnlyFields = array( | |
180 | 'name' => 'newname.txt', | |
181 | 'entity_table' => 'civicrm_domain', | |
182 | 'entity_id' => 5, | |
183 | 'upload_date' => '2010-11-12 13:14:15', | |
184 | ); | |
185 | foreach ($readOnlyFields as $readOnlyField => $newValue) { | |
186 | $cases[] = array( | |
187 | 'CRM_Activity_DAO_Activity', | |
188 | array( | |
189 | 'name' => self::getFilePrefix() . 'exampleFromContent.txt', | |
190 | 'mime_type' => 'text/plain', | |
191 | 'description' => 'My test description', | |
192 | 'content' => 'My test content', | |
193 | ), | |
194 | array( | |
195 | 'check_permissions' => 1, | |
196 | $readOnlyField => $newValue, | |
197 | ), | |
21dfd5f5 | 198 | "/Cannot modify $readOnlyField/", |
56154d36 TO |
199 | ); |
200 | } | |
201 | ||
202 | return $cases; | |
203 | } | |
204 | ||
f0be539a EM |
205 | /** |
206 | * @return array | |
207 | */ | |
56154d36 | 208 | public function okGetProvider() { |
39b959db SL |
209 | // array($getParams, $expectedNames) |
210 | $cases = array(); | |
56154d36 TO |
211 | |
212 | // Each search runs in a DB which contains these attachments: | |
213 | // Activity #123: example_123.txt (text/plain) and example_123.csv (text/csv) | |
214 | // Activity #456: example_456.txt (text/plain) and example_456.csv (text/csv) | |
215 | ||
e6b62a18 TO |
216 | // NOTE: Searching across multiple records (w/o entity_id) is currently |
217 | // prohibited by DynamicFKAuthorization. The technique used to authorize requests | |
218 | // does not adapt well to such searches. | |
219 | ||
220 | //$cases[] = array( | |
221 | // array('entity_table' => 'civicrm_activity'), | |
222 | // array( | |
223 | // self::getFilePrefix() . 'example_123.csv', | |
224 | // self::getFilePrefix() . 'example_123.txt', | |
225 | // self::getFilePrefix() . 'example_456.csv', | |
226 | // self::getFilePrefix() . 'example_456.txt', | |
227 | // ), | |
228 | //); | |
229 | //$cases[] = array( | |
230 | // array('entity_table' => 'civicrm_activity', 'mime_type' => 'text/plain'), | |
231 | // array(self::getFilePrefix() . 'example_123.txt', self::getFilePrefix() . 'example_456.txt'), | |
232 | //); | |
233 | ||
56154d36 TO |
234 | $cases[] = array( |
235 | array('entity_table' => 'civicrm_activity', 'entity_id' => '123'), | |
236 | array(self::getFilePrefix() . 'example_123.txt', self::getFilePrefix() . 'example_123.csv'), | |
237 | ); | |
238 | $cases[] = array( | |
239 | array('entity_table' => 'civicrm_activity', 'entity_id' => '456'), | |
240 | array(self::getFilePrefix() . 'example_456.txt', self::getFilePrefix() . 'example_456.csv'), | |
241 | ); | |
242 | $cases[] = array( | |
243 | array('entity_table' => 'civicrm_activity', 'entity_id' => '456', 'mime_type' => 'text/csv'), | |
244 | array(self::getFilePrefix() . 'example_456.csv'), | |
245 | ); | |
246 | $cases[] = array( | |
247 | array('entity_table' => 'civicrm_activity', 'entity_id' => '456', 'mime_type' => 'text/html'), | |
248 | array(), | |
249 | ); | |
250 | $cases[] = array( | |
251 | array('entity_table' => 'civicrm_activity', 'entity_id' => '999'), | |
252 | array(), | |
253 | ); | |
254 | ||
255 | return $cases; | |
256 | } | |
257 | ||
f0be539a EM |
258 | /** |
259 | * @return array | |
260 | */ | |
56154d36 | 261 | public function badGetProvider() { |
39b959db SL |
262 | // array($getParams, $expectedNames) |
263 | $cases = array(); | |
56154d36 TO |
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( | |
39b959db SL |
316 | 'entity_table' => $entity_table, |
317 | 'entity_id' => $entity->id, | |
318 | )); | |
56154d36 TO |
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( | |
39b959db SL |
362 | 'entity_table' => $entity_table, |
363 | 'entity_id' => $entity->id, | |
364 | )); | |
56154d36 TO |
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( | |
39b959db SL |
381 | 'entity_table' => $entity_table, |
382 | 'entity_id' => $entity->id, | |
383 | )); | |
56154d36 TO |
384 | $fileId = $createResult['id']; |
385 | $this->assertTrue(is_numeric($fileId)); | |
386 | ||
387 | $updateResult = $this->callAPIFailure('Attachment', 'create', $updateParams + array( | |
39b959db SL |
388 | 'id' => $fileId, |
389 | )); | |
56154d36 TO |
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']); | |
4994819e CW |
412 | // Check for appropriate icon |
413 | $this->assertEquals('fa-file-text-o', $createResult['values'][$fileId]['icon']); | |
56154d36 TO |
414 | } |
415 | ||
7aab0058 DA |
416 | public function testCreateShouldSetCreatedIdAsTheLoggedInUser() { |
417 | $loggedInUser = $this->createLoggedInUser(); | |
418 | ||
419 | $testEntityClass = 'CRM_Activity_DAO_Activity'; | |
420 | $entity = CRM_Core_DAO::createTestObject($testEntityClass); | |
421 | $entity_table = CRM_Core_DAO_AllCoreTables::getTableForClass($testEntityClass); | |
422 | $this->assertTrue(is_numeric($entity->id)); | |
423 | ||
424 | $createResult = $this->callAPISuccess('Attachment', 'create', array( | |
425 | 'name' => self::getFilePrefix() . 'exampleFromContent.txt', | |
426 | 'mime_type' => 'text/plain', | |
427 | 'content' => 'My test content', | |
428 | 'entity_table' => $entity_table, | |
429 | 'entity_id' => $entity->id, | |
430 | )); | |
431 | ||
432 | $fileId = $createResult['id']; | |
433 | $this->assertEquals($loggedInUser, $createResult['values'][$fileId]['created_id']); | |
434 | } | |
435 | ||
436 | public function testCreateShouldKeepCreatedIdEmptyIfTheresNoLoggedInUser() { | |
437 | $testEntityClass = 'CRM_Activity_DAO_Activity'; | |
438 | $entity = CRM_Core_DAO::createTestObject($testEntityClass); | |
439 | $entity_table = CRM_Core_DAO_AllCoreTables::getTableForClass($testEntityClass); | |
440 | $this->assertTrue(is_numeric($entity->id)); | |
441 | ||
442 | $createResult = $this->callAPISuccess('Attachment', 'create', array( | |
443 | 'name' => self::getFilePrefix() . 'exampleFromContent.txt', | |
444 | 'mime_type' => 'text/plain', | |
445 | 'content' => 'My test content', | |
446 | 'entity_table' => $entity_table, | |
447 | 'entity_id' => $entity->id, | |
448 | )); | |
449 | ||
450 | $fileId = $createResult['id']; | |
451 | $this->assertEmpty($createResult['values'][$fileId]['created_id']); | |
452 | } | |
453 | ||
454 | public function testCreateShouldNotUpdateTheCreatedId() { | |
455 | $testEntityClass = 'CRM_Activity_DAO_Activity'; | |
456 | $entity = CRM_Core_DAO::createTestObject($testEntityClass); | |
457 | $entity_table = CRM_Core_DAO_AllCoreTables::getTableForClass($testEntityClass); | |
458 | $this->assertTrue(is_numeric($entity->id)); | |
459 | ||
460 | $attachmentParams = array( | |
461 | 'name' => self::getFilePrefix() . 'exampleFromContent.txt', | |
462 | 'mime_type' => 'text/plain', | |
463 | 'description' => 'My test description', | |
464 | 'content' => 'My test content', | |
465 | 'entity_table' => $entity_table, | |
466 | 'entity_id' => $entity->id, | |
467 | ); | |
468 | ||
469 | $createResult = $this->callAPISuccess('Attachment', 'create', $attachmentParams); | |
470 | ||
471 | $fileId = $createResult['id']; | |
472 | $this->assertEmpty($createResult['values'][$fileId]['created_id']); | |
473 | ||
474 | $attachmentParams['id'] = $fileId; | |
475 | $attachmentParams['description'] = 'My updated description'; | |
476 | ||
477 | $loggedInUser = $this->createLoggedInUser(); | |
478 | ||
479 | $this->callAPISuccess('Attachment', 'create', $attachmentParams); | |
480 | ||
481 | $updatedAttachment = $this->callAPISuccess('Attachment', 'get', array( | |
482 | 'id' => $fileId, | |
483 | 'entity_id' => $attachmentParams['entity_id'], | |
ae2c7e00 | 484 | 'entity_table' => $attachmentParams['entity_table'], |
7aab0058 DA |
485 | )); |
486 | ||
487 | $this->assertNotEmpty($loggedInUser); | |
488 | $this->assertEmpty($updatedAttachment['values'][$fileId]['created_id']); | |
489 | $this->assertEquals($attachmentParams['description'], $updatedAttachment['values'][$fileId]['description']); | |
490 | } | |
491 | ||
56154d36 TO |
492 | /** |
493 | * @param $getParams | |
494 | * @param $expectedNames | |
495 | * @dataProvider okGetProvider | |
496 | */ | |
497 | public function testGet($getParams, $expectedNames) { | |
498 | foreach (array(123, 456) as $entity_id) { | |
499 | foreach (array('text/plain' => '.txt', 'text/csv' => '.csv') as $mime => $ext) { | |
500 | $this->callAPISuccess('Attachment', 'create', array( | |
501 | 'name' => self::getFilePrefix() . 'example_' . $entity_id . $ext, | |
502 | 'mime_type' => $mime, | |
503 | 'description' => 'My test description', | |
504 | 'content' => 'My test content', | |
505 | 'entity_table' => 'civicrm_activity', | |
506 | 'entity_id' => $entity_id, | |
507 | )); | |
508 | } | |
509 | } | |
510 | ||
511 | $getResult = $this->callAPISuccess('Attachment', 'get', $getParams); | |
512 | $actualNames = array_values(CRM_Utils_Array::collect('name', $getResult['values'])); | |
ff9aeadb SL |
513 | // Verify the hash generated by the API is valid if we were to try and load the file. |
514 | foreach ($getResult['values'] as $result) { | |
515 | $queryResult = []; | |
516 | $parsedURl = parse_url($result['url']); | |
517 | $parsedQuery = parse_str($parsedURl['query'], $queryResult); | |
c5c40df7 | 518 | $this->assertTrue(CRM_Core_BAO_File::validateFileHash($queryResult['fcs'], $queryResult['eid'], $queryResult['id'])); |
ff9aeadb SL |
519 | } |
520 | ||
56154d36 TO |
521 | sort($actualNames); |
522 | sort($expectedNames); | |
523 | $this->assertEquals($expectedNames, $actualNames); | |
524 | } | |
525 | ||
526 | /** | |
527 | * @param $getParams | |
528 | * @param $expectedError | |
529 | * @dataProvider badGetProvider | |
530 | */ | |
531 | public function testGetError($getParams, $expectedError) { | |
532 | foreach (array(123, 456) as $entity_id) { | |
533 | foreach (array('text/plain' => '.txt', 'text/csv' => '.csv') as $mime => $ext) { | |
534 | $this->callAPISuccess('Attachment', 'create', array( | |
535 | 'name' => self::getFilePrefix() . 'example_' . $entity_id . $ext, | |
536 | 'mime_type' => $mime, | |
537 | 'description' => 'My test description', | |
538 | 'content' => 'My test content', | |
539 | 'entity_table' => 'civicrm_activity', | |
540 | 'entity_id' => $entity_id, | |
541 | )); | |
542 | } | |
543 | } | |
544 | ||
545 | $getResult = $this->callAPIFailure('Attachment', 'get', $getParams); | |
546 | $this->assertRegExp($expectedError, $getResult['error_message']); | |
547 | } | |
548 | ||
549 | /** | |
550 | * Take the values from a "get", make a small change, and then send | |
551 | * the full thing back in as an update ("create"). This ensures some | |
552 | * consistency in the acceptable formats. | |
553 | */ | |
554 | public function testGetThenUpdate() { | |
555 | $entity = CRM_Core_DAO::createTestObject('CRM_Activity_DAO_Activity'); | |
556 | $this->assertTrue(is_numeric($entity->id)); | |
557 | ||
558 | $createResult = $this->callAPISuccess('Attachment', 'create', array( | |
559 | 'name' => self::getFilePrefix() . 'getThenUpdate.txt', | |
560 | 'mime_type' => 'text/plain', | |
561 | 'description' => 'My test description', | |
562 | 'content' => 'My test content', | |
563 | 'entity_table' => 'civicrm_activity', | |
564 | 'entity_id' => $entity->id, | |
565 | )); | |
566 | $fileId = $createResult['id']; | |
567 | $this->assertTrue(is_numeric($fileId)); | |
568 | $this->assertEquals(self::getFilePrefix() . 'getThenUpdate.txt', $createResult['values'][$fileId]['name']); | |
569 | $this->assertAttachmentExistence(TRUE, $createResult); | |
570 | ||
571 | $getResult = $this->callAPISuccess('Attachment', 'get', array( | |
572 | 'id' => $fileId, | |
573 | )); | |
574 | $this->assertTrue(is_array($getResult['values'][$fileId])); | |
575 | ||
576 | $updateParams = $getResult['values'][$fileId]; | |
577 | $updateParams['description'] = 'new description'; | |
578 | $this->callAPISuccess('Attachment', 'create', $updateParams); | |
579 | $this->assertAttachmentExistence(TRUE, $createResult); | |
580 | } | |
581 | ||
582 | /** | |
583 | * Create an attachment and delete using its ID. Assert that the records are correctly created and destroyed | |
584 | * in the DB and the filesystem. | |
585 | */ | |
586 | public function testDeleteByID() { | |
587 | $entity = CRM_Core_DAO::createTestObject('CRM_Activity_DAO_Activity'); | |
588 | $this->assertTrue(is_numeric($entity->id)); | |
589 | ||
590 | foreach (array('first', 'second') as $n) { | |
591 | $createResults[$n] = $this->callAPISuccess('Attachment', 'create', array( | |
592 | 'name' => self::getFilePrefix() . 'testDeleteByID.txt', | |
593 | 'mime_type' => 'text/plain', | |
594 | 'content' => 'My test content', | |
595 | 'entity_table' => 'civicrm_activity', | |
596 | 'entity_id' => $entity->id, | |
597 | )); | |
598 | $this->assertTrue(is_numeric($createResults[$n]['id'])); | |
599 | $this->assertEquals(self::getFilePrefix() . 'testDeleteByID.txt', $createResults[$n]['values'][$createResults[$n]['id']]['name']); | |
600 | } | |
601 | $this->assertAttachmentExistence(TRUE, $createResults['first']); | |
602 | $this->assertAttachmentExistence(TRUE, $createResults['second']); | |
603 | ||
604 | $this->callAPISuccess('Attachment', 'delete', array( | |
605 | 'id' => $createResults['first']['id'], | |
606 | )); | |
607 | $this->assertAttachmentExistence(FALSE, $createResults['first']); | |
608 | $this->assertAttachmentExistence(TRUE, $createResults['second']); | |
609 | } | |
610 | ||
611 | /** | |
612 | * Create an attachment and delete using its ID. Assert that the records are correctly created and destroyed | |
613 | * in the DB and the filesystem. | |
614 | */ | |
615 | public function testDeleteByEntity() { | |
616 | // create 2 entities (keepme,delme) -- each with 2 attachments (first,second) | |
617 | foreach (array('keepme', 'delme') as $e) { | |
618 | $entities[$e] = CRM_Core_DAO::createTestObject('CRM_Activity_DAO_Activity'); | |
619 | $this->assertTrue(is_numeric($entities[$e]->id)); | |
620 | foreach (array('first', 'second') as $n) { | |
621 | $createResults[$e][$n] = $this->callAPISuccess('Attachment', 'create', array( | |
622 | 'name' => self::getFilePrefix() . 'testDeleteByEntity.txt', | |
623 | 'mime_type' => 'text/plain', | |
624 | 'content' => 'My test content', | |
625 | 'entity_table' => 'civicrm_activity', | |
626 | 'entity_id' => $entities[$e]->id, | |
627 | )); | |
628 | $this->assertTrue(is_numeric($createResults[$e][$n]['id'])); | |
629 | } | |
630 | } | |
631 | $this->assertAttachmentExistence(TRUE, $createResults['keepme']['first']); | |
632 | $this->assertAttachmentExistence(TRUE, $createResults['keepme']['second']); | |
633 | $this->assertAttachmentExistence(TRUE, $createResults['delme']['first']); | |
634 | $this->assertAttachmentExistence(TRUE, $createResults['delme']['second']); | |
635 | ||
636 | $this->callAPISuccess('Attachment', 'delete', array( | |
637 | 'entity_table' => 'civicrm_activity', | |
638 | 'entity_id' => $entities[$e]->id, | |
639 | )); | |
640 | $this->assertAttachmentExistence(TRUE, $createResults['keepme']['first']); | |
641 | $this->assertAttachmentExistence(TRUE, $createResults['keepme']['second']); | |
642 | $this->assertAttachmentExistence(FALSE, $createResults['delme']['first']); | |
643 | $this->assertAttachmentExistence(FALSE, $createResults['delme']['second']); | |
644 | } | |
645 | ||
4994819e CW |
646 | /** |
647 | * Ensure mime type is converted to appropriate icon. | |
648 | */ | |
649 | public function testGetIcon() { | |
650 | $entity = CRM_Core_DAO::createTestObject('CRM_Activity_DAO_Activity'); | |
651 | $this->assertTrue(is_numeric($entity->id)); | |
652 | ||
653 | $createResult = $this->callAPISuccess('Attachment', 'create', array( | |
654 | 'name' => self::getFilePrefix() . 'hasIcon.docx', | |
655 | 'mime_type' => 'application/vnd.openxmlformats-officedocument.wordprocessingml.document', | |
656 | 'description' => 'My test description', | |
657 | 'content' => 'My test content', | |
658 | 'entity_table' => 'civicrm_activity', | |
659 | 'entity_id' => $entity->id, | |
660 | )); | |
661 | $fileId = $createResult['id']; | |
662 | $this->assertEquals('fa-file-word-o', $createResult['values'][$fileId]['icon']); | |
892be376 | 663 | |
4994819e CW |
664 | $createResult = $this->callAPISuccess('Attachment', 'create', array( |
665 | 'name' => self::getFilePrefix() . 'hasIcon.jpg', | |
666 | 'mime_type' => 'image/jpg', | |
667 | 'description' => 'My test description', | |
668 | 'content' => 'My test content', | |
669 | 'entity_table' => 'civicrm_activity', | |
670 | 'entity_id' => $entity->id, | |
671 | )); | |
672 | $fileId = $createResult['id']; | |
673 | $this->assertEquals('fa-file-image-o', $createResult['values'][$fileId]['icon']); | |
674 | } | |
675 | ||
f0be539a EM |
676 | /** |
677 | * @param $name | |
678 | * @return string | |
679 | */ | |
56154d36 TO |
680 | protected function tmpFile($name) { |
681 | $tmpDir = sys_get_temp_dir(); | |
682 | $this->assertTrue($tmpDir && is_dir($tmpDir), 'Tmp dir must exist: ' . $tmpDir); | |
683 | return $tmpDir . '/' . self::getFilePrefix() . $name; | |
684 | } | |
685 | ||
686 | protected function cleanupFiles() { | |
687 | $config = CRM_Core_Config::singleton(); | |
688 | $dirs = array( | |
689 | sys_get_temp_dir(), | |
690 | $config->customFileUploadDir, | |
691 | ); | |
692 | foreach ($dirs as $dir) { | |
693 | $files = (array) glob($dir . "/" . self::getFilePrefix() . "*"); | |
694 | foreach ($files as $file) { | |
695 | unlink($file); | |
696 | } | |
697 | ||
698 | } | |
699 | } | |
96025800 | 700 | |
56154d36 | 701 | } |