From d2c5306b39a44b45b3ed08ea48c8420c9fc64142 Mon Sep 17 00:00:00 2001 From: Etienne Laurent Date: Thu, 23 Nov 2023 17:21:22 +0100 Subject: [PATCH] arr --- lib/methods/import.js | 12 ++ test/index.js | 200 ++++++++++++++++++ .../components/AposDuplicateImportModal.vue | 7 +- 3 files changed, 218 insertions(+), 1 deletion(-) diff --git a/lib/methods/import.js b/lib/methods/import.js index 9aa9b52c..e0764da2 100644 --- a/lib/methods/import.js +++ b/lib/methods/import.js @@ -120,6 +120,7 @@ module.exports = self => { } const results = { + overrideLocale, duplicatedDocs, importedAttachments, type: moduleName, @@ -439,6 +440,7 @@ module.exports = self => { }, async overrideDuplicates(req) { + const overrideLocale = self.apos.launder.boolean(req.body.overrideLocale); const exportPath = self.apos.launder.string(req.body.exportPath); const docIds = self.apos.launder.strings(req.body.docIds); const jobId = self.apos.launder.string(req.body.jobId); @@ -450,6 +452,16 @@ module.exports = self => { const { docs, attachmentsInfo } = await self.getFilesData(exportPath, docIds); + const differentDocsLocale = self.getFirstDifferentLocale(req, docs); + const siteHasMultipleLocales = Object.keys(self.apos.i18n.locales).length > 1; + + // Re-write locale if `overrideLocale` param is passed-on from the import process + // (i.e if the user chose "Yes") + // or re-write locale automatically on a single-locale site + if (differentDocsLocale && (!siteHasMultipleLocales || overrideLocale)) { + self.rewriteDocsWithCurrentLocale(req, docs); + } + for (const doc of docs) { try { const attachmentsToOverride = self.getRelatedDocsFromSchema(req, { diff --git a/test/index.js b/test/index.js index d92019f3..e1ebb4be 100644 --- a/test/index.js +++ b/test/index.js @@ -1021,6 +1021,206 @@ describe('@apostrophecms/import-export', function () { }); }); + describe.only('#overrideDuplicates - overriding locales integration tests', function() { + let req; + let jobManager; + let getFilesData; + let rewriteDocsWithCurrentLocale; + let insertOrUpdateDoc; + + this.beforeEach(async function() { + req = apos.task.getReq({ + locale: 'en', + body: {} + }); + jobManager = apos.modules['@apostrophecms/job']; + getFilesData = apos.modules['@apostrophecms/import-export'].getFilesData; + rewriteDocsWithCurrentLocale = apos.modules['@apostrophecms/import-export'].rewriteDocsWithCurrentLocale; + insertOrUpdateDoc = apos.modules['@apostrophecms/import-export'].insertOrUpdateDoc; + + jobManager.success = () => {}; + jobManager.failure = () => {}; + + await deletePieces(apos); + await deletePage(apos); + await deleteAttachments(apos, attachmentPath); + }); + + this.afterEach(function() { + apos.modules['@apostrophecms/job'].jobManager = jobManager; + apos.modules['@apostrophecms/import-export'].getFilesData = getFilesData; + apos.modules['@apostrophecms/import-export'].rewriteDocsWithCurrentLocale = rewriteDocsWithCurrentLocale; + apos.modules['@apostrophecms/import-export'].insertOrUpdateDoc = insertOrUpdateDoc; + }); + + describe('when the site has only one locale', function() { + it('should not rewrite the docs locale when the locale is not different', async function() { + apos.modules['@apostrophecms/import-export'].getFilesData = async exportPath => { + return { + docs: [ + { + _id: '4:en:draft', + aposMode: 'draft', + aposLocale: 'en:draft', + title: 'topic1', + type: 'topic' + } + ], + attachmentsInfo: [] + }; + }; + apos.modules['@apostrophecms/import-export'].rewriteDocsWithCurrentLocale = (req, docs) => { + throw new Error('should not have been called'); + }; + + await importExportManager.overrideDuplicates(req); + }); + + it('should rewrite the docs locale when the locale is different', async function() { + apos.modules['@apostrophecms/import-export'].getFilesData = async exportPath => { + return { + docs: [ + { + _id: '4:fr:draft', + aposMode: 'draft', + aposLocale: 'fr:draft', + title: 'topic1', + type: 'topic' + } + ], + attachmentsInfo: [] + }; + }; + apos.modules['@apostrophecms/import-export'].rewriteDocsWithCurrentLocale = (req, docs) => { + assert.deepEqual(docs, [ + { + _id: '4:fr:draft', + aposMode: 'draft', + aposLocale: 'fr:draft', + title: 'topic1', + type: 'topic' + } + ]); + + return rewriteDocsWithCurrentLocale(req, docs); + }; + + await importExportManager.overrideDuplicates(req); + }); + }); + + describe('when the site has multiple locales', function() { + let _apos; + let _req; + let _importExportManager; + + before(async function () { + _apos = await t.create({ + root: module, + testModule: true, + modules: getAppConfig({ + '@apostrophecms/express': { + options: { + session: { secret: 'supersecret' }, + port: 3001 + } + }, + '@apostrophecms/i18n': { + options: { + defaultLocale: 'en', + locales: { + en: { label: 'English' }, + fr: { + label: 'French', + prefix: '/fr' + } + } + } + } + }) + }); + + _req = _apos.task.getReq({ + locale: 'en', + body: {} + }); + + _importExportManager = _apos.modules['@apostrophecms/import-export']; + }); + + after(async function() { + await t.destroy(_apos); + }); + + this.beforeEach(async function() { + jobManager = _apos.modules['@apostrophecms/job']; + + jobManager.success = () => {}; + jobManager.failure = () => {}; + }); + + it('should not rewrite the docs locale when the locale is not different', async function() { + _apos.modules['@apostrophecms/import-export'].getFilesData = async exportPath => { + return { + docs: [ + { + _id: '4:en:draft', + aposMode: 'draft', + aposLocale: 'en:draft', + title: 'topic1', + type: 'topic' + } + ], + attachmentsInfo: [] + }; + }; + _apos.modules['@apostrophecms/import-export'].rewriteDocsWithCurrentLocale = (req, docs) => { + throw new Error('should not have been called'); + }; + + await _importExportManager.overrideDuplicates(_req); + }); + + it('should rewrite the docs locale when the locale is different and the `overrideLocale` param is provided', async function() { + const _req = _apos.task.getReq({ + locale: 'en', + body: { + overrideLocale: true + } + }); + + _apos.modules['@apostrophecms/import-export'].getFilesData = async exportPath => { + return { + docs: [ + { + _id: '4:fr:draft', + aposMode: 'draft', + aposLocale: 'fr:draft', + title: 'topic1', + type: 'topic' + } + ], + attachmentsInfo: [] + }; + }; + _apos.modules['@apostrophecms/import-export'].rewriteDocsWithCurrentLocale = (req, docs) => { + assert.deepEqual(docs, [ + { + _id: '4:fr:draft', + aposMode: 'draft', + aposLocale: 'fr:draft', + title: 'topic1', + type: 'topic' + } + ]); + + return rewriteDocsWithCurrentLocale(req, docs); + }; + + await _importExportManager.overrideDuplicates(_req); + }); + }); + }); }); function extractFileNames (files) { diff --git a/ui/apos/components/AposDuplicateImportModal.vue b/ui/apos/components/AposDuplicateImportModal.vue index 06f014fb..2345a935 100644 --- a/ui/apos/components/AposDuplicateImportModal.vue +++ b/ui/apos/components/AposDuplicateImportModal.vue @@ -111,6 +111,10 @@ export default { type: String, required: true }, + overrideLocale: { + type: Boolean, + required: true + }, duplicatedDocs: { type: Array, required: true @@ -214,7 +218,8 @@ export default { docIds: this.checked, importedAttachments: this.importedAttachments, exportPath: this.exportPath, - jobId: this.jobId + jobId: this.jobId, + overrideLocale: this.overrideLocale } }).catch(() => { apos.notify('aposImportExport:exportFailed', {