Skip to content

Commit

Permalink
Add support for restoreByAssetIds Admin API
Browse files Browse the repository at this point in the history
  • Loading branch information
const-cloudinary committed Jan 7, 2025
1 parent 121376a commit 703006b
Show file tree
Hide file tree
Showing 2 changed files with 69 additions and 0 deletions.
18 changes: 18 additions & 0 deletions src/Api/Admin/AssetsTrait.php
Original file line number Diff line number Diff line change
Expand Up @@ -301,6 +301,24 @@ public function restore(array|string $publicIds, array $options = []): ApiRespon
return $this->apiClient->postJson($uri, $params);
}

/**
* Reverts to the latest backed up version of the specified deleted assets by asset IDs.
*
* @param array|string $assetIds The asset IDs of the backed up assets to restore. They can be existing or
* deleted assets.
* @param array $options The optional parameters.
*
* @return ApiResponse The result of the restore operation.
*/
public function restoreByAssetIds(array|string $assetIds, array $options = []): ApiResponse
{
$uri = [ApiEndPoint::ASSETS, 'restore'];

$params = array_merge($options, ['asset_ids' => ArrayUtils::build($assetIds)]);

return $this->apiClient->postJson($uri, $params);
}

/**
* Updates details of an existing asset.
*
Expand Down
51 changes: 51 additions & 0 deletions tests/Unit/Admin/AssetsTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -127,6 +127,57 @@ public function testRestoreDeletedAssetSpecificVersion($publicIds, $options, $ur
self::assertRequestJsonBodySubset($lastRequest, $bodyFields);
}

/**
* @return array[]
*/
public function restoreDeletedAssetByAssetIDSpecificVersionDataProvider()
{
return [
[
'assetIds' => self::API_TEST_ASSET_ID,
'options' => [
'versions' => ['293272f6bd9ec6ae9fa643e295b4dd1b'],
],
'url' => '/resources/restore',
'bodyFields' => [
'asset_ids' => [self::API_TEST_ASSET_ID],
'versions' => ['293272f6bd9ec6ae9fa643e295b4dd1b'],
],
],
[
'assetIds' => [self::API_TEST_ASSET_ID2, self::API_TEST_ASSET_ID3],
'options' => [
'versions' => ['9fa643e295b4dd1b293272f6bd9ec6ae', 'b4dd1b293272f6bd9fa643e2959ec6ae'],
],
'url' => '/resources/restore',
'bodyFields' => [
'asset_ids' => [self::API_TEST_ASSET_ID2, self::API_TEST_ASSET_ID3],
'versions' => ['9fa643e295b4dd1b293272f6bd9ec6ae', 'b4dd1b293272f6bd9fa643e2959ec6ae'],
],
],
];
}

/**
* Restore specific versions of a deleted asset by asset IDs.
*
* @dataProvider restoreDeletedAssetByAssetIDSpecificVersionDataProvider
*
* @param array|string $assetIds
* @param array $options
* @param string $url
* @param array $bodyFields
*/
public function testRestoreDeletedAssetByAssetIDSpecificVersion($assetIds, $options, $url, $bodyFields)
{
$mockAdminApi = new MockAdminApi();
$mockAdminApi->restoreByAssetIds($assetIds, $options);
$lastRequest = $mockAdminApi->getMockHandler()->getLastRequest();

self::assertRequestUrl($lastRequest, $url);
self::assertRequestJsonBodySubset($lastRequest, $bodyFields);
}

/**
* Test update assets complex fields serialization.
*/
Expand Down

0 comments on commit 703006b

Please sign in to comment.