Skip to content

Commit 121376a

Browse files
Add support for deleteAssetsByAssetIds Admin API
1 parent cbea5ba commit 121376a

File tree

2 files changed

+73
-12
lines changed

2 files changed

+73
-12
lines changed

src/Api/Admin/AssetsTrait.php

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -373,6 +373,22 @@ public function deleteAssets(array|string $publicIds, array $options = []): ApiR
373373
return $this->apiClient->delete($uri, $params);
374374
}
375375

376+
/**
377+
* Deletes the specified assets by asset IDs.
378+
*
379+
* @param array|string $assetIds The asset IDs of the assets to delete.
380+
* @param array $options Additional optional parameters.
381+
*
382+
* @return ApiResponse The result of the command.
383+
*/
384+
public function deleteAssetsByAssetIds(array|string $assetIds, array $options = []): ApiResponse
385+
{
386+
$uri = [ApiEndPoint::ASSETS];
387+
$params = self::prepareDeleteAssetParams($options, ['asset_ids' => ArrayUtils::build($assetIds)]);
388+
389+
return $this->apiClient->deleteJson($uri, $params);
390+
}
391+
376392
/**
377393
* Deletes assets by prefix.
378394
*

tests/Integration/Admin/Assets/DeleteAssetsTest.php

Lines changed: 57 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -27,13 +27,16 @@ final class DeleteAssetsTest extends IntegrationTestCase
2727
const TRANSFORMATION = ['width' => 400, 'height' => 400, 'crop' => 'crop'];
2828
const TRANSFORMATION_AS_STRING = 'c_crop,h_400,w_400';
2929

30-
const MULTI_DELETE_OPTION_1 = 'multi_delete_option_1';
31-
const MULTI_DELETE_OPTION_2 = 'multi_delete_option_2';
32-
const MULTI_DELETE_1 = 'multi_delete_1';
33-
const MULTI_DELETE_2 = 'multi_delete_2';
34-
const DELETE_DERIVED = 'delete_derived';
35-
const DELETE_SINGLE = 'delete_single';
36-
const PRIVATE_ASSET = 'private_asset';
30+
const MULTI_DELETE_OPTION_1 = 'multi_delete_option_1';
31+
const MULTI_DELETE_OPTION_2 = 'multi_delete_option_2';
32+
const MULTI_DELETE_1 = 'multi_delete_1';
33+
const MULTI_DELETE_ASSET_ID_1 = 'multi_delete_asset_id_1';
34+
const MULTI_DELETE_2 = 'multi_delete_2';
35+
const MULTI_DELETE_ASSET_ID_2 = 'multi_delete_asset_id_2';
36+
const DELETE_DERIVED = 'delete_derived';
37+
const DELETE_SINGLE = 'delete_single';
38+
const DELETE_SINGLE_ASSET_ID = 'delete_single_asset_id';
39+
const PRIVATE_ASSET = 'private_asset';
3740

3841
private static $DELETE_PREFIX;
3942
private static $FULL_DELETE_PREFIX;
@@ -62,8 +65,11 @@ public static function setUpBeforeClass()
6265
],
6366
self::DELETE_DERIVED,
6467
self::MULTI_DELETE_1,
68+
self::MULTI_DELETE_ASSET_ID_1,
6569
self::MULTI_DELETE_2,
70+
self::MULTI_DELETE_ASSET_ID_2,
6671
self::DELETE_SINGLE,
72+
self::DELETE_SINGLE_ASSET_ID,
6773
self::$DELETE_PREFIX,
6874
[
6975
'options' => [
@@ -139,6 +145,21 @@ public function testDeleteSingleAssetByPublicId()
139145
self::$adminApi->asset(self::getTestAssetPublicId(self::DELETE_SINGLE));
140146
}
141147

148+
/**
149+
* Delete uploaded images by a single Asset ID given as a string.
150+
*
151+
* @throws ApiError
152+
*/
153+
public function testDeleteSingleAssetByAssetId()
154+
{
155+
$result = self::$adminApi->deleteAssetsByAssetIds(self::getTestAssetAssetId(self::DELETE_SINGLE_ASSET_ID));
156+
157+
self::assertAssetDeleted($result, self::getTestAssetAssetId(self::DELETE_SINGLE_ASSET_ID));
158+
159+
$this->expectException(NotFound::class);
160+
self::$adminApi->assetByAssetId(self::getTestAssetAssetId(self::DELETE_SINGLE_ASSET_ID));
161+
}
162+
142163
/**
143164
* Delete multiple uploaded images by public IDs given in an array.
144165
*
@@ -163,6 +184,30 @@ public function testDeleteMultipleAssetsByPublicIds()
163184
self::$adminApi->asset(self::getTestAssetPublicId(self::MULTI_DELETE_2));
164185
}
165186

187+
/**
188+
* Delete multiple uploaded images by asset IDs given in an array.
189+
*
190+
* @throws ApiError
191+
*/
192+
public function testDeleteMultipleAssetsByAssetIds()
193+
{
194+
$result = self::$adminApi->deleteAssetsByAssetIds(
195+
[
196+
self::getTestAssetAssetId(self::MULTI_DELETE_ASSET_ID_1),
197+
self::getTestAssetAssetId(self::MULTI_DELETE_ASSET_ID_2),
198+
]
199+
);
200+
201+
self::assertAssetDeleted($result, self::getTestAssetAssetId(self::MULTI_DELETE_ASSET_ID_1), 2);
202+
self::assertAssetDeleted($result, self::getTestAssetAssetId(self::MULTI_DELETE_ASSET_ID_2), 2);
203+
204+
$this->expectException(NotFound::class);
205+
self::$adminApi->assetByAssetId(self::getTestAssetAssetId(self::MULTI_DELETE_ASSET_ID_1));
206+
207+
$this->expectException(NotFound::class);
208+
self::$adminApi->assetByAssetId(self::getTestAssetAssetId(self::MULTI_DELETE_ASSET_ID_2));
209+
}
210+
166211
/**
167212
* Delete uploaded images by public IDs with options.
168213
*
@@ -177,7 +222,7 @@ public function testDeleteAssetsByPublicIdWithOptions()
177222
'nonexistent_id',
178223
],
179224
[
180-
DeliveryType::KEY => DeliveryType::PRIVATE_DELIVERY
225+
DeliveryType::KEY => DeliveryType::PRIVATE_DELIVERY,
181226
]
182227
);
183228

@@ -264,7 +309,7 @@ public function testDeleteDerivedImagesOnly()
264309

265310
$result = self::$adminApi->deleteAssets(
266311
[
267-
self::getTestAssetPublicId(self::DELETE_DERIVED)
312+
self::getTestAssetPublicId(self::DELETE_DERIVED),
268313
],
269314
[
270315
'keep_original' => true,
@@ -315,7 +360,7 @@ public function testDeleteAssetsByOptions()
315360
'file' => self::TEST_DOCX_PATH,
316361
'tags' => [self::$UNIQUE_TEST_TAG_DELETE_OPTIONS],
317362
],
318-
]
363+
],
319364
]
320365
);
321366
$result = self::$adminApi->deleteAllAssets(
@@ -330,7 +375,7 @@ public function testDeleteAssetsByOptions()
330375
$assets = self::$adminApi->assetsByTag(
331376
self::$UNIQUE_TEST_TAG_DELETE_OPTIONS,
332377
[
333-
AssetType::KEY => AssetType::RAW
378+
AssetType::KEY => AssetType::RAW,
334379
]
335380
);
336381

@@ -339,7 +384,7 @@ public function testDeleteAssetsByOptions()
339384
$assets['resources'][0],
340385
[
341386
DeliveryType::KEY => DeliveryType::UPLOAD,
342-
AssetType::KEY => AssetType::RAW
387+
AssetType::KEY => AssetType::RAW,
343388
]
344389
);
345390
}

0 commit comments

Comments
 (0)