Skip to content

Commit

Permalink
arr
Browse files Browse the repository at this point in the history
  • Loading branch information
Etienne Laurent committed Nov 23, 2023
1 parent f444864 commit d2c5306
Show file tree
Hide file tree
Showing 3 changed files with 218 additions and 1 deletion.
12 changes: 12 additions & 0 deletions lib/methods/import.js
Original file line number Diff line number Diff line change
Expand Up @@ -120,6 +120,7 @@ module.exports = self => {
}

const results = {
overrideLocale,
duplicatedDocs,
importedAttachments,
type: moduleName,
Expand Down Expand Up @@ -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);
Expand All @@ -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, {
Expand Down
200 changes: 200 additions & 0 deletions test/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down
7 changes: 6 additions & 1 deletion ui/apos/components/AposDuplicateImportModal.vue
Original file line number Diff line number Diff line change
Expand Up @@ -111,6 +111,10 @@ export default {
type: String,
required: true
},
overrideLocale: {
type: Boolean,
required: true
},
duplicatedDocs: {
type: Array,
required: true
Expand Down Expand Up @@ -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', {
Expand Down

0 comments on commit d2c5306

Please sign in to comment.