Skip to content

Commit

Permalink
Unpublish Plugin Hook (#419)
Browse files Browse the repository at this point in the history
* Adding in an unpublish plugin hook

* Adding test
  • Loading branch information
jonwinton authored Jul 12, 2017
1 parent 23addd6 commit 4d16939
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 3 deletions.
14 changes: 11 additions & 3 deletions lib/services/uris.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,8 @@ const _ = require('lodash'),
db = require('./db'),
references = require('./references'),
notifications = require('./notifications'),
siteService = require('./sites');
siteService = require('./sites'),
plugins = require('../plugins');

/**
* @param {string} uri
Expand Down Expand Up @@ -50,9 +51,16 @@ function del(uri, locals) {
return get(uri, locals).then(function (oldData) {
return db.del(uri).then(function () {
const prefix = references.getUriPrefix(uri),
site = siteService.getSiteFromPrefix(prefix);
site = siteService.getSiteFromPrefix(prefix),
pageUrl = buf.decode(uri.split('/').pop());

notifications.notify(site, 'unpublished', { url: buf.decode(uri.split('/').pop())});
// Call the unpublish hook for plugins
plugins.executeHook('unpublish', {
url: pageUrl,
uri: oldData
});

notifications.notify(site, 'unpublished', { url: pageUrl });
}).return(oldData);
});
}
Expand Down
16 changes: 16 additions & 0 deletions lib/services/uris.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ const _ = require('lodash'),
sinon = require('sinon'),
siteService = require('./sites'),
notifications = require('./notifications'),
plugins = require('../plugins'),
lib = require('./' + filename);

describe(_.startCase(filename), function () {
Expand All @@ -17,6 +18,7 @@ describe(_.startCase(filename), function () {
sandbox.stub(db, 'get');
sandbox.stub(db, 'del');
sandbox.stub(notifications, 'notify');
sandbox.stub(plugins, 'executeHook');
sandbox.stub(siteService, 'getSiteFromPrefix');
});

Expand All @@ -41,6 +43,20 @@ describe(_.startCase(filename), function () {
});
});

it('plugin unpublish hook', function () {
const uri = 'something/uris/bmljZXVybA==',
site = {a: 'b'},
oldData = {url: 'niceurl'};

db.del.returns(bluebird.resolve());
db.get.returns(bluebird.resolve(oldData));
siteService.getSiteFromPrefix.returns(site);

return fn(uri).then(function () {
sinon.assert.calledWith(plugins.executeHook, 'unpublish');
});
});

it('deletes', function () {
const uri = 'something/uris/some-uri',
site = {a: 'b'},
Expand Down

0 comments on commit 4d16939

Please sign in to comment.