@akemona-org/strapi-plugin-i18n 3.7.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (147) hide show
  1. package/LICENSE +22 -0
  2. package/README.md +19 -0
  3. package/admin/src/assets/images/logo.svg +1 -0
  4. package/admin/src/components/CMEditViewCopyLocale/index.js +183 -0
  5. package/admin/src/components/CMEditViewCopyLocale/utils/cleanData.js +36 -0
  6. package/admin/src/components/CMEditViewCopyLocale/utils/generateOptions.js +22 -0
  7. package/admin/src/components/CMEditViewCopyLocale/utils/index.js +2 -0
  8. package/admin/src/components/CMEditViewCopyLocale/utils/removePasswordAndRelationsFieldFromData.js +54 -0
  9. package/admin/src/components/CMEditViewCopyLocale/utils/tests/cleanData.test.js +83 -0
  10. package/admin/src/components/CMEditViewCopyLocale/utils/tests/data.js +219 -0
  11. package/admin/src/components/CMEditViewCopyLocale/utils/tests/generateOptions.test.js +79 -0
  12. package/admin/src/components/CMEditViewCopyLocale/utils/tests/removePasswordAndRelationsFieldFromData.test.js +40 -0
  13. package/admin/src/components/CMEditViewInjectedComponents/index.js +58 -0
  14. package/admin/src/components/CMEditViewLocalePicker/Option.js +66 -0
  15. package/admin/src/components/CMEditViewLocalePicker/Wrapper.js +8 -0
  16. package/admin/src/components/CMEditViewLocalePicker/index.js +160 -0
  17. package/admin/src/components/CMEditViewLocalePicker/utils/addStatusColorToLocale.js +24 -0
  18. package/admin/src/components/CMEditViewLocalePicker/utils/createLocalesOption.js +20 -0
  19. package/admin/src/components/CMEditViewLocalePicker/utils/index.js +2 -0
  20. package/admin/src/components/CheckboxConfirmation/Wrapper.js +12 -0
  21. package/admin/src/components/CheckboxConfirmation/index.js +70 -0
  22. package/admin/src/components/DeleteModalAdditionalInfos/index.js +25 -0
  23. package/admin/src/components/LocaleList/index.js +101 -0
  24. package/admin/src/components/LocaleListCell/LocaleListCell.js +90 -0
  25. package/admin/src/components/LocaleListCell/tests/LocaleListCell.test.js +128 -0
  26. package/admin/src/components/LocalePicker/index.js +126 -0
  27. package/admin/src/components/LocaleRow/index.js +77 -0
  28. package/admin/src/components/ModalCreate/AdvancedForm.js +45 -0
  29. package/admin/src/components/ModalCreate/BaseForm.js +103 -0
  30. package/admin/src/components/ModalCreate/index.js +136 -0
  31. package/admin/src/components/ModalDelete/index.js +49 -0
  32. package/admin/src/components/ModalEdit/AdvancedForm.js +51 -0
  33. package/admin/src/components/ModalEdit/BaseForm.js +91 -0
  34. package/admin/src/components/ModalEdit/index.js +122 -0
  35. package/admin/src/components/SettingsModal.js +66 -0
  36. package/admin/src/components/index.js +2 -0
  37. package/admin/src/containers/Initializer.js +31 -0
  38. package/admin/src/containers/SettingsPage/LocaleSettingsPage.js +69 -0
  39. package/admin/src/containers/SettingsPage/index.js +33 -0
  40. package/admin/src/containers/SettingsPage/tests/SettingsPage.test.js +744 -0
  41. package/admin/src/containers/SettingsPage/tests/__snapshots__/SettingsPage.test.js.snap +241 -0
  42. package/admin/src/hooks/constants.js +6 -0
  43. package/admin/src/hooks/reducers.js +63 -0
  44. package/admin/src/hooks/tests/reducers.test.js +203 -0
  45. package/admin/src/hooks/useAddLocale/index.js +60 -0
  46. package/admin/src/hooks/useContentTypePermissions/index.js +16 -0
  47. package/admin/src/hooks/useDefaultLocales/index.js +27 -0
  48. package/admin/src/hooks/useDeleteLocale/index.js +45 -0
  49. package/admin/src/hooks/useEditLocale/index.js +46 -0
  50. package/admin/src/hooks/useHasI18n/index.js +13 -0
  51. package/admin/src/hooks/useLocales/index.js +35 -0
  52. package/admin/src/index.js +169 -0
  53. package/admin/src/middlewares/addCommonFieldsToInitialDataMiddleware.js +83 -0
  54. package/admin/src/middlewares/addLocaleColumnToListViewMiddleware.js +32 -0
  55. package/admin/src/middlewares/addLocaleToCollectionTypesMiddleware.js +25 -0
  56. package/admin/src/middlewares/addLocaleToSingleTypesMiddleware.js +25 -0
  57. package/admin/src/middlewares/extendCMEditViewLayoutMiddleware.js +159 -0
  58. package/admin/src/middlewares/extendCTBAttributeInitialDataMiddleware.js +58 -0
  59. package/admin/src/middlewares/extendCTBInitialDataMiddleware.js +33 -0
  60. package/admin/src/middlewares/index.js +21 -0
  61. package/admin/src/middlewares/localePermissionMiddleware.js +39 -0
  62. package/admin/src/middlewares/tests/addCommonFieldsToInitialDataMiddleware.test.js +97 -0
  63. package/admin/src/middlewares/tests/addLocaleColumnToListViewMiddleware.test.js +68 -0
  64. package/admin/src/middlewares/tests/addLocaleToCollectionTypesMiddleware.test.js +200 -0
  65. package/admin/src/middlewares/tests/addLocaleToSingleTypesMiddleware.test.js +193 -0
  66. package/admin/src/middlewares/tests/extendCMEditViewLayoutMiddleware.test.js +556 -0
  67. package/admin/src/middlewares/tests/extendCTBAttrributeInitialDataMiddleware.test.js +124 -0
  68. package/admin/src/middlewares/tests/extendCTBInitialDataMiddleware.test.js +92 -0
  69. package/admin/src/middlewares/tests/localePermissionMiddleware.test.js +150 -0
  70. package/admin/src/middlewares/utils/addLocaleToLinksSearch.js +56 -0
  71. package/admin/src/middlewares/utils/tests/addLocaleToLinksSearch.test.js +137 -0
  72. package/admin/src/permissions.js +9 -0
  73. package/admin/src/pluginId.js +5 -0
  74. package/admin/src/schemas.js +7 -0
  75. package/admin/src/selectors/selectCollectionTypesRelatedPermissions.js +4 -0
  76. package/admin/src/selectors/selectI18nLocales.js +3 -0
  77. package/admin/src/translations/en.json +60 -0
  78. package/admin/src/translations/fr.json +9 -0
  79. package/admin/src/translations/index.js +11 -0
  80. package/admin/src/translations/zh-Hans.json +60 -0
  81. package/admin/src/utils/getDefaultLocale.js +60 -0
  82. package/admin/src/utils/getInitialLocale.js +14 -0
  83. package/admin/src/utils/getLocaleFromQuery.js +7 -0
  84. package/admin/src/utils/getTrad.js +5 -0
  85. package/admin/src/utils/index.js +2 -0
  86. package/admin/src/utils/localizedFields.js +23 -0
  87. package/admin/src/utils/mutateCTBContentTypeSchema.js +66 -0
  88. package/admin/src/utils/tests/getDefaultLocale.test.js +337 -0
  89. package/admin/src/utils/tests/getInitialLocale.test.js +106 -0
  90. package/admin/src/utils/tests/mutateCTBContentTypeSchema.test.js +205 -0
  91. package/config/functions/bootstrap.js +57 -0
  92. package/config/functions/migrations/__tests__/content-type.test.js +255 -0
  93. package/config/functions/migrations/__tests__/field.test.js +150 -0
  94. package/config/functions/migrations/content-type/disable/index.js +34 -0
  95. package/config/functions/migrations/content-type/disable/migrate-for-bookshelf.js +58 -0
  96. package/config/functions/migrations/content-type/disable/migrate-for-mongoose.js +39 -0
  97. package/config/functions/migrations/content-type/enable/index.js +40 -0
  98. package/config/functions/migrations/content-type/utils/index.js +27 -0
  99. package/config/functions/migrations/field/__tests__/utils.test.js +53 -0
  100. package/config/functions/migrations/field/index.js +37 -0
  101. package/config/functions/migrations/field/migrate-for-bookshelf.js +72 -0
  102. package/config/functions/migrations/field/migrate-for-mongoose.js +24 -0
  103. package/config/functions/migrations/field/migrate.js +55 -0
  104. package/config/functions/migrations/field/utils.js +58 -0
  105. package/config/functions/register.js +46 -0
  106. package/config/policies/validateLocaleCreation.js +68 -0
  107. package/config/routes.json +64 -0
  108. package/constants/__tests__/index.test.js +27 -0
  109. package/constants/index.js +36 -0
  110. package/constants/iso-locales.json +2002 -0
  111. package/controllers/__tests__/content-types.test.js +113 -0
  112. package/controllers/__tests__/iso-locales.test.js +26 -0
  113. package/controllers/__tests__/locales.test.js +308 -0
  114. package/controllers/content-types.js +64 -0
  115. package/controllers/iso-locales.js +11 -0
  116. package/controllers/locales.js +104 -0
  117. package/domain/locale.js +10 -0
  118. package/middlewares/i18n/defaults.json +5 -0
  119. package/middlewares/i18n/index.js +28 -0
  120. package/models/Locale.settings.json +31 -0
  121. package/oas.yml +195 -0
  122. package/package.json +31 -0
  123. package/services/__tests__/__snapshots__/iso-locales.test.js.snap +2006 -0
  124. package/services/__tests__/content-types.test.js +545 -0
  125. package/services/__tests__/core-api.test.js +106 -0
  126. package/services/__tests__/entity-service-decorator.test.js +280 -0
  127. package/services/__tests__/iso-locales.test.js +11 -0
  128. package/services/__tests__/locales.test.js +237 -0
  129. package/services/__tests__/localizations.test.js +187 -0
  130. package/services/__tests__/metrics.test.js +90 -0
  131. package/services/content-types.js +200 -0
  132. package/services/core-api.js +296 -0
  133. package/services/entity-service-decorator.js +155 -0
  134. package/services/iso-locales.js +9 -0
  135. package/services/locales.js +97 -0
  136. package/services/localizations.js +65 -0
  137. package/services/metrics.js +24 -0
  138. package/services/permissions/actions.js +124 -0
  139. package/services/permissions/engine.js +63 -0
  140. package/services/permissions/sections-builder.js +48 -0
  141. package/services/permissions.js +11 -0
  142. package/tests/content-manager/list-relation.test.e2e.js +122 -0
  143. package/tests/graphql.test.e2e.js +120 -0
  144. package/tests/locales.test.e2e.js +414 -0
  145. package/utils/index.js +20 -0
  146. package/validation/content-types.js +30 -0
  147. package/validation/locales.js +39 -0
