mirror of
https://github.com/strapi/strapi.git
synced 2025-12-29 16:16:20 +00:00
Remove middlewares that mutates the CM links and use the hooks api instead
Signed-off-by: soupette <cyril.lpz@gmail.com>
This commit is contained in:
parent
b675884e6f
commit
2c7bcbf70e
@ -31,6 +31,9 @@ export default {
|
||||
|
||||
// Hook that allows to mutate the displayed headers of the list view table
|
||||
app.createHook('cm/inject-column-in-table');
|
||||
// Hook that allows to mutate the CM's link pre-set filters
|
||||
app.createHook('cm/mutate-collection-type-links');
|
||||
app.createHook('cm/mutate-single-type-links');
|
||||
|
||||
app.registerPlugin({
|
||||
description: pluginDescription,
|
||||
|
||||
@ -1,4 +1,4 @@
|
||||
import { request, useNotification, useRBACProvider } from '@strapi/helper-plugin';
|
||||
import { request, useNotification, useRBACProvider, useStrapiApp } from '@strapi/helper-plugin';
|
||||
import { useEffect, useRef } from 'react';
|
||||
import { useDispatch, useSelector } from 'react-redux';
|
||||
import { getData, resetProps, setContentTypeLinks } from './actions';
|
||||
@ -12,6 +12,7 @@ const useModels = () => {
|
||||
const state = useSelector(selectAppDomain());
|
||||
const fetchDataRef = useRef();
|
||||
const { allPermissions } = useRBACProvider();
|
||||
const { runHookWaterfall } = useStrapiApp();
|
||||
|
||||
const fetchData = async signal => {
|
||||
dispatch(getData());
|
||||
@ -29,12 +30,16 @@ const useModels = () => {
|
||||
toggleNotification
|
||||
);
|
||||
|
||||
const actionToDispatch = setContentTypeLinks(
|
||||
authorizedCtLinks,
|
||||
authorizedStLinks,
|
||||
const { ctLinks } = runHookWaterfall('cm/mutate-collection-type-links', {
|
||||
ctLinks: authorizedCtLinks,
|
||||
models,
|
||||
components
|
||||
);
|
||||
});
|
||||
const { stLinks } = runHookWaterfall('cm/mutate-single-type-links', {
|
||||
stLinks: authorizedStLinks,
|
||||
models,
|
||||
});
|
||||
|
||||
const actionToDispatch = setContentTypeLinks(ctLinks, stLinks, models, components);
|
||||
|
||||
dispatch(actionToDispatch);
|
||||
} catch (err) {
|
||||
|
||||
@ -0,0 +1,23 @@
|
||||
import addLocaleToLinksSearch from './utils/addLocaleToLinksSearch';
|
||||
|
||||
const addLocaleToCollectionTypesLinksHook = ({ ctLinks, models }, store) => {
|
||||
if (!ctLinks.length) {
|
||||
return { ctLinks, models };
|
||||
}
|
||||
|
||||
const storeState = store.getState();
|
||||
const { locales } = storeState.i18n_locales;
|
||||
const { collectionTypesRelatedPermissions } = storeState.rbacProvider;
|
||||
|
||||
const mutatedLinks = addLocaleToLinksSearch(
|
||||
ctLinks,
|
||||
'collectionType',
|
||||
models,
|
||||
locales,
|
||||
collectionTypesRelatedPermissions
|
||||
);
|
||||
|
||||
return { ctLinks: mutatedLinks, models };
|
||||
};
|
||||
|
||||
export default addLocaleToCollectionTypesLinksHook;
|
||||
@ -0,0 +1,23 @@
|
||||
import addLocaleToLinksSearch from './utils/addLocaleToLinksSearch';
|
||||
|
||||
const addLocaleToSingleTypesLinks = ({ stLinks, models }, store) => {
|
||||
if (!stLinks.length) {
|
||||
return { stLinks, models };
|
||||
}
|
||||
|
||||
const storeState = store.getState();
|
||||
const { locales } = storeState.i18n_locales;
|
||||
const { collectionTypesRelatedPermissions } = storeState.rbacProvider;
|
||||
|
||||
const mutatedLinks = addLocaleToLinksSearch(
|
||||
stLinks,
|
||||
'singleType',
|
||||
models,
|
||||
locales,
|
||||
collectionTypesRelatedPermissions
|
||||
);
|
||||
|
||||
return { stLinks: mutatedLinks, models };
|
||||
};
|
||||
|
||||
export default addLocaleToSingleTypesLinks;
|
||||
@ -0,0 +1,120 @@
|
||||
import { fixtures } from '../../../../../../admin-test-utils/lib';
|
||||
import addLocaleToCollectionTypesLinksHook from '../addLocaleToCollectionTypesLinks';
|
||||
|
||||
describe('i18n | contentManagerHooks | addLocaleToCollectionTypesLinksHook', () => {
|
||||
let store;
|
||||
|
||||
beforeEach(() => {
|
||||
store = {
|
||||
...fixtures.store.state,
|
||||
i18n_locales: { locales: [] },
|
||||
};
|
||||
store.rbacProvider.allPermissions = [];
|
||||
|
||||
store.rbacProvider.collectionTypesRelatedPermissions = {
|
||||
test: {
|
||||
'plugins::content-manager.explorer.read': [],
|
||||
'plugins::content-manager.explorer.create': [],
|
||||
},
|
||||
};
|
||||
store.getState = function() {
|
||||
return this;
|
||||
};
|
||||
});
|
||||
|
||||
it('should return the initialValues when the ctLinks array is empty', () => {
|
||||
const data = {
|
||||
ctLinks: [],
|
||||
models: [],
|
||||
};
|
||||
|
||||
const results = addLocaleToCollectionTypesLinksHook(data, store);
|
||||
|
||||
expect(results).toEqual(data);
|
||||
});
|
||||
|
||||
it('should not add the search key to a collection type link when i18n is not enabled on the single type', () => {
|
||||
const data = {
|
||||
ctLinks: [{ to: 'cm/collectionType/test' }],
|
||||
models: [{ uid: 'test', pluginOptions: { i18n: { localized: false } } }],
|
||||
};
|
||||
|
||||
const results = addLocaleToCollectionTypesLinksHook(data, store);
|
||||
|
||||
expect(results).toEqual(data);
|
||||
});
|
||||
|
||||
it('should add a search key with the default locale when the user has the right to read it', () => {
|
||||
store.i18n_locales = { locales: [{ code: 'en', isDefault: true }] };
|
||||
store.rbacProvider.collectionTypesRelatedPermissions.test[
|
||||
'plugins::content-manager.explorer.read'
|
||||
] = [{ properties: { locales: ['en'] } }];
|
||||
|
||||
const data = {
|
||||
ctLinks: [{ to: 'cm/collectionType/test', search: null }],
|
||||
models: [{ uid: 'test', pluginOptions: { i18n: { localized: true } } }],
|
||||
};
|
||||
|
||||
const results = addLocaleToCollectionTypesLinksHook(data, store);
|
||||
|
||||
const expected = {
|
||||
ctLinks: [{ to: 'cm/collectionType/test', search: 'plugins[i18n][locale]=en' }],
|
||||
models: [{ uid: 'test', pluginOptions: { i18n: { localized: true } } }],
|
||||
};
|
||||
|
||||
expect(results).toEqual(expected);
|
||||
});
|
||||
|
||||
it('should set the isDisplayed key to false when the user does not have the right to read any locale', () => {
|
||||
store.i18n_locales.locales = [{ code: 'en', isDefault: true }];
|
||||
store.rbacProvider.collectionTypesRelatedPermissions.test[
|
||||
'plugins::content-manager.explorer.read'
|
||||
] = [{ properties: { locales: [] } }];
|
||||
|
||||
const data = {
|
||||
ctLinks: [{ to: 'cm/collectionType/test', search: 'page=1&pageSize=10' }],
|
||||
models: [{ uid: 'test', pluginOptions: { i18n: { localized: true } } }],
|
||||
};
|
||||
|
||||
const results = addLocaleToCollectionTypesLinksHook(data, store);
|
||||
|
||||
const expected = {
|
||||
ctLinks: [
|
||||
{
|
||||
to: 'cm/collectionType/test',
|
||||
isDisplayed: false,
|
||||
search: 'page=1&pageSize=10',
|
||||
},
|
||||
],
|
||||
models: [{ uid: 'test', pluginOptions: { i18n: { localized: true } } }],
|
||||
};
|
||||
|
||||
expect(results).toEqual(expected);
|
||||
});
|
||||
|
||||
it('should keep the previous search', () => {
|
||||
store.i18n_locales.locales = [{ code: 'en', isDefault: true }];
|
||||
store.rbacProvider.collectionTypesRelatedPermissions.test[
|
||||
'plugins::content-manager.explorer.read'
|
||||
] = [{ properties: { locales: ['en'] } }];
|
||||
|
||||
const data = {
|
||||
ctLinks: [{ to: 'cm/collectionType/test', search: 'plugins[plugin][test]=test' }],
|
||||
models: [{ uid: 'test', pluginOptions: { i18n: { localized: true } } }],
|
||||
};
|
||||
|
||||
const results = addLocaleToCollectionTypesLinksHook(data, store);
|
||||
|
||||
const expected = {
|
||||
ctLinks: [
|
||||
{
|
||||
to: 'cm/collectionType/test',
|
||||
search: 'plugins[plugin][test]=test&plugins[i18n][locale]=en',
|
||||
},
|
||||
],
|
||||
models: [{ uid: 'test', pluginOptions: { i18n: { localized: true } } }],
|
||||
};
|
||||
|
||||
expect(results).toEqual(expected);
|
||||
});
|
||||
});
|
||||
@ -0,0 +1,113 @@
|
||||
import { fixtures } from '../../../../../../admin-test-utils/lib';
|
||||
import addLocaleToSingleTypesLinks from '../addLocaleToSingleTypesLinks';
|
||||
|
||||
describe('i18n | contentManagerHooks | addLocaleToSingleTypesLinks', () => {
|
||||
let store;
|
||||
|
||||
beforeEach(() => {
|
||||
store = {
|
||||
...fixtures.store.state,
|
||||
i18n_locales: { locales: [] },
|
||||
};
|
||||
store.rbacProvider.allPermissions = [];
|
||||
|
||||
store.rbacProvider.collectionTypesRelatedPermissions = {
|
||||
test: {
|
||||
'plugins::content-manager.explorer.read': [],
|
||||
'plugins::content-manager.explorer.create': [],
|
||||
},
|
||||
};
|
||||
|
||||
store.getState = function() {
|
||||
return this;
|
||||
};
|
||||
});
|
||||
|
||||
it('should forward when the stLinks array is empty', () => {
|
||||
const data = {
|
||||
stLinks: [],
|
||||
models: [],
|
||||
};
|
||||
|
||||
const results = addLocaleToSingleTypesLinks(data, store);
|
||||
|
||||
expect(results).toEqual(data);
|
||||
});
|
||||
|
||||
it('should not add the search key to a single type link when i18n is not enabled on the single type', () => {
|
||||
const data = {
|
||||
stLinks: [{ to: 'cm/singleType/test' }],
|
||||
models: [{ uid: 'test', pluginOptions: { i18n: { localized: false } } }],
|
||||
};
|
||||
|
||||
const results = addLocaleToSingleTypesLinks(data, store);
|
||||
|
||||
expect(results).toEqual(data);
|
||||
});
|
||||
|
||||
it('should add a search key with the default locale when the user has the right to read it', () => {
|
||||
store.i18n_locales.locales = [{ code: 'en', isDefault: true }];
|
||||
store.rbacProvider.collectionTypesRelatedPermissions.test[
|
||||
'plugins::content-manager.explorer.read'
|
||||
] = [{ properties: { locales: ['en'] } }];
|
||||
|
||||
const data = {
|
||||
stLinks: [{ to: 'cm/singleType/test' }],
|
||||
models: [{ uid: 'test', pluginOptions: { i18n: { localized: true } } }],
|
||||
};
|
||||
|
||||
const results = addLocaleToSingleTypesLinks(data, store);
|
||||
|
||||
const expected = {
|
||||
stLinks: [{ to: 'cm/singleType/test', search: 'plugins[i18n][locale]=en' }],
|
||||
models: [{ uid: 'test', pluginOptions: { i18n: { localized: true } } }],
|
||||
};
|
||||
|
||||
expect(results).toEqual(expected);
|
||||
});
|
||||
|
||||
it('should set the isDisplayed key to false when the user does not have the right to read any locale', () => {
|
||||
store.i18n_locales.locales = [{ code: 'en', isDefault: true }];
|
||||
store.rbacProvider.collectionTypesRelatedPermissions.test[
|
||||
'plugins::content-manager.explorer.read'
|
||||
] = [{ properties: { locales: [] } }];
|
||||
|
||||
const data = {
|
||||
stLinks: [{ to: 'cm/singleType/test' }],
|
||||
models: [{ uid: 'test', pluginOptions: { i18n: { localized: true } } }],
|
||||
};
|
||||
const results = addLocaleToSingleTypesLinks(data, store);
|
||||
|
||||
const expected = {
|
||||
stLinks: [{ to: 'cm/singleType/test', isDisplayed: false }],
|
||||
models: [{ uid: 'test', pluginOptions: { i18n: { localized: true } } }],
|
||||
};
|
||||
|
||||
expect(results).toEqual(expected);
|
||||
});
|
||||
|
||||
it('should keep the previous search', () => {
|
||||
store.i18n_locales.locales = [{ code: 'en', isDefault: true }];
|
||||
store.rbacProvider.collectionTypesRelatedPermissions.test[
|
||||
'plugins::content-manager.explorer.read'
|
||||
] = [{ properties: { locales: ['en'] } }];
|
||||
|
||||
const data = {
|
||||
stLinks: [{ to: 'cm/singleType/test', search: 'plugins[plugin][test]=test' }],
|
||||
models: [{ uid: 'test', pluginOptions: { i18n: { localized: true } } }],
|
||||
};
|
||||
const results = addLocaleToSingleTypesLinks(data, store);
|
||||
|
||||
const expected = {
|
||||
stLinks: [
|
||||
{
|
||||
to: 'cm/singleType/test',
|
||||
search: 'plugins[plugin][test]=test&plugins[i18n][locale]=en',
|
||||
},
|
||||
],
|
||||
models: [{ uid: 'test', pluginOptions: { i18n: { localized: true } } }],
|
||||
};
|
||||
|
||||
expect(results).toEqual(expected);
|
||||
});
|
||||
});
|
||||
@ -1,6 +1,6 @@
|
||||
import addLocaleToLinksSearch from '../addLocaleToLinksSearch';
|
||||
|
||||
describe('i18n | middlewares | utils | addLocaleToLinksSearch', () => {
|
||||
describe('i18n | contentManagerHooks | utils | addLocaleToLinksSearch', () => {
|
||||
it('should return an array', () => {
|
||||
expect(addLocaleToLinksSearch([])).toEqual([]);
|
||||
});
|
||||
@ -16,6 +16,8 @@ import mutateCTBContentTypeSchema from './utils/mutateCTBContentTypeSchema';
|
||||
import LOCALIZED_FIELDS from './utils/localizedFields';
|
||||
import i18nReducers from './hooks/reducers';
|
||||
import DeleteModalAdditionalInfos from './components/DeleteModalAdditionalInfos';
|
||||
import addLocaleToCollectionTypesLinksHook from './contentManagerHooks/addLocaleToCollectionTypesLinks';
|
||||
import addLocaleToSingleTypesLinksHook from './contentManagerHooks/addLocaleToSingleTypesLinks';
|
||||
import addColumnToTableHook from './contentManagerHooks/addColumnToTable';
|
||||
|
||||
const pluginDescription = pluginPkg.strapi.description || pluginPkg.description;
|
||||
@ -40,6 +42,10 @@ export default {
|
||||
});
|
||||
},
|
||||
boot(app) {
|
||||
// Hooks that mutate the collection types links in order to add the locale filter
|
||||
app.registerHook('cm/mutate-collection-type-links', addLocaleToCollectionTypesLinksHook);
|
||||
app.registerHook('cm/mutate-single-type-links', addLocaleToSingleTypesLinksHook);
|
||||
// Hook that adds a column into the CM's LV table
|
||||
app.registerHook('cm/inject-column-in-table', addColumnToTableHook);
|
||||
// Add the settings link
|
||||
app.addSettingsLink('global', {
|
||||
|
||||
@ -1,25 +0,0 @@
|
||||
import addLocaleToLinksSearch from './utils/addLocaleToLinksSearch';
|
||||
|
||||
const addLocaleToCollectionTypesMiddleware = () => ({ getState }) => next => action => {
|
||||
if (action.type !== 'ContentManager/App/SET_CONTENT_TYPE_LINKS') {
|
||||
return next(action);
|
||||
}
|
||||
|
||||
if (action.data.authorizedCtLinks.length) {
|
||||
const store = getState();
|
||||
const { locales } = store.i18n_locales;
|
||||
const { collectionTypesRelatedPermissions } = store.rbacProvider;
|
||||
|
||||
action.data.authorizedCtLinks = addLocaleToLinksSearch(
|
||||
action.data.authorizedCtLinks,
|
||||
'collectionType',
|
||||
action.data.contentTypeSchemas,
|
||||
locales,
|
||||
collectionTypesRelatedPermissions
|
||||
);
|
||||
}
|
||||
|
||||
return next(action);
|
||||
};
|
||||
|
||||
export default addLocaleToCollectionTypesMiddleware;
|
||||
@ -1,25 +0,0 @@
|
||||
import addLocaleToLinksSearch from './utils/addLocaleToLinksSearch';
|
||||
|
||||
const addLocaleToSingleTypesMiddleware = () => ({ getState }) => next => action => {
|
||||
if (action.type !== 'ContentManager/App/SET_CONTENT_TYPE_LINKS') {
|
||||
return next(action);
|
||||
}
|
||||
|
||||
if (action.data.authorizedStLinks.length) {
|
||||
const store = getState();
|
||||
const { locales } = store.i18n_locales;
|
||||
const { collectionTypesRelatedPermissions } = store.rbacProvider;
|
||||
|
||||
action.data.authorizedStLinks = addLocaleToLinksSearch(
|
||||
action.data.authorizedStLinks,
|
||||
'singleType',
|
||||
action.data.contentTypeSchemas,
|
||||
locales,
|
||||
collectionTypesRelatedPermissions
|
||||
);
|
||||
}
|
||||
|
||||
return next(action);
|
||||
};
|
||||
|
||||
export default addLocaleToSingleTypesMiddleware;
|
||||
@ -1,6 +1,4 @@
|
||||
import addCommonFieldsToInitialDataMiddleware from './addCommonFieldsToInitialDataMiddleware';
|
||||
import addLocaleToCollectionTypesMiddleware from './addLocaleToCollectionTypesMiddleware';
|
||||
import addLocaleToSingleTypesMiddleware from './addLocaleToSingleTypesMiddleware';
|
||||
import extendCMEditViewLayoutMiddleware from './extendCMEditViewLayoutMiddleware';
|
||||
import extendCTBInitialDataMiddleware from './extendCTBInitialDataMiddleware';
|
||||
import extendCTBAttributeInitialDataMiddleware from './extendCTBAttributeInitialDataMiddleware';
|
||||
@ -8,8 +6,6 @@ import localePermissionMiddleware from './localePermissionMiddleware';
|
||||
|
||||
const middlewares = [
|
||||
addCommonFieldsToInitialDataMiddleware,
|
||||
addLocaleToCollectionTypesMiddleware,
|
||||
addLocaleToSingleTypesMiddleware,
|
||||
extendCMEditViewLayoutMiddleware,
|
||||
extendCTBInitialDataMiddleware,
|
||||
extendCTBAttributeInitialDataMiddleware,
|
||||
|
||||
@ -1,180 +0,0 @@
|
||||
import { fixtures } from '../../../../../../admin-test-utils';
|
||||
import addLocaleToCollectionTypesMiddleware from '../addLocaleToCollectionTypesMiddleware';
|
||||
|
||||
describe('i18n | middlewares | addLocaleToCollectionTypesMiddleware', () => {
|
||||
let getState;
|
||||
let store;
|
||||
|
||||
beforeEach(() => {
|
||||
store = {
|
||||
...fixtures.store.state,
|
||||
i18n_locales: { locales: [] },
|
||||
};
|
||||
store.rbacProvider.allPermissions = [];
|
||||
|
||||
store.rbacProvider.collectionTypesRelatedPermissions = {
|
||||
test: {
|
||||
'plugins::content-manager.explorer.read': [],
|
||||
'plugins::content-manager.explorer.create': [],
|
||||
},
|
||||
};
|
||||
|
||||
getState = jest.fn(() => store);
|
||||
});
|
||||
|
||||
it('should forward the action when the type is undefined', () => {
|
||||
const action = { test: true, type: undefined };
|
||||
|
||||
const next = jest.fn();
|
||||
const middleware = addLocaleToCollectionTypesMiddleware()({ getState });
|
||||
|
||||
middleware(next)(action);
|
||||
|
||||
expect(next).toBeCalledWith(action);
|
||||
});
|
||||
|
||||
it('should forward the action when the type is not ContentManager/App/SET_CONTENT_TYPE_LINKS', () => {
|
||||
const action = { test: true, type: 'TEST' };
|
||||
|
||||
const next = jest.fn();
|
||||
|
||||
const middleware = addLocaleToCollectionTypesMiddleware()({ getState });
|
||||
middleware(next)(action);
|
||||
|
||||
expect(next).toBeCalledWith(action);
|
||||
});
|
||||
|
||||
it('should forward when the authorizedStLinks array is empty', () => {
|
||||
const action = {
|
||||
type: 'ContentManager/App/SET_CONTENT_TYPE_LINKS',
|
||||
data: {
|
||||
authorizedCtLinks: [],
|
||||
},
|
||||
};
|
||||
const middleware = addLocaleToCollectionTypesMiddleware()({ getState });
|
||||
|
||||
const next = jest.fn();
|
||||
|
||||
middleware(next)(action);
|
||||
|
||||
expect(next).toBeCalledWith(action);
|
||||
});
|
||||
|
||||
it('should not add the search key to a single type link when i18n is not enabled on the single type', () => {
|
||||
const action = {
|
||||
type: 'ContentManager/App/SET_CONTENT_TYPE_LINKS',
|
||||
data: {
|
||||
authorizedCtLinks: [{ to: 'cm/collectionType/test' }],
|
||||
contentTypeSchemas: [{ uid: 'test', pluginOptions: { i18n: { localized: false } } }],
|
||||
},
|
||||
};
|
||||
const middleware = addLocaleToCollectionTypesMiddleware()({ getState });
|
||||
|
||||
const next = jest.fn();
|
||||
|
||||
middleware(next)(action);
|
||||
|
||||
expect(next).toBeCalledWith(action);
|
||||
});
|
||||
|
||||
it('should add a search key with the default locale when the user has the right to read it', () => {
|
||||
store.i18n_locales = { locales: [{ code: 'en', isDefault: true }] };
|
||||
store.rbacProvider.collectionTypesRelatedPermissions.test[
|
||||
'plugins::content-manager.explorer.read'
|
||||
] = [{ properties: { locales: ['en'] } }];
|
||||
|
||||
const action = {
|
||||
type: 'ContentManager/App/SET_CONTENT_TYPE_LINKS',
|
||||
data: {
|
||||
authorizedCtLinks: [{ to: 'cm/collectionType/test', search: null }],
|
||||
contentTypeSchemas: [{ uid: 'test', pluginOptions: { i18n: { localized: true } } }],
|
||||
},
|
||||
};
|
||||
const middleware = addLocaleToCollectionTypesMiddleware()({ getState });
|
||||
|
||||
const next = jest.fn();
|
||||
|
||||
middleware(next)(action);
|
||||
|
||||
const expected = {
|
||||
type: 'ContentManager/App/SET_CONTENT_TYPE_LINKS',
|
||||
data: {
|
||||
authorizedCtLinks: [{ to: 'cm/collectionType/test', search: 'plugins[i18n][locale]=en' }],
|
||||
contentTypeSchemas: [{ uid: 'test', pluginOptions: { i18n: { localized: true } } }],
|
||||
},
|
||||
};
|
||||
|
||||
expect(next).toBeCalledWith(expected);
|
||||
});
|
||||
|
||||
it('should set the isDisplayed key to false when the user does not have the right to read any locale', () => {
|
||||
store.i18n_locales.locales = [{ code: 'en', isDefault: true }];
|
||||
store.rbacProvider.collectionTypesRelatedPermissions.test[
|
||||
'plugins::content-manager.explorer.read'
|
||||
] = [{ properties: { locales: [] } }];
|
||||
|
||||
const action = {
|
||||
type: 'ContentManager/App/SET_CONTENT_TYPE_LINKS',
|
||||
data: {
|
||||
authorizedCtLinks: [{ to: 'cm/collectionType/test', search: 'page=1&pageSize=10' }],
|
||||
contentTypeSchemas: [{ uid: 'test', pluginOptions: { i18n: { localized: true } } }],
|
||||
},
|
||||
};
|
||||
const middleware = addLocaleToCollectionTypesMiddleware()({ getState });
|
||||
|
||||
const next = jest.fn();
|
||||
|
||||
middleware(next)(action);
|
||||
|
||||
const expected = {
|
||||
type: 'ContentManager/App/SET_CONTENT_TYPE_LINKS',
|
||||
data: {
|
||||
authorizedCtLinks: [
|
||||
{
|
||||
to: 'cm/collectionType/test',
|
||||
isDisplayed: false,
|
||||
search: 'page=1&pageSize=10',
|
||||
},
|
||||
],
|
||||
contentTypeSchemas: [{ uid: 'test', pluginOptions: { i18n: { localized: true } } }],
|
||||
},
|
||||
};
|
||||
|
||||
expect(next).toBeCalledWith(expected);
|
||||
});
|
||||
|
||||
it('should keep the previous search', () => {
|
||||
store.i18n_locales.locales = [{ code: 'en', isDefault: true }];
|
||||
store.rbacProvider.collectionTypesRelatedPermissions.test[
|
||||
'plugins::content-manager.explorer.read'
|
||||
] = [{ properties: { locales: ['en'] } }];
|
||||
|
||||
const action = {
|
||||
type: 'ContentManager/App/SET_CONTENT_TYPE_LINKS',
|
||||
data: {
|
||||
authorizedCtLinks: [{ to: 'cm/collectionType/test', search: 'plugins[plugin][test]=test' }],
|
||||
contentTypeSchemas: [{ uid: 'test', pluginOptions: { i18n: { localized: true } } }],
|
||||
},
|
||||
};
|
||||
const middleware = addLocaleToCollectionTypesMiddleware()({ getState });
|
||||
|
||||
const next = jest.fn();
|
||||
|
||||
middleware(next)(action);
|
||||
|
||||
const expected = {
|
||||
type: 'ContentManager/App/SET_CONTENT_TYPE_LINKS',
|
||||
data: {
|
||||
authorizedCtLinks: [
|
||||
{
|
||||
to: 'cm/collectionType/test',
|
||||
search: 'plugins[plugin][test]=test&plugins[i18n][locale]=en',
|
||||
},
|
||||
],
|
||||
contentTypeSchemas: [{ uid: 'test', pluginOptions: { i18n: { localized: true } } }],
|
||||
},
|
||||
};
|
||||
|
||||
expect(next).toBeCalledWith(expected);
|
||||
});
|
||||
});
|
||||
@ -1,174 +0,0 @@
|
||||
import { fixtures } from '../../../../../../admin-test-utils';
|
||||
import addLocaleToSingleTypesMiddleware from '../addLocaleToSingleTypesMiddleware';
|
||||
|
||||
describe('i18n | middlewares | addLocaleToSingleTypesMiddleware', () => {
|
||||
let getState;
|
||||
let store;
|
||||
|
||||
beforeEach(() => {
|
||||
store = {
|
||||
...fixtures.store.state,
|
||||
i18n_locales: { locales: [] },
|
||||
};
|
||||
store.rbacProvider.allPermissions = [];
|
||||
|
||||
store.rbacProvider.collectionTypesRelatedPermissions = {
|
||||
test: {
|
||||
'plugins::content-manager.explorer.read': [],
|
||||
'plugins::content-manager.explorer.create': [],
|
||||
},
|
||||
};
|
||||
|
||||
getState = jest.fn(() => store);
|
||||
});
|
||||
|
||||
it('should forward the action when the type is undefined', () => {
|
||||
const action = { test: true, type: undefined };
|
||||
|
||||
const next = jest.fn();
|
||||
const middleware = addLocaleToSingleTypesMiddleware()({ getState });
|
||||
|
||||
middleware(next)(action);
|
||||
|
||||
expect(next).toBeCalledWith(action);
|
||||
});
|
||||
|
||||
it('should forward the action when the type is not ContentManager/App/SET_CONTENT_TYPE_LINKS', () => {
|
||||
const action = { test: true, type: 'TEST' };
|
||||
|
||||
const next = jest.fn();
|
||||
|
||||
const middleware = addLocaleToSingleTypesMiddleware()({ getState });
|
||||
middleware(next)(action);
|
||||
|
||||
expect(next).toBeCalledWith(action);
|
||||
});
|
||||
|
||||
it('should forward when the authorizedStLinks array is empty', () => {
|
||||
const action = {
|
||||
type: 'ContentManager/App/SET_CONTENT_TYPE_LINKS',
|
||||
data: {
|
||||
authorizedStLinks: [],
|
||||
},
|
||||
};
|
||||
const middleware = addLocaleToSingleTypesMiddleware()({ getState });
|
||||
|
||||
const next = jest.fn();
|
||||
|
||||
middleware(next)(action);
|
||||
|
||||
expect(next).toBeCalledWith(action);
|
||||
});
|
||||
|
||||
it('should not add the search key to a single type link when i18n is not enabled on the single type', () => {
|
||||
const action = {
|
||||
type: 'ContentManager/App/SET_CONTENT_TYPE_LINKS',
|
||||
data: {
|
||||
authorizedStLinks: [{ to: 'cm/singleType/test' }],
|
||||
contentTypeSchemas: [{ uid: 'test', pluginOptions: { i18n: { localized: false } } }],
|
||||
},
|
||||
};
|
||||
const middleware = addLocaleToSingleTypesMiddleware()({ getState });
|
||||
|
||||
const next = jest.fn();
|
||||
|
||||
middleware(next)(action);
|
||||
|
||||
expect(next).toBeCalledWith(action);
|
||||
});
|
||||
|
||||
it('should add a search key with the default locale when the user has the right to read it', () => {
|
||||
store.i18n_locales.locales = [{ code: 'en', isDefault: true }];
|
||||
store.rbacProvider.collectionTypesRelatedPermissions.test[
|
||||
'plugins::content-manager.explorer.read'
|
||||
] = [{ properties: { locales: ['en'] } }];
|
||||
|
||||
const action = {
|
||||
type: 'ContentManager/App/SET_CONTENT_TYPE_LINKS',
|
||||
data: {
|
||||
authorizedStLinks: [{ to: 'cm/singleType/test' }],
|
||||
contentTypeSchemas: [{ uid: 'test', pluginOptions: { i18n: { localized: true } } }],
|
||||
},
|
||||
};
|
||||
const middleware = addLocaleToSingleTypesMiddleware()({ getState });
|
||||
|
||||
const next = jest.fn();
|
||||
|
||||
middleware(next)(action);
|
||||
|
||||
const expected = {
|
||||
type: 'ContentManager/App/SET_CONTENT_TYPE_LINKS',
|
||||
data: {
|
||||
authorizedStLinks: [{ to: 'cm/singleType/test', search: 'plugins[i18n][locale]=en' }],
|
||||
contentTypeSchemas: [{ uid: 'test', pluginOptions: { i18n: { localized: true } } }],
|
||||
},
|
||||
};
|
||||
|
||||
expect(next).toBeCalledWith(expected);
|
||||
});
|
||||
|
||||
it('should set the isDisplayed key to false when the user does not have the right to read any locale', () => {
|
||||
store.i18n_locales.locales = [{ code: 'en', isDefault: true }];
|
||||
store.rbacProvider.collectionTypesRelatedPermissions.test[
|
||||
'plugins::content-manager.explorer.read'
|
||||
] = [{ properties: { locales: [] } }];
|
||||
|
||||
const action = {
|
||||
type: 'ContentManager/App/SET_CONTENT_TYPE_LINKS',
|
||||
data: {
|
||||
authorizedStLinks: [{ to: 'cm/singleType/test' }],
|
||||
contentTypeSchemas: [{ uid: 'test', pluginOptions: { i18n: { localized: true } } }],
|
||||
},
|
||||
};
|
||||
const middleware = addLocaleToSingleTypesMiddleware()({ getState });
|
||||
|
||||
const next = jest.fn();
|
||||
|
||||
middleware(next)(action);
|
||||
|
||||
const expected = {
|
||||
type: 'ContentManager/App/SET_CONTENT_TYPE_LINKS',
|
||||
data: {
|
||||
authorizedStLinks: [{ to: 'cm/singleType/test', isDisplayed: false }],
|
||||
contentTypeSchemas: [{ uid: 'test', pluginOptions: { i18n: { localized: true } } }],
|
||||
},
|
||||
};
|
||||
|
||||
expect(next).toBeCalledWith(expected);
|
||||
});
|
||||
|
||||
it('should keep the previous search', () => {
|
||||
store.i18n_locales.locales = [{ code: 'en', isDefault: true }];
|
||||
store.rbacProvider.collectionTypesRelatedPermissions.test[
|
||||
'plugins::content-manager.explorer.read'
|
||||
] = [{ properties: { locales: ['en'] } }];
|
||||
|
||||
const action = {
|
||||
type: 'ContentManager/App/SET_CONTENT_TYPE_LINKS',
|
||||
data: {
|
||||
authorizedStLinks: [{ to: 'cm/singleType/test', search: 'plugins[plugin][test]=test' }],
|
||||
contentTypeSchemas: [{ uid: 'test', pluginOptions: { i18n: { localized: true } } }],
|
||||
},
|
||||
};
|
||||
const middleware = addLocaleToSingleTypesMiddleware()({ getState });
|
||||
|
||||
const next = jest.fn();
|
||||
|
||||
middleware(next)(action);
|
||||
|
||||
const expected = {
|
||||
type: 'ContentManager/App/SET_CONTENT_TYPE_LINKS',
|
||||
data: {
|
||||
authorizedStLinks: [
|
||||
{
|
||||
to: 'cm/singleType/test',
|
||||
search: 'plugins[plugin][test]=test&plugins[i18n][locale]=en',
|
||||
},
|
||||
],
|
||||
contentTypeSchemas: [{ uid: 'test', pluginOptions: { i18n: { localized: true } } }],
|
||||
},
|
||||
};
|
||||
|
||||
expect(next).toBeCalledWith(expected);
|
||||
});
|
||||
});
|
||||
Loading…
x
Reference in New Issue
Block a user