Skip to content

Commit

Permalink
FIX
Browse files Browse the repository at this point in the history
  • Loading branch information
ETLaurent committed Nov 23, 2023
1 parent 88c9078 commit abf2f59
Showing 1 changed file with 97 additions and 53 deletions.
150 changes: 97 additions & 53 deletions test/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -835,12 +835,22 @@ describe('@apostrophecms/import-export', function () {
});

describe('when the site has multiple locales', function() {
let _apos;
let _req;
let _importExportManager;
let apos;
let importExportManager;

before(async function () {
_apos = await t.create({
let req;
let notify;
let getFilesData;
let readExportFile;
let rewriteDocsWithCurrentLocale;
let insertDocs;

after(async function() {
await t.destroy(apos);
});

before(async function() {
apos = await t.create({
root: module,
testModule: true,
modules: getAppConfig({
Expand All @@ -865,25 +875,45 @@ describe('@apostrophecms/import-export', function () {
})
});

_req = _apos.task.getReq({
importExportManager = apos.modules['@apostrophecms/import-export'];
importExportManager.removeExportFileFromUploadFs = () => {};
importExportManager.cleanFile = () => {};

await insertAdminUser(apos);
await insertPieces(apos);
});

this.beforeEach(async function() {
req = apos.task.getReq({
locale: 'en',
body: {}
});

_importExportManager = _apos.modules['@apostrophecms/import-export'];
notify = apos.notify;
getFilesData = apos.modules['@apostrophecms/import-export'].getFilesData;
readExportFile = apos.modules['@apostrophecms/import-export'].readExportFile;
rewriteDocsWithCurrentLocale = apos.modules['@apostrophecms/import-export'].rewriteDocsWithCurrentLocale;
insertDocs = apos.modules['@apostrophecms/import-export'].insertDocs;

await deletePieces(apos);
await deletePage(apos);
await deleteAttachments(apos, attachmentPath);
});

after(async function() {
await t.destroy(_apos);
this.afterEach(function() {
apos.notify = notify;
apos.modules['@apostrophecms/import-export'].getFilesData = getFilesData;
apos.modules['@apostrophecms/import-export'].readExportFile = readExportFile;
apos.modules['@apostrophecms/import-export'].rewriteDocsWithCurrentLocale = rewriteDocsWithCurrentLocale;
apos.modules['@apostrophecms/import-export'].insertDocs = insertDocs;
});

it('should not rewrite the docs locale nor ask about it when the locale is not different', async function() {
const _req = _apos.task.getReq({
const req = apos.task.getReq({
locale: 'fr',
body: {}
});

_apos.modules['@apostrophecms/import-export'].readExportFile = async req => {
apos.modules['@apostrophecms/import-export'].readExportFile = async req => {
return {
docs: [
{
Expand All @@ -897,10 +927,10 @@ describe('@apostrophecms/import-export', function () {
attachmentsInfo: []
};
};
_apos.modules['@apostrophecms/import-export'].rewriteDocsWithCurrentLocale = () => {
apos.modules['@apostrophecms/import-export'].rewriteDocsWithCurrentLocale = () => {
throw new Error('should not have been called');
};
_apos.modules['@apostrophecms/import-export'].insertDocs = async (req, docs) => {
apos.modules['@apostrophecms/import-export'].insertDocs = async (req, docs) => {
assert.deepEqual(docs, [
{
_id: '4:fr:draft',
Expand All @@ -917,18 +947,18 @@ describe('@apostrophecms/import-export', function () {
failedIds: []
};
};
_apos.notify = async (req, message, options) => {
apos.notify = async (req, message, options) => {
if (options?.event?.name === 'import-locale-differs') {
throw new Error('should not have been called with event "import-locale-differ"');
}
return {};
};

await _importExportManager.import(_req);
await importExportManager.import(req);
});

it('should not rewrite the docs locales nor insert them but ask about it when the locale is different', async function() {
_apos.modules['@apostrophecms/import-export'].getFilesData = async exportPath => {
apos.modules['@apostrophecms/import-export'].readExportFile = async req => {
return {
docs: [
{
Expand All @@ -943,28 +973,28 @@ describe('@apostrophecms/import-export', function () {
};
};

_apos.modules['@apostrophecms/import-export'].rewriteDocsWithCurrentLocale = () => {
apos.modules['@apostrophecms/import-export'].rewriteDocsWithCurrentLocale = () => {
throw new Error('should not have been called');
};
_apos.modules['@apostrophecms/import-export'].insertDocs = async (req, docs) => {
apos.modules['@apostrophecms/import-export'].insertDocs = async (req, docs) => {
throw new Error('should not have been called');
};
_apos.notify = async (req, message, options) => {
apos.notify = async (req, message, options) => {
assert.equal(options.event.name, 'import-locale-differs');
};

await _importExportManager.import(_req);
await importExportManager.import(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({
const req = apos.task.getReq({
locale: 'en',
body: {
overrideLocale: true
}
});

_apos.modules['@apostrophecms/import-export'].getFilesData = async exportPath => {
apos.modules['@apostrophecms/import-export'].readExportFile = async req => {
return {
docs: [
{
Expand All @@ -979,7 +1009,7 @@ describe('@apostrophecms/import-export', function () {
};
};

_apos.modules['@apostrophecms/import-export'].rewriteDocsWithCurrentLocale = (req, docs) => {
apos.modules['@apostrophecms/import-export'].rewriteDocsWithCurrentLocale = (req, docs) => {
assert.deepEqual(docs, [
{
_id: '4:fr:draft',
Expand All @@ -992,7 +1022,7 @@ describe('@apostrophecms/import-export', function () {

return rewriteDocsWithCurrentLocale(req, docs);
};
_apos.modules['@apostrophecms/import-export'].insertDocs = async (req, docs) => {
apos.modules['@apostrophecms/import-export'].insertDocs = async (req, docs) => {
assert.deepEqual(docs, [
{
_id: '4:en:draft',
Expand All @@ -1009,24 +1039,23 @@ describe('@apostrophecms/import-export', function () {
failedIds: []
};
};
_apos.notify = async (req, message, options) => {
apos.notify = async (req, message, options) => {
if (options?.event?.name === 'import-locale-differs') {
throw new Error('should not have been called with event "import-locale-differ"');
}
return {};
};

await _importExportManager.import(_req);
await importExportManager.import(req);
});
});
});

describe('#overrideDuplicates - overriding locales integration tests', function() {
let req;
let jobManager;
let getFilesData;
let rewriteDocsWithCurrentLocale;
let insertOrUpdateDoc;
let jobManager;

this.beforeEach(async function() {
req = apos.task.getReq({
Expand All @@ -1036,7 +1065,6 @@ describe('@apostrophecms/import-export', function () {
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 = () => {};
Expand All @@ -1050,7 +1078,6 @@ describe('@apostrophecms/import-export', 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() {
Expand Down Expand Up @@ -1110,12 +1137,18 @@ describe('@apostrophecms/import-export', function () {
});

describe('when the site has multiple locales', function() {
let _apos;
let _req;
let _importExportManager;
let apos;
let importExportManager;

let getFilesData;
let rewriteDocsWithCurrentLocale;

after(async function() {
await t.destroy(apos);
});

before(async function () {
_apos = await t.create({
before(async function() {
apos = await t.create({
root: module,
testModule: true,
modules: getAppConfig({
Expand All @@ -1140,27 +1173,38 @@ describe('@apostrophecms/import-export', function () {
})
});

_req = _apos.task.getReq({
locale: 'en',
body: {}
});

_importExportManager = _apos.modules['@apostrophecms/import-export'];
});
importExportManager = apos.modules['@apostrophecms/import-export'];
importExportManager.removeExportFileFromUploadFs = () => {};
importExportManager.cleanFile = () => {};

after(async function() {
await t.destroy(_apos);
await insertAdminUser(apos);
await insertPieces(apos);
});

this.beforeEach(async function() {
jobManager = _apos.modules['@apostrophecms/job'];
req = apos.task.getReq({
locale: 'en',
body: {}
});
getFilesData = apos.modules['@apostrophecms/import-export'].getFilesData;
rewriteDocsWithCurrentLocale = apos.modules['@apostrophecms/import-export'].rewriteDocsWithCurrentLocale;

jobManager = apos.modules['@apostrophecms/job'];
jobManager.success = () => {};
jobManager.failure = () => {};

await deletePieces(apos);
await deletePage(apos);
await deleteAttachments(apos, attachmentPath);
});

this.afterEach(function() {
apos.modules['@apostrophecms/import-export'].getFilesData = getFilesData;
apos.modules['@apostrophecms/import-export'].rewriteDocsWithCurrentLocale = rewriteDocsWithCurrentLocale;
});

it('should not rewrite the docs locale when the locale is not different', async function() {
_apos.modules['@apostrophecms/import-export'].getFilesData = async exportPath => {
apos.modules['@apostrophecms/import-export'].getFilesData = async exportPath => {
return {
docs: [
{
Expand All @@ -1174,22 +1218,22 @@ describe('@apostrophecms/import-export', function () {
attachmentsInfo: []
};
};
_apos.modules['@apostrophecms/import-export'].rewriteDocsWithCurrentLocale = (req, docs) => {
apos.modules['@apostrophecms/import-export'].rewriteDocsWithCurrentLocale = (req, docs) => {
throw new Error('should not have been called');
};

await _importExportManager.overrideDuplicates(_req);
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({
const req = apos.task.getReq({
locale: 'en',
body: {
overrideLocale: true
}
});

_apos.modules['@apostrophecms/import-export'].getFilesData = async exportPath => {
apos.modules['@apostrophecms/import-export'].getFilesData = async exportPath => {
return {
docs: [
{
Expand All @@ -1203,7 +1247,7 @@ describe('@apostrophecms/import-export', function () {
attachmentsInfo: []
};
};
_apos.modules['@apostrophecms/import-export'].rewriteDocsWithCurrentLocale = (req, docs) => {
apos.modules['@apostrophecms/import-export'].rewriteDocsWithCurrentLocale = (req, docs) => {
assert.deepEqual(docs, [
{
_id: '4:fr:draft',
Expand All @@ -1217,7 +1261,7 @@ describe('@apostrophecms/import-export', function () {
return rewriteDocsWithCurrentLocale(req, docs);
};

await _importExportManager.overrideDuplicates(_req);
await importExportManager.overrideDuplicates(req);
});
});
});
Expand Down

0 comments on commit abf2f59

Please sign in to comment.