Skip to content

Commit

Permalink
fix: 3 unit test with new data set from url_alias and url_pattern
Browse files Browse the repository at this point in the history
  • Loading branch information
Tim Schipper committed Oct 13, 2024
1 parent 8889c07 commit 8c3f2c5
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 18 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -96,9 +96,9 @@ describe('Query layer decorator', () => {
});

expect(updatedPage?.url_alias[0]?.id).not.toBe(oldAliasId);
expect(updatedPage).toHaveProperty('url_alias[0].url_path', '/page/some-updated-page');
expect(updatedPage).toHaveProperty('url_alias[0].generated', true);
expect(updatedPage).toHaveProperty('url_alias[0].contenttype', 'api::test.test');
expect(updatedPage).toHaveProperty('url_alias[0]?.url_path', '/page/some-updated-page');
expect(updatedPage).toHaveProperty('url_alias[0]?.generated', true);
expect(updatedPage).toHaveProperty('url_alias[0]?.contenttype', 'api::test.test');
});

it('Update - Should re-generate an existing URL alias if generated is set to true', async () => {
Expand Down Expand Up @@ -137,7 +137,7 @@ describe('Query layer decorator', () => {
const page = await strapi.entityService.create("api::test.test", {
data: {
title: 'Some about to be updated new page',
url_alias: alias[0].id,
url_alias: alias.id,
},
populate: ['url_alias']
});
Expand Down Expand Up @@ -192,7 +192,7 @@ describe('Query layer decorator', () => {

await strapi.entityService.delete("api::test.test", page.id);

const alias = await strapi.entityService.findOne("plugin::webtools.url-alias", page.url_alias.id);
const alias = await strapi.entityService.findOne("plugin::webtools.url-alias", page.url_alias[0]?.id);

expect(alias).toBeNull();
});
Expand Down Expand Up @@ -241,8 +241,8 @@ describe('Query layer decorator', () => {
filters: { id: { $in: [page1.id, page2.id] } }
});

const alias1 = await strapi.entityService.findOne("plugin::webtools.url-alias", page1.url_alias.id);
const alias2 = await strapi.entityService.findOne("plugin::webtools.url-alias", page2.url_alias.id);
const alias1 = await strapi.entityService.findOne("plugin::webtools.url-alias", page1.url_alias[0]?.id);
const alias2 = await strapi.entityService.findOne("plugin::webtools.url-alias", page2.url_alias[0]?.id);

expect(alias1).toBeNull();
expect(alias2).toBeNull();
Expand Down
34 changes: 23 additions & 11 deletions packages/core/server/admin-api/services/query-layer-decorator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -192,11 +192,11 @@ const decorator = (service: IDecoratedService) => ({
localizations: undefined,
};

// If a URL alias is already present, fetch it.
if (opts.data.url_alias) {
// @ts-ignore
if (opts.data.url_alias?.length) {
urlAliasEntity = await getPluginService('urlAliasService').findOne(opts.data.url_alias);
// @ts-ignore
} else if (entity?.url_alias) {
} else if (entity?.url_alias?.length) {
// @ts-ignore
urlAliasEntity = entity.url_alias;
}
Expand Down Expand Up @@ -225,8 +225,9 @@ const decorator = (service: IDecoratedService) => ({
});
}

// If no URL alias is present, create one.
if (!urlAliasEntity) {
// @ts-ignore
if (!urlAliasEntity?.length) {
console.log('creating new url alias because empty array', urlAliasEntity);
urlAliasEntity = await getPluginService('urlAliasService').create({
url_path: generatedPath,
generated: true,
Expand Down Expand Up @@ -259,7 +260,7 @@ const decorator = (service: IDecoratedService) => ({
...opts,
data: {
...opts.data,
url_alias: [urlAliasEntity.id],
url_alias: urlAliasEntity.id,
},
});
},
Expand All @@ -282,8 +283,16 @@ const decorator = (service: IDecoratedService) => ({
});

// If a URL alias is present, delete it.
if (entity.url_alias?.id) {
await getPluginService('urlAliasService').delete(entity.url_alias.id);
// @ts-ignore
if (entity.url_alias.length) {
// @ts-ignore
// eslint-disable-next-line max-len
// eslint-disable-next-line @typescript-eslint/no-unsafe-argument, @typescript-eslint/no-unsafe-member-access
await Promise.all(entity.url_alias.map(async (url_alias: { id: string | number; }) => {
if (url_alias?.id) {
await getPluginService('urlAliasService').delete(url_alias.id);
}
}));
}

// Eventually delete the entity.
Expand Down Expand Up @@ -311,7 +320,6 @@ const decorator = (service: IDecoratedService) => ({
await Promise.all(languages.map(async (lang) => {
const urlPattern = await getPluginService('urlPatternService').findByUid(uid, lang);
const languageRelations = getPluginService('urlPatternService').getRelationsFromPattern(urlPattern);

relations = [...relations, ...languageRelations];
}));

Expand Down Expand Up @@ -394,11 +402,15 @@ const decorator = (service: IDecoratedService) => ({

entitiesToDelete.map(async (entity) => {
// @ts-ignore
if (entity.url_alias) {
if (entity.url_alias.length) {
// @ts-ignore
// eslint-disable-next-line max-len
// eslint-disable-next-line @typescript-eslint/no-unsafe-argument, @typescript-eslint/no-unsafe-member-access
await getPluginService('urlAliasService').delete(entity.url_alias.id);
await Promise.all(entity.url_alias.map(async (url_alias: { id: string | number; }) => {
if (url_alias?.id) {
await getPluginService('urlAliasService').delete(url_alias.id);
}
}));
}
});

Expand Down

0 comments on commit 8c3f2c5

Please sign in to comment.