From 5c5967ad463077d0738baa14f26e3852c3fd7eea Mon Sep 17 00:00:00 2001 From: Constantine Nathanson <35217733+const-cloudinary@users.noreply.github.com> Date: Tue, 7 Jan 2025 02:40:51 +0200 Subject: [PATCH] Add support for `restoreByAssetIds` Admin API --- src/Api/Admin/AssetsTrait.php | 18 ++++++++++++ tests/Unit/Admin/AssetsTest.php | 51 +++++++++++++++++++++++++++++++++ 2 files changed, 69 insertions(+) diff --git a/src/Api/Admin/AssetsTrait.php b/src/Api/Admin/AssetsTrait.php index ebf926a1..ff0994a2 100644 --- a/src/Api/Admin/AssetsTrait.php +++ b/src/Api/Admin/AssetsTrait.php @@ -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. * diff --git a/tests/Unit/Admin/AssetsTest.php b/tests/Unit/Admin/AssetsTest.php index 25c31ae0..aace4b19 100644 --- a/tests/Unit/Admin/AssetsTest.php +++ b/tests/Unit/Admin/AssetsTest.php @@ -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. */