2022-10-07 20:33:30 +02:00
|
|
|
'use strict';
|
|
|
|
|
2023-04-05 10:32:20 +02:00
|
|
|
const { createStrapiInstance } = require('api-tests/strapi');
|
|
|
|
const { createTestBuilder } = require('api-tests/builder');
|
|
|
|
const { createAuthRequest } = require('api-tests/request');
|
2022-10-07 20:33:30 +02:00
|
|
|
|
|
|
|
const builder = createTestBuilder();
|
|
|
|
let strapi;
|
|
|
|
let rq;
|
2022-10-10 14:25:44 +02:00
|
|
|
const data = {
|
|
|
|
categories: [],
|
|
|
|
categoriesdp: {
|
|
|
|
published: [],
|
|
|
|
draft: [],
|
|
|
|
},
|
|
|
|
};
|
2022-10-07 20:33:30 +02:00
|
|
|
|
2022-10-10 14:25:44 +02:00
|
|
|
const productModel = {
|
2022-10-07 20:33:30 +02:00
|
|
|
displayName: 'Product',
|
|
|
|
singularName: 'product',
|
|
|
|
pluralName: 'products',
|
|
|
|
description: '',
|
|
|
|
collectionName: '',
|
|
|
|
attributes: {
|
|
|
|
name: {
|
|
|
|
type: 'string',
|
|
|
|
required: true,
|
|
|
|
},
|
|
|
|
categoriesdp: {
|
|
|
|
type: 'relation',
|
|
|
|
relation: 'oneToMany',
|
|
|
|
target: 'api::categorydp.categorydp',
|
|
|
|
targetAttribute: 'product',
|
|
|
|
},
|
2022-10-10 14:25:44 +02:00
|
|
|
onecategorydp: {
|
|
|
|
type: 'relation',
|
|
|
|
relation: 'oneToOne',
|
|
|
|
target: 'api::categorydp.categorydp',
|
|
|
|
targetAttribute: 'oneproduct',
|
|
|
|
},
|
2022-10-07 20:33:30 +02:00
|
|
|
categories: {
|
|
|
|
type: 'relation',
|
|
|
|
relation: 'oneToMany',
|
|
|
|
target: 'api::category.category',
|
|
|
|
targetAttribute: 'product',
|
|
|
|
},
|
|
|
|
compo: {
|
|
|
|
component: 'default.compo',
|
|
|
|
type: 'component',
|
|
|
|
},
|
|
|
|
comporep: {
|
|
|
|
component: 'default.compo',
|
|
|
|
type: 'component',
|
2022-10-10 14:25:44 +02:00
|
|
|
repeatable: true,
|
2022-10-07 20:33:30 +02:00
|
|
|
},
|
|
|
|
dz: {
|
|
|
|
components: ['default.compo'],
|
|
|
|
type: 'dynamiczone',
|
|
|
|
},
|
|
|
|
},
|
|
|
|
};
|
|
|
|
|
2022-10-10 14:25:44 +02:00
|
|
|
const categoryDPModel = {
|
2022-10-07 20:33:30 +02:00
|
|
|
displayName: 'Category Draft & Publish',
|
|
|
|
singularName: 'categorydp',
|
|
|
|
pluralName: 'categoriesdp',
|
|
|
|
attributes: {
|
|
|
|
name: {
|
|
|
|
type: 'string',
|
|
|
|
},
|
|
|
|
},
|
|
|
|
};
|
|
|
|
|
2022-10-10 14:25:44 +02:00
|
|
|
const categoryModel = {
|
2022-10-07 20:33:30 +02:00
|
|
|
displayName: 'Category',
|
|
|
|
singularName: 'category',
|
|
|
|
pluralName: 'categories',
|
|
|
|
attributes: {
|
|
|
|
name: {
|
|
|
|
type: 'string',
|
|
|
|
},
|
|
|
|
},
|
|
|
|
};
|
|
|
|
|
2022-10-10 14:25:44 +02:00
|
|
|
const compoModel = {
|
2022-10-07 20:33:30 +02:00
|
|
|
displayName: 'compo',
|
|
|
|
attributes: {
|
|
|
|
name: {
|
|
|
|
type: 'string',
|
|
|
|
},
|
|
|
|
categoriesdp: {
|
|
|
|
type: 'relation',
|
|
|
|
relation: 'oneToMany',
|
|
|
|
target: 'api::categorydp.categorydp',
|
|
|
|
},
|
|
|
|
categories: {
|
|
|
|
type: 'relation',
|
|
|
|
relation: 'oneToMany',
|
|
|
|
target: 'api::category.category',
|
2022-10-10 14:25:44 +02:00
|
|
|
},
|
|
|
|
onecategorydp: {
|
|
|
|
type: 'relation',
|
|
|
|
relation: 'oneToOne',
|
|
|
|
target: 'api::categorydp.categorydp',
|
2022-10-07 20:33:30 +02:00
|
|
|
},
|
|
|
|
},
|
|
|
|
};
|
|
|
|
|
|
|
|
describe('CM API - Basic', () => {
|
|
|
|
beforeAll(async () => {
|
|
|
|
await builder
|
2022-10-10 14:25:44 +02:00
|
|
|
.addContentTypes([categoryDPModel, categoryModel])
|
|
|
|
.addComponent(compoModel)
|
|
|
|
.addContentTypes([productModel])
|
2022-10-07 20:33:30 +02:00
|
|
|
.build();
|
|
|
|
|
|
|
|
strapi = await createStrapiInstance();
|
|
|
|
rq = await createAuthRequest({ strapi });
|
2022-10-10 14:25:44 +02:00
|
|
|
|
|
|
|
const { body: category } = await rq({
|
|
|
|
method: 'POST',
|
|
|
|
url: '/content-manager/collection-types/api::category.category',
|
|
|
|
body: { name: 'Food' },
|
|
|
|
});
|
2023-10-24 10:57:31 +02:00
|
|
|
|
|
|
|
await rq({
|
|
|
|
method: 'POST',
|
|
|
|
url: `/content-manager/collection-types/api::category.category/${category.id}/actions/publish`,
|
|
|
|
});
|
|
|
|
|
2022-10-10 14:25:44 +02:00
|
|
|
data.categories.push(category);
|
|
|
|
|
|
|
|
const { body: categoryPublished } = await rq({
|
|
|
|
method: 'POST',
|
|
|
|
url: '/content-manager/collection-types/api::categorydp.categorydp',
|
|
|
|
body: { name: 'Food' },
|
|
|
|
});
|
2023-10-24 10:57:31 +02:00
|
|
|
|
2022-10-10 14:25:44 +02:00
|
|
|
await rq({
|
|
|
|
method: 'POST',
|
|
|
|
url: `/content-manager/collection-types/api::categorydp.categorydp/${categoryPublished.id}/actions/publish`,
|
|
|
|
});
|
2023-10-24 10:57:31 +02:00
|
|
|
|
2022-10-10 14:25:44 +02:00
|
|
|
data.categoriesdp.published.push(categoryPublished);
|
|
|
|
|
|
|
|
const { body: categoryDraft1 } = await rq({
|
|
|
|
method: 'POST',
|
|
|
|
url: '/content-manager/collection-types/api::categorydp.categorydp',
|
|
|
|
body: { name: 'Food' },
|
|
|
|
});
|
2023-10-24 10:57:31 +02:00
|
|
|
|
2022-10-10 14:25:44 +02:00
|
|
|
data.categoriesdp.draft.push(categoryDraft1);
|
|
|
|
|
|
|
|
const { body: categoryDraft2 } = await rq({
|
|
|
|
method: 'POST',
|
|
|
|
url: '/content-manager/collection-types/api::categorydp.categorydp',
|
|
|
|
body: { name: 'Food' },
|
|
|
|
});
|
2023-10-24 10:57:31 +02:00
|
|
|
|
2022-10-10 14:25:44 +02:00
|
|
|
data.categoriesdp.draft.push(categoryDraft2);
|
2022-10-07 20:33:30 +02:00
|
|
|
});
|
|
|
|
|
|
|
|
afterAll(async () => {
|
|
|
|
await strapi.destroy();
|
|
|
|
await builder.cleanup();
|
|
|
|
});
|
|
|
|
|
|
|
|
test('Return 0 when no relations are set', async () => {
|
2022-10-10 14:25:44 +02:00
|
|
|
const { body: product } = await rq({
|
|
|
|
method: 'POST',
|
|
|
|
url: '/content-manager/collection-types/api::product.product',
|
|
|
|
body: { name: 'Pizza' },
|
|
|
|
});
|
|
|
|
|
|
|
|
const { body } = await rq({
|
|
|
|
method: 'GET',
|
2023-07-18 09:03:55 +02:00
|
|
|
url: `/content-manager/collection-types/api::product.product/${product.id}/actions/countDraftRelations`,
|
2022-10-10 14:25:44 +02:00
|
|
|
});
|
|
|
|
|
|
|
|
expect(body.data).toBe(0);
|
|
|
|
});
|
|
|
|
|
|
|
|
test('Return 0 when only relations without d&p are set', async () => {
|
2022-10-07 20:33:30 +02:00
|
|
|
const { body: product } = await rq({
|
|
|
|
method: 'POST',
|
|
|
|
url: '/content-manager/collection-types/api::product.product',
|
|
|
|
body: {
|
|
|
|
name: 'Pizza',
|
2022-10-10 14:25:44 +02:00
|
|
|
onecategorydp: data.categories[0].id,
|
|
|
|
categories: [data.categories[0].id],
|
|
|
|
compo: {
|
|
|
|
onecategorydp: data.categories[0].id,
|
|
|
|
categories: [data.categories[0].id],
|
|
|
|
},
|
|
|
|
comporep: [{ categories: [data.categories[0].id], onecategorydp: data.categories[0].id }],
|
|
|
|
dz: [
|
|
|
|
{
|
|
|
|
__component: 'default.compo',
|
|
|
|
categories: [data.categories[0].id],
|
|
|
|
onecategorydp: data.categories[0].id,
|
|
|
|
},
|
|
|
|
],
|
2022-10-07 20:33:30 +02:00
|
|
|
},
|
|
|
|
});
|
|
|
|
|
|
|
|
const { body } = await rq({
|
|
|
|
method: 'GET',
|
2023-07-18 09:03:55 +02:00
|
|
|
url: `/content-manager/collection-types/api::product.product/${product.id}/actions/countDraftRelations`,
|
2022-10-07 20:33:30 +02:00
|
|
|
});
|
|
|
|
|
|
|
|
expect(body.data).toBe(0);
|
|
|
|
});
|
2022-10-10 14:25:44 +02:00
|
|
|
|
|
|
|
test('Return 0 when relations without d&p are set & published relations only', async () => {
|
|
|
|
const categoryId = data.categories[0].id;
|
|
|
|
const publishedId = data.categoriesdp.published[0].id;
|
|
|
|
|
|
|
|
const { body: product } = await rq({
|
|
|
|
method: 'POST',
|
|
|
|
url: '/content-manager/collection-types/api::product.product',
|
|
|
|
body: {
|
|
|
|
name: 'Pizza',
|
|
|
|
onecategorydp: publishedId,
|
|
|
|
categories: [categoryId],
|
|
|
|
categoriesdp: [publishedId],
|
|
|
|
compo: {
|
|
|
|
onecategorydp: publishedId,
|
|
|
|
categories: [categoryId],
|
|
|
|
categoriesdp: [publishedId],
|
|
|
|
},
|
|
|
|
comporep: [
|
|
|
|
{ onecategorydp: publishedId, categories: [categoryId], categoriesdp: [publishedId] },
|
|
|
|
],
|
|
|
|
dz: [
|
|
|
|
{
|
|
|
|
__component: 'default.compo',
|
|
|
|
onecategorydp: publishedId,
|
|
|
|
categories: [categoryId],
|
|
|
|
categoriesdp: [publishedId],
|
|
|
|
},
|
|
|
|
],
|
|
|
|
},
|
|
|
|
});
|
|
|
|
|
|
|
|
const { body } = await rq({
|
|
|
|
method: 'GET',
|
2023-07-18 09:03:55 +02:00
|
|
|
url: `/content-manager/collection-types/api::product.product/${product.id}/actions/countDraftRelations`,
|
2022-10-10 14:25:44 +02:00
|
|
|
});
|
|
|
|
|
|
|
|
expect(body.data).toBe(0);
|
|
|
|
});
|
|
|
|
|
|
|
|
test('Return 8 when there are 8 drafts (1 xToOne & 1 xToMany on ct, compo, comporep, dz)', async () => {
|
|
|
|
const categoryId = data.categories[0].id;
|
|
|
|
const draftId = data.categoriesdp.draft[0].id;
|
|
|
|
|
|
|
|
const { body: product } = await rq({
|
|
|
|
method: 'POST',
|
|
|
|
url: '/content-manager/collection-types/api::product.product',
|
|
|
|
body: {
|
|
|
|
name: 'Pizza',
|
|
|
|
onecategorydp: draftId,
|
|
|
|
categories: [categoryId],
|
|
|
|
categoriesdp: [draftId],
|
|
|
|
compo: {
|
|
|
|
onecategorydp: draftId,
|
|
|
|
categories: [categoryId],
|
|
|
|
categoriesdp: [draftId],
|
|
|
|
},
|
|
|
|
comporep: [{ onecategorydp: draftId, categories: [categoryId], categoriesdp: [draftId] }],
|
|
|
|
dz: [
|
|
|
|
{
|
|
|
|
__component: 'default.compo',
|
|
|
|
onecategorydp: draftId,
|
|
|
|
categories: [categoryId],
|
|
|
|
categoriesdp: [draftId],
|
|
|
|
},
|
|
|
|
],
|
|
|
|
},
|
|
|
|
});
|
|
|
|
|
|
|
|
const { body } = await rq({
|
|
|
|
method: 'GET',
|
2023-07-18 09:03:55 +02:00
|
|
|
url: `/content-manager/collection-types/api::product.product/${product.id}/actions/countDraftRelations`,
|
2022-10-10 14:25:44 +02:00
|
|
|
});
|
|
|
|
|
|
|
|
expect(body.data).toBe(8);
|
|
|
|
});
|
|
|
|
|
|
|
|
test('Return 8 when there are 8 drafts (1 xToOne & 1/2 xToMany on ct, compo, comporep, dz)', async () => {
|
|
|
|
const categoryId = data.categories[0].id;
|
|
|
|
const draftId = data.categoriesdp.draft[0].id;
|
|
|
|
|
|
|
|
const { body: product } = await rq({
|
|
|
|
method: 'POST',
|
|
|
|
url: '/content-manager/collection-types/api::product.product',
|
|
|
|
body: {
|
|
|
|
name: 'Pizza',
|
|
|
|
onecategorydp: draftId,
|
|
|
|
categories: [categoryId],
|
|
|
|
categoriesdp: [draftId, categoryId],
|
|
|
|
compo: {
|
|
|
|
onecategorydp: draftId,
|
|
|
|
categories: [categoryId],
|
|
|
|
categoriesdp: [draftId, categoryId],
|
|
|
|
},
|
|
|
|
comporep: [
|
|
|
|
{ onecategorydp: draftId, categories: [categoryId], categoriesdp: [draftId, categoryId] },
|
|
|
|
],
|
|
|
|
dz: [
|
|
|
|
{
|
|
|
|
onecategorydp: draftId,
|
|
|
|
__component: 'default.compo',
|
|
|
|
categories: [categoryId],
|
|
|
|
categoriesdp: [draftId, categoryId],
|
|
|
|
},
|
|
|
|
],
|
|
|
|
},
|
|
|
|
});
|
|
|
|
|
|
|
|
const { body } = await rq({
|
|
|
|
method: 'GET',
|
2023-07-18 09:03:55 +02:00
|
|
|
url: `/content-manager/collection-types/api::product.product/${product.id}/actions/countDraftRelations`,
|
2022-10-10 14:25:44 +02:00
|
|
|
});
|
|
|
|
|
|
|
|
expect(body.data).toBe(8);
|
|
|
|
});
|
|
|
|
|
|
|
|
test('Return 12 when there are 12 drafts (1 xToOne & 2 xToMany on ct, compo, comporep, dz)', async () => {
|
|
|
|
const categoryId = data.categories[0].id;
|
|
|
|
const draft1Id = data.categoriesdp.draft[0].id;
|
|
|
|
const draft2Id = data.categoriesdp.draft[1].id;
|
|
|
|
|
|
|
|
const { body: product } = await rq({
|
|
|
|
method: 'POST',
|
|
|
|
url: '/content-manager/collection-types/api::product.product',
|
|
|
|
body: {
|
|
|
|
name: 'Pizza',
|
|
|
|
onecategorydp: draft1Id,
|
|
|
|
categories: [categoryId],
|
|
|
|
categoriesdp: [draft1Id, draft2Id],
|
|
|
|
compo: {
|
|
|
|
onecategorydp: draft1Id,
|
|
|
|
categories: [categoryId],
|
|
|
|
categoriesdp: [draft1Id, draft2Id],
|
|
|
|
},
|
|
|
|
comporep: [
|
|
|
|
{ onecategorydp: draft1Id, categories: [categoryId], categoriesdp: [draft1Id, draft2Id] },
|
|
|
|
],
|
|
|
|
dz: [
|
|
|
|
{
|
|
|
|
onecategorydp: draft1Id,
|
|
|
|
__component: 'default.compo',
|
|
|
|
categories: [categoryId],
|
|
|
|
categoriesdp: [draft1Id, draft2Id],
|
|
|
|
},
|
|
|
|
],
|
|
|
|
},
|
|
|
|
});
|
|
|
|
|
|
|
|
const { body } = await rq({
|
|
|
|
method: 'GET',
|
2023-07-18 09:03:55 +02:00
|
|
|
url: `/content-manager/collection-types/api::product.product/${product.id}/actions/countDraftRelations`,
|
2022-10-10 14:25:44 +02:00
|
|
|
});
|
|
|
|
|
|
|
|
expect(body.data).toBe(12);
|
|
|
|
});
|
2022-10-07 20:33:30 +02:00
|
|
|
});
|