Skip to content

Commit d2c5306

Browse files
author
Etienne Laurent
committed
arr
1 parent f444864 commit d2c5306

File tree

3 files changed

+218
-1
lines changed

3 files changed

+218
-1
lines changed

lib/methods/import.js

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -120,6 +120,7 @@ module.exports = self => {
120120
}
121121

122122
const results = {
123+
overrideLocale,
123124
duplicatedDocs,
124125
importedAttachments,
125126
type: moduleName,
@@ -439,6 +440,7 @@ module.exports = self => {
439440
},
440441

441442
async overrideDuplicates(req) {
443+
const overrideLocale = self.apos.launder.boolean(req.body.overrideLocale);
442444
const exportPath = self.apos.launder.string(req.body.exportPath);
443445
const docIds = self.apos.launder.strings(req.body.docIds);
444446
const jobId = self.apos.launder.string(req.body.jobId);
@@ -450,6 +452,16 @@ module.exports = self => {
450452

451453
const { docs, attachmentsInfo } = await self.getFilesData(exportPath, docIds);
452454

455+
const differentDocsLocale = self.getFirstDifferentLocale(req, docs);
456+
const siteHasMultipleLocales = Object.keys(self.apos.i18n.locales).length > 1;
457+
458+
// Re-write locale if `overrideLocale` param is passed-on from the import process
459+
// (i.e if the user chose "Yes")
460+
// or re-write locale automatically on a single-locale site
461+
if (differentDocsLocale && (!siteHasMultipleLocales || overrideLocale)) {
462+
self.rewriteDocsWithCurrentLocale(req, docs);
463+
}
464+
453465
for (const doc of docs) {
454466
try {
455467
const attachmentsToOverride = self.getRelatedDocsFromSchema(req, {

test/index.js

Lines changed: 200 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1021,6 +1021,206 @@ describe('@apostrophecms/import-export', function () {
10211021
});
10221022
});
10231023

1024+
describe.only('#overrideDuplicates - overriding locales integration tests', function() {
1025+
let req;
1026+
let jobManager;
1027+
let getFilesData;
1028+
let rewriteDocsWithCurrentLocale;
1029+
let insertOrUpdateDoc;
1030+
1031+
this.beforeEach(async function() {
1032+
req = apos.task.getReq({
1033+
locale: 'en',
1034+
body: {}
1035+
});
1036+
jobManager = apos.modules['@apostrophecms/job'];
1037+
getFilesData = apos.modules['@apostrophecms/import-export'].getFilesData;
1038+
rewriteDocsWithCurrentLocale = apos.modules['@apostrophecms/import-export'].rewriteDocsWithCurrentLocale;
1039+
insertOrUpdateDoc = apos.modules['@apostrophecms/import-export'].insertOrUpdateDoc;
1040+
1041+
jobManager.success = () => {};
1042+
jobManager.failure = () => {};
1043+
1044+
await deletePieces(apos);
1045+
await deletePage(apos);
1046+
await deleteAttachments(apos, attachmentPath);
1047+
});
1048+
1049+
this.afterEach(function() {
1050+
apos.modules['@apostrophecms/job'].jobManager = jobManager;
1051+
apos.modules['@apostrophecms/import-export'].getFilesData = getFilesData;
1052+
apos.modules['@apostrophecms/import-export'].rewriteDocsWithCurrentLocale = rewriteDocsWithCurrentLocale;
1053+
apos.modules['@apostrophecms/import-export'].insertOrUpdateDoc = insertOrUpdateDoc;
1054+
});
1055+
1056+
describe('when the site has only one locale', function() {
1057+
it('should not rewrite the docs locale when the locale is not different', async function() {
1058+
apos.modules['@apostrophecms/import-export'].getFilesData = async exportPath => {
1059+
return {
1060+
docs: [
1061+
{
1062+
_id: '4:en:draft',
1063+
aposMode: 'draft',
1064+
aposLocale: 'en:draft',
1065+
title: 'topic1',
1066+
type: 'topic'
1067+
}
1068+
],
1069+
attachmentsInfo: []
1070+
};
1071+
};
1072+
apos.modules['@apostrophecms/import-export'].rewriteDocsWithCurrentLocale = (req, docs) => {
1073+
throw new Error('should not have been called');
1074+
};
1075+
1076+
await importExportManager.overrideDuplicates(req);
1077+
});
1078+
1079+
it('should rewrite the docs locale when the locale is different', async function() {
1080+
apos.modules['@apostrophecms/import-export'].getFilesData = async exportPath => {
1081+
return {
1082+
docs: [
1083+
{
1084+
_id: '4:fr:draft',
1085+
aposMode: 'draft',
1086+
aposLocale: 'fr:draft',
1087+
title: 'topic1',
1088+
type: 'topic'
1089+
}
1090+
],
1091+
attachmentsInfo: []
1092+
};
1093+
};
1094+
apos.modules['@apostrophecms/import-export'].rewriteDocsWithCurrentLocale = (req, docs) => {
1095+
assert.deepEqual(docs, [
1096+
{
1097+
_id: '4:fr:draft',
1098+
aposMode: 'draft',
1099+
aposLocale: 'fr:draft',
1100+
title: 'topic1',
1101+
type: 'topic'
1102+
}
1103+
]);
1104+
1105+
return rewriteDocsWithCurrentLocale(req, docs);
1106+
};
1107+
1108+
await importExportManager.overrideDuplicates(req);
1109+
});
1110+
});
1111+
1112+
describe('when the site has multiple locales', function() {
1113+
let _apos;
1114+
let _req;
1115+
let _importExportManager;
1116+
1117+
before(async function () {
1118+
_apos = await t.create({
1119+
root: module,
1120+
testModule: true,
1121+
modules: getAppConfig({
1122+
'@apostrophecms/express': {
1123+
options: {
1124+
session: { secret: 'supersecret' },
1125+
port: 3001
1126+
}
1127+
},
1128+
'@apostrophecms/i18n': {
1129+
options: {
1130+
defaultLocale: 'en',
1131+
locales: {
1132+
en: { label: 'English' },
1133+
fr: {
1134+
label: 'French',
1135+
prefix: '/fr'
1136+
}
1137+
}
1138+
}
1139+
}
1140+
})
1141+
});
1142+
1143+
_req = _apos.task.getReq({
1144+
locale: 'en',
1145+
body: {}
1146+
});
1147+
1148+
_importExportManager = _apos.modules['@apostrophecms/import-export'];
1149+
});
1150+
1151+
after(async function() {
1152+
await t.destroy(_apos);
1153+
});
1154+
1155+
this.beforeEach(async function() {
1156+
jobManager = _apos.modules['@apostrophecms/job'];
1157+
1158+
jobManager.success = () => {};
1159+
jobManager.failure = () => {};
1160+
});
1161+
1162+
it('should not rewrite the docs locale when the locale is not different', async function() {
1163+
_apos.modules['@apostrophecms/import-export'].getFilesData = async exportPath => {
1164+
return {
1165+
docs: [
1166+
{
1167+
_id: '4:en:draft',
1168+
aposMode: 'draft',
1169+
aposLocale: 'en:draft',
1170+
title: 'topic1',
1171+
type: 'topic'
1172+
}
1173+
],
1174+
attachmentsInfo: []
1175+
};
1176+
};
1177+
_apos.modules['@apostrophecms/import-export'].rewriteDocsWithCurrentLocale = (req, docs) => {
1178+
throw new Error('should not have been called');
1179+
};
1180+
1181+
await _importExportManager.overrideDuplicates(_req);
1182+
});
1183+
1184+
it('should rewrite the docs locale when the locale is different and the `overrideLocale` param is provided', async function() {
1185+
const _req = _apos.task.getReq({
1186+
locale: 'en',
1187+
body: {
1188+
overrideLocale: true
1189+
}
1190+
});
1191+
1192+
_apos.modules['@apostrophecms/import-export'].getFilesData = async exportPath => {
1193+
return {
1194+
docs: [
1195+
{
1196+
_id: '4:fr:draft',
1197+
aposMode: 'draft',
1198+
aposLocale: 'fr:draft',
1199+
title: 'topic1',
1200+
type: 'topic'
1201+
}
1202+
],
1203+
attachmentsInfo: []
1204+
};
1205+
};
1206+
_apos.modules['@apostrophecms/import-export'].rewriteDocsWithCurrentLocale = (req, docs) => {
1207+
assert.deepEqual(docs, [
1208+
{
1209+
_id: '4:fr:draft',
1210+
aposMode: 'draft',
1211+
aposLocale: 'fr:draft',
1212+
title: 'topic1',
1213+
type: 'topic'
1214+
}
1215+
]);
1216+
1217+
return rewriteDocsWithCurrentLocale(req, docs);
1218+
};
1219+
1220+
await _importExportManager.overrideDuplicates(_req);
1221+
});
1222+
});
1223+
});
10241224
});
10251225

10261226
function extractFileNames (files) {

ui/apos/components/AposDuplicateImportModal.vue

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -111,6 +111,10 @@ export default {
111111
type: String,
112112
required: true
113113
},
114+
overrideLocale: {
115+
type: Boolean,
116+
required: true
117+
},
114118
duplicatedDocs: {
115119
type: Array,
116120
required: true
@@ -214,7 +218,8 @@ export default {
214218
docIds: this.checked,
215219
importedAttachments: this.importedAttachments,
216220
exportPath: this.exportPath,
217-
jobId: this.jobId
221+
jobId: this.jobId,
222+
overrideLocale: this.overrideLocale
218223
}
219224
}).catch(() => {
220225
apos.notify('aposImportExport:exportFailed', {

0 commit comments

Comments
 (0)