strapi/api-tests/plugins/i18n/locales.test.api.js

473 lines
12 KiB
JavaScript
Raw Normal View History

'use strict';
2021-01-27 17:52:41 +01:00
const { omit } = require('lodash/fp');
const { createTestBuilder } = require('api-tests/builder');
const { createStrapiInstance } = require('api-tests/strapi');
const { createAuthRequest } = require('api-tests/request');
const data = {
locales: [],
2021-02-12 15:38:25 +01:00
deletedLocales: [],
};
const omitTimestamps = omit(['updatedAt', 'createdAt']);
2021-01-27 17:52:41 +01:00
const compareLocales = (a, b) => (a.code < b.code ? -1 : 1);
const productModel = {
pluginOptions: {
i18n: {
localized: true,
},
},
attributes: {
name: {
type: 'string',
},
},
2021-09-13 16:57:04 +02:00
displayName: 'Product',
singularName: 'product',
pluralName: 'products',
description: '',
collectionName: '',
};
describe('CRUD locales', () => {
let rq;
let strapi;
const builder = createTestBuilder();
2021-04-12 12:32:09 +02:00
let localeService;
beforeAll(async () => {
await builder.addContentType(productModel).build();
strapi = await createStrapiInstance();
rq = await createAuthRequest({ strapi });
2021-04-12 12:32:09 +02:00
2021-08-19 22:27:00 +02:00
localeService = strapi.plugin('i18n').service('locales');
});
afterAll(async () => {
2021-04-12 12:32:09 +02:00
await localeService.setDefaultLocale({ code: 'en' });
await strapi.destroy();
2021-04-02 12:16:07 +02:00
await builder.cleanup();
});
describe('Default locale', () => {
test('Default locale is already created', async () => {
2022-08-08 15:50:34 +02:00
const res = await rq({
url: '/i18n/locales',
method: 'GET',
});
expect(res.statusCode).toBe(200);
expect(res.body).toHaveLength(1);
expect(res.body[0].isDefault).toBe(true);
data.locales.push(res.body[0]);
});
});
describe('Creation', () => {
test('Can create a locale', async () => {
const locale = {
name: 'French',
code: 'fr',
2021-02-05 15:39:11 +01:00
isDefault: false,
};
2022-08-08 15:50:34 +02:00
const res = await rq({
url: '/i18n/locales',
method: 'POST',
body: locale,
});
expect(res.statusCode).toBe(200);
expect(res.body).toMatchObject({
id: expect.anything(),
...locale,
});
data.locales.push(res.body);
});
2021-02-05 15:39:11 +01:00
test('Cannot create a locale if code or isDefault is missing', async () => {
const locale = {
name: 'Italian',
};
2022-08-08 15:50:34 +02:00
const res = await rq({
url: '/i18n/locales',
method: 'POST',
body: locale,
});
expect(res.statusCode).toBe(400);
expect(res.body).toMatchObject({
2021-10-20 17:30:05 +02:00
data: null,
error: {
details: {
errors: [
{
message: 'code is a required field',
name: 'ValidationError',
path: ['code'],
},
{
message: 'isDefault is a required field',
name: 'ValidationError',
path: ['isDefault'],
},
],
},
message: '2 errors occurred',
name: 'ValidationError',
status: 400,
2021-02-05 15:39:11 +01:00
},
});
});
test('Cannot create a locale if code already exists', async () => {
const locale = {
code: 'fr',
name: 'random name',
2021-02-05 15:39:11 +01:00
isDefault: false,
};
2022-08-08 15:50:34 +02:00
const res = await rq({
url: '/i18n/locales',
method: 'POST',
body: locale,
});
expect(res.statusCode).toBe(400);
2021-10-20 17:30:05 +02:00
expect(res.body).toMatchObject({
data: null,
error: {
status: 400,
name: 'ApplicationError',
message: 'This locale already exists',
details: {},
},
});
});
test('Can create a locale even if name already exists', async () => {
const locale = {
name: 'French',
code: 'fr-FR',
2021-02-05 15:39:11 +01:00
isDefault: false,
};
2022-08-08 15:50:34 +02:00
const res = await rq({
url: '/i18n/locales',
method: 'POST',
body: locale,
});
expect(res.statusCode).toBe(200);
expect(res.body).toMatchObject({
id: expect.anything(),
...locale,
});
data.locales.push(res.body);
});
2021-02-05 15:39:11 +01:00
test('Only one locale can be default (POST)', async () => {
let res = await rq({
url: '/i18n/locales',
method: 'POST',
body: { code: 'bas', name: 'random', isDefault: true },
2021-02-05 15:39:11 +01:00
});
expect(res.statusCode).toBe(200);
expect(res.body.isDefault).toBe(true);
data.locales[0].isDefault = false;
2021-02-05 15:39:11 +01:00
res = await rq({
url: '/i18n/locales',
method: 'POST',
body: { code: 'en-US', name: 'random', isDefault: true },
});
expect(res.statusCode).toBe(200);
expect(res.body.isDefault).toBe(true);
res = await rq({
url: '/i18n/locales',
method: 'GET',
});
expect(res.statusCode).toBe(200);
2022-08-08 23:33:39 +02:00
const enLocale = res.body.find((locale) => locale.code === 'bas');
const enUsLocale = res.body.find((locale) => locale.code === 'en-US');
2021-02-05 15:39:11 +01:00
expect(enLocale.isDefault).toBe(false);
expect(enUsLocale.isDefault).toBe(true);
data.locales.push(enLocale);
data.locales.push(enUsLocale);
});
});
describe('Read', () => {
test('Can list the locales', async () => {
2022-08-08 15:50:34 +02:00
const res = await rq({
2021-02-05 15:39:11 +01:00
url: '/i18n/locales',
method: 'GET',
});
expect(res.statusCode).toBe(200);
expect(res.body).toHaveLength(data.locales.length);
expect(res.body.sort(compareLocales)).toMatchObject(
data.locales.slice().sort(compareLocales)
);
2021-02-05 15:39:11 +01:00
});
});
describe('Update', () => {
test('Can update the name of a locale', async () => {
const localeUpdate = {
name: 'French update',
2021-02-05 15:39:11 +01:00
isDefault: false,
};
2022-08-08 15:50:34 +02:00
const res = await rq({
url: `/i18n/locales/${data.locales[1].id}`,
method: 'PUT',
body: localeUpdate,
});
expect(res.statusCode).toBe(200);
expect(res.body).toMatchObject({
...omitTimestamps(data.locales[1]),
...localeUpdate,
});
data.locales[1] = res.body;
});
test('Cannot update the code of a locale (without name)', async () => {
const localeUpdate = {
code: 'ak',
};
2022-08-08 15:50:34 +02:00
const res = await rq({
url: `/i18n/locales/${data.locales[0].id}`,
method: 'PUT',
body: localeUpdate,
});
expect(res.statusCode).toBe(400);
expect(res.body).toMatchObject({
2021-10-20 17:30:05 +02:00
data: null,
error: {
details: {
errors: [
{
2021-11-04 10:54:13 +01:00
path: [],
2021-10-20 17:30:05 +02:00
name: 'ValidationError',
message: 'this field has unspecified keys: code',
},
],
},
message: 'this field has unspecified keys: code',
name: 'ValidationError',
status: 400,
},
});
});
test('Cannot update the code of a locale (with name)', async () => {
const localeUpdate = {
name: 'French',
code: 'ak',
};
2022-08-08 15:50:34 +02:00
const res = await rq({
url: `/i18n/locales/${data.locales[0].id}`,
method: 'PUT',
body: localeUpdate,
});
expect(res.statusCode).toBe(400);
expect(res.body).toMatchObject({
2021-10-20 17:30:05 +02:00
data: null,
error: {
details: {
errors: [
{
2021-11-04 10:54:13 +01:00
path: [],
2021-10-20 17:30:05 +02:00
name: 'ValidationError',
message: 'this field has unspecified keys: code',
},
],
},
message: 'this field has unspecified keys: code',
name: 'ValidationError',
status: 400,
},
});
});
2021-02-05 15:39:11 +01:00
test('Only one locale can be default (PUT)', async () => {
let res = await rq({
2021-02-05 15:39:11 +01:00
url: `/i18n/locales/${data.locales[0].id}`,
method: 'PUT',
body: { isDefault: true },
});
expect(res.statusCode).toBe(200);
expect(res.body.isDefault).toBe(true);
res = await rq({
url: `/i18n/locales/${data.locales[1].id}`,
method: 'PUT',
body: { isDefault: true },
});
expect(res.statusCode).toBe(200);
expect(res.body.isDefault).toBe(true);
res = await rq({
url: '/i18n/locales',
method: 'GET',
});
expect(res.statusCode).toBe(200);
2022-08-08 23:33:39 +02:00
expect(res.body.find((locale) => locale.code === data.locales[0].code).isDefault).toBe(false);
expect(res.body.find((locale) => locale.code === data.locales[1].code).isDefault).toBe(true);
2021-02-05 15:39:11 +01:00
});
test('Cannot unselect isDefault', async () => {
let res = await rq({
url: `/i18n/locales/${data.locales[0].id}`,
method: 'PUT',
body: { isDefault: true },
});
expect(res.statusCode).toBe(200);
expect(res.body.isDefault).toBe(true);
res = await rq({
url: `/i18n/locales/${data.locales[0].id}`,
method: 'PUT',
body: { isDefault: false },
});
expect(res.statusCode).toBe(200);
expect(res.body.isDefault).toBe(true);
});
});
2021-02-12 15:38:25 +01:00
describe('Delete', () => {
test('Cannot delete default locale', async () => {
let res = await rq({
url: `/i18n/locales/${data.locales[0].id}`,
method: 'PUT',
body: { isDefault: true },
});
expect(res.statusCode).toBe(200);
expect(res.body.isDefault).toBe(true);
data.locales[1].isDefault = false;
res = await rq({
url: `/i18n/locales/${data.locales[0].id}`,
method: 'DELETE',
});
expect(res.statusCode).toBe(400);
2021-10-20 17:30:05 +02:00
expect(res.body).toMatchObject({
data: null,
error: {
status: 400,
name: 'ApplicationError',
message: 'Cannot delete the default locale',
details: {},
},
});
2021-02-12 15:38:25 +01:00
});
2021-04-02 12:16:07 +02:00
test('Simply delete a locale', async () => {
2021-04-02 10:45:44 +02:00
const res = await rq({
url: `/i18n/locales/${data.locales[1].id}`,
method: 'DELETE',
});
expect(res.statusCode).toBe(200);
expect(res.body).toMatchObject(omitTimestamps(data.locales[1]));
data.deletedLocales.push(res.body);
data.locales.splice(1, 1);
});
2021-04-02 12:16:07 +02:00
test('Delete a locale and entities in this locale', async () => {
const { body: frenchProduct } = await rq({
2021-08-06 18:09:49 +02:00
url: '/content-manager/collection-types/api::product.product',
method: 'POST',
qs: { locale: 'fr-FR' },
body: { name: 'product name' },
});
await rq({
url: `/content-manager/collection-types/api::product.product/${frenchProduct.id}`,
method: 'PUT',
[core] document unique field validation per content type and locale (#19153) * feat: use document service in content manager * feat: update contracts with meta information * chore: group metadata types into a single type * feat: metadata information in single types * chore: change meta contract to return documents instead of strings * fix: remove unused type * fix: ignore doc id if entry is null * fix: update contract metadata * feat: document metadata service * feat: locale and status filtering * chore: add comment * chore: refactor metadata service * chore: refactor entity manager exists to handle single types * feat: refactor single type controllers to use documents * feat: get locale param from in cm endpoints * Revert "feat: get locale param from in cm endpoints" This reverts commit 856c38588b8f8521cadd85c8c933f42a36a2178a. * feat: get locale param from cm endpoints * Update packages/plugins/i18n/server/src/controllers/validate-locale-creation.ts Co-authored-by: Ben Irvin <ben@innerdvations.com> * fix: entity manager unit tests * chore: unit test document metadata * feat: prevent empty string locale filtering * feat(core): document unique field validation per content type and locale * feat(e2e): test document unique fields * fix(admin): ts build * fix: cm contract import * chore: test new d&p cm features * feat(core): validate document unique fields within one publication state chore: remove build:ts * fix: search content manager api test * fix: cm tests * fix: cm tests * fix: cm tests * fix(content-manager): access to non default locale documents (#19190) * fix(content-manager): access to non default locale documents * chore(content-manager): revert route construction * fix(content-manager): api tests for number of draft relations * test(content-manager): counting number of draft relations for non default locales * chore(content-manager): remove default locale from entity manager countDraftRelations * test(document-service): api tests for unique document fields per publication state * test(i18n): api tests for unique document fields per locale * chore(api-tests): adjust author schema * chore(e2e): disable edit view tests (#19235) * fix(core): unique validation on publish * fix(api-tests): un-localise author * fix(e2e): incorrect path * fix(admin): tidy up * fix(admin): pass correct locale query params fix(core): pass locale outside of filters * chore(api-tests): wip comment for relatedEntityId logic * fix(core): entity validator tests * chore(api-tests): wip skip failing relations i18n test * chore(e2e): clean up editview spec * fix(content-manager): pass locale to publish and unpublish * fix(content-manager): fix test:ts:back * fix(e2e): update edit view expected path * fix(entity-validator): default to null locale fix(api-tests): wip i18n test changes * fix(e2e): wip UID fields * chore(content-type-builder): temporarily disable component unique field checkboxes (#19253) * fix(admin): wip correctly pass params feat(e2e): reintroduce uniqueness e2e tests * Refactor UID validation for V5 (#19285) * fix(database): wip remove UID unique DB constraint * feat(content-manager): update uid checking endpoints to look at locale * chore(admin): clean up * fix(content-manager): single type unit tests * fix(content-manager): send all params to uid endpoints * feat(e2e): update tar backup to support unique CT * fix(e2e): uniqueness spec * chore(e2e): remove only * fix(content-manager): only check UID availability against drafts feat(api-tests): test case for UID validation across locales * fix(content-manager): uid unit tests * chore(api-tests): clean up * chore(content-manager): clean up * chore(i18n): remove relatedEntityId reference --------- Co-authored-by: Marc-Roig <marc12info@gmail.com> Co-authored-by: Ben Irvin <ben@innerdvations.com> Co-authored-by: Josh <37798644+joshuaellis@users.noreply.github.com>
2024-01-29 15:35:20 +00:00
body: { name: 'product name' },
qs: {
locale: 'en',
},
});
const {
body: { results: createdProducts },
} = await rq({
2021-08-06 18:09:49 +02:00
url: '/content-manager/collection-types/api::product.product',
method: 'GET',
2021-07-19 16:47:24 +02:00
qs: { locale: 'fr-FR' },
});
expect(createdProducts).toHaveLength(1);
// expect(createdProducts[0].localizations[0].locale).toBe('en');
2021-02-12 15:38:25 +01:00
const res = await rq({
url: `/i18n/locales/${data.locales[1].id}`,
method: 'DELETE',
});
const {
body: { results: frenchProducts },
} = await rq({
2021-08-06 18:09:49 +02:00
url: '/content-manager/collection-types/api::product.product',
method: 'GET',
2021-07-19 16:47:24 +02:00
qs: { locale: 'fr-FR' },
});
expect(frenchProducts).toHaveLength(0);
const {
body: { results: englishProducts },
} = await rq({
2021-08-06 18:09:49 +02:00
url: '/content-manager/collection-types/api::product.product',
method: 'GET',
2021-07-19 16:47:24 +02:00
qs: { locale: 'en' },
});
expect(englishProducts).toHaveLength(1);
2021-02-12 15:38:25 +01:00
expect(res.statusCode).toBe(200);
expect(res.body).toMatchObject(omitTimestamps(data.locales[1]));
data.deletedLocales.push(res.body);
data.locales.splice(1, 1);
});
test('Cannot delete not found locale', async () => {
2022-08-08 15:50:34 +02:00
const res = await rq({
2021-02-12 15:38:25 +01:00
url: `/i18n/locales/${data.deletedLocales[0].id}`,
method: 'DELETE',
});
expect(res.statusCode).toBe(404);
2021-10-20 17:30:05 +02:00
expect(res.body).toMatchObject({
data: null,
error: {
status: 404,
name: 'NotFoundError',
message: 'locale.notFound',
details: {},
},
});
2021-02-12 15:38:25 +01:00
});
});
});