don't copy published_at as a non-localized-field (#9761)

This commit is contained in:
Pierre Noël 2021-03-18 12:38:36 +01:00 committed by GitHub
parent 2c3bf778a3
commit ddd4b37fb2
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 50 additions and 6 deletions

View File

@ -1,6 +1,7 @@
'use strict';
const _ = require('lodash');
const { PUBLISHED_AT_ATTRIBUTE } = require('strapi-utils').contentTypes.constants;
const { getService } = require('../../utils');
@ -18,7 +19,7 @@ module.exports = () => {
configurable: false,
visible: false,
collection: modelName,
populate: ['id', 'locale', 'published_at'],
populate: ['id', 'locale', PUBLISHED_AT_ATTRIBUTE],
});
_.set(attributes, 'locale', {

View File

@ -76,6 +76,42 @@ describe('content-types service', () => {
})
).toEqual(['stars', 'price']);
});
test('Consider locale, localizations & published_at as localized', () => {
expect(
getNonLocalizedAttributes({
uid: 'test-model',
attributes: {
title: {
type: 'string',
pluginOptions: {
i18n: {
localized: true,
},
},
},
stars: {
type: 'integer',
},
price: {
type: 'integer',
},
locale: {
type: 'string',
visible: false,
},
localizations: {
collection: 'test-model',
visible: false,
},
published_at: {
type: 'datetime',
visible: false,
},
},
})
).toEqual(['stars', 'price']);
});
});
describe('getValidLocale', () => {
@ -329,7 +365,7 @@ describe('content-types service', () => {
});
describe('copyNonLocalizedAttributes', () => {
test('Does not copy locale & localizations', () => {
test('Does not copy locale, localizations & published_at', () => {
const model = {
attributes: {
title: {
@ -349,9 +385,15 @@ describe('content-types service', () => {
},
locale: {
type: 'string',
visible: false,
},
localizations: {
collection: 'test-model',
visible: false,
},
published_at: {
type: 'datetime',
visible: false,
},
},
};
@ -364,6 +406,7 @@ describe('content-types service', () => {
description: 'My super description',
locale: 'en',
localizations: [1, 2, 3],
published_at: '2021-03-18T09:47:37.557Z',
};
const result = copyNonLocalizedAttributes(model, input);

View File

@ -2,7 +2,7 @@
const { has, prop, isNil } = require('lodash/fp');
const { cloneDeepWith, pick, pipe } = require('lodash/fp');
const { isRelationalAttribute } = require('strapi-utils').contentTypes;
const { isRelationalAttribute, getVisibleAttributes } = require('strapi-utils').contentTypes;
const { getService } = require('../utils');
const isLocalized = modelOrAttribute => {
@ -72,9 +72,9 @@ const isLocalizedAttribute = (model, attributeName) => {
* @returns {string[]}
*/
const getNonLocalizedAttributes = model => {
return Object.keys(model.attributes)
.filter(attributeName => !['locale', 'localizations'].includes(attributeName))
.filter(attributeName => !isLocalizedAttribute(model, attributeName));
return getVisibleAttributes(model).filter(
attributeName => !isLocalizedAttribute(model, attributeName)
);
};
const removeIds = cloneDeepWith(value => {