Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 223695b

Browse files
Add support for related assets Admin APIs
1 parent 71be0d6 commit 223695b

File tree

6 files changed

+202
-3
lines changed

6 files changed

+202
-3
lines changed

src/Api/Admin/ApiEndPoint.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ class ApiEndPoint
1919
const USAGE = 'usage';
2020
const ASSETS = 'resources';
2121
const DERIVED_ASSETS = 'derived_resources';
22+
const RELATED_ASSETS = 'related_assets';
2223
const FOLDERS = 'folders';
2324
const TAGS = 'tags';
2425
const STREAMING_PROFILES = 'streaming_profiles';

src/Api/Admin/AssetsTrait.php

Lines changed: 90 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -505,6 +505,94 @@ public function deleteDerivedByTransformation($publicIds, $transformations = [],
505505
return $this->apiClient->delete($uri, $params);
506506
}
507507

508+
/**
509+
* Relates an asset to other assets by public IDs.
510+
*
511+
* @param string $publicId The public ID of the asset to update.
512+
* @param array $assetsToRelate The array of up to 10 fully_qualified_public_ids given as
513+
* resource_type/type/public_id.
514+
* @param array $options The optional parameters. See the
515+
* <a href=https://cloudinary.com/documentation/admin_api#add_related_assets target="_blank"> Admin API</a> documentation.
516+
*
517+
* @return ApiResponse
518+
*/
519+
public function addRelatedAssets($publicId, $assetsToRelate, $options = [])
520+
{
521+
$assetType = ArrayUtils::get($options, AssetType::KEY, AssetType::IMAGE);
522+
$type = ArrayUtils::get($options, DeliveryType::KEY, DeliveryType::UPLOAD);
523+
524+
$uri = [ApiEndPoint::ASSETS, ApiEndPoint::RELATED_ASSETS, $assetType, $type, $publicId];
525+
526+
$params = [
527+
'assets_to_relate' => ArrayUtils::build($assetsToRelate),
528+
];
529+
530+
return $this->apiClient->postJson($uri, $params);
531+
}
532+
533+
/**
534+
* Relates an asset to other assets by asset IDs.
535+
*
536+
* @param string $assetId The asset ID of the asset to update.
537+
* @param array $assetsToRelate The array of up to 10 asset IDs.
538+
*
539+
* @return ApiResponse
540+
*/
541+
public function addRelatedAssetsByAssetIds($assetId, $assetsToRelate)
542+
{
543+
$uri = [ApiEndPoint::ASSETS, ApiEndPoint::RELATED_ASSETS, $assetId];
544+
545+
$params = [
546+
'assets_to_relate' => ArrayUtils::build($assetsToRelate),
547+
];
548+
549+
return $this->apiClient->postJson($uri, $params);
550+
}
551+
552+
/**
553+
* Unrelates an asset from other assets by public IDs.
554+
*
555+
* @param string $publicId The public ID of the asset to update.
556+
* @param array $assetsToUnrelate The array of up to 10 fully_qualified_public_ids given as
557+
* resource_type/type/public_id.
558+
* @param array $options The optional parameters. See the
559+
* <a href=https://cloudinary.com/documentation/admin_api#delete_related_assets target="_blank"> Admin API</a> documentation.
560+
*
561+
* @return ApiResponse
562+
*/
563+
public function deleteRelatedAssets($publicId, $assetsToUnrelate, $options = [])
564+
{
565+
$assetType = ArrayUtils::get($options, AssetType::KEY, AssetType::IMAGE);
566+
$type = ArrayUtils::get($options, DeliveryType::KEY, DeliveryType::UPLOAD);
567+
568+
$uri = [ApiEndPoint::ASSETS, ApiEndPoint::RELATED_ASSETS, $assetType, $type, $publicId];
569+
570+
$params = [
571+
'assets_to_unrelate' => ArrayUtils::build($assetsToUnrelate),
572+
];
573+
574+
return $this->apiClient->deleteJson($uri, $params);
575+
}
576+
577+
/**
578+
* Unrelates an asset from other assets by asset IDs.
579+
*
580+
* @param string $assetId The asset ID of the asset to update.
581+
* @param array $assetsToUnrelate The array of up to 10 asset IDs.
582+
*
583+
* @return ApiResponse
584+
*/
585+
public function deleteRelatedAssetsByAssetIds($assetId, $assetsToUnrelate)
586+
{
587+
$uri = [ApiEndPoint::ASSETS, ApiEndPoint::RELATED_ASSETS, $assetId];
588+
589+
$params = [
590+
'assets_to_unrelate' => ArrayUtils::build($assetsToUnrelate),
591+
];
592+
593+
return $this->apiClient->deleteJson($uri, $params);
594+
}
595+
508596
/**
509597
* Prepares optional parameters for delete asset API calls.
510598
*
@@ -553,6 +641,8 @@ protected static function prepareAssetDetailsParams($options)
553641
'derived_next_cursor',
554642
'accessibility_analysis',
555643
'versions',
644+
'related',
645+
'related_next_cursor',
556646
]
557647
);
558648
}

tests/Unit/Admin/AdminApiTest.php

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,4 +44,19 @@ public function testAccessibilityAnalysisUploadPreset()
4444

4545
self::assertRequestBodySubset($lastRequest, ['accessibility_analysis' => '1']);
4646
}
47+
48+
/**
49+
* Should allow listing related assets in the asset function.
50+
*/
51+
public function testAssetRelatedAssets()
52+
{
53+
$mockAdminApi = new MockAdminApi();
54+
$mockAdminApi->asset(self::$UNIQUE_TEST_ID, ['related' => true, 'related_next_cursor' => self::NEXT_CURSOR]);
55+
$lastRequest = $mockAdminApi->getMockHandler()->getLastRequest();
56+
57+
self::assertRequestQueryStringSubset(
58+
$lastRequest,
59+
['related' => '1', 'related_next_cursor' => self::NEXT_CURSOR]
60+
);
61+
}
4762
}

