Skip to content

Commit 2b77c5b

Browse files
FEATURE: Adjust AssetApi and e2e tests
1 parent 67ccf53 commit 2b77c5b

2 files changed

Lines changed: 83 additions & 12 deletions

File tree

Tests/Functional/GraphQL/AssetApiTest.php

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -175,14 +175,17 @@ public function testUpdateAsset(): void
175175
$asset = $assets->assets[0];
176176
$this->assertEquals($file->clientFilename, $asset->filename->value);
177177

178-
$updatedAsset = $this->mediaApi->updateAsset(
178+
$updateResult = $this->mediaApi->updateAsset(
179179
$asset->id,
180180
$asset->assetSource->id,
181181
'some label',
182182
'some caption',
183183
'copyright notice',
184184
);
185185

186+
$this->assertTrue($updateResult->success);
187+
188+
$updatedAsset = $this->mediaApi->asset($asset->id, $asset->assetSource->id);
186189
$this->assertEquals($asset->id, $updatedAsset->id);
187190
$this->assertEquals('some label', $this->assetResolver->label($updatedAsset));
188191
$this->assertEquals('some caption', $this->assetResolver->caption($updatedAsset));

packages/dev-server/src/server.ts

Lines changed: 79 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -165,18 +165,19 @@ const __dirname = path.dirname(fileURLToPath(import.meta.url));
165165
}),
166166
},
167167
Mutation: {
168-
updateAsset: ($_, { id, assetSourceId, label, caption, copyrightNotice }): Asset => {
168+
updateAsset: ($_, { id, assetSourceId, label, caption, copyrightNotice }) => {
169169
const asset = assets.find((asset) => asset.id === id && asset.assetSource.id === assetSourceId);
170-
asset.label = label;
171-
asset.caption = caption;
172-
asset.copyrightNotice = copyrightNotice;
170+
if (!asset) return { success: false, messages: ['Asset not found'] };
171+
if (label !== undefined) asset.label = label;
172+
if (caption !== undefined) asset.caption = caption;
173+
if (copyrightNotice !== undefined) asset.copyrightNotice = copyrightNotice;
173174
asset.lastModified = new Date();
174175
addAssetChange({
175176
lastModified: asset.lastModified,
176177
assetId: id,
177178
type: 'ASSET_UPDATED',
178179
});
179-
return asset;
180+
return { success: true, messages: [] };
180181
},
181182
setAssetCollectionParent: (
182183
$_,
@@ -251,15 +252,16 @@ const __dirname = path.dirname(fileURLToPath(import.meta.url));
251252
setAssetTags: (
252253
$_,
253254
{ id, assetSourceId, tagIds }: { id: string; assetSourceId: string; tagIds: string[] }
254-
): Asset => {
255+
) => {
255256
const asset = assets.find((asset) => asset.id === id && asset.assetSource.id === assetSourceId);
257+
if (!asset) return { success: false, messages: ['Asset not found'] };
256258
asset.tags = tags.filter((tag) => tagIds.includes(tag.id));
257259
addAssetChange({
258260
lastModified: asset.lastModified,
259261
assetId: id,
260262
type: 'ASSET_UPDATED',
261263
});
262-
return asset;
264+
return { success: true, messages: [] };
263265
},
264266
setAssetCollections: (
265267
$_,
@@ -329,11 +331,77 @@ const __dirname = path.dirname(fileURLToPath(import.meta.url));
329331
editAsset: ($_, { id, assetSourceId, filename, options }): boolean => {
330332
throw new Error('Not implemented');
331333
},
332-
tagAsset: ($_, { id, assetSourceId, tagId }): Asset => {
333-
throw new Error('Not implemented');
334+
tagAsset: ($_, { id, assetSourceId, tagId }) => {
335+
const asset = assets.find((asset) => asset.id === id && asset.assetSource.id === assetSourceId);
336+
if (!asset) return { success: false, messages: ['Asset not found'] };
337+
const tag = tags.find((tag) => tag.id === tagId);
338+
if (!tag) return { success: false, messages: ['Tag not found'] };
339+
if (!asset.tags.find((t) => t.id === tagId)) {
340+
asset.tags.push(tag);
341+
}
342+
addAssetChange({ lastModified: new Date(), assetId: id, type: 'ASSET_UPDATED' });
343+
return { success: true, messages: [] };
334344
},
335-
untagAsset: ($_, { id, assetSourceId, tagId }): Asset => {
336-
throw new Error('Not implemented');
345+
untagAsset: ($_, { id, assetSourceId, tagId }) => {
346+
const asset = assets.find((asset) => asset.id === id && asset.assetSource.id === assetSourceId);
347+
if (!asset) return { success: false, messages: ['Asset not found'] };
348+
asset.tags = asset.tags.filter((tag) => tag.id !== tagId);
349+
addAssetChange({ lastModified: new Date(), assetId: id, type: 'ASSET_UPDATED' });
350+
return { success: true, messages: [] };
351+
},
352+
deleteAssets: ($_, { identities }) => {
353+
return identities.map(({ assetId, assetSourceId }) => {
354+
const assetIndex = assets.findIndex(
355+
(asset) => asset.id === assetId && asset.assetSource.id === assetSourceId
356+
);
357+
if (assetIndex >= 0) {
358+
assets.splice(assetIndex, 1);
359+
addAssetChange({ lastModified: new Date(), assetId, type: 'ASSET_REMOVED' });
360+
return { success: true, messages: [] };
361+
}
362+
return { success: false, messages: ['Asset not found'] };
363+
});
364+
},
365+
tagAssets: ($_, { identities, tagId }) => {
366+
const tag = tags.find((tag) => tag.id === tagId);
367+
if (!tag) return [{ success: false, messages: ['Tag not found'] }];
368+
return identities.map(({ assetId, assetSourceId }) => {
369+
const asset = assets.find((a) => a.id === assetId && a.assetSource.id === assetSourceId);
370+
if (!asset) return { success: false, messages: ['Asset not found'] };
371+
if (!asset.tags.find((t) => t.id === tagId)) {
372+
asset.tags.push(tag);
373+
}
374+
return { success: true, messages: [] };
375+
});
376+
},
377+
untagAssets: ($_, { identities, tagId }) => {
378+
const tag = tags.find((tag) => tag.id === tagId);
379+
if (!tag) return [{ success: false, messages: ['Tag not found'] }];
380+
return identities.map(({ assetId, assetSourceId }) => {
381+
const asset = assets.find((a) => a.id === assetId && a.assetSource.id === assetSourceId);
382+
if (!asset) return { success: false, messages: ['Asset not found'] };
383+
asset.tags = asset.tags.filter((t) => t.id !== tagId);
384+
return { success: true, messages: [] };
385+
});
386+
},
387+
assignAssetsToCollection: ($_, { identities, assetCollectionId }) => {
388+
const collection = assetCollections.find((c) => c.id === assetCollectionId);
389+
if (!collection) return [{ success: false, messages: ['Collection not found'] }];
390+
return identities.map(({ assetId, assetSourceId }) => {
391+
const asset = assets.find((a) => a.id === assetId && a.assetSource.id === assetSourceId);
392+
if (!asset) return { success: false, messages: ['Asset not found'] };
393+
asset.collections = [collection];
394+
return { success: true, messages: [] };
395+
});
396+
},
397+
updateAssets: ($_, { identities, copyrightNotice }) => {
398+
return identities.map(({ assetId, assetSourceId }) => {
399+
const asset = assets.find((a) => a.id === assetId && a.assetSource.id === assetSourceId);
400+
if (!asset) return { success: false, messages: ['Asset not found'] };
401+
if (copyrightNotice !== undefined) asset.copyrightNotice = copyrightNotice;
402+
asset.lastModified = new Date();
403+
return { success: true, messages: [] };
404+
});
337405
},
338406
uploadFiles: ($_, { files, tagId, assetCollectionId }): FileUploadResult[] => {
339407
throw new Error('Not implemented');

0 commit comments

Comments
 (0)