@@ -0,0 +1,39 @@
1
+ import get from 'lodash/get';
2
+
3
+ const filterPermissionWithLocale = locale => permission =>
4
+ get(permission, 'properties.locales', []).indexOf(locale) !== -1;
5
+
6
+ const localePermissionMiddleware = () => () => next => action => {
7
+ if (action.type !== 'ContentManager/RBACManager/SET_PERMISSIONS') {
8
+ return next(action);
9
+ }
10
+
11
+ const containerName = get(action, '__meta__.containerName', null);
12
+
13
+ if (!['editView', 'listView'].includes(containerName)) {
14
+ return next(action);
15
+ }
16
+
17
+ const locale = get(action, '__meta__.plugins.i18n.locale', null);
18
+
19
+ if (!locale) {
20
+ return next(action);
21
+ }
22
+
23
+ const permissions = action.permissions;
24
+
25
+ const nextPermissions = Object.keys(permissions).reduce((acc, key) => {
26
+ const currentPermission = permissions[key];
27
+ const filteredPermissions = currentPermission.filter(filterPermissionWithLocale(locale));
28
+
29
+ if (filteredPermissions.length) {
30
+ acc[key] = filteredPermissions;
31
+ }
32
+
33
+ return acc;
34
+ }, {});
35
+
36
+ return next({ ...action, permissions: nextPermissions });
37
+ };
38
+
39
+ export default localePermissionMiddleware;
@@ -0,0 +1,97 @@
1
+ import addCommonFieldsToInitialDataMiddleware from '../addCommonFieldsToInitialDataMiddleware';
2
+
3
+ jest.mock('strapi-helper-plugin', () => ({
4
+ request: () => ({
5
+ nonLocalizedFields: { common: 'test' },
6
+ localizations: ['test'],
7
+ }),
8
+ formatComponentData: data => data,
9
+ contentManagementUtilRemoveFieldsFromData: data => data,
10
+ }));
11
+
12
+ describe('i18n | middlewares | addCommonFieldsToInitialDataMiddleware', () => {
13
+ let getState;
14
+ const dispatch = jest.fn();
15
+
16
+ beforeEach(() => {
17
+ const store = new Map();
18
+
19
+ store.set('content-manager_editViewCrudReducer', {
20
+ contentTypeDataStructure: { name: 'test', common: 'common default value' },
21
+ });
22
+ store.set('content-manager_editViewLayoutManager', {
23
+ currentLayout: {
24
+ components: {},
25
+ contentType: {
26
+ uid: 'article',
27
+ attributes: {
28
+ name: { type: 'string' },
29
+ common: { type: 'string' },
30
+ },
31
+ },
32
+ },
33
+ });
34
+
35
+ getState = () => store;
36
+ });
37
+
38
+ it('should forward the action when the type is undefined', () => {
39
+ const action = { test: true, type: undefined };
40
+
41
+ const next = jest.fn();
42
+ const middleware = addCommonFieldsToInitialDataMiddleware()({ getState, dispatch });
43
+
44
+ middleware(next)(action);
45
+
46
+ expect(next).toBeCalledWith(action);
47
+ });
48
+
49
+ it('should forward the action when the rawQuery is empty', () => {
50
+ const action = { type: 'ContentManager/CrudReducer/INIT_FORM', rawQuery: '' };
51
+
52
+ const next = jest.fn();
53
+ const middleware = addCommonFieldsToInitialDataMiddleware()({ getState, dispatch });
54
+
55
+ middleware(next)(action);
56
+
57
+ expect(next).toBeCalledWith(action);
58
+ });
59
+
60
+ it('should forward the action when the relatedEntityId is not defined', () => {
61
+ const action = {
62
+ type: 'ContentManager/CrudReducer/INIT_FORM',
63
+ rawQuery: '?plugins[i18n][locale]=en',
64
+ };
65
+
66
+ const next = jest.fn();
67
+ const middleware = addCommonFieldsToInitialDataMiddleware()({ getState, dispatch });
68
+
69
+ middleware(next)(action);
70
+
71
+ expect(next).toBeCalledWith(action);
72
+ });
73
+
74
+ it('should makes a request to retrieve the common field when the relatedEntityId is defined', async () => {
75
+ const action = {
76
+ type: 'ContentManager/CrudReducer/INIT_FORM',
77
+ rawQuery: '?plugins[i18n][relatedEntityId]=1',
78
+ };
79
+
80
+ const next = jest.fn(x => x);
81
+ const middleware = addCommonFieldsToInitialDataMiddleware()({ getState, dispatch });
82
+
83
+ const nextAction = await middleware(next)(action);
84
+
85
+ expect(dispatch).toHaveBeenCalledWith({ type: 'ContentManager/CrudReducer/GET_DATA' });
86
+
87
+ expect(nextAction).toEqual({
88
+ type: 'ContentManager/CrudReducer/INIT_FORM',
89
+ rawQuery: '?plugins[i18n][relatedEntityId]=1',
90
+ data: {
91
+ name: 'test',
92
+ localizations: ['test'],
93
+ common: 'test',
94
+ },
95
+ });
96
+ });
97
+ });
@@ -0,0 +1,68 @@
1
+ import addLocaleColumnToListViewMiddleware from '../addLocaleColumnToListViewMiddleware';
2
+
3
+ describe('addLocaleColumnToListViewMiddleware', () => {
4
+ let getState;
5
+
6
+ beforeEach(() => {
7
+ const store = new Map();
8
+
9
+ store.set('i18n_locales', { locales: [] });
10
+
11
+ getState = () => store;
12
+ });
13
+
14
+ it('does nothing on unknown actions', () => {
15
+ const middleware = addLocaleColumnToListViewMiddleware()({ getState });
16
+ const nextFn = jest.fn();
17
+ const action = { type: 'UNKNOWN' };
18
+
19
+ middleware(nextFn)(action);
20
+
21
+ expect(nextFn).toBeCalledWith(action);
22
+ expect(action).toEqual({
23
+ type: 'UNKNOWN',
24
+ });
25
+ });
26
+
27
+ it('does nothing when there s no i18n.localized key in the action', () => {
28
+ const middleware = addLocaleColumnToListViewMiddleware()({ getState });
29
+ const nextFn = jest.fn();
30
+ const action = {
31
+ type: 'ContentManager/ListView/SET_LIST_LAYOUT ',
32
+ contentType: { pluginOptions: {} },
33
+ };
34
+
35
+ middleware(nextFn)(action);
36
+
37
+ expect(nextFn).toBeCalledWith(action);
38
+ expect(action).toEqual({
39
+ contentType: { pluginOptions: {} },
40
+ type: 'ContentManager/ListView/SET_LIST_LAYOUT ',
41
+ });
42
+ });
43
+
44
+ it('adds a header to the displayedHeaders array when the content type is localized', () => {
45
+ const middleware = addLocaleColumnToListViewMiddleware()({ getState });
46
+ const nextFn = jest.fn();
47
+ const action = {
48
+ type: 'ContentManager/ListView/SET_LIST_LAYOUT ',
49
+ displayedHeaders: [],
50
+ contentType: {
51
+ pluginOptions: {
52
+ i18n: { localized: true },
53
+ },
54
+ },
55
+ };
56
+
57
+ middleware(nextFn)(action);
58
+
59
+ expect(nextFn).toBeCalledWith(action);
60
+ // The anonymous function of cellFormatter creates problem, because it's anonymous
61
+ // In our scenario, it's even more tricky because we use a closure in order to pass
62
+ // the locales.
63
+ // Stringifying the action allows us to have a name inside the expectation for the "cellFormatter" key
64
+ expect(JSON.stringify(action)).toBe(
65
+ '{"type":"ContentManager/ListView/SET_LIST_LAYOUT ","displayedHeaders":[{"key":"__locale_key__","fieldSchema":{"type":"string"},"metadatas":{"label":"Content available in","searchable":false,"sortable":false},"name":"locales"}],"contentType":{"pluginOptions":{"i18n":{"localized":true}}}}'
66
+ );
67
+ });
68
+ });
@@ -0,0 +1,200 @@
1
+ import addLocaleToCollectionTypesMiddleware from '../addLocaleToCollectionTypesMiddleware';
2
+
3
+ describe('i18n | middlewares | addLocaleToCollectionTypesMiddleware', () => {
4
+ let getState;
5
+
6
+ beforeEach(() => {
7
+ const store = new Map();
8
+ store.set('i18n_locales', { locales: [] });
9
+ store.set('permissionsManager', { userPermissions: [] });
10
+ store.set('permissionsManager', {
11
+ collectionTypesRelatedPermissions: {
12
+ test: {
13
+ 'plugins::content-manager.explorer.read': [],
14
+ 'plugins::content-manager.explorer.create': [],
15
+ },
16
+ },
17
+ });
18
+
19
+ getState = () => store;
20
+ });
21
+
22
+ it('should forward the action when the type is undefined', () => {
23
+ const action = { test: true, type: undefined };
24
+
25
+ const next = jest.fn();
26
+ const middleware = addLocaleToCollectionTypesMiddleware()({ getState });
27
+
28
+ middleware(next)(action);
29
+
30
+ expect(next).toBeCalledWith(action);
31
+ });
32
+
33
+ it('should forward the action when the type is not StrapiAdmin/LeftMenu/SET_CT_OR_ST_LINKS', () => {
34
+ const action = { test: true, type: 'TEST' };
35
+
36
+ const next = jest.fn();
37
+
38
+ const middleware = addLocaleToCollectionTypesMiddleware()({ getState });
39
+ middleware(next)(action);
40
+
41
+ expect(next).toBeCalledWith(action);
42
+ });
43
+
44
+ it('should forward when the authorizedStLinks array is empty', () => {
45
+ const action = {
46
+ type: 'StrapiAdmin/LeftMenu/SET_CT_OR_ST_LINKS',
47
+ data: {
48
+ authorizedCtLinks: [],
49
+ },
50
+ };
51
+ const middleware = addLocaleToCollectionTypesMiddleware()({ getState });
52
+
53
+ const next = jest.fn();
54
+
55
+ middleware(next)(action);
56
+
57
+ expect(next).toBeCalledWith(action);
58
+ });
59
+
60
+ it('should not add the search key to a single type link when i18n is not enabled on the single type', () => {
61
+ const action = {
62
+ type: 'StrapiAdmin/LeftMenu/SET_CT_OR_ST_LINKS',
63
+ data: {
64
+ authorizedCtLinks: [{ destination: 'cm/collectionType/test' }],
65
+ contentTypeSchemas: [{ uid: 'test', pluginOptions: { i18n: { localized: false } } }],
66
+ },
67
+ };
68
+ const middleware = addLocaleToCollectionTypesMiddleware()({ getState });
69
+
70
+ const next = jest.fn();
71
+
72
+ middleware(next)(action);
73
+
74
+ expect(next).toBeCalledWith(action);
75
+ });
76
+
77
+ it('should add a search key with the default locale when the user has the right to read it', () => {
78
+ const tempStore = new Map();
79
+ tempStore.set('i18n_locales', { locales: [{ code: 'en', isDefault: true }] });
80
+ tempStore.set('permissionsManager', { userPermissions: [] });
81
+ tempStore.set('permissionsManager', {
82
+ collectionTypesRelatedPermissions: {
83
+ test: {
84
+ 'plugins::content-manager.explorer.read': [{ properties: { locales: ['en'] } }],
85
+ 'plugins::content-manager.explorer.create': [],
86
+ },
87
+ },
88
+ });
89
+ const action = {
90
+ type: 'StrapiAdmin/LeftMenu/SET_CT_OR_ST_LINKS',
91
+ data: {
92
+ authorizedCtLinks: [{ destination: 'cm/collectionType/test', search: null }],
93
+ contentTypeSchemas: [{ uid: 'test', pluginOptions: { i18n: { localized: true } } }],
94
+ },
95
+ };
96
+ const middleware = addLocaleToCollectionTypesMiddleware()({ getState: () => tempStore });
97
+
98
+ const next = jest.fn();
99
+
100
+ middleware(next)(action);
101
+
102
+ const expected = {
103
+ type: 'StrapiAdmin/LeftMenu/SET_CT_OR_ST_LINKS',
104
+ data: {
105
+ authorizedCtLinks: [
106
+ { destination: 'cm/collectionType/test', search: 'plugins[i18n][locale]=en' },
107
+ ],
108
+ contentTypeSchemas: [{ uid: 'test', pluginOptions: { i18n: { localized: true } } }],
109
+ },
110
+ };
111
+
112
+ expect(next).toBeCalledWith(expected);
113
+ });
114
+
115
+ it('should set the isDisplayed key to false when the user does not have the right to read any locale', () => {
116
+ const tempStore = new Map();
117
+ tempStore.set('i18n_locales', { locales: [{ code: 'en', isDefault: true }] });
118
+ tempStore.set('permissionsManager', { userPermissions: [] });
119
+ tempStore.set('permissionsManager', {
120
+ collectionTypesRelatedPermissions: {
121
+ test: {
122
+ 'plugins::content-manager.explorer.read': [{ properties: { locales: [] } }],
123
+ },
124
+ },
125
+ });
126
+ const action = {
127
+ type: 'StrapiAdmin/LeftMenu/SET_CT_OR_ST_LINKS',
128
+ data: {
129
+ authorizedCtLinks: [
130
+ { destination: 'cm/collectionType/test', search: 'page=1&pageSize=10' },
131
+ ],
132
+ contentTypeSchemas: [{ uid: 'test', pluginOptions: { i18n: { localized: true } } }],
133
+ },
134
+ };
135
+ const middleware = addLocaleToCollectionTypesMiddleware()({ getState: () => tempStore });
136
+
137
+ const next = jest.fn();
138
+
139
+ middleware(next)(action);
140
+
141
+ const expected = {
142
+ type: 'StrapiAdmin/LeftMenu/SET_CT_OR_ST_LINKS',
143
+ data: {
144
+ authorizedCtLinks: [
145
+ {
146
+ destination: 'cm/collectionType/test',
147
+ isDisplayed: false,
148
+ search: 'page=1&pageSize=10',
149
+ },
150
+ ],
151
+ contentTypeSchemas: [{ uid: 'test', pluginOptions: { i18n: { localized: true } } }],
152
+ },
153
+ };
154
+
155
+ expect(next).toBeCalledWith(expected);
156
+ });
157
+
158
+ it('should keep the previous search', () => {
159
+ const tempStore = new Map();
160
+ tempStore.set('i18n_locales', { locales: [{ code: 'en', isDefault: true }] });
161
+ tempStore.set('permissionsManager', { userPermissions: [] });
162
+ tempStore.set('permissionsManager', {
163
+ collectionTypesRelatedPermissions: {
164
+ test: {
165
+ 'plugins::content-manager.explorer.read': [{ properties: { locales: ['en'] } }],
166
+ 'plugins::content-manager.explorer.create': [],
167
+ },
168
+ },
169
+ });
170
+ const action = {
171
+ type: 'StrapiAdmin/LeftMenu/SET_CT_OR_ST_LINKS',
172
+ data: {
173
+ authorizedCtLinks: [
174
+ { destination: 'cm/collectionType/test', search: 'plugins[plugin][test]=test' },
175
+ ],
176
+ contentTypeSchemas: [{ uid: 'test', pluginOptions: { i18n: { localized: true } } }],
177
+ },
178
+ };
179
+ const middleware = addLocaleToCollectionTypesMiddleware()({ getState: () => tempStore });
180
+
181
+ const next = jest.fn();
182
+
183
+ middleware(next)(action);
184
+
185
+ const expected = {
186
+ type: 'StrapiAdmin/LeftMenu/SET_CT_OR_ST_LINKS',
187
+ data: {
188
+ authorizedCtLinks: [
189
+ {
190
+ destination: 'cm/collectionType/test',
191
+ search: 'plugins[plugin][test]=test&plugins[i18n][locale]=en',
192
+ },
193
+ ],
194
+ contentTypeSchemas: [{ uid: 'test', pluginOptions: { i18n: { localized: true } } }],
195
+ },
196
+ };
197
+
198
+ expect(next).toBeCalledWith(expected);
199
+ });
200
+ });
@@ -0,0 +1,193 @@
1
+ import addLocaleToSingleTypesMiddleware from '../addLocaleToSingleTypesMiddleware';
2
+
3
+ describe('i18n | middlewares | addLocaleToSingleTypesMiddleware', () => {
4
+ let getState;
5
+
6
+ beforeEach(() => {
7
+ const store = new Map();
8
+ store.set('i18n_locales', { locales: [] });
9
+ store.set('permissionsManager', { userPermissions: [] });
10
+ store.set('permissionsManager', {
11
+ collectionTypesRelatedPermissions: {
12
+ test: {
13
+ 'plugins::content-manager.explorer.read': [],
14
+ 'plugins::content-manager.explorer.create': [],
15
+ },
16
+ },
17
+ });
18
+
19
+ getState = () => store;
20
+ });
21
+
22
+ it('should forward the action when the type is undefined', () => {
23
+ const action = { test: true, type: undefined };
24
+
25
+ const next = jest.fn();
26
+ const middleware = addLocaleToSingleTypesMiddleware()({ getState });
27
+
28
+ middleware(next)(action);
29
+
30
+ expect(next).toBeCalledWith(action);
31
+ });
32
+
33
+ it('should forward the action when the type is not StrapiAdmin/LeftMenu/SET_CT_OR_ST_LINKS', () => {
34
+ const action = { test: true, type: 'TEST' };
35
+
36
+ const next = jest.fn();
37
+
38
+ const middleware = addLocaleToSingleTypesMiddleware()({ getState });
39
+ middleware(next)(action);
40
+
41
+ expect(next).toBeCalledWith(action);
42
+ });
43
+
44
+ it('should forward when the authorizedStLinks array is empty', () => {
45
+ const action = {
46
+ type: 'StrapiAdmin/LeftMenu/SET_CT_OR_ST_LINKS',
47
+ data: {
48
+ authorizedStLinks: [],
49
+ },
50
+ };
51
+ const middleware = addLocaleToSingleTypesMiddleware()({ getState });
52
+
53
+ const next = jest.fn();
54
+
55
+ middleware(next)(action);
56
+
57
+ expect(next).toBeCalledWith(action);
58
+ });
59
+
60
+ it('should not add the search key to a single type link when i18n is not enabled on the single type', () => {
61
+ const action = {
62
+ type: 'StrapiAdmin/LeftMenu/SET_CT_OR_ST_LINKS',
63
+ data: {
64
+ authorizedStLinks: [{ destination: 'cm/singleType/test' }],
65
+ contentTypeSchemas: [{ uid: 'test', pluginOptions: { i18n: { localized: false } } }],
66
+ },
67
+ };
68
+ const middleware = addLocaleToSingleTypesMiddleware()({ getState });
69
+
70
+ const next = jest.fn();
71
+
72
+ middleware(next)(action);
73
+
74
+ expect(next).toBeCalledWith(action);
75
+ });
76
+
77
+ it('should add a search key with the default locale when the user has the right to read it', () => {
78
+ const tempStore = new Map();
79
+ tempStore.set('i18n_locales', { locales: [{ code: 'en', isDefault: true }] });
80
+ tempStore.set('permissionsManager', { userPermissions: [] });
81
+ tempStore.set('permissionsManager', {
82
+ collectionTypesRelatedPermissions: {
83
+ test: {
84
+ 'plugins::content-manager.explorer.read': [{ properties: { locales: ['en'] } }],
85
+ 'plugins::content-manager.explorer.create': [],
86
+ },
87
+ },
88
+ });
89
+ const action = {
90
+ type: 'StrapiAdmin/LeftMenu/SET_CT_OR_ST_LINKS',
91
+ data: {
92
+ authorizedStLinks: [{ destination: 'cm/singleType/test' }],
93
+ contentTypeSchemas: [{ uid: 'test', pluginOptions: { i18n: { localized: true } } }],
94
+ },
95
+ };
96
+ const middleware = addLocaleToSingleTypesMiddleware()({ getState: () => tempStore });
97
+
98
+ const next = jest.fn();
99
+
100
+ middleware(next)(action);
101
+
102
+ const expected = {
103
+ type: 'StrapiAdmin/LeftMenu/SET_CT_OR_ST_LINKS',
104
+ data: {
105
+ authorizedStLinks: [
106
+ { destination: 'cm/singleType/test', search: 'plugins[i18n][locale]=en' },
107
+ ],
108
+ contentTypeSchemas: [{ uid: 'test', pluginOptions: { i18n: { localized: true } } }],
109
+ },
110
+ };
111
+
112
+ expect(next).toBeCalledWith(expected);
113
+ });
114
+
115
+ it('should set the isDisplayed key to false when the user does not have the right to read any locale', () => {
116
+ const tempStore = new Map();
117
+ tempStore.set('i18n_locales', { locales: [{ code: 'en', isDefault: true }] });
118
+ tempStore.set('permissionsManager', { userPermissions: [] });
119
+ tempStore.set('permissionsManager', {
120
+ collectionTypesRelatedPermissions: {
121
+ test: {
122
+ 'plugins::content-manager.explorer.read': [{ properties: { locales: [] } }],
123
+ 'plugins::content-manager.explorer.create': [],
124
+ },
125
+ },
126
+ });
127
+ const action = {
128
+ type: 'StrapiAdmin/LeftMenu/SET_CT_OR_ST_LINKS',
129
+ data: {
130
+ authorizedStLinks: [{ destination: 'cm/singleType/test' }],
131
+ contentTypeSchemas: [{ uid: 'test', pluginOptions: { i18n: { localized: true } } }],
132
+ },
133
+ };
134
+ const middleware = addLocaleToSingleTypesMiddleware()({ getState: () => tempStore });
135
+
136
+ const next = jest.fn();
137
+
138
+ middleware(next)(action);
139
+
140
+ const expected = {
141
+ type: 'StrapiAdmin/LeftMenu/SET_CT_OR_ST_LINKS',
142
+ data: {
143
+ authorizedStLinks: [{ destination: 'cm/singleType/test', isDisplayed: false }],
144
+ contentTypeSchemas: [{ uid: 'test', pluginOptions: { i18n: { localized: true } } }],
145
+ },
146
+ };
147
+
148
+ expect(next).toBeCalledWith(expected);
149
+ });
150
+
151
+ it('should keep the previous search', () => {
152
+ const tempStore = new Map();
153
+ tempStore.set('i18n_locales', { locales: [{ code: 'en', isDefault: true }] });
154
+ tempStore.set('permissionsManager', { userPermissions: [] });
155
+ tempStore.set('permissionsManager', {
156
+ collectionTypesRelatedPermissions: {
157
+ test: {
158
+ 'plugins::content-manager.explorer.read': [{ properties: { locales: ['en'] } }],
159
+ 'plugins::content-manager.explorer.create': [],
160
+ },
161
+ },
162
+ });
163
+ const action = {
164
+ type: 'StrapiAdmin/LeftMenu/SET_CT_OR_ST_LINKS',
165
+ data: {
166
+ authorizedStLinks: [
167
+ { destination: 'cm/singleType/test', search: 'plugins[plugin][test]=test' },
168
+ ],
169
+ contentTypeSchemas: [{ uid: 'test', pluginOptions: { i18n: { localized: true } } }],
170
+ },
171
+ };
172
+ const middleware = addLocaleToSingleTypesMiddleware()({ getState: () => tempStore });
173
+
174
+ const next = jest.fn();
175
+
176
+ middleware(next)(action);
177
+
178
+ const expected = {
179
+ type: 'StrapiAdmin/LeftMenu/SET_CT_OR_ST_LINKS',
180
+ data: {
181
+ authorizedStLinks: [
182
+ {
183
+ destination: 'cm/singleType/test',
184
+ search: 'plugins[plugin][test]=test&plugins[i18n][locale]=en',
185
+ },
186
+ ],
187
+ contentTypeSchemas: [{ uid: 'test', pluginOptions: { i18n: { localized: true } } }],
188
+ },
189
+ };
190
+
191
+ expect(next).toBeCalledWith(expected);
192
+ });
193
+ });