| 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 | |
| 43 | public static function getFilePrefix() { |
| 44 | if (!self::$filePrefix) { |
| 45 | self::$filePrefix = "test_" . CRM_Utils_String::createRandom(5, CRM_Utils_String::ALPHANUMERIC) . '_'; |
| 46 | } |
| 47 | return self::$filePrefix; |
| 48 | } |
| 49 | |
| 50 | |
| 51 | protected function setUp() { |
| 52 | parent::setUp(); |
| 53 | $this->useTransaction(TRUE); |
| 54 | |
| 55 | $this->cleanupFiles(); |
| 56 | file_put_contents($this->tmpFile('mytest.txt'), 'This comes from a file'); |
| 57 | } |
| 58 | |
| 59 | protected function tearDown() { |
| 60 | parent::tearDown(); |
| 61 | $this->cleanupFiles(); |
| 62 | \Civi\Core\Container::singleton(TRUE); |
| 63 | } |
| 64 | |
| 65 | public function okCreateProvider() { |
| 66 | $cases = array(); // array($entityClass, $createParams, $expectedContent) |
| 67 | |
| 68 | $cases[] = array( |
| 69 | 'CRM_Activity_DAO_Activity', |
| 70 | array( |
| 71 | 'name' => self::getFilePrefix() . 'exampleFromContent.txt', |
| 72 | 'mime_type' => 'text/plain', |
| 73 | 'description' => 'My test description', |
| 74 | 'content' => 'My test content', |
| 75 | ), |
| 76 | 'My test content', |
| 77 | ); |
| 78 | |
| 79 | $cases[] = array( |
| 80 | 'CRM_Activity_DAO_Activity', |
| 81 | array( |
| 82 | 'name' => self::getFilePrefix() . 'exampleWithEmptyContent.txt', |
| 83 | 'mime_type' => 'text/plain', |
| 84 | 'description' => 'My test description', |
| 85 | 'content' => '', |
| 86 | ), |
| 87 | '', |
| 88 | ); |
| 89 | |
| 90 | $cases[] = array( |
| 91 | 'CRM_Activity_DAO_Activity', |
| 92 | array( |
| 93 | 'name' => self::getFilePrefix() . 'exampleFromMove.txt', |
| 94 | 'mime_type' => 'text/plain', |
| 95 | 'description' => 'My test description', |
| 96 | 'options' => array( |
| 97 | 'move-file' => $this->tmpFile('mytest.txt'), |
| 98 | ), |
| 99 | ), |
| 100 | 'This comes from a file', |
| 101 | ); |
| 102 | |
| 103 | return $cases; |
| 104 | } |
| 105 | |
| 106 | public function badCreateProvider() { |
| 107 | $cases = array(); // array($entityClass, $createParams, $expectedError) |
| 108 | |
| 109 | $cases[] = array( |
| 110 | 'CRM_Activity_DAO_Activity', |
| 111 | array( |
| 112 | 'id' => 12345, |
| 113 | 'name' => self::getFilePrefix() . 'exampleFromContent.txt', |
| 114 | 'mime_type' => 'text/plain', |
| 115 | 'description' => 'My test description', |
| 116 | 'content' => 'My test content', |
| 117 | ), |
| 118 | '/Invalid ID/', |
| 119 | ); |
| 120 | $cases[] = array( |
| 121 | 'CRM_Activity_DAO_Activity', |
| 122 | array( |
| 123 | 'name' => self::getFilePrefix() . 'failedExample.txt', |
| 124 | 'mime_type' => 'text/plain', |
| 125 | 'description' => 'My test description', |
| 126 | ), |
| 127 | "/Mandatory key\\(s\\) missing from params array: 'id' or 'content' or 'options.move-file'/", |
| 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 | 'content' => 'too much content', |
| 136 | 'options' => array( |
| 137 | 'move-file' => $this->tmpFile('too-much.txt'), |
| 138 | ), |
| 139 | ), |
| 140 | "/'content' and 'options.move-file' are mutually exclusive/", |
| 141 | ); |
| 142 | $cases[] = array( |
| 143 | 'CRM_Activity_DAO_Activity', |
| 144 | array( |
| 145 | 'name' => 'inv/alid.txt', |
| 146 | 'mime_type' => 'text/plain', |
| 147 | 'description' => 'My test description', |
| 148 | 'content' => 'My test content', |
| 149 | ), |
| 150 | "/Malformed name/", |
| 151 | ); |
| 152 | $cases[] = array( |
| 153 | 'CRM_Core_DAO_Domain', |
| 154 | array( |
| 155 | 'name' => self::getFilePrefix() . 'exampleFromContent.txt', |
| 156 | 'mime_type' => 'text/plain', |
| 157 | 'description' => 'My test description', |
| 158 | 'content' => 'My test content', |
| 159 | ), |
| 160 | "/Unrecognized target entity/", |
| 161 | ); |
| 162 | |
| 163 | return $cases; |
| 164 | } |
| 165 | |
| 166 | public function badUpdateProvider() { |
| 167 | $cases = array(); // array($entityClass, $createParams, $updateParams, $expectedError) |
| 168 | |
| 169 | $readOnlyFields = array( |
| 170 | 'name' => 'newname.txt', |
| 171 | 'entity_table' => 'civicrm_domain', |
| 172 | 'entity_id' => 5, |
| 173 | 'upload_date' => '2010-11-12 13:14:15', |
| 174 | ); |
| 175 | foreach ($readOnlyFields as $readOnlyField => $newValue) { |
| 176 | $cases[] = array( |
| 177 | 'CRM_Activity_DAO_Activity', |
| 178 | array( |
| 179 | 'name' => self::getFilePrefix() . 'exampleFromContent.txt', |
| 180 | 'mime_type' => 'text/plain', |
| 181 | 'description' => 'My test description', |
| 182 | 'content' => 'My test content', |
| 183 | ), |
| 184 | array( |
| 185 | 'check_permissions' => 1, |
| 186 | $readOnlyField => $newValue, |
| 187 | ), |
| 188 | "/Cannot modify $readOnlyField/", |
| 189 | ); |
| 190 | } |
| 191 | |
| 192 | return $cases; |
| 193 | } |
| 194 | |
| 195 | public function okGetProvider() { |
| 196 | $cases = array(); // array($getParams, $expectedNames) |
| 197 | |
| 198 | // Each search runs in a DB which contains these attachments: |
| 199 | // Activity #123: example_123.txt (text/plain) and example_123.csv (text/csv) |
| 200 | // Activity #456: example_456.txt (text/plain) and example_456.csv (text/csv) |
| 201 | |
| 202 | $cases[] = array( |
| 203 | array('entity_table' => 'civicrm_activity'), |
| 204 | array( |
| 205 | self::getFilePrefix() . 'example_123.csv', |
| 206 | self::getFilePrefix() . 'example_123.txt', |
| 207 | self::getFilePrefix() . 'example_456.csv', |
| 208 | self::getFilePrefix() . 'example_456.txt', |
| 209 | ), |
| 210 | ); |
| 211 | $cases[] = array( |
| 212 | array('entity_table' => 'civicrm_activity', 'mime_type' => 'text/plain'), |
| 213 | array(self::getFilePrefix() . 'example_123.txt', self::getFilePrefix() . 'example_456.txt'), |
| 214 | ); |
| 215 | $cases[] = array( |
| 216 | array('entity_table' => 'civicrm_activity', 'entity_id' => '123'), |
| 217 | array(self::getFilePrefix() . 'example_123.txt', self::getFilePrefix() . 'example_123.csv'), |
| 218 | ); |
| 219 | $cases[] = array( |
| 220 | array('entity_table' => 'civicrm_activity', 'entity_id' => '456'), |
| 221 | array(self::getFilePrefix() . 'example_456.txt', self::getFilePrefix() . 'example_456.csv'), |
| 222 | ); |
| 223 | $cases[] = array( |
| 224 | array('entity_table' => 'civicrm_activity', 'entity_id' => '456', 'mime_type' => 'text/csv'), |
| 225 | array(self::getFilePrefix() . 'example_456.csv'), |
| 226 | ); |
| 227 | $cases[] = array( |
| 228 | array('entity_table' => 'civicrm_activity', 'entity_id' => '456', 'mime_type' => 'text/html'), |
| 229 | array(), |
| 230 | ); |
| 231 | $cases[] = array( |
| 232 | array('entity_table' => 'civicrm_activity', 'entity_id' => '999'), |
| 233 | array(), |
| 234 | ); |
| 235 | |
| 236 | return $cases; |
| 237 | } |
| 238 | |
| 239 | public function badGetProvider() { |
| 240 | $cases = array(); // array($getParams, $expectedNames) |
| 241 | |
| 242 | // Each search runs in a DB which contains these attachments: |
| 243 | // Activity #123: example_123.txt (text/plain) and example_123.csv (text/csv) |
| 244 | // Activity #456: example_456.txt (text/plain) and example_456.csv (text/csv) |
| 245 | |
| 246 | $cases[] = array( |
| 247 | array('check_permissions' => 1, 'mime_type' => 'text/plain'), |
| 248 | "/Mandatory key\\(s\\) missing from params array: 'id' or 'entity_table'/", |
| 249 | ); |
| 250 | $cases[] = array( |
| 251 | array('check_permissions' => 1, 'entity_id' => '123'), |
| 252 | "/Mandatory key\\(s\\) missing from params array: 'id' or 'entity_table'/", |
| 253 | ); |
| 254 | $cases[] = array( |
| 255 | array('check_permissions' => 1), |
| 256 | "/Mandatory key\\(s\\) missing from params array: 'id' or 'entity_table'/", |
| 257 | ); |
| 258 | $cases[] = array( |
| 259 | array('entity_table' => 'civicrm_activity', 'name' => 'example_456.csv'), |
| 260 | "/Get by name is not currently supported/", |
| 261 | ); |
| 262 | $cases[] = array( |
| 263 | array('entity_table' => 'civicrm_activity', 'content' => 'test'), |
| 264 | "/Get by content is not currently supported/", |
| 265 | ); |
| 266 | $cases[] = array( |
| 267 | array('entity_table' => 'civicrm_activity', 'path' => '/home/foo'), |
| 268 | "/Get by path is not currently supported/", |
| 269 | ); |
| 270 | $cases[] = array( |
| 271 | array('entity_table' => 'civicrm_activity', 'url' => '/index.php'), |
| 272 | "/Get by url is not currently supported/", |
| 273 | ); |
| 274 | |
| 275 | return $cases; |
| 276 | } |
| 277 | |
| 278 | /** |
| 279 | * Create an attachment using "content" and then "get" the attachment |
| 280 | * |
| 281 | * @param string $testEntityClass |
| 282 | * E.g. "CRM_Core_DAO_Activity". |
| 283 | * @param array $createParams |
| 284 | * @param string $expectedContent |
| 285 | * @dataProvider okCreateProvider |
| 286 | */ |
| 287 | public function testCreate($testEntityClass, $createParams, $expectedContent) { |
| 288 | $entity = CRM_Core_DAO::createTestObject($testEntityClass); |
| 289 | $entity_table = CRM_Core_DAO_AllCoreTables::getTableForClass($testEntityClass); |
| 290 | $this->assertTrue(is_numeric($entity->id)); |
| 291 | |
| 292 | $createResult = $this->callAPISuccess('Attachment', 'create', $createParams + array( |
| 293 | 'entity_table' => $entity_table, |
| 294 | 'entity_id' => $entity->id, |
| 295 | )); |
| 296 | $fileId = $createResult['id']; |
| 297 | $this->assertTrue(is_numeric($fileId)); |
| 298 | $this->assertEquals($entity_table, $createResult['values'][$fileId]['entity_table']); |
| 299 | $this->assertEquals($entity->id, $createResult['values'][$fileId]['entity_id']); |
| 300 | $this->assertEquals('My test description', $createResult['values'][$fileId]['description']); |
| 301 | $this->assertRegExp('/\d\d\d\d-\d\d-\d\d \d\d:\d\d:\d\d/', $createResult['values'][$fileId]['upload_date']); |
| 302 | $this->assertTrue(!isset($createResult['values'][$fileId]['content'])); |
| 303 | $this->assertTrue(!empty($createResult['values'][$fileId]['url'])); |
| 304 | $this->assertAttachmentExistence(TRUE, $createResult); |
| 305 | |
| 306 | $getResult = $this->callAPISuccess('Attachment', 'get', array( |
| 307 | 'entity_table' => $entity_table, |
| 308 | 'entity_id' => $entity->id, |
| 309 | )); |
| 310 | $this->assertEquals(1, $getResult['count']); |
| 311 | foreach (array('id', 'entity_table', 'entity_id', 'url') as $field) { |
| 312 | $this->assertEquals($createResult['values'][$fileId][$field], $getResult['values'][$fileId][$field], "Expect field $field to match"); |
| 313 | } |
| 314 | $this->assertTrue(!isset($getResult['values'][$fileId]['content'])); |
| 315 | |
| 316 | $getResult2 = $this->callAPISuccess('Attachment', 'get', array( |
| 317 | 'entity_table' => $entity_table, |
| 318 | 'entity_id' => $entity->id, |
| 319 | 'return' => array('content'), |
| 320 | )); |
| 321 | $this->assertEquals($expectedContent, $getResult2['values'][$fileId]['content']); |
| 322 | foreach (array('id', 'entity_table', 'entity_id', 'url') as $field) { |
| 323 | $this->assertEquals($createResult['values'][$fileId][$field], $getResult['values'][$fileId][$field], "Expect field $field to match"); |
| 324 | } |
| 325 | } |
| 326 | |
| 327 | /** |
| 328 | * @param $testEntityClass |
| 329 | * @param $createParams |
| 330 | * @param $expectedError |
| 331 | * @dataProvider badCreateProvider |
| 332 | */ |
| 333 | public function testCreateFailure($testEntityClass, $createParams, $expectedError) { |
| 334 | $entity = CRM_Core_DAO::createTestObject($testEntityClass); |
| 335 | $entity_table = CRM_Core_DAO_AllCoreTables::getTableForClass($testEntityClass); |
| 336 | $this->assertTrue(is_numeric($entity->id)); |
| 337 | |
| 338 | $createResult = $this->callAPIFailure('Attachment', 'create', $createParams + array( |
| 339 | 'entity_table' => $entity_table, |
| 340 | 'entity_id' => $entity->id, |
| 341 | )); |
| 342 | $this->assertRegExp($expectedError, $createResult['error_message']); |
| 343 | } |
| 344 | |
| 345 | /** |
| 346 | * @param $testEntityClass |
| 347 | * @param $createParams |
| 348 | * @param $updateParams |
| 349 | * @param $expectedError |
| 350 | * @dataProvider badUpdateProvider |
| 351 | */ |
| 352 | public function testCreateWithBadUpdate($testEntityClass, $createParams, $updateParams, $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->callAPISuccess('Attachment', 'create', $createParams + array( |
| 358 | 'entity_table' => $entity_table, |
| 359 | 'entity_id' => $entity->id, |
| 360 | )); |
| 361 | $fileId = $createResult['id']; |
| 362 | $this->assertTrue(is_numeric($fileId)); |
| 363 | |
| 364 | $updateResult = $this->callAPIFailure('Attachment', 'create', $updateParams + array( |
| 365 | 'id' => $fileId, |
| 366 | )); |
| 367 | $this->assertRegExp($expectedError, $updateResult['error_message']); |
| 368 | } |
| 369 | |
| 370 | /** |
| 371 | * If one submits a weird file name, it should be automatically converted |
| 372 | * to something safe. |
| 373 | */ |
| 374 | public function testCreateWithWeirdName() { |
| 375 | $entity = CRM_Core_DAO::createTestObject('CRM_Activity_DAO_Activity'); |
| 376 | $this->assertTrue(is_numeric($entity->id)); |
| 377 | |
| 378 | $createResult = $this->callAPISuccess('Attachment', 'create', array( |
| 379 | 'name' => self::getFilePrefix() . 'weird:na"me.txt', |
| 380 | 'mime_type' => 'text/plain', |
| 381 | 'description' => 'My test description', |
| 382 | 'content' => 'My test content', |
| 383 | 'entity_table' => 'civicrm_activity', |
| 384 | 'entity_id' => $entity->id, |
| 385 | )); |
| 386 | $fileId = $createResult['id']; |
| 387 | $this->assertTrue(is_numeric($fileId)); |
| 388 | $this->assertEquals(self::getFilePrefix() . 'weird_na_me.txt', $createResult['values'][$fileId]['name']); |
| 389 | } |
| 390 | |
| 391 | /** |
| 392 | * @param $getParams |
| 393 | * @param $expectedNames |
| 394 | * @dataProvider okGetProvider |
| 395 | */ |
| 396 | public function testGet($getParams, $expectedNames) { |
| 397 | foreach (array(123, 456) as $entity_id) { |
| 398 | foreach (array('text/plain' => '.txt', 'text/csv' => '.csv') as $mime => $ext) { |
| 399 | $this->callAPISuccess('Attachment', 'create', array( |
| 400 | 'name' => self::getFilePrefix() . 'example_' . $entity_id . $ext, |
| 401 | 'mime_type' => $mime, |
| 402 | 'description' => 'My test description', |
| 403 | 'content' => 'My test content', |
| 404 | 'entity_table' => 'civicrm_activity', |
| 405 | 'entity_id' => $entity_id, |
| 406 | )); |
| 407 | } |
| 408 | } |
| 409 | |
| 410 | $getResult = $this->callAPISuccess('Attachment', 'get', $getParams); |
| 411 | $actualNames = array_values(CRM_Utils_Array::collect('name', $getResult['values'])); |
| 412 | sort($actualNames); |
| 413 | sort($expectedNames); |
| 414 | $this->assertEquals($expectedNames, $actualNames); |
| 415 | } |
| 416 | |
| 417 | /** |
| 418 | * @param $getParams |
| 419 | * @param $expectedError |
| 420 | * @dataProvider badGetProvider |
| 421 | */ |
| 422 | public function testGetError($getParams, $expectedError) { |
| 423 | foreach (array(123, 456) as $entity_id) { |
| 424 | foreach (array('text/plain' => '.txt', 'text/csv' => '.csv') as $mime => $ext) { |
| 425 | $this->callAPISuccess('Attachment', 'create', array( |
| 426 | 'name' => self::getFilePrefix() . 'example_' . $entity_id . $ext, |
| 427 | 'mime_type' => $mime, |
| 428 | 'description' => 'My test description', |
| 429 | 'content' => 'My test content', |
| 430 | 'entity_table' => 'civicrm_activity', |
| 431 | 'entity_id' => $entity_id, |
| 432 | )); |
| 433 | } |
| 434 | } |
| 435 | |
| 436 | $getResult = $this->callAPIFailure('Attachment', 'get', $getParams); |
| 437 | $this->assertRegExp($expectedError, $getResult['error_message']); |
| 438 | } |
| 439 | |
| 440 | /** |
| 441 | * Take the values from a "get", make a small change, and then send |
| 442 | * the full thing back in as an update ("create"). This ensures some |
| 443 | * consistency in the acceptable formats. |
| 444 | */ |
| 445 | public function testGetThenUpdate() { |
| 446 | $entity = CRM_Core_DAO::createTestObject('CRM_Activity_DAO_Activity'); |
| 447 | $this->assertTrue(is_numeric($entity->id)); |
| 448 | |
| 449 | $createResult = $this->callAPISuccess('Attachment', 'create', array( |
| 450 | 'name' => self::getFilePrefix() . 'getThenUpdate.txt', |
| 451 | 'mime_type' => 'text/plain', |
| 452 | 'description' => 'My test description', |
| 453 | 'content' => 'My test content', |
| 454 | 'entity_table' => 'civicrm_activity', |
| 455 | 'entity_id' => $entity->id, |
| 456 | )); |
| 457 | $fileId = $createResult['id']; |
| 458 | $this->assertTrue(is_numeric($fileId)); |
| 459 | $this->assertEquals(self::getFilePrefix() . 'getThenUpdate.txt', $createResult['values'][$fileId]['name']); |
| 460 | $this->assertAttachmentExistence(TRUE, $createResult); |
| 461 | |
| 462 | $getResult = $this->callAPISuccess('Attachment', 'get', array( |
| 463 | 'id' => $fileId, |
| 464 | )); |
| 465 | $this->assertTrue(is_array($getResult['values'][$fileId])); |
| 466 | |
| 467 | $updateParams = $getResult['values'][$fileId]; |
| 468 | $updateParams['description'] = 'new description'; |
| 469 | $this->callAPISuccess('Attachment', 'create', $updateParams); |
| 470 | $this->assertAttachmentExistence(TRUE, $createResult); |
| 471 | } |
| 472 | |
| 473 | /** |
| 474 | * Create an attachment and delete using its ID. Assert that the records are correctly created and destroyed |
| 475 | * in the DB and the filesystem. |
| 476 | */ |
| 477 | public function testDeleteByID() { |
| 478 | $entity = CRM_Core_DAO::createTestObject('CRM_Activity_DAO_Activity'); |
| 479 | $this->assertTrue(is_numeric($entity->id)); |
| 480 | |
| 481 | foreach (array('first', 'second') as $n) { |
| 482 | $createResults[$n] = $this->callAPISuccess('Attachment', 'create', array( |
| 483 | 'name' => self::getFilePrefix() . 'testDeleteByID.txt', |
| 484 | 'mime_type' => 'text/plain', |
| 485 | 'content' => 'My test content', |
| 486 | 'entity_table' => 'civicrm_activity', |
| 487 | 'entity_id' => $entity->id, |
| 488 | )); |
| 489 | $this->assertTrue(is_numeric($createResults[$n]['id'])); |
| 490 | $this->assertEquals(self::getFilePrefix() . 'testDeleteByID.txt', $createResults[$n]['values'][$createResults[$n]['id']]['name']); |
| 491 | } |
| 492 | $this->assertAttachmentExistence(TRUE, $createResults['first']); |
| 493 | $this->assertAttachmentExistence(TRUE, $createResults['second']); |
| 494 | |
| 495 | $this->callAPISuccess('Attachment', 'delete', array( |
| 496 | 'id' => $createResults['first']['id'], |
| 497 | )); |
| 498 | $this->assertAttachmentExistence(FALSE, $createResults['first']); |
| 499 | $this->assertAttachmentExistence(TRUE, $createResults['second']); |
| 500 | } |
| 501 | |
| 502 | /** |
| 503 | * Create an attachment and delete using its ID. Assert that the records are correctly created and destroyed |
| 504 | * in the DB and the filesystem. |
| 505 | */ |
| 506 | public function testDeleteByEntity() { |
| 507 | // create 2 entities (keepme,delme) -- each with 2 attachments (first,second) |
| 508 | foreach (array('keepme', 'delme') as $e) { |
| 509 | $entities[$e] = CRM_Core_DAO::createTestObject('CRM_Activity_DAO_Activity'); |
| 510 | $this->assertTrue(is_numeric($entities[$e]->id)); |
| 511 | foreach (array('first', 'second') as $n) { |
| 512 | $createResults[$e][$n] = $this->callAPISuccess('Attachment', 'create', array( |
| 513 | 'name' => self::getFilePrefix() . 'testDeleteByEntity.txt', |
| 514 | 'mime_type' => 'text/plain', |
| 515 | 'content' => 'My test content', |
| 516 | 'entity_table' => 'civicrm_activity', |
| 517 | 'entity_id' => $entities[$e]->id, |
| 518 | )); |
| 519 | $this->assertTrue(is_numeric($createResults[$e][$n]['id'])); |
| 520 | } |
| 521 | } |
| 522 | $this->assertAttachmentExistence(TRUE, $createResults['keepme']['first']); |
| 523 | $this->assertAttachmentExistence(TRUE, $createResults['keepme']['second']); |
| 524 | $this->assertAttachmentExistence(TRUE, $createResults['delme']['first']); |
| 525 | $this->assertAttachmentExistence(TRUE, $createResults['delme']['second']); |
| 526 | |
| 527 | $this->callAPISuccess('Attachment', 'delete', array( |
| 528 | 'entity_table' => 'civicrm_activity', |
| 529 | 'entity_id' => $entities[$e]->id, |
| 530 | )); |
| 531 | $this->assertAttachmentExistence(TRUE, $createResults['keepme']['first']); |
| 532 | $this->assertAttachmentExistence(TRUE, $createResults['keepme']['second']); |
| 533 | $this->assertAttachmentExistence(FALSE, $createResults['delme']['first']); |
| 534 | $this->assertAttachmentExistence(FALSE, $createResults['delme']['second']); |
| 535 | } |
| 536 | |
| 537 | protected function assertAttachmentExistence($exists, $apiResult) { |
| 538 | $fileId = $apiResult['id']; |
| 539 | $this->assertTrue(is_numeric($fileId)); |
| 540 | $this->assertEquals($exists, file_exists($apiResult['values'][$fileId]['path'])); |
| 541 | $this->assertDBQuery($exists ? 1 : 0, 'SELECT count(*) FROM civicrm_file WHERE id = %1', array( |
| 542 | 1 => array($fileId, 'Int'), |
| 543 | )); |
| 544 | $this->assertDBQuery($exists ? 1 : 0, 'SELECT count(*) FROM civicrm_entity_file WHERE id = %1', array( |
| 545 | 1 => array($fileId, 'Int'), |
| 546 | )); |
| 547 | } |
| 548 | |
| 549 | protected function tmpFile($name) { |
| 550 | $tmpDir = sys_get_temp_dir(); |
| 551 | $this->assertTrue($tmpDir && is_dir($tmpDir), 'Tmp dir must exist: ' . $tmpDir); |
| 552 | return $tmpDir . '/' . self::getFilePrefix() . $name; |
| 553 | } |
| 554 | |
| 555 | protected function cleanupFiles() { |
| 556 | $config = CRM_Core_Config::singleton(); |
| 557 | $dirs = array( |
| 558 | sys_get_temp_dir(), |
| 559 | $config->customFileUploadDir, |
| 560 | ); |
| 561 | foreach ($dirs as $dir) { |
| 562 | $files = (array) glob($dir . "/" . self::getFilePrefix() . "*"); |
| 563 | foreach ($files as $file) { |
| 564 | unlink($file); |
| 565 | } |
| 566 | |
| 567 | } |
| 568 | } |
| 569 | } |