From 7523c3067e05b536adb41772ffa159c2529e35cb Mon Sep 17 00:00:00 2001 From: Constantine Nathanson <35217733+const-cloudinary@users.noreply.github.com> Date: Tue, 7 Jan 2025 02:20:31 +0200 Subject: [PATCH] Add support for `deleteAssetsByAssetIds` Admin API --- src/Api/Admin/AssetsTrait.php | 16 +++++ .../Admin/Assets/DeleteAssetsTest.php | 69 +++++++++++++++---- 2 files changed, 73 insertions(+), 12 deletions(-) diff --git a/src/Api/Admin/AssetsTrait.php b/src/Api/Admin/AssetsTrait.php index d058e3b3..ebf926a1 100644 --- a/src/Api/Admin/AssetsTrait.php +++ b/src/Api/Admin/AssetsTrait.php @@ -373,6 +373,22 @@ public function deleteAssets(array|string $publicIds, array $options = []): ApiR return $this->apiClient->delete($uri, $params); } + /** + * Deletes the specified assets by asset IDs. + * + * @param array|string $assetIds The asset IDs of the assets to delete. + * @param array $options Additional optional parameters. + * + * @return ApiResponse The result of the command. + */ + public function deleteAssetsByAssetIds(array|string $assetIds, array $options = []): ApiResponse + { + $uri = [ApiEndPoint::ASSETS]; + $params = self::prepareDeleteAssetParams($options, ['asset_ids' => ArrayUtils::build($assetIds)]); + + return $this->apiClient->deleteJson($uri, $params); + } + /** * Deletes assets by prefix. * diff --git a/tests/Integration/Admin/Assets/DeleteAssetsTest.php b/tests/Integration/Admin/Assets/DeleteAssetsTest.php index 8734876a..8f539bf0 100644 --- a/tests/Integration/Admin/Assets/DeleteAssetsTest.php +++ b/tests/Integration/Admin/Assets/DeleteAssetsTest.php @@ -27,13 +27,16 @@ final class DeleteAssetsTest extends IntegrationTestCase const TRANSFORMATION = ['width' => 400, 'height' => 400, 'crop' => 'crop']; const TRANSFORMATION_AS_STRING = 'c_crop,h_400,w_400'; - const MULTI_DELETE_OPTION_1 = 'multi_delete_option_1'; - const MULTI_DELETE_OPTION_2 = 'multi_delete_option_2'; - const MULTI_DELETE_1 = 'multi_delete_1'; - const MULTI_DELETE_2 = 'multi_delete_2'; - const DELETE_DERIVED = 'delete_derived'; - const DELETE_SINGLE = 'delete_single'; - const PRIVATE_ASSET = 'private_asset'; + const MULTI_DELETE_OPTION_1 = 'multi_delete_option_1'; + const MULTI_DELETE_OPTION_2 = 'multi_delete_option_2'; + const MULTI_DELETE_1 = 'multi_delete_1'; + const MULTI_DELETE_ASSET_ID_1 = 'multi_delete_asset_id_1'; + const MULTI_DELETE_2 = 'multi_delete_2'; + const MULTI_DELETE_ASSET_ID_2 = 'multi_delete_asset_id_2'; + const DELETE_DERIVED = 'delete_derived'; + const DELETE_SINGLE = 'delete_single'; + const DELETE_SINGLE_ASSET_ID = 'delete_single_asset_id'; + const PRIVATE_ASSET = 'private_asset'; private static $DELETE_PREFIX; private static $FULL_DELETE_PREFIX; @@ -62,8 +65,11 @@ public static function setUpBeforeClass() ], self::DELETE_DERIVED, self::MULTI_DELETE_1, + self::MULTI_DELETE_ASSET_ID_1, self::MULTI_DELETE_2, + self::MULTI_DELETE_ASSET_ID_2, self::DELETE_SINGLE, + self::DELETE_SINGLE_ASSET_ID, self::$DELETE_PREFIX, [ 'options' => [ @@ -139,6 +145,21 @@ public function testDeleteSingleAssetByPublicId() self::$adminApi->asset(self::getTestAssetPublicId(self::DELETE_SINGLE)); } + /** + * Delete uploaded images by a single Asset ID given as a string. + * + * @throws ApiError + */ + public function testDeleteSingleAssetByAssetId() + { + $result = self::$adminApi->deleteAssetsByAssetIds(self::getTestAssetAssetId(self::DELETE_SINGLE_ASSET_ID)); + + self::assertAssetDeleted($result, self::getTestAssetAssetId(self::DELETE_SINGLE_ASSET_ID)); + + $this->expectException(NotFound::class); + self::$adminApi->assetByAssetId(self::getTestAssetAssetId(self::DELETE_SINGLE_ASSET_ID)); + } + /** * Delete multiple uploaded images by public IDs given in an array. * @@ -163,6 +184,30 @@ public function testDeleteMultipleAssetsByPublicIds() self::$adminApi->asset(self::getTestAssetPublicId(self::MULTI_DELETE_2)); } + /** + * Delete multiple uploaded images by asset IDs given in an array. + * + * @throws ApiError + */ + public function testDeleteMultipleAssetsByAssetIds() + { + $result = self::$adminApi->deleteAssetsByAssetIds( + [ + self::getTestAssetAssetId(self::MULTI_DELETE_ASSET_ID_1), + self::getTestAssetAssetId(self::MULTI_DELETE_ASSET_ID_2), + ] + ); + + self::assertAssetDeleted($result, self::getTestAssetAssetId(self::MULTI_DELETE_ASSET_ID_1), 2); + self::assertAssetDeleted($result, self::getTestAssetAssetId(self::MULTI_DELETE_ASSET_ID_2), 2); + + $this->expectException(NotFound::class); + self::$adminApi->assetByAssetId(self::getTestAssetAssetId(self::MULTI_DELETE_ASSET_ID_1)); + + $this->expectException(NotFound::class); + self::$adminApi->assetByAssetId(self::getTestAssetAssetId(self::MULTI_DELETE_ASSET_ID_2)); + } + /** * Delete uploaded images by public IDs with options. * @@ -177,7 +222,7 @@ public function testDeleteAssetsByPublicIdWithOptions() 'nonexistent_id', ], [ - DeliveryType::KEY => DeliveryType::PRIVATE_DELIVERY + DeliveryType::KEY => DeliveryType::PRIVATE_DELIVERY, ] ); @@ -264,7 +309,7 @@ public function testDeleteDerivedImagesOnly() $result = self::$adminApi->deleteAssets( [ - self::getTestAssetPublicId(self::DELETE_DERIVED) + self::getTestAssetPublicId(self::DELETE_DERIVED), ], [ 'keep_original' => true, @@ -315,7 +360,7 @@ public function testDeleteAssetsByOptions() 'file' => self::TEST_DOCX_PATH, 'tags' => [self::$UNIQUE_TEST_TAG_DELETE_OPTIONS], ], - ] + ], ] ); $result = self::$adminApi->deleteAllAssets( @@ -330,7 +375,7 @@ public function testDeleteAssetsByOptions() $assets = self::$adminApi->assetsByTag( self::$UNIQUE_TEST_TAG_DELETE_OPTIONS, [ - AssetType::KEY => AssetType::RAW + AssetType::KEY => AssetType::RAW, ] ); @@ -339,7 +384,7 @@ public function testDeleteAssetsByOptions() $assets['resources'][0], [ DeliveryType::KEY => DeliveryType::UPLOAD, - AssetType::KEY => AssetType::RAW + AssetType::KEY => AssetType::RAW, ] ); }