Skip to content

Commit

Permalink
Add support for related assets Admin APIs
Browse files Browse the repository at this point in the history
  • Loading branch information
const-cloudinary committed Apr 11, 2023
1 parent 71be0d6 commit 223695b
Show file tree
Hide file tree
Showing 6 changed files with 202 additions and 3 deletions.
1 change: 1 addition & 0 deletions src/Api/Admin/ApiEndPoint.php
Expand Up @@ -19,6 +19,7 @@ class ApiEndPoint
const USAGE = 'usage';
const ASSETS = 'resources';
const DERIVED_ASSETS = 'derived_resources';
const RELATED_ASSETS = 'related_assets';
const FOLDERS = 'folders';
const TAGS = 'tags';
const STREAMING_PROFILES = 'streaming_profiles';
Expand Down
90 changes: 90 additions & 0 deletions src/Api/Admin/AssetsTrait.php
Expand Up @@ -505,6 +505,94 @@ public function deleteDerivedByTransformation($publicIds, $transformations = [],
return $this->apiClient->delete($uri, $params);
}

/**
* Relates an asset to other assets by public IDs.
*
* @param string $publicId The public ID of the asset to update.
* @param array $assetsToRelate The array of up to 10 fully_qualified_public_ids given as
* resource_type/type/public_id.
* @param array $options The optional parameters. See the
* <a href=https://cloudinary.com/documentation/admin_api#add_related_assets target="_blank"> Admin API</a> documentation.
*
* @return ApiResponse
*/
public function addRelatedAssets($publicId, $assetsToRelate, $options = [])
{
$assetType = ArrayUtils::get($options, AssetType::KEY, AssetType::IMAGE);
$type = ArrayUtils::get($options, DeliveryType::KEY, DeliveryType::UPLOAD);

$uri = [ApiEndPoint::ASSETS, ApiEndPoint::RELATED_ASSETS, $assetType, $type, $publicId];

$params = [
'assets_to_relate' => ArrayUtils::build($assetsToRelate),
];

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

/**
* Relates an asset to other assets by asset IDs.
*
* @param string $assetId The asset ID of the asset to update.
* @param array $assetsToRelate The array of up to 10 asset IDs.
*
* @return ApiResponse
*/
public function addRelatedAssetsByAssetIds($assetId, $assetsToRelate)
{
$uri = [ApiEndPoint::ASSETS, ApiEndPoint::RELATED_ASSETS, $assetId];

$params = [
'assets_to_relate' => ArrayUtils::build($assetsToRelate),
];

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

/**
* Unrelates an asset from other assets by public IDs.
*
* @param string $publicId The public ID of the asset to update.
* @param array $assetsToUnrelate The array of up to 10 fully_qualified_public_ids given as
* resource_type/type/public_id.
* @param array $options The optional parameters. See the
* <a href=https://cloudinary.com/documentation/admin_api#delete_related_assets target="_blank"> Admin API</a> documentation.
*
* @return ApiResponse
*/
public function deleteRelatedAssets($publicId, $assetsToUnrelate, $options = [])
{
$assetType = ArrayUtils::get($options, AssetType::KEY, AssetType::IMAGE);
$type = ArrayUtils::get($options, DeliveryType::KEY, DeliveryType::UPLOAD);

$uri = [ApiEndPoint::ASSETS, ApiEndPoint::RELATED_ASSETS, $assetType, $type, $publicId];

$params = [
'assets_to_unrelate' => ArrayUtils::build($assetsToUnrelate),
];

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

/**
* Unrelates an asset from other assets by asset IDs.
*
* @param string $assetId The asset ID of the asset to update.
* @param array $assetsToUnrelate The array of up to 10 asset IDs.
*
* @return ApiResponse
*/
public function deleteRelatedAssetsByAssetIds($assetId, $assetsToUnrelate)
{
$uri = [ApiEndPoint::ASSETS, ApiEndPoint::RELATED_ASSETS, $assetId];

$params = [
'assets_to_unrelate' => ArrayUtils::build($assetsToUnrelate),
];

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

/**
* Prepares optional parameters for delete asset API calls.
*
Expand Down Expand Up @@ -553,6 +641,8 @@ protected static function prepareAssetDetailsParams($options)
'derived_next_cursor',
'accessibility_analysis',
'versions',
'related',
'related_next_cursor',
]
);
}
Expand Down
15 changes: 15 additions & 0 deletions tests/Unit/Admin/AdminApiTest.php
Expand Up @@ -44,4 +44,19 @@ public function testAccessibilityAnalysisUploadPreset()

self::assertRequestBodySubset($lastRequest, ['accessibility_analysis' => '1']);
}

/**
* Should allow listing related assets in the asset function.
*/
public function testAssetRelatedAssets()
{
$mockAdminApi = new MockAdminApi();
$mockAdminApi->asset(self::$UNIQUE_TEST_ID, ['related' => true, 'related_next_cursor' => self::NEXT_CURSOR]);
$lastRequest = $mockAdminApi->getMockHandler()->getLastRequest();

self::assertRequestQueryStringSubset(
$lastRequest,
['related' => '1', 'related_next_cursor' => self::NEXT_CURSOR]
);
}
}
89 changes: 88 additions & 1 deletion tests/Unit/Admin/AssetsTest.php
Expand Up @@ -77,7 +77,8 @@ public function testRestoreDeletedAssetSpecificVersion($publicIds, $options, $ur
public function testUpdateAssetFields()
{
$mockAdminApi = new MockAdminApi();
$mockAdminApi->update(self::$UNIQUE_TEST_ID,
$mockAdminApi->update(
self::$UNIQUE_TEST_ID,
[
'metadata' => ['key' => 'value'],
'asset_folder' => 'asset_folder',
Expand All @@ -96,4 +97,90 @@ public function testUpdateAssetFields()
]
);
}

/**
* Test related assets.
*/
public function testRelatedAssets()
{
$testIds = [
'image/upload/' . self::$UNIQUE_TEST_ID,
'raw/upload/' . self::$UNIQUE_TEST_ID,
];

$mockAdminApi = new MockAdminApi();
$mockAdminApi->addRelatedAssets(
self::$UNIQUE_TEST_ID,
$testIds
);

$lastRequest = $mockAdminApi->getMockHandler()->getLastRequest();

self::assertRequestUrl($lastRequest, '/resources/related_assets/image/upload/' . self::$UNIQUE_TEST_ID);
self::assertRequestJsonBodySubset(
$lastRequest,
[
'assets_to_relate' => $testIds
]
);

$mockAdminApi = new MockAdminApi();
$mockAdminApi->deleteRelatedAssets(
self::$UNIQUE_TEST_ID,
$testIds
);

$lastRequest = $mockAdminApi->getMockHandler()->getLastRequest();

self::assertRequestUrl($lastRequest, '/resources/related_assets/image/upload/' . self::$UNIQUE_TEST_ID);
self::assertRequestJsonBodySubset(
$lastRequest,
[
'assets_to_unrelate' => $testIds
]
);
}

/**
* Test related assets by asset Ids.
*/
public function testRelatedAssetsByAssetIds()
{
$testAssetIds = [
self::API_TEST_ASSET_ID2,
self::API_TEST_ASSET_ID3,
];

$mockAdminApi = new MockAdminApi();
$mockAdminApi->addRelatedAssetsByAssetIds(
self::API_TEST_ASSET_ID,
$testAssetIds
);

$lastRequest = $mockAdminApi->getMockHandler()->getLastRequest();

self::assertRequestUrl($lastRequest, '/resources/related_assets/' . self::API_TEST_ASSET_ID);
self::assertRequestJsonBodySubset(
$lastRequest,
[
'assets_to_relate' => $testAssetIds
]
);

$mockAdminApi = new MockAdminApi();
$mockAdminApi->deleteRelatedAssetsByAssetIds(
self::API_TEST_ASSET_ID,
$testAssetIds
);

$lastRequest = $mockAdminApi->getMockHandler()->getLastRequest();

self::assertRequestUrl($lastRequest, '/resources/related_assets/' . self::API_TEST_ASSET_ID);
self::assertRequestJsonBodySubset(
$lastRequest,
[
'assets_to_unrelate' => $testAssetIds
]
);
}
}
4 changes: 2 additions & 2 deletions tests/Unit/Search/SearchApiTest.php
Expand Up @@ -34,7 +34,7 @@ public function testExecuteWithParams()
$mockSearchApi
->expression('format:png')
->maxResults(10)
->nextCursor('8c452e112d4c88ac7c9ffb3a2a41c41bef24')
->nextCursor(self::NEXT_CURSOR)
->sortBy('created_at', 'asc')
->sortBy('updated_at')
->aggregate('format')
Expand All @@ -55,7 +55,7 @@ public function testExecuteWithParams()
'with_field' => ['tags', 'image_metadata'],
'expression' => 'format:png',
'max_results' => 10,
'next_cursor' => '8c452e112d4c88ac7c9ffb3a2a41c41bef24',
'next_cursor' => self::NEXT_CURSOR,
],
'Should use right headers for execution of advanced search api'
);
Expand Down
6 changes: 6 additions & 0 deletions tests/Unit/UnitTestCase.php
Expand Up @@ -29,6 +29,12 @@ abstract class UnitTestCase extends CloudinaryTestCase

const TEST_LOGGING = ['logging' => ['test' => ['level' => 'debug']]];

const API_TEST_ASSET_ID = '4af5a0d1d4047808528b5425d166c101';
const API_TEST_ASSET_ID2 = '4af5a0d1d4047808528b5425d166c102';
const API_TEST_ASSET_ID3 = '4af5a0d1d4047808528b5425d166c103';

const NEXT_CURSOR = '8c452e112d4c88ac7c9ffb3a2a41c41bef24';

protected $cloudinaryUrl;

private $cldUrlEnvBackup;
Expand Down

0 comments on commit 223695b

Please sign in to comment.