From 47640e56948855d90e3314ebb58cf787ca8d4665 Mon Sep 17 00:00:00 2001 From: itsrogil Date: Tue, 20 Feb 2024 15:05:04 +0100 Subject: [PATCH 1/7] ADD: Fix. Need Mocha tests --- lib/methods/import.js | 16 ++++++++++++++++ 1 file changed, 16 insertions(+) diff --git a/lib/methods/import.js b/lib/methods/import.js index 0a06c6ea..086f3806 100644 --- a/lib/methods/import.js +++ b/lib/methods/import.js @@ -374,6 +374,22 @@ module.exports = self => { } } + if (method === 'update' && doc.aposMode === 'draft') { + console.log('doc._id > ', doc._id) + const existing = await self.apos.doc.db.findOne({ _id: doc._id }, { + lastPublishedAt: 1 + }); + // const currentDoc = await manager + // .find(req, { _id: doc._id }) + // .project({ lastPublishedAt: 1 }) + // .toObject(); + console.log(existing); + + doc.lastPublishedAt = existing.lastPublishedAt; + } + + console.log('Plop', method, doc) + if (isPage) { return method === 'update' ? manager[method](req, doc, { setModified: false }) From bc68d70a795bad205484d270793a3cc93cbeaed5 Mon Sep 17 00:00:00 2001 From: itsrogil Date: Tue, 20 Feb 2024 16:22:17 +0100 Subject: [PATCH 2/7] REMOVE: console.log --- lib/methods/import.js | 2 -- 1 file changed, 2 deletions(-) diff --git a/lib/methods/import.js b/lib/methods/import.js index 086f3806..465a5a57 100644 --- a/lib/methods/import.js +++ b/lib/methods/import.js @@ -388,8 +388,6 @@ module.exports = self => { doc.lastPublishedAt = existing.lastPublishedAt; } - console.log('Plop', method, doc) - if (isPage) { return method === 'update' ? manager[method](req, doc, { setModified: false }) From 356dbd0f2307dbf11bc49c2dfe578a87433674d1 Mon Sep 17 00:00:00 2001 From: itsrogil Date: Tue, 20 Feb 2024 16:29:51 +0100 Subject: [PATCH 3/7] REMOVE: useless logs and comments --- lib/methods/import.js | 7 ------- 1 file changed, 7 deletions(-) diff --git a/lib/methods/import.js b/lib/methods/import.js index 465a5a57..e69a6f2b 100644 --- a/lib/methods/import.js +++ b/lib/methods/import.js @@ -375,16 +375,9 @@ module.exports = self => { } if (method === 'update' && doc.aposMode === 'draft') { - console.log('doc._id > ', doc._id) const existing = await self.apos.doc.db.findOne({ _id: doc._id }, { lastPublishedAt: 1 }); - // const currentDoc = await manager - // .find(req, { _id: doc._id }) - // .project({ lastPublishedAt: 1 }) - // .toObject(); - console.log(existing); - doc.lastPublishedAt = existing.lastPublishedAt; } From 579d7a80da394c4751b69d35ec7a8e42337d3443 Mon Sep 17 00:00:00 2001 From: itsrogil Date: Tue, 20 Feb 2024 16:47:19 +0100 Subject: [PATCH 4/7] ADD: Safety --- lib/methods/import.js | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/lib/methods/import.js b/lib/methods/import.js index e69a6f2b..7fc189ac 100644 --- a/lib/methods/import.js +++ b/lib/methods/import.js @@ -375,10 +375,12 @@ module.exports = self => { } if (method === 'update' && doc.aposMode === 'draft') { - const existing = await self.apos.doc.db.findOne({ _id: doc._id }, { + const existingDoc = await self.apos.doc.db.findOne({ _id: doc._id }, { lastPublishedAt: 1 }); - doc.lastPublishedAt = existing.lastPublishedAt; + if (existingDoc) { + doc.lastPublishedAt = existingDoc.lastPublishedAt; + } } if (isPage) { From 099e91d1c2987547a884b7d0d762adc39710b299 Mon Sep 17 00:00:00 2001 From: itsrogil Date: Mon, 26 Feb 2024 11:04:14 +0100 Subject: [PATCH 5/7] ADD: Test is passing! ...but breaks the other tests. --- test/index.js | 116 +++++++++++++++++++++++++++++++++++++++++++++++++- 1 file changed, 115 insertions(+), 1 deletion(-) diff --git a/test/index.js b/test/index.js index 4a746cef..2f783824 100644 --- a/test/index.js +++ b/test/index.js @@ -220,6 +220,108 @@ describe('@apostrophecms/import-export', function () { await cleanFile(exportPath); }); + it.only('should preserve lastPublishedAt property on import for existing drafts', async function() { + console.log('[TEST] should preserve lastPublishedAt property on import for existing drafts'); + + // Get the page2 that was meant for testing + const req = apos.task.getReq(); + const page2 = await apos.page.find(req, { title: 'page2' }).toObject(); + + // PUBLISH IT + const publishedResult = await apos.page.publish(req, page2); + // THEN UNPUBLISH IT + const draftPage = await apos.page.unpublish(req, page2); + + // THEN EXPORT IT (as draft) + req.body = { + _ids: [ draftPage._id ], + extension: 'gzip', + type: draftPage.type + }; + + const { url } = await importExportManager.export(req, apos.page); + const fileName = path.basename(url); + + pageTgzPath = path.join(exportsPath, fileName); + const exportPath = await gzip.input(pageTgzPath); + + const { docs } = await getExtractedFiles(exportPath); + const exportedDraftPage = docs.find(doc => doc._id === draftPage._id); + + console.log('docs (exported) >>>', docs); + + // @TODO Delete: + // Test exported doc + const exportedDoc = await apos.doc.db + .find({ type: /default-page|article|topic|@apostrophecms\/image/, title: 'page2' }) + .toArray(); + // Last published at est bien à "null" et c'est bien un draft + + // Seems it correctly exported + // Now we PUBLISH it + const result = await apos.page.publish(req, draftPage); + console.log({ result }); + + const updatedPage2 = await apos.doc.db + .find({ title: 'page2', aposMode: 'published' }) + .toArray(); + console.log(updatedPage2); + + // Rename tar file for import issue + const currentPath = pageTgzPath; + const newPath = currentPath.replace('.tar.gz', '-import.tar.gz'); + try { + await fs.rename(currentPath, newPath); + console.log(`Fichier renommé de ${currentPath} à ${newPath}`); + } catch (error) { + console.error('Erreur lors du renommage du fichier :', error); + } + + console.log('Then import it'); + + // IMPORT IT + req.body = {}; + req.files = { + file: { + path: newPath, + type: mimeType + } + }; + + const { + duplicatedDocs, + importedAttachments, + exportPathId, + jobId, + notificationId + } = await importExportManager.import(req); + + req.body = { + docIds: duplicatedDocs.map(doc => doc.aposDocId), + importedAttachments, + exportPathId, + jobId, + notificationId + } + + await importExportManager.overrideDuplicates(req); + + const NewUpdatedPage2 = await apos.doc.db + .find({ title: 'page2' }) + .toArray(); + console.log('NewUpdatedPage2 >>>', NewUpdatedPage2); + + // Check that the imported docs are not `null` + + assert.deepEqual(NewUpdatedPage2.some((doc) => { + console.log('doc.lastPublishedAt > ', doc.lastPublishedAt) + return doc.lastPublishedAt === null; + }), false, 'expected none of the imported docs to have a `lastPublishedAt` value of `null`'); + + // @TODO: Cleanup + // ... + }); + it('should import pieces with related documents from a compressed file', async function() { const req = apos.task.getReq(); @@ -1410,7 +1512,19 @@ async function insertPieces(apos) { } ], metaType: 'area' - } + }, + }); + + await apos.page.insert(req, '_home', 'lastChild', { + ...pageInstance, + title: 'page2', + type: 'default-page', + _articles: [], + main: { + _id: 'areaId', + items: [], + metaType: 'area' + }, }); } From 937d3b5993d5957b85aada28b42c6ad1f2d91cb3 Mon Sep 17 00:00:00 2001 From: Jed Date: Tue, 27 Feb 2024 10:40:31 +0100 Subject: [PATCH 6/7] fixes lastPublishedAt test --- test/index.js | 228 +++++++++++++++++++++++++------------------------- 1 file changed, 113 insertions(+), 115 deletions(-) diff --git a/test/index.js b/test/index.js index 2f783824..0f9a61c0 100644 --- a/test/index.js +++ b/test/index.js @@ -220,108 +220,6 @@ describe('@apostrophecms/import-export', function () { await cleanFile(exportPath); }); - it.only('should preserve lastPublishedAt property on import for existing drafts', async function() { - console.log('[TEST] should preserve lastPublishedAt property on import for existing drafts'); - - // Get the page2 that was meant for testing - const req = apos.task.getReq(); - const page2 = await apos.page.find(req, { title: 'page2' }).toObject(); - - // PUBLISH IT - const publishedResult = await apos.page.publish(req, page2); - // THEN UNPUBLISH IT - const draftPage = await apos.page.unpublish(req, page2); - - // THEN EXPORT IT (as draft) - req.body = { - _ids: [ draftPage._id ], - extension: 'gzip', - type: draftPage.type - }; - - const { url } = await importExportManager.export(req, apos.page); - const fileName = path.basename(url); - - pageTgzPath = path.join(exportsPath, fileName); - const exportPath = await gzip.input(pageTgzPath); - - const { docs } = await getExtractedFiles(exportPath); - const exportedDraftPage = docs.find(doc => doc._id === draftPage._id); - - console.log('docs (exported) >>>', docs); - - // @TODO Delete: - // Test exported doc - const exportedDoc = await apos.doc.db - .find({ type: /default-page|article|topic|@apostrophecms\/image/, title: 'page2' }) - .toArray(); - // Last published at est bien à "null" et c'est bien un draft - - // Seems it correctly exported - // Now we PUBLISH it - const result = await apos.page.publish(req, draftPage); - console.log({ result }); - - const updatedPage2 = await apos.doc.db - .find({ title: 'page2', aposMode: 'published' }) - .toArray(); - console.log(updatedPage2); - - // Rename tar file for import issue - const currentPath = pageTgzPath; - const newPath = currentPath.replace('.tar.gz', '-import.tar.gz'); - try { - await fs.rename(currentPath, newPath); - console.log(`Fichier renommé de ${currentPath} à ${newPath}`); - } catch (error) { - console.error('Erreur lors du renommage du fichier :', error); - } - - console.log('Then import it'); - - // IMPORT IT - req.body = {}; - req.files = { - file: { - path: newPath, - type: mimeType - } - }; - - const { - duplicatedDocs, - importedAttachments, - exportPathId, - jobId, - notificationId - } = await importExportManager.import(req); - - req.body = { - docIds: duplicatedDocs.map(doc => doc.aposDocId), - importedAttachments, - exportPathId, - jobId, - notificationId - } - - await importExportManager.overrideDuplicates(req); - - const NewUpdatedPage2 = await apos.doc.db - .find({ title: 'page2' }) - .toArray(); - console.log('NewUpdatedPage2 >>>', NewUpdatedPage2); - - // Check that the imported docs are not `null` - - assert.deepEqual(NewUpdatedPage2.some((doc) => { - console.log('doc.lastPublishedAt > ', doc.lastPublishedAt) - return doc.lastPublishedAt === null; - }), false, 'expected none of the imported docs to have a `lastPublishedAt` value of `null`'); - - // @TODO: Cleanup - // ... - }); - it('should import pieces with related documents from a compressed file', async function() { const req = apos.task.getReq(); @@ -683,6 +581,118 @@ describe('@apostrophecms/import-export', function () { await cleanFile(pageTgzPath.replace(gzip.allowedExtension, '')); }); + it('should preserve lastPublishedAt property on import for existing drafts', async function() { + const req = apos.task.getReq(); + const manager = apos.doc.getManager('default-page'); + const pageInstance = manager.newInstance(); + + await apos.page.insert(req, '_home', 'lastChild', { + ...pageInstance, + title: 'page2', + type: 'default-page', + _articles: [], + main: { + _id: 'areaId', + items: [], + metaType: 'area' + } + }); + const page2 = await apos.page.find(req, { title: 'page2' }).toObject(); + + const publishedResult = await apos.page.publish(req, page2); + const draftPage = await apos.page.unpublish(req, page2); + + req.body = { + _ids: [ draftPage._id ], + extension: 'gzip', + type: draftPage.type + }; + + const { url } = await importExportManager.export(req, apos.page); + const fileName = path.basename(url); + + pageTgzPath = path.join(exportsPath, fileName); + const exportPath = await gzip.input(pageTgzPath); + + const { docs } = await getExtractedFiles(exportPath); + const exportedDraftPage = docs.find(doc => doc._id === draftPage._id); + + // @TODO Delete: + // Test exported doc + const exportedDoc = await apos.doc.db + .find({ + type: /default-page|article|topic|@apostrophecms\/image/, + title: 'page2' + }) + .toArray(); + // Last published at est bien à "null" et c'est bien un draft + + // Seems it correctly exported + // Now we PUBLISH it + const result = await apos.page.publish(req, draftPage); + + const updatedPage2 = await apos.doc.db + .find({ + title: 'page2', + aposMode: 'published' + }) + .toArray(); + + // Rename tar file for import issue + const currentPath = pageTgzPath; + const newPath = currentPath.replace('.tar.gz', '-import.tar.gz'); + try { + await fs.rename(currentPath, newPath); + console.log(`Fichier renommé de ${currentPath} à ${newPath}`); + } catch (error) { + console.error('Erreur lors du renommage du fichier :', error); + } + + console.log('Then import it'); + + // IMPORT IT + req.body = {}; + req.files = { + file: { + path: newPath, + type: mimeType + } + }; + + const { + duplicatedDocs, + importedAttachments, + exportPathId, + jobId, + notificationId + } = await importExportManager.import(req); + + req.body = { + docIds: duplicatedDocs.map(doc => doc.aposDocId), + importedAttachments, + exportPathId, + jobId, + notificationId + }; + + await importExportManager.overrideDuplicates(req); + + const NewUpdatedPage2 = await apos.doc.db + .find({ title: 'page2' }) + .toArray(); + console.log('NewUpdatedPage2 >>>', NewUpdatedPage2); + + // Check that the imported docs are not `null` + + assert.deepEqual(NewUpdatedPage2.some((doc) => { + console.log('doc.lastPublishedAt > ', doc.lastPublishedAt); + return doc.lastPublishedAt === null; + }), false, 'expected none of the imported docs to have a `lastPublishedAt` value of `null`'); + + // @TODO: Cleanup + // ... + }); + describe('#getFirstDifferentLocale', function() { it('should find among the docs the first locale that is different from the req one', async function() { const req = { @@ -1512,19 +1522,7 @@ async function insertPieces(apos) { } ], metaType: 'area' - }, - }); - - await apos.page.insert(req, '_home', 'lastChild', { - ...pageInstance, - title: 'page2', - type: 'default-page', - _articles: [], - main: { - _id: 'areaId', - items: [], - metaType: 'area' - }, + } }); } From ffc43b5c1f32f4021e4b215e63c69dd92871c106 Mon Sep 17 00:00:00 2001 From: itsrogil Date: Tue, 27 Feb 2024 14:53:47 +0100 Subject: [PATCH 7/7] UPDATE: Finished up work --- test/index.js | 62 ++++++++++----------------------------------------- 1 file changed, 12 insertions(+), 50 deletions(-) diff --git a/test/index.js b/test/index.js index 0f9a61c0..639fe634 100644 --- a/test/index.js +++ b/test/index.js @@ -586,6 +586,7 @@ describe('@apostrophecms/import-export', function () { const manager = apos.doc.getManager('default-page'); const pageInstance = manager.newInstance(); + // PUBLISH a new page await apos.page.insert(req, '_home', 'lastChild', { ...pageInstance, title: 'page2', @@ -597,11 +598,13 @@ describe('@apostrophecms/import-export', function () { metaType: 'area' } }); + const page2 = await apos.page.find(req, { title: 'page2' }).toObject(); - const publishedResult = await apos.page.publish(req, page2); + // UNPUBLISH (draft) the page const draftPage = await apos.page.unpublish(req, page2); + // EXPORT the page (as draft) req.body = { _ids: [ draftPage._id ], extension: 'gzip', @@ -612,49 +615,15 @@ describe('@apostrophecms/import-export', function () { const fileName = path.basename(url); pageTgzPath = path.join(exportsPath, fileName); - const exportPath = await gzip.input(pageTgzPath); - const { docs } = await getExtractedFiles(exportPath); - const exportedDraftPage = docs.find(doc => doc._id === draftPage._id); + // Now that it's exported as draft, PUBLISH the page again + const { lastPublishedAt } = await apos.page.publish(req, draftPage); - // @TODO Delete: - // Test exported doc - const exportedDoc = await apos.doc.db - .find({ - type: /default-page|article|topic|@apostrophecms\/image/, - title: 'page2' - }) - .toArray(); - // Last published at est bien à "null" et c'est bien un draft - - // Seems it correctly exported - // Now we PUBLISH it - const result = await apos.page.publish(req, draftPage); - - const updatedPage2 = await apos.doc.db - .find({ - title: 'page2', - aposMode: 'published' - }) - .toArray(); - - // Rename tar file for import issue - const currentPath = pageTgzPath; - const newPath = currentPath.replace('.tar.gz', '-import.tar.gz'); - try { - await fs.rename(currentPath, newPath); - console.log(`Fichier renommé de ${currentPath} à ${newPath}`); - } catch (error) { - console.error('Erreur lors du renommage du fichier :', error); - } - - console.log('Then import it'); - - // IMPORT IT + // Finally, IMPORT the previously exported draft page req.body = {}; req.files = { file: { - path: newPath, + path: pageTgzPath, type: mimeType } }; @@ -677,20 +646,13 @@ describe('@apostrophecms/import-export', function () { await importExportManager.overrideDuplicates(req); - const NewUpdatedPage2 = await apos.doc.db + const updatedPage = await apos.doc.db .find({ title: 'page2' }) .toArray(); - console.log('NewUpdatedPage2 >>>', NewUpdatedPage2); - - // Check that the imported docs are not `null` - - assert.deepEqual(NewUpdatedPage2.some((doc) => { - console.log('doc.lastPublishedAt > ', doc.lastPublishedAt); - return doc.lastPublishedAt === null; - }), false, 'expected none of the imported docs to have a `lastPublishedAt` value of `null`'); - // @TODO: Cleanup - // ... + assert.deepEqual(updatedPage.every((doc) => { + return String(doc.lastPublishedAt) === String(lastPublishedAt); + }), true, `expected imported docs 'lastPublishedAt' value to be of '${lastPublishedAt}'`); }); describe('#getFirstDifferentLocale', function() {