Skip to content

Commit

Permalink
Add support for deleteAssetsByAssetIds Admin API
Browse files Browse the repository at this point in the history
  • Loading branch information
const-cloudinary committed Jan 7, 2025
1 parent cbea5ba commit 7523c30
Show file tree
Hide file tree
Showing 2 changed files with 73 additions and 12 deletions.
16 changes: 16 additions & 0 deletions src/Api/Admin/AssetsTrait.php
Original file line number Diff line number Diff line change
Expand Up @@ -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.
*
Expand Down
69 changes: 57 additions & 12 deletions tests/Integration/Admin/Assets/DeleteAssetsTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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' => [
Expand Down Expand Up @@ -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.
*
Expand All @@ -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.
*
Expand All @@ -177,7 +222,7 @@ public function testDeleteAssetsByPublicIdWithOptions()
'nonexistent_id',
],
[
DeliveryType::KEY => DeliveryType::PRIVATE_DELIVERY
DeliveryType::KEY => DeliveryType::PRIVATE_DELIVERY,
]
);

Expand Down Expand Up @@ -264,7 +309,7 @@ public function testDeleteDerivedImagesOnly()

$result = self::$adminApi->deleteAssets(
[
self::getTestAssetPublicId(self::DELETE_DERIVED)
self::getTestAssetPublicId(self::DELETE_DERIVED),
],
[
'keep_original' => true,
Expand Down Expand Up @@ -315,7 +360,7 @@ public function testDeleteAssetsByOptions()
'file' => self::TEST_DOCX_PATH,
'tags' => [self::$UNIQUE_TEST_TAG_DELETE_OPTIONS],
],
]
],
]
);
$result = self::$adminApi->deleteAllAssets(
Expand All @@ -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,
]
);

Expand All @@ -339,7 +384,7 @@ public function testDeleteAssetsByOptions()
$assets['resources'][0],
[
DeliveryType::KEY => DeliveryType::UPLOAD,
AssetType::KEY => AssetType::RAW
AssetType::KEY => AssetType::RAW,
]
);
}
Expand Down

0 comments on commit 7523c30

Please sign in to comment.