Skip to content

Commit 5f7e709

Browse files
FEATURE: Adjust AssetApi and e2e tests
1 parent 67ccf53 commit 5f7e709

2 files changed

Lines changed: 90 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: 86 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,84 @@ 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 inUse = Fixtures.getUsageDetailsForAsset(assetId).reduce(
355+
(prev, { usages }) => prev || usages.length > 0,
356+
false
357+
);
358+
if (inUse) {
359+
return { success: false, messages: ['Asset is in use'] };
360+
}
361+
const assetIndex = assets.findIndex(
362+
(asset) => asset.id === assetId && asset.assetSource.id === assetSourceId
363+
);
364+
if (assetIndex >= 0) {
365+
assets.splice(assetIndex, 1);
366+
addAssetChange({ lastModified: new Date(), assetId, type: 'ASSET_REMOVED' });
367+
return { success: true, messages: [] };
368+
}
369+
return { success: false, messages: ['Asset not found'] };
370+
});
371+
},
372+
tagAssets: ($_, { identities, tagId }) => {
373+
const tag = tags.find((tag) => tag.id === tagId);
374+
if (!tag) return [{ success: false, messages: ['Tag not found'] }];
375+
return identities.map(({ assetId, assetSourceId }) => {
376+
const asset = assets.find((a) => a.id === assetId && a.assetSource.id === assetSourceId);
377+
if (!asset) return { success: false, messages: ['Asset not found'] };
378+
if (!asset.tags.find((t) => t.id === tagId)) {
379+
asset.tags.push(tag);
380+
}
381+
return { success: true, messages: [] };
382+
});
383+
},
384+
untagAssets: ($_, { identities, tagId }) => {
385+
const tag = tags.find((tag) => tag.id === tagId);
386+
if (!tag) return [{ success: false, messages: ['Tag not found'] }];
387+
return identities.map(({ assetId, assetSourceId }) => {
388+
const asset = assets.find((a) => a.id === assetId && a.assetSource.id === assetSourceId);
389+
if (!asset) return { success: false, messages: ['Asset not found'] };
390+
asset.tags = asset.tags.filter((t) => t.id !== tagId);
391+
return { success: true, messages: [] };
392+
});
393+
},
394+
assignAssetsToCollection: ($_, { identities, assetCollectionId }) => {
395+
const collection = assetCollections.find((c) => c.id === assetCollectionId);
396+
if (!collection) return [{ success: false, messages: ['Collection not found'] }];
397+
return identities.map(({ assetId, assetSourceId }) => {
398+
const asset = assets.find((a) => a.id === assetId && a.assetSource.id === assetSourceId);
399+
if (!asset) return { success: false, messages: ['Asset not found'] };
400+
asset.collections = [collection];
401+
return { success: true, messages: [] };
402+
});
403+
},
404+
updateAssets: ($_, { identities, copyrightNotice }) => {
405+
return identities.map(({ assetId, assetSourceId }) => {
406+
const asset = assets.find((a) => a.id === assetId && a.assetSource.id === assetSourceId);
407+
if (!asset) return { success: false, messages: ['Asset not found'] };
408+
if (copyrightNotice !== undefined) asset.copyrightNotice = copyrightNotice;
409+
asset.lastModified = new Date();
410+
return { success: true, messages: [] };
411+
});
337412
},
338413
uploadFiles: ($_, { files, tagId, assetCollectionId }): FileUploadResult[] => {
339414
throw new Error('Not implemented');

0 commit comments

Comments
 (0)