diff --git a/packages/core/content-manager/admin/src/index.js b/packages/core/content-manager/admin/src/index.js index 51be4181ff..b76d52dad2 100644 --- a/packages/core/content-manager/admin/src/index.js +++ b/packages/core/content-manager/admin/src/index.js @@ -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, diff --git a/packages/core/content-manager/admin/src/pages/App/useModels.js b/packages/core/content-manager/admin/src/pages/App/useModels.js index 1cee8f2432..0f6e5cf052 100644 --- a/packages/core/content-manager/admin/src/pages/App/useModels.js +++ b/packages/core/content-manager/admin/src/pages/App/useModels.js @@ -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) { diff --git a/packages/plugins/i18n/admin/src/contentManagerHooks/addLocaleToCollectionTypesLinks.js b/packages/plugins/i18n/admin/src/contentManagerHooks/addLocaleToCollectionTypesLinks.js new file mode 100644 index 0000000000..0b38c443a5 --- /dev/null +++ b/packages/plugins/i18n/admin/src/contentManagerHooks/addLocaleToCollectionTypesLinks.js @@ -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; diff --git a/packages/plugins/i18n/admin/src/contentManagerHooks/addLocaleToSingleTypesLinks.js b/packages/plugins/i18n/admin/src/contentManagerHooks/addLocaleToSingleTypesLinks.js new file mode 100644 index 0000000000..47d5e3a4a6 --- /dev/null +++ b/packages/plugins/i18n/admin/src/contentManagerHooks/addLocaleToSingleTypesLinks.js @@ -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; diff --git a/packages/plugins/i18n/admin/src/contentManagerHooks/tests/addLocaleToCollectionTypesLinks.test.js b/packages/plugins/i18n/admin/src/contentManagerHooks/tests/addLocaleToCollectionTypesLinks.test.js new file mode 100644 index 0000000000..5a5f898e91 --- /dev/null +++ b/packages/plugins/i18n/admin/src/contentManagerHooks/tests/addLocaleToCollectionTypesLinks.test.js @@ -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); + }); +}); diff --git a/packages/plugins/i18n/admin/src/contentManagerHooks/tests/addLocaleToSingleTypesLinks.test.js b/packages/plugins/i18n/admin/src/contentManagerHooks/tests/addLocaleToSingleTypesLinks.test.js new file mode 100644 index 0000000000..2ef035133c --- /dev/null +++ b/packages/plugins/i18n/admin/src/contentManagerHooks/tests/addLocaleToSingleTypesLinks.test.js @@ -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); + }); +}); diff --git a/packages/plugins/i18n/admin/src/middlewares/utils/addLocaleToLinksSearch.js b/packages/plugins/i18n/admin/src/contentManagerHooks/utils/addLocaleToLinksSearch.js similarity index 100% rename from packages/plugins/i18n/admin/src/middlewares/utils/addLocaleToLinksSearch.js rename to packages/plugins/i18n/admin/src/contentManagerHooks/utils/addLocaleToLinksSearch.js diff --git a/packages/plugins/i18n/admin/src/middlewares/utils/tests/addLocaleToLinksSearch.test.js b/packages/plugins/i18n/admin/src/contentManagerHooks/utils/tests/addLocaleToLinksSearch.test.js similarity index 97% rename from packages/plugins/i18n/admin/src/middlewares/utils/tests/addLocaleToLinksSearch.test.js rename to packages/plugins/i18n/admin/src/contentManagerHooks/utils/tests/addLocaleToLinksSearch.test.js index f5fd22411e..d6b89dd9f8 100644 --- a/packages/plugins/i18n/admin/src/middlewares/utils/tests/addLocaleToLinksSearch.test.js +++ b/packages/plugins/i18n/admin/src/contentManagerHooks/utils/tests/addLocaleToLinksSearch.test.js @@ -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([]); }); diff --git a/packages/plugins/i18n/admin/src/index.js b/packages/plugins/i18n/admin/src/index.js index 7f6d82511b..3889033e43 100644 --- a/packages/plugins/i18n/admin/src/index.js +++ b/packages/plugins/i18n/admin/src/index.js @@ -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', { diff --git a/packages/plugins/i18n/admin/src/middlewares/addLocaleToCollectionTypesMiddleware.js b/packages/plugins/i18n/admin/src/middlewares/addLocaleToCollectionTypesMiddleware.js deleted file mode 100644 index 1125295806..0000000000 --- a/packages/plugins/i18n/admin/src/middlewares/addLocaleToCollectionTypesMiddleware.js +++ /dev/null @@ -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; diff --git a/packages/plugins/i18n/admin/src/middlewares/addLocaleToSingleTypesMiddleware.js b/packages/plugins/i18n/admin/src/middlewares/addLocaleToSingleTypesMiddleware.js deleted file mode 100644 index 2abf49486c..0000000000 --- a/packages/plugins/i18n/admin/src/middlewares/addLocaleToSingleTypesMiddleware.js +++ /dev/null @@ -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; diff --git a/packages/plugins/i18n/admin/src/middlewares/index.js b/packages/plugins/i18n/admin/src/middlewares/index.js index b465e9331b..ca3a0220e7 100644 --- a/packages/plugins/i18n/admin/src/middlewares/index.js +++ b/packages/plugins/i18n/admin/src/middlewares/index.js @@ -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, diff --git a/packages/plugins/i18n/admin/src/middlewares/tests/addLocaleToCollectionTypesMiddleware.test.js b/packages/plugins/i18n/admin/src/middlewares/tests/addLocaleToCollectionTypesMiddleware.test.js deleted file mode 100644 index a8dfcd8ab8..0000000000 --- a/packages/plugins/i18n/admin/src/middlewares/tests/addLocaleToCollectionTypesMiddleware.test.js +++ /dev/null @@ -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); - }); -}); diff --git a/packages/plugins/i18n/admin/src/middlewares/tests/addLocaleToSingleTypesMiddleware.test.js b/packages/plugins/i18n/admin/src/middlewares/tests/addLocaleToSingleTypesMiddleware.test.js deleted file mode 100644 index 891f1eaff1..0000000000 --- a/packages/plugins/i18n/admin/src/middlewares/tests/addLocaleToSingleTypesMiddleware.test.js +++ /dev/null @@ -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); - }); -});