From 35f5e3780cead45b427024bfe31f3d79bbfa1539 Mon Sep 17 00:00:00 2001 From: Monish Deb Date: Mon, 7 Jun 2021 20:28:10 +0530 Subject: [PATCH] test failure fixes --- CRM/Core/BAO/Attachment.php | 11 +++++++++-- .../Service/Spec/Provider/AttachmentSpecProvider.php | 3 ++- api/v3/Attachment.php | 5 +++-- tests/phpunit/api/v3/SyntaxConformanceTest.php | 3 +++ 4 files changed, 17 insertions(+), 5 deletions(-) diff --git a/CRM/Core/BAO/Attachment.php b/CRM/Core/BAO/Attachment.php index b9c52e7e2aed..7709d8bb7625 100644 --- a/CRM/Core/BAO/Attachment.php +++ b/CRM/Core/BAO/Attachment.php @@ -25,7 +25,7 @@ class CRM_Core_BAO_Attachment extends CRM_Core_DAO { * * @param array $params */ - public static function create($params): void { + public static function create($params): array { $config = CRM_Core_Config::singleton(); list($id, $file, $entityFile, $name, $content, $moveFile, $isTrusted, $returnContent) = self::parseParams($params); @@ -71,6 +71,11 @@ public static function create($params): void { $entityFileDao->file_id = $fileDao->id; $entityFileDao->save(); + $attachment = array_merge($fileDao->toArray(), [ + 'entity_table' => $entityFileDao->entity_table, + 'entity_id' => $entityFileDao->entity_id, + ]); + $path = $config->customFileUploadDir . DIRECTORY_SEPARATOR . $fileDao->uri; if (is_string($content)) { file_put_contents($path, $content); @@ -93,8 +98,10 @@ public static function create($params): void { } $result = [ - $fileDao->id => self::formatResult($fileDao, $entityFileDao, $returnContent, $isTrusted), + $attachment['id'] => self::formatResult($attachment, $isTrusted, ($params['return'] ?? [])), ]; + + return $result; } /** diff --git a/Civi/Api4/Service/Spec/Provider/AttachmentSpecProvider.php b/Civi/Api4/Service/Spec/Provider/AttachmentSpecProvider.php index 0927b9f16bb4..88ad22d4ba27 100644 --- a/Civi/Api4/Service/Spec/Provider/AttachmentSpecProvider.php +++ b/Civi/Api4/Service/Spec/Provider/AttachmentSpecProvider.php @@ -32,6 +32,8 @@ public function modifySpec(RequestSpec $spec) { if ($action == 'create') { $spec->getFieldByName('mime_type')->setRequired(TRUE); + $spec->getFieldByName('entity_table')->setRequiredIf('empty($values.field_name)'); + $spec->getFieldByName('content')->setRequiredIf('empty($values.options.move-file)'); } if ($action == 'update') { $spec->getFieldByName('id')->setRequired(TRUE); @@ -49,7 +51,6 @@ public function modifySpec(RequestSpec $spec) { else { $spec->getFieldByName('entity_id')->setRequired(TRUE); $spec->getFieldByName('upload_date')->setDefaultValue('now'); - $spec->getFieldByName('entity_table')->setRequiredIf('empty($values.id) && empty($values.field_name)'); } } diff --git a/api/v3/Attachment.php b/api/v3/Attachment.php index 349159f10451..b80df233bb9a 100644 --- a/api/v3/Attachment.php +++ b/api/v3/Attachment.php @@ -92,7 +92,8 @@ function civicrm_api3_attachment_create($params) { // When creating we need either entity_table or field_name. civicrm_api3_verify_one_mandatory($params, NULL, ['entity_table', 'field_name']); } - return _civicrm_api3_basic_get(_civicrm_api3_get_BAO(__FUNCTION__), $params);; + $result = CRM_Core_BAO_Attachment::create($params); + return civicrm_api3_create_success($result, $params, 'Attachment', 'create'); } /** @@ -111,7 +112,7 @@ function civicrm_api3_attachment_get($params) { $result = []; $returnProperties = $params['return'] ?? []; foreach ($attachments as $attachment) { - $result[$attachement['id']] = CRM_Core_BAO_Attachment::formatResult($attachment, $isTrusted, $returnProperties); + $result[$attachment['id']] = CRM_Core_BAO_Attachment::formatResult($attachment, $isTrusted, $returnProperties); } return civicrm_api3_create_success($result, $params, 'Attachment', 'create'); } diff --git a/tests/phpunit/api/v3/SyntaxConformanceTest.php b/tests/phpunit/api/v3/SyntaxConformanceTest.php index 01e7c2a8a490..a9d71c5eb3fa 100644 --- a/tests/phpunit/api/v3/SyntaxConformanceTest.php +++ b/tests/phpunit/api/v3/SyntaxConformanceTest.php @@ -465,6 +465,7 @@ public static function toBeSkipped_automock($sequential = FALSE) { 'System', 'Logging', 'Payment', + 'Attachment', ]; if ($sequential === TRUE) { return $entitiesWithoutGet; @@ -562,6 +563,8 @@ public static function toBeSkipped_getlimit() { //a bit of a pseudoapi - keys by domain 'Payment', // pseudoapi - problems with creating required sub entities. + 'Attachment', + // pseudoapi - problems with creating required sub entities. ]; return $entitiesWithout; }