diff --git a/src/cli/cms/data/regex.js b/src/cli/cms/data/regex.js index 447d1f8..de5a4ee 100755 --- a/src/cli/cms/data/regex.js +++ b/src/cli/cms/data/regex.js @@ -95,4 +95,18 @@ export function getTagAbeWithTab(text, tab) { export function validDataAbe(str){ return str.replace(/\[([0-9]*)\]/g, '$1') +} + +export function getWorkflowFromOperationsUrl(str){ + let regUrl = /\/abe\/operations\/(.*?)\/(.*?)\// + var workflow = 'draft' + var match = str.match(regUrl) + if (match != null && match[1] != null) { + workflow = match[1] + } + var postUrl = str.replace(regUrl, '') + return { + workflow: workflow, + postUrl: postUrl + } } \ No newline at end of file diff --git a/src/cli/cms/editor/handlebars/listPage.js b/src/cli/cms/editor/handlebars/listPage.js index b1652ee..5616bdb 100755 --- a/src/cli/cms/editor/handlebars/listPage.js +++ b/src/cli/cms/editor/handlebars/listPage.js @@ -66,7 +66,7 @@ export default function listPage(file, index, text) {
` if(file.publish != null) { - res += ` @@ -74,7 +74,7 @@ export default function listPage(file, index, text) { ` } - res += ` { - router.post(`/abe/save/${workflow}/reject*`, operations.postReject) - router.post(`/abe/save/${workflow}/submit*`, operations.postSubmit) - router.post(`/abe/save/${workflow}/edit*`, operations.postSubmit) + router.get(`/abe/operations/${workflow}/unpublish*`, operations.getUnpublish) + router.get(`/abe/operations/${workflow}/delete*`, operations.getDelete) + router.post(`/abe/operations/${workflow}/reject*`, operations.postReject) + router.post(`/abe/operations/${workflow}/submit*`, operations.postSubmit) + router.post(`/abe/operations/${workflow}/edit*`, operations.postSubmit) }) var routes = abeExtend.plugins.instance.getRoutes() diff --git a/src/server/routes/index.js b/src/server/routes/index.js index b341ddd..2f968a1 100644 --- a/src/server/routes/index.js +++ b/src/server/routes/index.js @@ -9,8 +9,6 @@ import getPage from './get-page' import postPage from './post-page' import getGeneratePost from './get-generate-posts' import getSaveConfig from './get-save-config' -import getUnpublish from './get-unpublish' -import getDelete from './get-delete' import postUpload from './post-upload' import postSqlRequest from './post-sql-request' import postReference from './post-reference' @@ -33,8 +31,6 @@ export { postPage, getGeneratePost, getSaveConfig, - getUnpublish, - getDelete, postUpload, postSqlRequest, postReference, diff --git a/src/server/routes/get-delete.js b/src/server/routes/operations/get/delete.js similarity index 56% rename from src/server/routes/get-delete.js rename to src/server/routes/operations/get/delete.js index 1b0b499..ba5e91c 100644 --- a/src/server/routes/get-delete.js +++ b/src/server/routes/operations/get/delete.js @@ -1,19 +1,20 @@ import { - abeExtend, - cmsOperations -} from '../../cli' + abeExtend + ,cmsOperations + ,cmsData +} from '../../../../cli' var route = function(req, res, next){ abeExtend.hooks.instance.trigger('beforeRoute', req, res, next) if(typeof res._header !== 'undefined' && res._header !== null) return - var filepath = req.originalUrl.replace('/abe/delete', '') + var operation = cmsData.regex.getWorkflowFromOperationsUrl(req.originalUrl) - cmsOperations.remove.remove(filepath) + cmsOperations.remove.remove(operation.postUrl) var result = { success: 1, - file: filepath + file: operation.postUrl } res.set('Content-Type', 'application/json') res.send(JSON.stringify(result)) diff --git a/src/server/routes/get-unpublish.js b/src/server/routes/operations/get/unpublish.js similarity index 61% rename from src/server/routes/get-unpublish.js rename to src/server/routes/operations/get/unpublish.js index 3e14ecc..750084e 100644 --- a/src/server/routes/get-unpublish.js +++ b/src/server/routes/operations/get/unpublish.js @@ -1,18 +1,20 @@ import { cmsOperations ,abeExtend -} from '../../cli' + ,cmsData +} from '../../../../cli' var route = function(req, res, next){ abeExtend.hooks.instance.trigger('beforeRoute', req, res, next) if(typeof res._header !== 'undefined' && res._header !== null) return - var filepath = req.originalUrl.replace('/abe/unpublish', '') - cmsOperations.post.unpublish(filepath) + var operation = cmsData.regex.getWorkflowFromOperationsUrl(req.originalUrl) + + cmsOperations.post.unpublish(operation.postUrl) var result = { success: 1, - file: filepath + file: operation.postUrl } res.set('Content-Type', 'application/json') res.send(JSON.stringify(result)) diff --git a/src/server/routes/operations/index.js b/src/server/routes/operations/index.js index c8212ef..c8215a7 100644 --- a/src/server/routes/operations/index.js +++ b/src/server/routes/operations/index.js @@ -1,7 +1,11 @@ +import getUnpublish from './get/unpublish' +import getDelete from './get/delete' import postSubmit from './post/submit' import postReject from './post/reject' export { postSubmit ,postReject + ,getUnpublish + ,getDelete } \ No newline at end of file diff --git a/src/server/routes/operations/post/edit.js b/src/server/routes/operations/post/edit.js deleted file mode 100644 index ce7435c..0000000 --- a/src/server/routes/operations/post/edit.js +++ /dev/null @@ -1,45 +0,0 @@ -import { - cmsOperations, - abeExtend -} from '../../../../cli' - -var route = function(req, res, next){ - abeExtend.hooks.instance.trigger('beforeRoute', req, res, next) - if(typeof res._header !== 'undefined' && res._header !== null) return - - let regUrl = /\/abe\/save\/(.*?)\/edit\// - var workflow = 'draft' - var match = req.originalUrl.match(regUrl) - if (match != null && match[1] != null) { - workflow = match[1] - } - var postUrl = req.originalUrl.replace(regUrl, '') - var json = req.body.json - - var p - if (workflow === 'publish') { - p = cmsOperations.post.publish( - postUrl, - json - ) - }else { - p = cmsOperations.post.draft( - postUrl, - json, - workflow - ) - } - - p.then((result) => { - res.set('Content-Type', 'application/json') - res.send(JSON.stringify(result)) - }, - (result) => { - res.set('Content-Type', 'application/json') - res.send(JSON.stringify(result)) - }).catch(function(e) { - console.error('[ERROR] post-reject.js', e) - }) -} - -export default route \ No newline at end of file diff --git a/src/server/routes/operations/post/reject.js b/src/server/routes/operations/post/reject.js index 804ad9d..349a1cf 100644 --- a/src/server/routes/operations/post/reject.js +++ b/src/server/routes/operations/post/reject.js @@ -1,24 +1,19 @@ import { - cmsOperations, - abeExtend + cmsOperations + ,abeExtend + ,cmsData } from '../../../../cli' var route = function(req, res, next){ abeExtend.hooks.instance.trigger('beforeRoute', req, res, next) if(typeof res._header !== 'undefined' && res._header !== null) return - let regUrl = /\/abe\/save\/(.*?)\/reject\// - var workflow = 'draft' - var match = req.originalUrl.match(regUrl) - if (match != null && match[1] != null) { - workflow = match[1] - } - var postUrl = req.originalUrl.replace(regUrl, '') - + var operation = cmsData.regex.getWorkflowFromOperationsUrl(req.originalUrl) + var p = cmsOperations.post.reject( - postUrl, + operation.postUrl, req.body.json, - workflow + operation.workflow ) p.then((result) => { diff --git a/src/server/routes/operations/post/submit.js b/src/server/routes/operations/post/submit.js index e1f0dea..668fe05 100644 --- a/src/server/routes/operations/post/submit.js +++ b/src/server/routes/operations/post/submit.js @@ -1,32 +1,26 @@ import { - cmsOperations, - abeExtend + cmsOperations + ,abeExtend + ,cmsData } from '../../../../cli' var route = function(req, res, next){ abeExtend.hooks.instance.trigger('beforeRoute', req, res, next) if(typeof res._header !== 'undefined' && res._header !== null) return - let regUrl = /\/abe\/save\/(.*?)\/submit\// - var workflow = 'draft' - var match = req.originalUrl.match(regUrl) - if (match != null && match[1] != null) { - workflow = match[1] - } - var postUrl = req.originalUrl.replace(regUrl, '') - var json = req.body.json - + var operation = cmsData.regex.getWorkflowFromOperationsUrl(req.originalUrl) + var p - if (workflow === 'publish') { + if (operation.workflow === 'publish') { p = cmsOperations.post.publish( - postUrl, - json + operation.postUrl, + req.body.json ) }else { p = cmsOperations.post.draft( - postUrl, - json, - workflow + operation.postUrl, + req.body.json, + operation.workflow ) } diff --git a/test/regex.js b/test/cms/data/regex.js similarity index 76% rename from test/regex.js rename to test/cms/data/regex.js index bc47c50..17ad173 100644 --- a/test/regex.js +++ b/test/cms/data/regex.js @@ -1,22 +1,22 @@ var chai = require('chai'); var path = require('path'); -var config = require('../src/cli').config -config.set({root: path.join(__dirname,'fixtures')}) +var config = require('../../../src/cli').config +config.set({root: path.join(process.cwd(), 'test', 'fixtures')}) -var cmsData = require('../src/cli').cmsData -var Manager = require('../src/cli').Manager; +var cmsData = require('../../../src/cli').cmsData +var Manager = require('../../../src/cli').Manager; var fse = require('fs-extra'); -describe('Request', function() { +describe('regex', function() { before( function(done) { Manager.instance.init() .then(function () { this.fixture = { - articleSingle: fse.readFileSync(path.join(__dirname, 'fixtures', 'templates', 'article-single-abe.html'), 'utf8'), - articleEach: fse.readFileSync(path.join(__dirname, 'fixtures', 'templates', 'article-each-abe.html'), 'utf8'), - articleRequest: fse.readFileSync(path.join(__dirname, 'fixtures', 'templates', 'article-request.html'), 'utf8') + articleSingle: fse.readFileSync(path.join(process.cwd(), 'test', 'fixtures', 'templates', 'article-single-abe.html'), 'utf8'), + articleEach: fse.readFileSync(path.join(process.cwd(), 'test', 'fixtures', 'templates', 'article-each-abe.html'), 'utf8'), + articleRequest: fse.readFileSync(path.join(process.cwd(), 'test', 'fixtures', 'templates', 'article-request.html'), 'utf8') } done() diff --git a/test/cms/operations/create.js b/test/cms/operations/create.js new file mode 100644 index 0000000..8dc9e0b --- /dev/null +++ b/test/cms/operations/create.js @@ -0,0 +1,74 @@ +var chai = require('chai'); +var sinonChai = require('sinon-chai') +var expect = chai.expect +chai.use(sinonChai) +var sinon = require('sinon'); +var path = require('path'); +var fse = require('fs-extra'); + +var config = require('../../../src/cli').config +config.set({root: path.join(process.cwd(), 'test','fixtures')}) + +var abeExtend = require('../../../src/cli').abeExtend +var cmsData = require('../../../src/cli').cmsData +var Manager = require('../../../src/cli').Manager +var coreUtils = require('../../../src/cli').coreUtils +var cmsOperations = require('../../../src/cli').cmsOperations +var cmsTemplates = require('../../../src/cli').cmsTemplates +var Manager = require('../../../src/cli').Manager; +var Page = require('../../../src/cli').Page; + +describe('cmsOperations', function() { + before( function(done) { + Manager.instance.init() + .then(function () { + Manager.instance._whereKeys = ['title', 'priority', 'abe_meta', 'articles'] + Manager.instance.updateList() + + this.fixture = { + htmlArticle: fse.readFileSync(path.join(process.cwd(), 'test', 'fixtures', 'templates', 'article.html'), 'utf8'), + jsonArticle: fse.readJsonSync(path.join(process.cwd(), 'test', 'fixtures', 'files', 'article-2.json')), + jsonHomepage: fse.readJsonSync(path.join(process.cwd(), 'test', 'fixtures', 'data', 'homepage-1.json')) + } + done() + + }.bind(this)) + }); + + it('cmsOperations.create()', function(done) { + // stub + var s = sinon.sandbox.create(); + s.stub(abeExtend.hooks.instance, 'trigger', function (str, obj, body, json) { + if (str == 'beforeFirstSave') { + return { + postUrl: obj, + json: json + } + } + return str, obj; + }.bind(this)); + s.stub(coreUtils.slug, 'clean', function (p) { return p; }.bind(this)); + s.stub(Manager.instance, 'postExist', function (p) { return false; }.bind(this)); + s.stub(cmsData.metas, 'create', function (json, template, postUrl) { return json; }.bind(this)); + s.stub(cmsTemplates.template, 'getTemplate', function () { return this.fixture.htmlArticle; }.bind(this)); + s.stub(cmsData.values, 'removeDuplicate', function (templateText, json) { return json; }.bind(this)); + s.stub(cmsOperations.post, 'draft', function () { + return Promise.resolve({json: JSON.parse(JSON.stringify(this.fixture.jsonArticle))}) + }.bind(this)); + + cmsOperations.create('article', '', 'article-2.html', {query: ''}, JSON.parse(JSON.stringify(this.fixture.jsonArticle)), false) + .then(function(resSave) { + var json = path.join(config.root, config.data.url, resSave.abe_meta.latest.abeUrl.replace('.html', '.json')) + + abeExtend.hooks.instance.trigger.restore() + coreUtils.slug.clean.restore() + Manager.instance.postExist.restore() + cmsData.metas.create.restore() + cmsTemplates.template.getTemplate.restore() + cmsData.values.removeDuplicate.restore() + cmsOperations.post.draft.restore() + + done() + }.bind(this)); + }); +}); diff --git a/test/cms/operations/duplicate.js b/test/cms/operations/duplicate.js new file mode 100644 index 0000000..3bfbe17 --- /dev/null +++ b/test/cms/operations/duplicate.js @@ -0,0 +1,78 @@ +var chai = require('chai'); +var sinonChai = require('sinon-chai') +var expect = chai.expect +chai.use(sinonChai) +var sinon = require('sinon'); +var path = require('path'); +var fse = require('fs-extra'); + +var config = require('../../../src/cli').config +config.set({root: path.join(process.cwd(), 'test','fixtures')}) + +var abeExtend = require('../../../src/cli').abeExtend +var cmsData = require('../../../src/cli').cmsData +var Manager = require('../../../src/cli').Manager +var coreUtils = require('../../../src/cli').coreUtils +var cmsOperations = require('../../../src/cli').cmsOperations +var cmsTemplates = require('../../../src/cli').cmsTemplates +var Manager = require('../../../src/cli').Manager; +var Page = require('../../../src/cli').Page; + +describe('cmsOperations', function() { + before( function(done) { + Manager.instance.init() + .then(function () { + Manager.instance._whereKeys = ['title', 'priority', 'abe_meta', 'articles'] + Manager.instance.updateList() + + this.fixture = { + htmlArticle: fse.readFileSync(path.join(process.cwd(), 'test', 'fixtures', 'templates', 'article.html'), 'utf8'), + jsonArticle: fse.readJsonSync(path.join(process.cwd(), 'test', 'fixtures', 'files', 'article-2.json')), + jsonHomepage: fse.readJsonSync(path.join(process.cwd(), 'test', 'fixtures', 'data', 'homepage-1.json')) + } + done() + + }.bind(this)) + }); + + it('cmsOperations.duplicate()', function(done) { + // stub + var s = sinon.sandbox.create(); + s.stub(abeExtend.hooks.instance, 'trigger', function (str, obj) { return str, obj; }.bind(this)); + s.stub(Manager.instance, 'getList', function (str, obj) { return [this.fixture.jsonArticle]; }.bind(this)); + s.stub(coreUtils.slug, 'clean', function (p) { return p; }.bind(this)); + s.stub(coreUtils.array, 'filter', function () { return [this.fixture.jsonArticle]; }.bind(this)); + s.stub(cmsData.file, 'get', function () { return this.fixture.jsonArticle; }.bind(this)); + s.stub(cmsOperations, 'create', function () { return Promise.resolve(this.fixture.jsonArticle); }.bind(this)); + s.stub(cmsOperations.remove, 'remove', function () { return null; }.bind(this)); + + // test + var newPostUrl = 'article-2.html' + cmsOperations.duplicate('article-1.html', 'article', '', newPostUrl, {}, false) + .then(function(resSave) { + chai.expect(resSave.abe_meta).to.not.be.undefined; + chai.expect(resSave.abe_meta.link).to.be.equal('/article-2.html'); + + cmsOperations.duplicate('article-1.html', 'article', '', newPostUrl, {}, true) + .then(function(resSave2) { + chai.expect(resSave2.abe_meta).to.not.be.undefined; + chai.expect(resSave2.abe_meta.link).to.be.equal('/article-2.html'); + + // unstub + abeExtend.hooks.instance.trigger.restore() + sinon.assert.calledTwice(Manager.instance.getList) + Manager.instance.getList.restore() + sinon.assert.calledTwice(coreUtils.slug.clean) + coreUtils.slug.clean.restore() + sinon.assert.calledTwice(coreUtils.array.filter) + coreUtils.array.filter.restore() + cmsData.file.get.restore() + sinon.assert.calledTwice(cmsOperations.create) + cmsOperations.create.restore() + sinon.assert.calledOnce(cmsOperations.remove.remove) + cmsOperations.remove.remove.restore() + done() + }.bind(this)) + }.bind(this)) + }); +}); diff --git a/test/cms/operations/post.js b/test/cms/operations/post.js new file mode 100644 index 0000000..3527cde --- /dev/null +++ b/test/cms/operations/post.js @@ -0,0 +1,173 @@ +var chai = require('chai'); +var sinonChai = require('sinon-chai') +var expect = chai.expect +chai.use(sinonChai) +var sinon = require('sinon'); +var path = require('path'); +var fse = require('fs-extra'); + +var config = require('../../../src/cli').config +config.set({root: path.join(process.cwd(), 'test','fixtures')}) + +var abeExtend = require('../../../src/cli').abeExtend +var cmsData = require('../../../src/cli').cmsData +var Manager = require('../../../src/cli').Manager +var coreUtils = require('../../../src/cli').coreUtils +var cmsOperations = require('../../../src/cli').cmsOperations +var cmsTemplates = require('../../../src/cli').cmsTemplates +var Manager = require('../../../src/cli').Manager; +var Page = require('../../../src/cli').Page; + +describe('cmsOperations', function() { + before( function(done) { + Manager.instance.init() + .then(function () { + Manager.instance._whereKeys = ['title', 'priority', 'abe_meta', 'articles'] + Manager.instance.updateList() + + this.fixture = { + htmlArticle: fse.readFileSync(path.join(process.cwd(), 'test', 'fixtures', 'templates', 'article.html'), 'utf8'), + jsonArticle: fse.readJsonSync(path.join(process.cwd(), 'test', 'fixtures', 'files', 'article-2.json')), + jsonHomepage: fse.readJsonSync(path.join(process.cwd(), 'test', 'fixtures', 'data', 'homepage-1.json')) + } + done() + + }.bind(this)) + }); + + /** + * cmsOperations.post.publish + * + */ + it('cmsOperations.post.publish()', function(done) { + // stub + var s = sinon.sandbox.create(); + s.stub(abeExtend.hooks.instance, 'trigger', function (str, obj) { return str, obj; }.bind(this)); + s.stub(cmsTemplates.template, 'getTemplate', function () { return this.fixture.htmlArticle; }.bind(this)); + s.stub(cmsData.source, 'getDataList', function () { + return Promise.resolve(JSON.parse(JSON.stringify(this.fixture.jsonArticle))) + }.bind(this)); + s.stub(cmsData.utils, 'getPercentOfRequiredTagsFilled', function () { return 100; }.bind(this)); + // s.stub(Page, 'getPercentOfRequiredTagsFilled', function () { return 100; }.bind(this)); + s.stub(cmsOperations.save, 'saveHtml', function () { return 100; }.bind(this)); + s.stub(cmsOperations.save, 'saveJson', function () { return 100; }.bind(this)); + s.stub(Manager.instance, 'updatePostInList', function () { return null; }.bind(this)); + + // test + cmsOperations.post.publish('article-2.html', JSON.parse(JSON.stringify(this.fixture.jsonArticle))) + .then(function(resSave) { + // unstub + abeExtend.hooks.instance.trigger.restore() + cmsTemplates.template.getTemplate.restore() + cmsData.source.getDataList.restore() + cmsData.utils.getPercentOfRequiredTagsFilled.restore() + cmsOperations.save.saveHtml.restore() + cmsOperations.save.saveJson.restore() + Manager.instance.updatePostInList.restore() + done() + }.bind(this)); + }); + + /** + * cmsOperations.post.unpublish + * + */ + it('cmsOperations.post.unpublish()', function(done) { + // stub + var s = sinon.sandbox.create(); + s.stub(abeExtend.hooks.instance, 'trigger', function (str, obj) { return str, obj; }.bind(this)); + s.stub(coreUtils.file, 'exist', function (revisionPath) { return true; }.bind(this)); + s.stub(cmsData.file, 'get', function () { return JSON.parse(JSON.stringify(this.fixture.jsonArticle)); }.bind(this)); + s.stub(cmsOperations.post, 'draft', function () { + return Promise.resolve({json: JSON.parse(JSON.stringify(this.fixture.jsonArticle))}) + }.bind(this)); + s.stub(cmsOperations.remove, 'removeFile', function () { return null; }.bind(this)); + s.stub(Manager.instance, 'updatePostInList', function () { return null; }.bind(this)); + + // test + cmsOperations.post.unpublish('article-2.html') + .then(function(resSave) { + + // unstub + abeExtend.hooks.instance.trigger.restore() + coreUtils.file.exist.restore() + cmsData.file.get.restore() + cmsOperations.post.draft.restore() + cmsOperations.remove.removeFile.restore() + Manager.instance.updatePostInList.restore() + done() + }.bind(this)); + }); + + /** + * cmsOperations.post.draft + * + */ + it('cmsOperations.post.draft()', function(done) { + var json = JSON.parse(JSON.stringify(this.fixture.jsonArticle)) + var meta = json.abe_meta + delete json.abe_meta + + // stub + var s = sinon.sandbox.create(); + s.stub(abeExtend.hooks.instance, 'trigger', function (str, obj) { return str, obj; }.bind(this)); + s.stub(coreUtils.file, 'addDateIsoToRevisionPath', function (revisionPath) { return revisionPath; }.bind(this)); + s.stub(cmsData.metas, 'add', function (json) { + json.abe_meta = meta + return json; + }.bind(this)); + s.stub(cmsTemplates.template, 'getTemplate', function () { return this.fixture.htmlArticle; }.bind(this)); + s.stub(cmsData.source, 'getDataList', function () { + return Promise.resolve(JSON.parse(JSON.stringify(this.fixture.jsonArticle))) + }.bind(this)); + s.stub(cmsOperations.save, 'saveJson', function () { return true; }.bind(this)); + s.stub(Manager.instance, 'updatePostInList', function () { return null; }.bind(this)); + s.stub(cmsData.utils, 'getPercentOfRequiredTagsFilled', function () { return 100; }.bind(this)); + + // test + cmsOperations.post.draft('article-2.html', JSON.parse(JSON.stringify(this.fixture.jsonArticle))) + .then(function(resSave) { + chai.expect(resSave.success).to.be.equal(1); + chai.expect(resSave.json.abe_meta).to.not.be.undefined; + + // unstub + abeExtend.hooks.instance.trigger.restore() + coreUtils.file.addDateIsoToRevisionPath.restore() + cmsData.utils.getPercentOfRequiredTagsFilled.restore() + cmsData.metas.add.restore() + cmsTemplates.template.getTemplate.restore() + cmsData.source.getDataList.restore() + cmsOperations.save.saveJson.restore() + Manager.instance.updatePostInList.restore() + + done() + }.bind(this)); + }); + + /** + * cmsOperations.post.reject + * + */ + it('cmsOperations.post.reject()', function(done) { + // stub + var s = sinon.sandbox.create(); + s.stub(abeExtend.hooks.instance, 'trigger', function (str, obj) { return str, obj; }.bind(this)); + s.stub(cmsOperations.post, 'draft', function (filePath, json, rejectToWorkflow) { + chai.expect(rejectToWorkflow).to.be.equal("draft"); + return Promise.resolve(this.fixture.jsonArticle); + }.bind(this)); + + // test + var json = JSON.parse(JSON.stringify(this.fixture.jsonArticle)) + json.abe_meta.status = 'publish' + cmsOperations.post.reject('article-2.html', json) + .then(function(resSave) { + chai.expect(resSave.abe_meta).to.not.be.undefined; + + // unstub + abeExtend.hooks.instance.trigger.restore() + cmsOperations.post.draft.restore() + done() + }.bind(this)); + }); +}); diff --git a/test/fixtures/files/article-2.json b/test/fixtures/files/article-2.json index f1c44f5..45009d2 100644 --- a/test/fixtures/files/article-2.json +++ b/test/fixtures/files/article-2.json @@ -4,7 +4,10 @@ "link": "/article-2.html", "status": "publish", "date": "2016-11-11T16:40:41.974Z", - "latest": {"date": "2016-11-11T16:40:41.974Z"} + "latest": { + "date": "2016-11-11T16:40:41.974Z", + "abeUrl": "/article-2.html" + } }, "priority":2, "title":"article 2" diff --git a/test/operations.js b/test/operations.js index c7c6342..f2477ca 100644 --- a/test/operations.js +++ b/test/operations.js @@ -1,12 +1,22 @@ var chai = require('chai'); +var sinonChai = require('sinon-chai') +var expect = chai.expect +chai.use(sinonChai) +var sinon = require('sinon'); var path = require('path'); +var fse = require('fs-extra'); var config = require('../src/cli').config -config.set({root: path.join(__dirname,'fixtures')}) +config.set({root: path.join(process.cwd(), 'test','fixtures')}) +var abeExtend = require('../src/cli').abeExtend +var cmsData = require('../src/cli').cmsData +var Manager = require('../src/cli').Manager +var coreUtils = require('../src/cli').coreUtils var cmsOperations = require('../src/cli').cmsOperations +var cmsTemplates = require('../src/cli').cmsTemplates var Manager = require('../src/cli').Manager; -var fse = require('fs-extra'); +var Page = require('../src/cli').Page; describe('cmsOperations', function() { before( function(done) { @@ -16,8 +26,9 @@ describe('cmsOperations', function() { Manager.instance.updateList() this.fixture = { - jsonArticle: fse.readJsonSync(path.join(__dirname, '/fixtures/files/article-2.json')), - jsonHomepage: fse.readJsonSync(path.join(__dirname, '/fixtures/data/homepage-1.json')) + htmlArticle: fse.readFileSync(path.join(process.cwd(), 'test', 'fixtures', 'templates', 'article.html'), 'utf8'), + jsonArticle: fse.readJsonSync(path.join(process.cwd(), 'test', 'fixtures', 'files', 'article-2.json')), + jsonHomepage: fse.readJsonSync(path.join(process.cwd(), 'test', 'fixtures', 'data', 'homepage-1.json')) } done() @@ -25,14 +36,38 @@ describe('cmsOperations', function() { }); it('cmsOperations.create()', function(done) { + // stub + var s = sinon.sandbox.create(); + s.stub(abeExtend.hooks.instance, 'trigger', function (str, obj, body, json) { + if (str == 'beforeFirstSave') { + return { + postUrl: obj, + json: json + } + } + return str, obj; + }.bind(this)); + s.stub(coreUtils.slug, 'clean', function (p) { return p; }.bind(this)); + s.stub(Manager.instance, 'postExist', function (p) { return false; }.bind(this)); + s.stub(cmsData.metas, 'create', function (json, template, postUrl) { return json; }.bind(this)); + s.stub(cmsTemplates.template, 'getTemplate', function () { return this.fixture.htmlArticle; }.bind(this)); + s.stub(cmsData.values, 'removeDuplicate', function (templateText, json) { return json; }.bind(this)); + s.stub(cmsOperations.post, 'draft', function () { + return Promise.resolve({json: JSON.parse(JSON.stringify(this.fixture.jsonArticle))}) + }.bind(this)); + cmsOperations.create('article', '', 'article-2.html', {query: ''}, JSON.parse(JSON.stringify(this.fixture.jsonArticle)), false) .then(function(resSave) { var json = path.join(config.root, config.data.url, resSave.abe_meta.latest.abeUrl.replace('.html', '.json')) - var stat = fse.statSync(json) - if (stat) { - chai.expect(stat).to.not.be.undefined; - } - fse.removeSync(json) + + abeExtend.hooks.instance.trigger.restore() + coreUtils.slug.clean.restore() + Manager.instance.postExist.restore() + cmsData.metas.create.restore() + cmsTemplates.template.getTemplate.restore() + cmsData.values.removeDuplicate.restore() + cmsOperations.post.draft.restore() + done() }.bind(this)); }); @@ -42,22 +77,31 @@ describe('cmsOperations', function() { * */ it('cmsOperations.post.publish()', function(done) { + // stub + var s = sinon.sandbox.create(); + s.stub(abeExtend.hooks.instance, 'trigger', function (str, obj) { return str, obj; }.bind(this)); + s.stub(cmsTemplates.template, 'getTemplate', function () { return this.fixture.htmlArticle; }.bind(this)); + s.stub(cmsData.source, 'getDataList', function () { + return Promise.resolve(JSON.parse(JSON.stringify(this.fixture.jsonArticle))) + }.bind(this)); + s.stub(cmsData.utils, 'getPercentOfRequiredTagsFilled', function () { return 100; }.bind(this)); + // s.stub(Page, 'getPercentOfRequiredTagsFilled', function () { return 100; }.bind(this)); + s.stub(cmsOperations.save, 'saveHtml', function () { return 100; }.bind(this)); + s.stub(cmsOperations.save, 'saveJson', function () { return 100; }.bind(this)); + s.stub(Manager.instance, 'updatePostInList', function () { return null; }.bind(this)); + + // test cmsOperations.post.publish('article-2.html', JSON.parse(JSON.stringify(this.fixture.jsonArticle))) .then(function(resSave) { - var json = path.join(config.root, config.data.url, resSave.json.abe_meta.latest.abeUrl.replace('.html', '.json')) - var stat = fse.statSync(json) - if (stat) { - chai.expect(stat).to.not.be.undefined; - } - fse.removeSync(json) - - var html = path.join(config.root, config.publish.url, resSave.json.abe_meta.link) - var stat = fse.statSync(html) - if (stat) { - chai.expect(stat).to.not.be.undefined; - } - fse.removeSync(html) - done() + // unstub + abeExtend.hooks.instance.trigger.restore() + cmsTemplates.template.getTemplate.restore() + cmsData.source.getDataList.restore() + cmsData.utils.getPercentOfRequiredTagsFilled.restore() + cmsOperations.save.saveHtml.restore() + cmsOperations.save.saveJson.restore() + Manager.instance.updatePostInList.restore() + done() }.bind(this)); }); @@ -66,31 +110,29 @@ describe('cmsOperations', function() { * */ it('cmsOperations.post.unpublish()', function(done) { - cmsOperations.post.publish('article-2.html', JSON.parse(JSON.stringify(this.fixture.jsonArticle))) + // stub + var s = sinon.sandbox.create(); + s.stub(abeExtend.hooks.instance, 'trigger', function (str, obj) { return str, obj; }.bind(this)); + s.stub(coreUtils.file, 'exist', function (revisionPath) { return true; }.bind(this)); + s.stub(cmsData.file, 'get', function () { return JSON.parse(JSON.stringify(this.fixture.jsonArticle)); }.bind(this)); + s.stub(cmsOperations.post, 'draft', function () { + return Promise.resolve({json: JSON.parse(JSON.stringify(this.fixture.jsonArticle))}) + }.bind(this)); + s.stub(cmsOperations.remove, 'removeFile', function () { return null; }.bind(this)); + s.stub(Manager.instance, 'updatePostInList', function () { return null; }.bind(this)); + + // test + cmsOperations.post.unpublish('article-2.html') .then(function(resSave) { - var json = path.join(config.root, config.data.url, resSave.json.abe_meta.latest.abeUrl.replace('.html', '.json')) - var html = path.join(config.root, config.publish.url, resSave.json.abe_meta.link) - cmsOperations.post.unpublish('article-2.html') - .then(function(resSave2) { - var stat - try { - var stat = fse.statSync(json) - }catch(e) { - chai.expect(stat).to.be.undefined; - } - try { - var stat = fse.statSync(html) - }catch(e) { - chai.expect(stat).to.be.undefined; - } - json = path.join(config.root, config.data.url, resSave2.json.abe_meta.latest.abeUrl.replace('.html', '.json')) - var stat = fse.statSync(json) - if (stat) { - chai.expect(stat).to.not.be.undefined; - } - fse.removeSync(json) - done() - }.bind(this)); + + // unstub + abeExtend.hooks.instance.trigger.restore() + coreUtils.file.exist.restore() + cmsData.file.get.restore() + cmsOperations.post.draft.restore() + cmsOperations.remove.removeFile.restore() + Manager.instance.updatePostInList.restore() + done() }.bind(this)); }); @@ -99,14 +141,42 @@ describe('cmsOperations', function() { * */ it('cmsOperations.post.draft()', function(done) { + var json = JSON.parse(JSON.stringify(this.fixture.jsonArticle)) + var meta = json.abe_meta + delete json.abe_meta + + // stub + var s = sinon.sandbox.create(); + s.stub(abeExtend.hooks.instance, 'trigger', function (str, obj) { return str, obj; }.bind(this)); + s.stub(coreUtils.file, 'addDateIsoToRevisionPath', function (revisionPath) { return revisionPath; }.bind(this)); + s.stub(cmsData.metas, 'add', function (json) { + json.abe_meta = meta + return json; + }.bind(this)); + s.stub(cmsTemplates.template, 'getTemplate', function () { return this.fixture.htmlArticle; }.bind(this)); + s.stub(cmsData.source, 'getDataList', function () { + return Promise.resolve(JSON.parse(JSON.stringify(this.fixture.jsonArticle))) + }.bind(this)); + s.stub(cmsOperations.save, 'saveJson', function () { return true; }.bind(this)); + s.stub(Manager.instance, 'updatePostInList', function () { return null; }.bind(this)); + s.stub(cmsData.utils, 'getPercentOfRequiredTagsFilled', function () { return 100; }.bind(this)); + + // test cmsOperations.post.draft('article-2.html', JSON.parse(JSON.stringify(this.fixture.jsonArticle))) .then(function(resSave) { - var json = path.join(config.root, config.data.url, resSave.json.abe_meta.latest.abeUrl.replace('.html', '.json')) - var stat = fse.statSync(json) - if (stat) { - chai.expect(stat).to.not.be.undefined; - } - fse.removeSync(json) + chai.expect(resSave.success).to.be.equal(1); + chai.expect(resSave.json.abe_meta).to.not.be.undefined; + + // unstub + abeExtend.hooks.instance.trigger.restore() + coreUtils.file.addDateIsoToRevisionPath.restore() + cmsData.utils.getPercentOfRequiredTagsFilled.restore() + cmsData.metas.add.restore() + cmsTemplates.template.getTemplate.restore() + cmsData.source.getDataList.restore() + cmsOperations.save.saveJson.restore() + Manager.instance.updatePostInList.restore() + done() }.bind(this)); }); @@ -116,41 +186,66 @@ describe('cmsOperations', function() { * */ it('cmsOperations.post.reject()', function(done) { - cmsOperations.post.reject('article-2.html', JSON.parse(JSON.stringify(this.fixture.jsonArticle))) + // stub + var s = sinon.sandbox.create(); + s.stub(abeExtend.hooks.instance, 'trigger', function (str, obj) { return str, obj; }.bind(this)); + s.stub(cmsOperations.post, 'draft', function (filePath, json, rejectToWorkflow) { + chai.expect(rejectToWorkflow).to.be.equal("draft"); + return Promise.resolve(this.fixture.jsonArticle); + }.bind(this)); + + // test + var json = JSON.parse(JSON.stringify(this.fixture.jsonArticle)) + json.abe_meta.status = 'publish' + cmsOperations.post.reject('article-2.html', json) .then(function(resSave) { - var json = path.join(config.root, config.data.url, resSave.json.abe_meta.latest.abeUrl.replace('.html', '.json')) - var stat = fse.statSync(json) - if (stat) { - chai.expect(stat).to.not.be.undefined; - } - fse.removeSync(json) + chai.expect(resSave.abe_meta).to.not.be.undefined; + + // unstub + abeExtend.hooks.instance.trigger.restore() + cmsOperations.post.draft.restore() done() }.bind(this)); }); it('cmsOperations.duplicate()', function(done) { - cmsOperations.duplicate('article-1.html', 'article', '', 'article-2.html', {}, false) - .then(function(resSave) { - var json = path.join(config.root, config.data.url, resSave.abe_meta.latest.abeUrl.replace('.html', '.json')) - var stat = fse.statSync(json) - if (stat) { - chai.expect(stat).to.not.be.undefined; - } - fse.removeSync(json) - done() - }.bind(this)) - }); + // stub + var s = sinon.sandbox.create(); + s.stub(abeExtend.hooks.instance, 'trigger', function (str, obj) { return str, obj; }.bind(this)); + s.stub(Manager.instance, 'getList', function (str, obj) { return [this.fixture.jsonArticle]; }.bind(this)); + s.stub(coreUtils.slug, 'clean', function (p) { return p; }.bind(this)); + s.stub(coreUtils.array, 'filter', function () { return [this.fixture.jsonArticle]; }.bind(this)); + s.stub(cmsData.file, 'get', function () { return this.fixture.jsonArticle; }.bind(this)); + s.stub(cmsOperations, 'create', function () { return Promise.resolve(this.fixture.jsonArticle); }.bind(this)); + s.stub(cmsOperations.remove, 'remove', function () { return null; }.bind(this)); - it('cmsOperations.duplicate() update', function(done) { - cmsOperations.duplicate('article-1.html', 'article', '', 'article-1.html', {}, true) + // test + var newPostUrl = 'article-2.html' + cmsOperations.duplicate('article-1.html', 'article', '', newPostUrl, {}, false) .then(function(resSave) { - var json = path.join(config.root, config.data.url, resSave.abe_meta.latest.abeUrl.replace('.html', '.json')) - var stat = fse.statSync(json) - if (stat) { - chai.expect(stat).to.not.be.undefined; - } - fse.removeSync(json) - done() + chai.expect(resSave.abe_meta).to.not.be.undefined; + chai.expect(resSave.abe_meta.link).to.be.equal('/article-2.html'); + + cmsOperations.duplicate('article-1.html', 'article', '', newPostUrl, {}, true) + .then(function(resSave2) { + chai.expect(resSave2.abe_meta).to.not.be.undefined; + chai.expect(resSave2.abe_meta.link).to.be.equal('/article-2.html'); + + // unstub + abeExtend.hooks.instance.trigger.restore() + sinon.assert.calledTwice(Manager.instance.getList) + Manager.instance.getList.restore() + sinon.assert.calledTwice(coreUtils.slug.clean) + coreUtils.slug.clean.restore() + sinon.assert.calledTwice(coreUtils.array.filter) + coreUtils.array.filter.restore() + cmsData.file.get.restore() + sinon.assert.calledTwice(cmsOperations.create) + cmsOperations.create.restore() + sinon.assert.calledOnce(cmsOperations.remove.remove) + cmsOperations.remove.remove.restore() + done() + }.bind(this)) }.bind(this)) }); });