tests/Unit/Admin/AssetsTest.php

Lines changed: 88 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -77,7 +77,8 @@ public function testRestoreDeletedAssetSpecificVersion($publicIds, $options, $ur
7777
public function testUpdateAssetFields()
7878
{
7979
$mockAdminApi = new MockAdminApi();
80-
$mockAdminApi->update(self::$UNIQUE_TEST_ID,
80+
$mockAdminApi->update(
81+
self::$UNIQUE_TEST_ID,
8182
[
8283
'metadata' => ['key' => 'value'],
8384
'asset_folder' => 'asset_folder',
@@ -96,4 +97,90 @@ public function testUpdateAssetFields()
9697
]
9798
);
9899
}
100+
101+
/**
102+
* Test related assets.
103+
*/
104+
public function testRelatedAssets()
105+
{
106+
$testIds = [
107+
'image/upload/' . self::$UNIQUE_TEST_ID,
108+
'raw/upload/' . self::$UNIQUE_TEST_ID,
109+
];
110+
111+
$mockAdminApi = new MockAdminApi();
112+
$mockAdminApi->addRelatedAssets(
113+
self::$UNIQUE_TEST_ID,
114+
$testIds
115+
);
116+
117+
$lastRequest = $mockAdminApi->getMockHandler()->getLastRequest();
118+
119+
self::assertRequestUrl($lastRequest, '/resources/related_assets/image/upload/' . self::$UNIQUE_TEST_ID);
120+
self::assertRequestJsonBodySubset(
121+
$lastRequest,
122+
[
123+
'assets_to_relate' => $testIds
124+
]
125+
);
126+
127+
$mockAdminApi = new MockAdminApi();
128+
$mockAdminApi->deleteRelatedAssets(
129+
self::$UNIQUE_TEST_ID,
130+
$testIds
131+
);
132+
133+
$lastRequest = $mockAdminApi->getMockHandler()->getLastRequest();
134+
135+
self::assertRequestUrl($lastRequest, '/resources/related_assets/image/upload/' . self::$UNIQUE_TEST_ID);
136+
self::assertRequestJsonBodySubset(
137+
$lastRequest,
138+
[
139+
'assets_to_unrelate' => $testIds
140+
]
141+
);
142+
}
143+
144+
/**
145+
* Test related assets by asset Ids.
146+
*/
147+
public function testRelatedAssetsByAssetIds()
148+
{
149+
$testAssetIds = [
150+
self::API_TEST_ASSET_ID2,
151+
self::API_TEST_ASSET_ID3,
152+
];
153+
154+
$mockAdminApi = new MockAdminApi();
155+
$mockAdminApi->addRelatedAssetsByAssetIds(
156+
self::API_TEST_ASSET_ID,
157+
$testAssetIds
158+
);
159+
160+
$lastRequest = $mockAdminApi->getMockHandler()->getLastRequest();
161+
162+
self::assertRequestUrl($lastRequest, '/resources/related_assets/' . self::API_TEST_ASSET_ID);
163+
self::assertRequestJsonBodySubset(
164+
$lastRequest,
165+
[
166+
'assets_to_relate' => $testAssetIds
167+
]
168+
);
169+
170+
$mockAdminApi = new MockAdminApi();
171+
$mockAdminApi->deleteRelatedAssetsByAssetIds(
172+
self::API_TEST_ASSET_ID,
173+
$testAssetIds
174+
);
175+
176+
$lastRequest = $mockAdminApi->getMockHandler()->getLastRequest();
177+
178+
self::assertRequestUrl($lastRequest, '/resources/related_assets/' . self::API_TEST_ASSET_ID);
179+
self::assertRequestJsonBodySubset(
180+
$lastRequest,
181+
[
182+
'assets_to_unrelate' => $testAssetIds
183+
]
184+
);
185+
}
99186
}

tests/Unit/Search/SearchApiTest.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ public function testExecuteWithParams()
3434
$mockSearchApi
3535
->expression('format:png')
3636
->maxResults(10)
37-
->nextCursor('8c452e112d4c88ac7c9ffb3a2a41c41bef24')
37+
->nextCursor(self::NEXT_CURSOR)
3838
->sortBy('created_at', 'asc')
3939
->sortBy('updated_at')
4040
->aggregate('format')
@@ -55,7 +55,7 @@ public function testExecuteWithParams()
5555
'with_field' => ['tags', 'image_metadata'],
5656
'expression' => 'format:png',
5757
'max_results' => 10,
58-
'next_cursor' => '8c452e112d4c88ac7c9ffb3a2a41c41bef24',
58+
'next_cursor' => self::NEXT_CURSOR,
5959
],
6060
'Should use right headers for execution of advanced search api'
6161
);

tests/Unit/UnitTestCase.php

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,12 @@ abstract class UnitTestCase extends CloudinaryTestCase
2929

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

32+
const API_TEST_ASSET_ID = '4af5a0d1d4047808528b5425d166c101';
33+
const API_TEST_ASSET_ID2 = '4af5a0d1d4047808528b5425d166c102';
34+
const API_TEST_ASSET_ID3 = '4af5a0d1d4047808528b5425d166c103';
35+
36+
const NEXT_CURSOR = '8c452e112d4c88ac7c9ffb3a2a41c41bef24';
37+
3238
protected $cloudinaryUrl;
3339

3440
private $cldUrlEnvBackup;

0 commit comments

Comments
 (0)