From 347000cc2d5597c1971942ebdbe97461c9fa5802 Mon Sep 17 00:00:00 2001 From: Bernhard Posselt Date: Fri, 10 May 2013 13:30:00 +0200 Subject: use a deleted flag for deleted feeds to handle undo --- appinfo/database.xml | 14 ++ appinfo/info.xml | 2 +- appinfo/routes.php | 12 ++ appinfo/version | 2 +- backgroundjob/task.php | 2 + businesslayer/feedbusinesslayer.php | 38 ++++- businesslayer/folderbusinesslayer.php | 44 +++++- controller/feedcontroller.php | 24 ++- controller/foldercontroller.php | 25 +++- db/feed.php | 2 + db/feedmapper.php | 16 ++ db/folder.php | 2 + db/foldermapper.php | 22 ++- dependencyinjection/dicontainer.php | 30 +++- external/feedapi.php | 2 + external/folderapi.php | 1 + .../businesslayer/folderbusinesslayer.coffee | 20 +-- js/app/services/undoqueue.coffee | 4 +- js/public/app.js | 34 ++--- tests/unit/businesslayer/FeedBusinessLayerTest.php | 161 ++++++++++++++------- .../unit/businesslayer/FolderBusinessLayerTest.php | 79 +++++++++- tests/unit/controller/FeedControllerTest.php | 63 +++++++- tests/unit/controller/FolderControllerTest.php | 57 +++++++- tests/unit/db/FeedMapperTest.php | 32 ++++ tests/unit/db/FolderMapperTest.php | 48 +++++- tests/unit/external/FeedAPITest.php | 11 +- tests/unit/external/FolderAPITest.php | 6 + 27 files changed, 642 insertions(+), 111 deletions(-) diff --git a/appinfo/database.xml b/appinfo/database.xml index af0073098..7d3cf2378 100644 --- a/appinfo/database.xml +++ b/appinfo/database.xml @@ -38,6 +38,13 @@ true true + + deleted_at + integer + 0 + false + true + news_folders_parent_id_index @@ -106,6 +113,13 @@ false true + + deleted_at + integer + 0 + false + true + folder_id integer diff --git a/appinfo/info.xml b/appinfo/info.xml index d20593797..4003fae84 100644 --- a/appinfo/info.xml +++ b/appinfo/info.xml @@ -5,6 +5,6 @@ An RSS/Atom feed reader. Requires the App Framework app and backgroundjobs need to be enabled. See the README.rst in the apps top directory AGPL Alessandro Cosentino, Bernhard Posselt, Jan-Christoph Borchardt. Powered by SimplePie (Ryan Parman, Geoffrey Sneddon, Ryan McCue and contributors). - 0.99 + 0.101 5.0.6 diff --git a/appinfo/routes.php b/appinfo/routes.php index 28a6cadff..8c89d49fb 100644 --- a/appinfo/routes.php +++ b/appinfo/routes.php @@ -71,6 +71,12 @@ $this->create('news_folders_delete', '/folders/{folderId}/delete')->post()->acti } ); +$this->create('news_folders_restore', '/folders/{folderId}/restore')->post()->action( + function($params){ + App::main('FolderController', 'restore', $params, new DIContainer()); + } +); + $this->create('news_folders_rename', '/folders/{folderId}/rename')->post()->action( function($params){ App::main('FolderController', 'rename', $params, new DIContainer()); @@ -110,6 +116,12 @@ $this->create('news_feeds_delete', '/feeds/{feedId}/delete')->post()->action( } ); +$this->create('news_feeds_restore', '/feeds/{feedId}/restore')->post()->action( + function($params){ + App::main('FeedController', 'restore', $params, new DIContainer()); + } +); + $this->create('news_feeds_update', '/feeds/{feedId}/update')->post()->action( function($params){ App::main('FeedController', 'update', $params, new DIContainer()); diff --git a/appinfo/version b/appinfo/version index f7274f3da..b4fad5b0f 100644 --- a/appinfo/version +++ b/appinfo/version @@ -1 +1 @@ -0.99 \ No newline at end of file +0.101 \ No newline at end of file diff --git a/backgroundjob/task.php b/backgroundjob/task.php index 820d36cc7..9a21a662f 100644 --- a/backgroundjob/task.php +++ b/backgroundjob/task.php @@ -34,7 +34,9 @@ class Task { static public function run() { $container = new DIContainer(); + $container['FolderBusinessLayer']->purgeDeleted(); $container['FeedBusinessLayer']->updateAll(); + $container['FeedBusinessLayer']->purgeDeleted(); $container['ItemBusinessLayer']->autoPurgeOld(); } diff --git a/businesslayer/feedbusinesslayer.php b/businesslayer/feedbusinesslayer.php index 1f5d5d6c7..f6ab1c144 100644 --- a/businesslayer/feedbusinesslayer.php +++ b/businesslayer/feedbusinesslayer.php @@ -43,17 +43,20 @@ class FeedBusinessLayer extends BusinessLayer { private $api; private $timeFactory; private $importParser; + private $autoPurgeMinimumInterval; public function __construct(FeedMapper $feedMapper, Fetcher $feedFetcher, ItemMapper $itemMapper, API $api, TimeFactory $timeFactory, - ImportParser $importParser){ + ImportParser $importParser, + $autoPurgeMinimumInterval){ parent::__construct($feedMapper); $this->feedFetcher = $feedFetcher; $this->itemMapper = $itemMapper; $this->api = $api; $this->timeFactory = $timeFactory; $this->importParser = $importParser; + $this->autoPurgeMinimumInterval = $autoPurgeMinimumInterval; } @@ -236,4 +239,37 @@ class FeedBusinessLayer extends BusinessLayer { } + /** + * Use this to mark a feed as deleted. That way it can be undeleted + * @throws BusinessLayerException when feed does not exist + */ + public function markDeleted($feedId, $userId) { + $feed = $this->find($feedId, $userId); + $feed->setDeletedAt($this->timeFactory->getTime()); + $this->mapper->update($feed); + } + + + /** + * Use this to undo a feed deletion + * @throws BusinessLayerException when feed does not exist + */ + public function unmarkDeleted($feedId, $userId) { + $feed = $this->find($feedId, $userId); + $feed->setDeletedAt(0); + $this->mapper->update($feed); + } + + + public function purgeDeleted($userId=null) { + $now = $this->timeFactory->getTime(); + $deleteOlderThan = $now - $this->autoPurgeMinimumInterval; + $toDelete = $this->mapper->getToDelete($deleteOlderThan, $userId); + + foreach ($toDelete as $feed) { + $this->mapper->delete($feed); + } + } + + } diff --git a/businesslayer/folderbusinesslayer.php b/businesslayer/folderbusinesslayer.php index a4c025b18..e511c4dda 100644 --- a/businesslayer/folderbusinesslayer.php +++ b/businesslayer/folderbusinesslayer.php @@ -26,6 +26,7 @@ namespace OCA\News\BusinessLayer; use \OCA\AppFramework\Core\API; +use \OCA\AppFramework\Utility\TimeFactory; use \OCA\News\Db\Folder; use \OCA\News\Db\FolderMapper; @@ -34,11 +35,17 @@ use \OCA\News\Db\FolderMapper; class FolderBusinessLayer extends BusinessLayer { private $api; + private $timeFactory; + private $autoPurgeMinimumInterval; public function __construct(FolderMapper $folderMapper, - API $api){ + API $api, + TimeFactory $timeFactory, + $autoPurgeMinimumInterval){ parent::__construct($folderMapper); $this->api = $api; + $this->timeFactory = $timeFactory; + $this->autoPurgeMinimumInterval = $autoPurgeMinimumInterval; } @@ -93,5 +100,40 @@ class FolderBusinessLayer extends BusinessLayer { } + /** + * Use this to mark a folder as deleted. That way it can be undeleted + * @throws BusinessLayerException when folder does not exist + */ + public function markDeleted($folderId, $userId) { + $folder = $this->find($folderId, $userId); + $folder->setDeletedAt($this->timeFactory->getTime()); + $this->mapper->update($folder); + } + + + /** + * Use this to undo a folder deletion + * @throws BusinessLayerException when folder does not exist + */ + public function unmarkDeleted($folderId, $userId) { + $folder = $this->find($folderId, $userId); + $folder->setDeletedAt(0); + $this->mapper->update($folder); + } + + + /** + * Purges marked as deleted folders + */ + public function purgeDeleted($userId=null) { + $now = $this->timeFactory->getTime(); + $deleteOlderThan = $now - $this->autoPurgeMinimumInterval; + $toDelete = $this->mapper->getToDelete($deleteOlderThan, $userId); + + foreach ($toDelete as $folder) { + $this->mapper->delete($folder); + } + } + } diff --git a/controller/feedcontroller.php b/controller/feedcontroller.php index b453338d0..d3cc62449 100644 --- a/controller/feedcontroller.php +++ b/controller/feedcontroller.php @@ -133,6 +133,10 @@ class FeedController extends Controller { $userId = $this->api->getUserId(); try { + // we need to purge deleted feeds if a feed is created to + // prevent already exists exceptions + $this->feedBusinessLayer->purgeDeleted($userId); + $feed = $this->feedBusinessLayer->create($url, $parentFolderId, $userId); $params = array( 'feeds' => array($feed) @@ -160,7 +164,7 @@ class FeedController extends Controller { $userId = $this->api->getUserId(); try { - $this->feedBusinessLayer->delete($feedId, $userId); + $this->feedBusinessLayer->markDeleted($feedId, $userId); return $this->renderJSON(); } catch(BusinessLayerException $ex) { return $this->renderJSON(array(), $ex->getMessage()); @@ -259,4 +263,22 @@ class FeedController extends Controller { } + /** + * @IsAdminExemption + * @IsSubAdminExemption + * @Ajax + */ + public function restore(){ + $feedId = (int) $this->params('feedId'); + $userId = $this->api->getUserId(); + + try { + $this->feedBusinessLayer->unmarkDeleted($feedId, $userId); + return $this->renderJSON(); + } catch(BusinessLayerException $ex) { + return $this->renderJSON(array(), $ex->getMessage()); + } + } + + } \ No newline at end of file diff --git a/controller/foldercontroller.php b/controller/foldercontroller.php index 7542a5260..4bae118c2 100644 --- a/controller/foldercontroller.php +++ b/controller/foldercontroller.php @@ -114,6 +114,10 @@ class FolderController extends Controller { $folderName = $this->params('folderName'); try { + // we need to purge deleted folders if a folder is created to + // prevent already exists exceptions + $this->folderBusinessLayer->purgeDeleted($userId); + $folder = $this->folderBusinessLayer->create($folderName, $userId); $params = array( @@ -138,7 +142,7 @@ class FolderController extends Controller { $folderId = (int) $this->params('folderId'); try { - $this->folderBusinessLayer->delete($folderId, $userId); + $this->folderBusinessLayer->markDeleted($folderId, $userId); return $this->renderJSON(); } catch (BusinessLayerException $ex){ return $this->renderJSON(array(), $ex->getMessage()); @@ -188,4 +192,23 @@ class FolderController extends Controller { } + /** + * @IsAdminExemption + * @IsSubAdminExemption + * @Ajax + */ + public function restore(){ + $userId = $this->api->getUserId(); + $folderId = (int) $this->params('folderId'); + + try { + $this->folderBusinessLayer->unmarkDeleted($folderId, $userId); + return $this->renderJSON(); + } catch (BusinessLayerException $ex){ + return $this->renderJSON(array(), $ex->getMessage()); + } + + } + + } \ No newline at end of file diff --git a/db/feed.php b/db/feed.php index 88879f8c4..22fbc359b 100644 --- a/db/feed.php +++ b/db/feed.php @@ -40,6 +40,7 @@ class Feed extends Entity implements IAPI { public $unreadCount; public $link; public $preventUpdate; + public $deletedAt; public function __construct(){ $this->addType('parentId', 'int'); @@ -47,6 +48,7 @@ class Feed extends Entity implements IAPI { $this->addType('folderId', 'int'); $this->addType('unreadCount', 'int'); $this->addType('preventUpdate', 'bool'); + $this->addType('deletedAt', 'int'); } diff --git a/db/feedmapper.php b/db/feedmapper.php index 9e371f1f0..438f2e78c 100644 --- a/db/feedmapper.php +++ b/db/feedmapper.php @@ -88,6 +88,7 @@ class FeedMapper extends Mapper implements IMapper { 'AND (`items`.`status` & ' . StatusFlag::UNREAD . ') = ' . StatusFlag::UNREAD . ' ' . 'WHERE `feeds`.`user_id` = ? ' . + 'AND `feeds`.`deleted_at` = 0 ' . 'GROUP BY `feeds`.`id`'; $params = array($userId); @@ -137,5 +138,20 @@ class FeedMapper extends Mapper implements IMapper { } + public function getToDelete($deleteOlderThan, $userId=null) { + $sql = 'SELECT * FROM `*PREFIX*news_feeds` ' . + 'WHERE `deleted_at` > 0 ' . + 'AND `deleted_at` < ?'; + $params = array($deleteOlderThan); + + // we need to sometimes only delete feeds of a user + if($userId !== null) { + $sql .= ' AND `user_id` = ?'; + array_push($params, $userId); + } + + return $this->findAllRows($sql, $params); + } + } \ No newline at end of file diff --git a/db/folder.php b/db/folder.php index 24fa80000..7d1403c02 100644 --- a/db/folder.php +++ b/db/folder.php @@ -34,10 +34,12 @@ class Folder extends Entity implements IAPI { public $name; public $userId; public $opened; + public $deletedAt; public function __construct(){ $this->addType('parentId', 'int'); $this->addType('opened', 'bool'); + $this->addType('deletedAt', 'int'); } diff --git a/db/foldermapper.php b/db/foldermapper.php index 8978bb45b..f8d088cdb 100644 --- a/db/foldermapper.php +++ b/db/foldermapper.php @@ -65,7 +65,8 @@ class FolderMapper extends Mapper implements IMapper { public function findAllFromUser($userId){ $sql = 'SELECT * FROM `*PREFIX*news_folders` ' . - 'WHERE `user_id` = ?'; + 'WHERE `user_id` = ? ' . + 'AND `deleted_at` = 0'; $params = array($userId); return $this->findAllRows($sql, $params); @@ -74,7 +75,7 @@ class FolderMapper extends Mapper implements IMapper { public function findByName($folderName, $userId){ $sql = 'SELECT * FROM `*PREFIX*news_folders` ' . - 'WHERE `name` = ?' . + 'WHERE `name` = ? ' . 'AND `user_id` = ?'; $params = array($folderName, $userId); @@ -96,4 +97,21 @@ class FolderMapper extends Mapper implements IMapper { } + public function getToDelete($deleteOlderThan, $userId=null) { + $sql = 'SELECT * FROM `*PREFIX*news_folders` ' . + 'WHERE `deleted_at` > 0 ' . + 'AND `deleted_at` < ?'; + $params = array($deleteOlderThan); + + // we need to sometimes only delete feeds of a user + if($userId !== null) { + $sql .= ' AND `user_id` = ?'; + array_push($params, $userId); + } + + return $this->findAllRows($sql, $params); + } + + + } \ No newline at end of file diff --git a/dependencyinjection/dicontainer.php b/dependencyinjection/dicontainer.php index 8334a40ad..464dc1d6a 100644 --- a/dependencyinjection/dicontainer.php +++ b/dependencyinjection/dicontainer.php @@ -70,7 +70,11 @@ class DIContainer extends BaseContainer { /** * Configuration values */ - $this['autoPurgeCount'] = 200; // per feed + $this['autoPurgeMinimumInterval'] = 60; // seconds, used to define how + // long deleted folders and feeds + // should still be kept for an + // undo actions + $this['autoPurgeCount'] = 200; // number of allowed unread articles per feed $this['simplePieCacheDuration'] = 30*60; // seconds $this['simplePieCacheDirectory'] = $this->share(function($c) { @@ -145,18 +149,30 @@ class DIContainer extends BaseContainer { * Business Layer */ $this['FolderBusinessLayer'] = $this->share(function($c){ - return new FolderBusinessLayer($c['FolderMapper'], $c['API']); + return new FolderBusinessLayer( + $c['FolderMapper'], + $c['API'], + $c['TimeFactory'], + $c['autoPurgeMinimumInterval']); }); $this['FeedBusinessLayer'] = $this->share(function($c){ - return new FeedBusinessLayer($c['FeedMapper'], $c['Fetcher'], - $c['ItemMapper'], $c['API'], $c['TimeFactory'], - $c['ImportParser']); + return new FeedBusinessLayer( + $c['FeedMapper'], + $c['Fetcher'], + $c['ItemMapper'], + $c['API'], + $c['TimeFactory'], + $c['ImportParser'], + $c['autoPurgeMinimumInterval']); }); $this['ItemBusinessLayer'] = $this->share(function($c){ - return new ItemBusinessLayer($c['ItemMapper'], $c['StatusFlag'], - $c['TimeFactory'], $c['autoPurgeCount']); + return new ItemBusinessLayer( + $c['ItemMapper'], + $c['StatusFlag'], + $c['TimeFactory'], + $c['autoPurgeCount']); }); diff --git a/external/feedapi.php b/external/feedapi.php index 7273a5413..9d8f75482 100644 --- a/external/feedapi.php +++ b/external/feedapi.php @@ -82,6 +82,8 @@ class FeedAPI extends Controller { $folderId = (int) $this->params('folderId', 0); try { + $this->feedBusinessLayer->purgeDeleted($userId); + $feed = $this->feedBusinessLayer->create($feedUrl, $folderId, $userId); $result = array( 'feeds' => array($feed->toAPI()) diff --git a/external/folderapi.php b/external/folderapi.php index 879d62b5a..314b4ea2f 100644 --- a/external/folderapi.php +++ b/external/folderapi.php @@ -72,6 +72,7 @@ class FolderAPI extends Controller { ); try { + $this->folderBusinessLayer->purgeDeleted($userId); $folder = $this->folderBusinessLayer->create($folderName, $userId); array_push($result['folders'], $folder->toAPI()); diff --git a/js/app/services/businesslayer/folderbusinesslayer.coffee b/js/app/services/businesslayer/folderbusinesslayer.coffee index 39c307c96..3d1fbb7c7 100644 --- a/js/app/services/businesslayer/folderbusinesslayer.coffee +++ b/js/app/services/businesslayer/folderbusinesslayer.coffee @@ -43,11 +43,12 @@ NewestItem, FeedModel) -> delete: (folderId) -> feeds = [] - folder = @_folderModel.removeById(folderId) - # also delete feeds for feed in @_feedBusinessLayer.getFeedsOfFolder(folderId) feeds.push(@_feedModel.removeById(feed.id)) + + folder = @_folderModel.removeById(folderId) + callback = => @_persistence.deleteFolder(folderId) @@ -159,8 +160,8 @@ NewestItem, FeedModel) -> _importElement: (opml, parentFolderId) -> for item in opml.getItems() - if item.isFolder() - do (item) => + do (item) => + if item.isFolder() try @create item.getName(), (data) => @_importElement(item, data.folders[0].id) @@ -171,14 +172,13 @@ NewestItem, FeedModel) -> @_importElement(item, folder.id) else console.info error - else - try - do (item) => + else + try @_feedBusinessLayer.create(item.getUrl(), parentFolderId) - catch error - if not error instanceof _ExistsError - console.info error + catch error + if not error instanceof _ExistsError + console.info error return new FolderBusinessLayer(FolderModel, FeedBusinessLayer, ShowAll, diff --git a/js/app/services/undoqueue.coffee b/js/app/services/undoqueue.coffee index 93f6fb577..631013d13 100644 --- a/js/app/services/undoqueue.coffee +++ b/js/app/services/undoqueue.coffee @@ -44,7 +44,7 @@ angular.module('News').factory 'UndoQueue', an command has been canceled. Usually this will add back a deleted object back to the interface, defaults to an empty function ### - @_executeAll() + @executeAll() command = _undoCallback: @_undoCallback or= -> @@ -70,7 +70,7 @@ angular.module('News').factory 'UndoQueue', @_queue.push(command) - _executeAll: -> + executeAll: -> ### Executes the callback before the timeout has run out This is useful to execute all remaining commands if a new command is diff --git a/js/public/app.js b/js/public/app.js index 8329f90f6..df5202f88 100644 --- a/js/public/app.js +++ b/js/public/app.js @@ -1076,12 +1076,12 @@ License along with this library. If not, see . _this = this; feeds = []; - folder = this._folderModel.removeById(folderId); _ref = this._feedBusinessLayer.getFeedsOfFolder(folderId); for (_i = 0, _len = _ref.length; _i < _len; _i++) { feed = _ref[_i]; feeds.push(this._feedModel.removeById(feed.id)); } + folder = this._folderModel.removeById(folderId); callback = function() { return _this._persistence.deleteFolder(folderId); }; @@ -1228,17 +1228,17 @@ License along with this library. If not, see . }; FolderBusinessLayer.prototype._importElement = function(opml, parentFolderId) { - var error, item, _i, _len, _ref, _results, + var item, _i, _len, _ref, _results, _this = this; _ref = opml.getItems(); _results = []; for (_i = 0, _len = _ref.length; _i < _len; _i++) { item = _ref[_i]; - if (item.isFolder()) { - _results.push((function(item) { - var error, folder; + _results.push((function(item) { + var error, folder; + if (item.isFolder()) { try { return _this.create(item.getName(), function(data) { return _this._importElement(item, data.folders[0].id); @@ -1253,21 +1253,17 @@ License along with this library. If not, see . return console.info(error); } } - })(item)); - } else { - try { - _results.push((function(item) { + } else { + try { return _this._feedBusinessLayer.create(item.getUrl(), parentFolderId); - })(item)); - } catch (_error) { - error = _error; - if (!error instanceof _ExistsError) { - _results.push(console.info(error)); - } else { - _results.push(void 0); + } catch (_error) { + error = _error; + if (!error instanceof _ExistsError) { + return console.info(error); + } } } - } + })(item)); } return _results; }; @@ -3234,7 +3230,7 @@ License along with this library. If not, see . object back to the interface, defaults to an empty function */ - this._executeAll(); + this.executeAll(); command = { _undoCallback: this._undoCallback || (this._undoCallback = function() {}), _callback: this._callback, @@ -3260,7 +3256,7 @@ License along with this library. If not, see . return this._queue.push(command); }; - UndoQueue.prototype._executeAll = function() { + UndoQueue.prototype.executeAll = function() { /* Executes the callback before the timeout has run out This is useful to execute all remaining commands if a new command is diff --git a/tests/unit/businesslayer/FeedBusinessLayerTest.php b/tests/unit/businesslayer/FeedBusinessLayerTest.php index 34a6030e4..882106693 100644 --- a/tests/unit/businesslayer/FeedBusinessLayerTest.php +++ b/tests/unit/businesslayer/FeedBusinessLayerTest.php @@ -38,8 +38,8 @@ use \OCA\News\Utility\ImportParser; class FeedBusinessLayerTest extends \OCA\AppFramework\Utility\TestUtility { - private $mapper; - private $businessLayer; + private $feedMapper; + private $feedBusinessLayer; private $user; private $response; private $fetcher; @@ -47,10 +47,12 @@ class FeedBusinessLayerTest extends \OCA\AppFramework\Utility\TestUtility { private $threshold; private $time; private $importParser; + private $autoPurgeMinimumInterval; protected function setUp(){ $this->api = $this->getAPIMock(); $this->time = 222; + $this->autoPurgeMinimumInterval = 10; $timeFactory = $this->getMockBuilder( '\OCA\AppFramework\Utility\TimeFactory') ->disableOriginalConstructor() @@ -58,7 +60,7 @@ class FeedBusinessLayerTest extends \OCA\AppFramework\Utility\TestUtility { $timeFactory->expects($this->any()) ->method('getTime') ->will($this->returnValue($this->time)); - $this->mapper = $this->getMockBuilder('\OCA\News\Db\FeedMapper') + $this->feedMapper = $this->getMockBuilder('\OCA\News\Db\FeedMapper') ->disableOriginalConstructor() ->getMock(); $this->fetcher = $this->getMockBuilder('\OCA\News\Utility\Fetcher') @@ -70,21 +72,21 @@ class FeedBusinessLayerTest extends \OCA\AppFramework\Utility\TestUtility { $this->importParser = $this->getMockBuilder('\OCA\News\Utility\ImportParser') ->disableOriginalConstructor() ->getMock(); - $this->businessLayer = new FeedBusinessLayer($this->mapper, + $this->feedBusinessLayer = new FeedBusinessLayer($this->feedMapper, $this->fetcher, $this->itemMapper, $this->api, - $timeFactory, $this->importParser); + $timeFactory, $this->importParser, $this->autoPurgeMinimumInterval); $this->user = 'jack'; $response = 'hi'; } public function testFindAll(){ - $this->mapper->expects($this->once()) + $this->feedMapper->expects($this->once()) ->method('findAllFromUser') ->with($this->equalTo($this->user)) ->will($this->returnValue($this->response)); - $result = $this->businessLayer->findAll($this->user); + $result = $this->feedBusinessLayer->findAll($this->user); $this->assertEquals($this->response, $result); } @@ -98,7 +100,7 @@ class FeedBusinessLayerTest extends \OCA\AppFramework\Utility\TestUtility { $this->api->expects($this->once()) ->method('getTrans') ->will($this->returnValue($trans)); - $this->mapper->expects($this->once()) + $this->feedMapper->expects($this->once()) ->method('findByUrlHash') ->with($this->equalTo(md5($url)), $this->equalTo($this->user)) ->will($this->throwException(new DoesNotExistException('yo'))); @@ -107,7 +109,7 @@ class FeedBusinessLayerTest extends \OCA\AppFramework\Utility\TestUtility { ->with($this->equalTo($url)) ->will($this->throwException($ex)); $this->setExpectedException('\OCA\News\BusinessLayer\BusinessLayerException'); - $this->businessLayer->create($url, 1, $this->user); + $this->feedBusinessLayer->create($url, 1, $this->user); } public function testCreate(){ @@ -125,7 +127,7 @@ class FeedBusinessLayerTest extends \OCA\AppFramework\Utility\TestUtility { array($item1, $item2) ); - $this->mapper->expects($this->once()) + $this->feedMapper->expects($this->once()) ->method('findByUrlHash') ->with($this->equalTo(md5($url)), $this->equalTo($this->user)) ->will($this->throwException($ex)); @@ -133,7 +135,7 @@ class FeedBusinessLayerTest extends \OCA\AppFramework\Utility\TestUtility { ->method('fetch') ->with($this->equalTo($url)) ->will($this->returnValue($return)); - $this->mapper->expects($this->once()) + $this->feedMapper->expects($this->once()) ->method('insert') ->with($this->equalTo($createdFeed)) ->will($this->returnValue($createdFeed)); @@ -158,7 +160,7 @@ class FeedBusinessLayerTest extends \OCA\AppFramework\Utility\TestUtility { ->method('insert') ->with($this->equalTo($return[1][0])); - $feed = $this->businessLayer->create($url, $folderId, $this->user); + $feed = $this->feedBusinessLayer->create($url, $folderId, $this->user); $this->assertEquals($feed->getFolderId(), $folderId); $this->assertEquals($feed->getUrl(), $url); @@ -180,7 +182,7 @@ class FeedBusinessLayerTest extends \OCA\AppFramework\Utility\TestUtility { array($item1, $item2) ); - $this->mapper->expects($this->once()) + $this->feedMapper->expects($this->once()) ->method('findByUrlHash') ->with($this->equalTo(md5($url)), $this->equalTo($this->user)) ->will($this->throwException($ex)); @@ -188,7 +190,7 @@ class FeedBusinessLayerTest extends \OCA\AppFramework\Utility\TestUtility { ->method('fetch') ->with($this->equalTo($url)) ->will($this->returnValue($return)); - $this->mapper->expects($this->once()) + $this->feedMapper->expects($this->once()) ->method('insert') ->with($this->equalTo($createdFeed)) ->will($this->returnValue($createdFeed)); @@ -209,7 +211,7 @@ class FeedBusinessLayerTest extends \OCA\AppFramework\Utility\TestUtility { $this->equalTo($item1->getFeedId()), $this->equalTo($this->user)); - $feed = $this->businessLayer->create($url, $folderId, $this->user); + $feed = $this->feedBusinessLayer->create($url, $folderId, $this->user); $this->assertEquals($feed->getFolderId(), $folderId); $this->assertEquals($feed->getUrl(), $url); @@ -233,7 +235,7 @@ class FeedBusinessLayerTest extends \OCA\AppFramework\Utility\TestUtility { $fetchReturn = array($feed, $items); - $this->mapper->expects($this->at(0)) + $this->feedMapper->expects($this->at(0)) ->method('find') ->with($this->equalTo($feed->getId()), $this->equalTo($this->user)) @@ -251,12 +253,12 @@ class FeedBusinessLayerTest extends \OCA\AppFramework\Utility\TestUtility { ->method('insert') ->with($this->equalTo($items[0])); - $this->mapper->expects($this->at(1)) + $this->feedMapper->expects($this->at(1)) ->method('find') ->with($feed->getId(), $this->user) ->will($this->returnValue($feed)); - $return = $this->businessLayer->update($feed->getId(), $this->user); + $return = $this->feedBusinessLayer->update($feed->getId(), $this->user); $this->assertEquals($return, $feed); } @@ -276,7 +278,7 @@ class FeedBusinessLayerTest extends \OCA\AppFramework\Utility\TestUtility { $fetchReturn = array($feed, $items); - $this->mapper->expects($this->at(0)) + $this->feedMapper->expects($this->at(0)) ->method('find') ->with($this->equalTo($feed->getId()), $this->equalTo($this->user)) @@ -295,12 +297,12 @@ class FeedBusinessLayerTest extends \OCA\AppFramework\Utility\TestUtility { $this->itemMapper->expects($this->never()) ->method('delete'); - $this->mapper->expects($this->at(1)) + $this->feedMapper->expects($this->at(1)) ->method('find') ->with($feed->getId(), $this->user) ->will($this->returnValue($feed)); - $return = $this->businessLayer->update($feed->getId(), $this->user); + $return = $this->feedBusinessLayer->update($feed->getId(), $this->user); $this->assertEquals($return, $feed); } @@ -323,7 +325,7 @@ class FeedBusinessLayerTest extends \OCA\AppFramework\Utility\TestUtility { $fetchReturn = array($feed, $items); - $this->mapper->expects($this->at(0)) + $this->feedMapper->expects($this->at(0)) ->method('find') ->with($this->equalTo($feed->getId()), $this->equalTo($this->user)) @@ -342,12 +344,12 @@ class FeedBusinessLayerTest extends \OCA\AppFramework\Utility\TestUtility { $this->itemMapper->expects($this->never()) ->method('delete'); - $this->mapper->expects($this->at(1)) + $this->feedMapper->expects($this->at(1)) ->method('find') ->with($feed->getId(), $this->user) ->will($this->returnValue($feed)); - $return = $this->businessLayer->update($feed->getId(), $this->user); + $return = $this->feedBusinessLayer->update($feed->getId(), $this->user); $this->assertEquals($return, $feed); } @@ -369,7 +371,7 @@ class FeedBusinessLayerTest extends \OCA\AppFramework\Utility\TestUtility { $fetchReturn = array($feed, $items); - $this->mapper->expects($this->at(0)) + $this->feedMapper->expects($this->at(0)) ->method('find') ->with($this->equalTo($feed->getId()), $this->equalTo($this->user)) @@ -388,12 +390,12 @@ class FeedBusinessLayerTest extends \OCA\AppFramework\Utility\TestUtility { $this->itemMapper->expects($this->never()) ->method('delete'); - $this->mapper->expects($this->at(1)) + $this->feedMapper->expects($this->at(1)) ->method('find') ->with($feed->getId(), $this->user) ->will($this->returnValue($feed)); - $return = $this->businessLayer->update($feed->getId(), $this->user); + $return = $this->feedBusinessLayer->update($feed->getId(), $this->user); $this->assertEquals($return, $feed); } @@ -415,7 +417,7 @@ class FeedBusinessLayerTest extends \OCA\AppFramework\Utility\TestUtility { $fetchReturn = array($feed, $items); - $this->mapper->expects($this->at(0)) + $this->feedMapper->expects($this->at(0)) ->method('find') ->with($this->equalTo($feed->getId()), $this->equalTo($this->user)) @@ -436,12 +438,12 @@ class FeedBusinessLayerTest extends \OCA\AppFramework\Utility\TestUtility { ->method('insert') ->with($this->equalTo($item)); - $this->mapper->expects($this->at(1)) + $this->feedMapper->expects($this->at(1)) ->method('find') ->with($feed->getId(), $this->user) ->will($this->returnValue($feed)); - $return = $this->businessLayer->update($feed->getId(), $this->user); + $return = $this->feedBusinessLayer->update($feed->getId(), $this->user); $this->assertEquals($return, $feed); $this->assertTrue($item->isUnread()); @@ -454,7 +456,7 @@ class FeedBusinessLayerTest extends \OCA\AppFramework\Utility\TestUtility { $feed->getUrl('test'); $ex = new FetcherException(''); - $this->mapper->expects($this->at(0)) + $this->feedMapper->expects($this->at(0)) ->method('find') ->with($this->equalTo($feed->getId()), $this->equalTo($this->user)) @@ -465,12 +467,12 @@ class FeedBusinessLayerTest extends \OCA\AppFramework\Utility\TestUtility { $this->api->expects($this->any()) ->method('log'); - $this->mapper->expects($this->at(1)) + $this->feedMapper->expects($this->at(1)) ->method('find') ->with($feed->getId(), $this->user) ->will($this->returnValue($feed)); - $return = $this->businessLayer->update($feed->getId(), $this->user); + $return = $this->feedBusinessLayer->update($feed->getId(), $this->user); $this->assertEquals($return, $feed); } @@ -483,14 +485,14 @@ class FeedBusinessLayerTest extends \OCA\AppFramework\Utility\TestUtility { $ex = new DoesNotExistException(''); - $this->mapper->expects($this->at(0)) + $this->feedMapper->expects($this->at(0)) ->method('find') ->with($this->equalTo($feed->getId()), $this->equalTo($this->user)) ->will($this->throwException($ex)); $this->setExpectedException('\OCA\News\BusinessLayer\BusinessLayerException'); - $return = $this->businessLayer->update($feed->getId(), $this->user); + $return = $this->feedBusinessLayer->update($feed->getId(), $this->user); } @@ -513,7 +515,7 @@ class FeedBusinessLayerTest extends \OCA\AppFramework\Utility\TestUtility { $fetchReturn = array($feed, $items); $ex = new DoesNotExistException(''); - $this->mapper->expects($this->at(0)) + $this->feedMapper->expects($this->at(0)) ->method('find') ->with($this->equalTo($feed->getId()), $this->equalTo($this->user)) @@ -528,14 +530,14 @@ class FeedBusinessLayerTest extends \OCA\AppFramework\Utility\TestUtility { $this->equalTo($this->user)) ->will($this->returnValue($item2));; - $this->mapper->expects($this->at(1)) + $this->feedMapper->expects($this->at(1)) ->method('find') ->with($this->equalTo($feed->getId()), $this->equalTo($this->user)) ->will($this->throwException($ex)); $this->setExpectedException('\OCA\News\BusinessLayer\BusinessLayerException'); - $return = $this->businessLayer->update($feed->getId(), $this->user); + $return = $this->feedBusinessLayer->update($feed->getId(), $this->user); } @@ -547,7 +549,7 @@ class FeedBusinessLayerTest extends \OCA\AppFramework\Utility\TestUtility { $feed->setId($feedId); $feed->setPreventUpdate(true); - $this->mapper->expects($this->once()) + $this->feedMapper->expects($this->once()) ->method('find') ->with($this->equalTo($feedId), $this->equalTo($this->user)) @@ -555,7 +557,7 @@ class FeedBusinessLayerTest extends \OCA\AppFramework\Utility\TestUtility { $this->fetcher->expects($this->never()) ->method('fetch'); - $this->businessLayer->update($feedId, $this->user); + $this->feedBusinessLayer->update($feedId, $this->user); } @@ -566,16 +568,16 @@ class FeedBusinessLayerTest extends \OCA\AppFramework\Utility\TestUtility { $feed->setFolderId(16); $feed->setId($feedId); - $this->mapper->expects($this->once()) + $this->feedMapper->expects($this->once()) ->method('find') ->with($this->equalTo($feedId), $this->equalTo($this->user)) ->will($this->returnValue($feed)); - $this->mapper->expects($this->once()) + $this->feedMapper->expects($this->once()) ->method('update') ->with($this->equalTo($feed)); - $this->businessLayer->move($feedId, $folderId, $this->user); + $this->feedBusinessLayer->move($feedId, $folderId, $this->user); $this->assertEquals($folderId, $feed->getFolderId()); } @@ -597,15 +599,15 @@ class FeedBusinessLayerTest extends \OCA\AppFramework\Utility\TestUtility { $items = array(new Item()); - $this->mapper->expects($this->at(0)) + $this->feedMapper->expects($this->at(0)) ->method('findByUrlHash') ->with($this->equalTo($urlHash), $this->equalTo($this->user)) ->will($this->throwException(new DoesNotExistException('hi'))); - $this->mapper->expects($this->at(1)) + $this->feedMapper->expects($this->at(1)) ->method('insert') ->will($this->returnValue($feed)); - $this->mapper->expects($this->at(2)) + $this->feedMapper->expects($this->at(2)) ->method('findByUrlHash') ->with($this->equalTo($urlHash), $this->equalTo($this->user)) @@ -619,7 +621,7 @@ class FeedBusinessLayerTest extends \OCA\AppFramework\Utility\TestUtility { ->method('insert'); - $result = $this->businessLayer->importGoogleReaderJSON(array(), $this->user); + $result = $this->feedBusinessLayer->importGoogleReaderJSON(array(), $this->user); $this->assertEquals($feed, $result); } @@ -649,12 +651,12 @@ class FeedBusinessLayerTest extends \OCA\AppFramework\Utility\TestUtility { $in = array(); - $this->mapper->expects($this->at(0)) + $this->feedMapper->expects($this->at(0)) ->method('findByUrlHash') ->with($this->equalTo($urlHash), $this->equalTo($this->user)) ->will($this->returnValue($feed)); - $this->mapper->expects($this->at(1)) + $this->feedMapper->expects($this->at(1)) ->method('findByUrlHash') ->with($this->equalTo($urlHash), $this->equalTo($this->user)) @@ -673,10 +675,69 @@ class FeedBusinessLayerTest extends \OCA\AppFramework\Utility\TestUtility { ->method('insert') ->with($this->equalTo($savedItem)); - $result = $this->businessLayer->importGoogleReaderJSON($in, $this->user); + $result = $this->feedBusinessLayer->importGoogleReaderJSON($in, $this->user); $this->assertEquals($feed, $result); } + public function testMarkDeleted() { + $id = 3; + $feed = new Feed(); + $feed2 = new Feed(); + $feed2->setDeletedAt($this->time); + + $this->feedMapper->expects($this->once()) + ->method('find') + ->with($this->equalTo($id), $this->equalTo($this->user)) + ->will($this->returnValue($feed)); + $this->feedMapper->expects($this->once()) + ->method('update') + ->with($this->equalTo($feed2)); + + $this->feedBusinessLayer->markDeleted($id, $this->user); + } + + + public function testUnmarkDeleted() { + $id = 3; + $feed = new Feed(); + $feed2 = new Feed(); + $feed2->setDeletedAt(0); + + $this->feedMapper->expects($this->once()) + ->method('find') + ->with($this->equalTo($id), $this->equalTo($this->user)) + ->will($this->returnValue($feed)); + $this->feedMapper->expects($this->once()) + ->method('update') + ->with($this->equalTo($feed2)); + + $this->feedBusinessLayer->unmarkDeleted($id, $this->user); + } + + + public function testPurgeDeleted(){ + $feed1 = new Feed(); + $feed1->setId(3); + $feed2 = new Feed(); + $feed2->setId(5); + $feeds = array($feed1, $feed2); + + $time = $this->time - $this->autoPurgeMinimumInterval; + $this->feedMapper->expects($this->once()) + ->method('getToDelete') + ->with($this->equalTo($time), $this->equalTo($this->user)) + ->will($this->returnValue($feeds)); + $this->feedMapper->expects($this->at(1)) + ->method('delete') + ->with($this->equalTo($feed1)); + $this->feedMapper->expects($this->at(2)) + ->method('delete') + ->with($this->equalTo($feed2)); + + $this->feedBusinessLayer->purgeDeleted($this->user); + } + + } diff --git a/tests/unit/businesslayer/FolderBusinessLayerTest.php b/tests/unit/businesslayer/FolderBusinessLayerTest.php index ad4e188e4..1e26f0226 100644 --- a/tests/unit/businesslayer/FolderBusinessLayerTest.php +++ b/tests/unit/businesslayer/FolderBusinessLayerTest.php @@ -33,17 +33,31 @@ use \OCA\News\Db\Folder; class FolderBusinessLayerTest extends \OCA\AppFramework\Utility\TestUtility { - protected $api; - protected $folderMapper; - protected $folderBusinessLayer; + private $folderMapper; + private $folderBusinessLayer; + private $time; + private $user; + private $autoPurgeMinimumInterval; protected function setUp(){ $this->api = $this->getAPIMock(); + $this->time = 222; + $timeFactory = $this->getMockBuilder( + '\OCA\AppFramework\Utility\TimeFactory') + ->disableOriginalConstructor() + ->getMock(); + $timeFactory->expects($this->any()) + ->method('getTime') + ->will($this->returnValue($this->time)); $this->folderMapper = $this->getMockBuilder( '\OCA\News\Db\FolderMapper') ->disableOriginalConstructor() ->getMock(); - $this->folderBusinessLayer = new FolderBusinessLayer($this->folderMapper, $this->api); + $this->autoPurgeMinimumInterval = 10; + $this->folderBusinessLayer = new FolderBusinessLayer( + $this->folderMapper, $this->api, $timeFactory, + $this->autoPurgeMinimumInterval); + $this->user = 'hi'; } @@ -161,4 +175,61 @@ class FolderBusinessLayerTest extends \OCA\AppFramework\Utility\TestUtility { } + public function testMarkDeleted() { + $id = 3; + $folder = new Folder(); + $folder2 = new Folder(); + $folder2->setDeletedAt($this->time); + + $this->folderMapper->expects($this->once()) + ->method('find') + ->with($this->equalTo($id), $this->equalTo($this->user)) + ->will($this->returnValue($folder)); + $this->folderMapper->expects($this->once()) + ->method('update') + ->with($this->equalTo($folder2)); + + $this->folderBusinessLayer->markDeleted($id, $this->user); + } + + + public function testUnmarkDeleted() { + $id = 3; + $folder = new Folder(); + $folder2 = new Folder(); + $folder2->setDeletedAt(0); + + $this->folderMapper->expects($this->once()) + ->method('find') + ->with($this->equalTo($id), $this->equalTo($this->user)) + ->will($this->returnValue($folder)); + $this->folderMapper->expects($this->once()) + ->method('update') + ->with($this->equalTo($folder2)); + + $this->folderBusinessLayer->unmarkDeleted($id, $this->user); + } + + public function testPurgeDeleted(){ + $folder1 = new Folder(); + $folder1->setId(3); + $folder2 = new Folder(); + $folder2->setId(5); + $feeds = array($folder1, $folder2); + + $time = $this->time - $this->autoPurgeMinimumInterval; + $this->folderMapper->expects($this->once()) + ->method('getToDelete') + ->with($this->equalTo($time), $this->equalTo($this->user)) + ->will($this->returnValue($feeds)); + $this->folderMapper->expects($this->at(1)) + ->method('delete') + ->with($this->equalTo($folder1)); + $this->folderMapper->expects($this->at(2)) + ->method('delete') + ->with($this->equalTo($folder2)); + + $this->folderBusinessLayer->purgeDeleted($this->user); + } + } diff --git a/tests/unit/controller/FeedControllerTest.php b/tests/unit/controller/FeedControllerTest.php index c89d432d6..841ba5ea2 100644 --- a/tests/unit/controller/FeedControllerTest.php +++ b/tests/unit/controller/FeedControllerTest.php @@ -111,6 +111,11 @@ class FeedControllerTest extends ControllerTestUtility { } + public function testRestoreAnnotations(){ + $this->assertFeedControllerAnnotations('restore'); + } + + public function testUpdateAnnotations(){ $this->assertFeedControllerAnnotations('update'); } @@ -311,7 +316,9 @@ class FeedControllerTest extends ControllerTestUtility { $this->itemBusinessLayer->expects($this->once()) ->method('getNewestItemId') ->will($this->returnValue($result['newestItemId'])); - + $this->feedBusinessLayer->expects($this->once()) + ->method('purgeDeleted') + ->with($this->equalTo($this->user)); $this->feedBusinessLayer->expects($this->once()) ->method('create') ->with($this->equalTo($post['url']), @@ -340,6 +347,9 @@ class FeedControllerTest extends ControllerTestUtility { $this->api->expects($this->once()) ->method('getUserId') ->will($this->returnValue($this->user)); + $this->feedBusinessLayer->expects($this->once()) + ->method('purgeDeleted') + ->with($this->equalTo($this->user)); $this->itemBusinessLayer->expects($this->once()) ->method('getNewestItemId') @@ -362,6 +372,12 @@ class FeedControllerTest extends ControllerTestUtility { public function testCreateReturnsErrorForInvalidCreate(){ $msg = 'except'; $ex = new BusinessLayerException($msg); + $this->api->expects($this->once()) + ->method('getUserId') + ->will($this->returnValue($this->user)); + $this->feedBusinessLayer->expects($this->once()) + ->method('purgeDeleted') + ->with($this->equalTo($this->user)); $this->feedBusinessLayer->expects($this->once()) ->method('create') ->will($this->throwException($ex)); @@ -385,7 +401,7 @@ class FeedControllerTest extends ControllerTestUtility { ->method('getUserId') ->will($this->returnValue($this->user)); $this->feedBusinessLayer->expects($this->once()) - ->method('delete') + ->method('markDeleted') ->with($this->equalTo($url['feedId'])); $response = $this->controller->delete(); @@ -404,7 +420,7 @@ class FeedControllerTest extends ControllerTestUtility { ->method('getUserId') ->will($this->returnValue($this->user)); $this->feedBusinessLayer->expects($this->once()) - ->method('delete') + ->method('markDeleted') ->will($this->throwException(new BusinessLayerException($msg))); $response = $this->controller->delete(); @@ -583,4 +599,45 @@ class FeedControllerTest extends ControllerTestUtility { $this->assertEquals($expected, $response->getParams()); } + + public function testRestore() { + $url = array( + 'feedId' => 4 + ); + $this->controller = $this->getPostController(array(), $url); + + $this->api->expects($this->once()) + ->method('getUserId') + ->will($this->returnValue($this->user)); + $this->feedBusinessLayer->expects($this->once()) + ->method('unmarkDeleted') + ->with($this->equalTo($url['feedId'])); + + $response = $this->controller->restore(); + $this->assertTrue($response instanceof JSONResponse); + } + + + public function testRestoreDoesNotExist(){ + $url = array( + 'feedId' => 4 + ); + $msg = 'hehe'; + $this->controller = $this->getPostController(array(), $url); + + $this->api->expects($this->once()) + ->method('getUserId') + ->will($this->returnValue($this->user)); + $this->feedBusinessLayer->expects($this->once()) + ->method('unmarkDeleted') + ->will($this->throwException(new BusinessLayerException($msg))); + + $response = $this->controller->restore(); + $params = json_decode($response->render(), true); + + $this->assertEquals('error', $params['status']); + $this->assertEquals($msg, $params['msg']); + $this->assertTrue($response instanceof JSONResponse); + } + } \ No newline at end of file diff --git a/tests/unit/controller/FolderControllerTest.php b/tests/unit/controller/FolderControllerTest.php index 30edf737f..64df7dc16 100644 --- a/tests/unit/controller/FolderControllerTest.php +++ b/tests/unit/controller/FolderControllerTest.php @@ -118,6 +118,11 @@ class FolderControllerTest extends ControllerTestUtility { } + public function testRestoreAnnotations(){ + $this->assertFolderControllerAnnotations('restore'); + } + + public function testRenameAnnotations(){ $this->assertFolderControllerAnnotations('rename'); } @@ -233,6 +238,9 @@ class FolderControllerTest extends ControllerTestUtility { $this->api->expects($this->once()) ->method('getUserId') ->will($this->returnValue($this->user)); + $this->folderBusinessLayer->expects($this->once()) + ->method('purgeDeleted') + ->with($this->equalTo($this->user)); $this->folderBusinessLayer->expects($this->once()) ->method('create') ->with($this->equalTo($post['folderName']), @@ -249,6 +257,12 @@ class FolderControllerTest extends ControllerTestUtility { public function testCreateReturnsErrorForInvalidCreate(){ $msg = 'except'; $ex = new BusinessLayerExistsException($msg); + $this->api->expects($this->once()) + ->method('getUserId') + ->will($this->returnValue($this->user)); + $this->folderBusinessLayer->expects($this->once()) + ->method('purgeDeleted') + ->with($this->equalTo($this->user)); $this->folderBusinessLayer->expects($this->once()) ->method('create') ->will($this->throwException($ex)); @@ -270,7 +284,7 @@ class FolderControllerTest extends ControllerTestUtility { ->method('getUserId') ->will($this->returnValue($this->user)); $this->folderBusinessLayer->expects($this->once()) - ->method('delete') + ->method('markDeleted') ->with($this->equalTo($url['folderId']), $this->equalTo($this->user)); @@ -288,7 +302,7 @@ class FolderControllerTest extends ControllerTestUtility { ->method('getUserId') ->will($this->returnValue($this->user)); $this->folderBusinessLayer->expects($this->once()) - ->method('delete') + ->method('markDeleted') ->will($this->throwException(new BusinessLayerException($this->msg))); $response = $this->controller->delete(); @@ -373,4 +387,43 @@ class FolderControllerTest extends ControllerTestUtility { $this->assertEquals($expected, $response->getParams()); } + + public function testRestore(){ + $url = array('folderId' => 5); + $this->controller = $this->getPostController(array(), $url); + + $this->api->expects($this->once()) + ->method('getUserId') + ->will($this->returnValue($this->user)); + $this->folderBusinessLayer->expects($this->once()) + ->method('unmarkDeleted') + ->with($this->equalTo($url['folderId']), + $this->equalTo($this->user)); + + $response = $this->controller->restore(); + + $this->assertTrue($response instanceof JSONResponse); + } + + + public function testRestoreDoesNotExist(){ + $url = array('folderId' => 5); + $this->controller = $this->getPostController(array(), $url); + + $this->api->expects($this->once()) + ->method('getUserId') + ->will($this->returnValue($this->user)); + $this->folderBusinessLayer->expects($this->once()) + ->method('unmarkDeleted') + ->will($this->throwException(new BusinessLayerException($this->msg))); + + $response = $this->controller->restore(); + + $params = json_decode($response->render(), true); + + $this->assertEquals('error', $params['status']); + $this->assertEquals($this->msg, $params['msg']); + $this->assertTrue($response instanceof JSONResponse); + } + } \ No newline at end of file diff --git a/tests/unit/db/FeedMapperTest.php b/tests/unit/db/FeedMapperTest.php index 1f8d08ef8..8f7d139e1 100644 --- a/tests/unit/db/FeedMapperTest.php +++ b/tests/unit/db/FeedMapperTest.php @@ -146,6 +146,7 @@ class FeedMapperTest extends \OCA\AppFramework\Utility\MapperTestUtility { 'AND (`items`.`status` & ' . StatusFlag::UNREAD . ') = ' . StatusFlag::UNREAD . ' ' . 'WHERE `feeds`.`user_id` = ? ' . + 'AND `feeds`.`deleted_at` = 0 ' . 'GROUP BY `feeds`.`id`'; $this->setMapperResult($sql, @@ -260,6 +261,37 @@ class FeedMapperTest extends \OCA\AppFramework\Utility\MapperTestUtility { } + public function testGetPurgeDeleted(){ + $rows = array( + array('id' => $this->feeds[0]->getId()), + array('id' => $this->feeds[1]->getId()) + ); + $deleteOlderThan = 110; + $sql = 'SELECT * FROM `*PREFIX*news_feeds` ' . + 'WHERE `deleted_at` > 0 ' . + 'AND `deleted_at` < ?'; + $this->setMapperResult($sql, array($deleteOlderThan), $rows); + $result = $this->mapper->getToDelete($deleteOlderThan); + + $this->assertEquals($this->feeds, $result); + } + public function testGetPurgeDeletedFromUser(){ + $rows = array( + array('id' => $this->feeds[0]->getId()), + array('id' => $this->feeds[1]->getId()) + ); + $deleteOlderThan = 110; + $sql = 'SELECT * FROM `*PREFIX*news_feeds` ' . + 'WHERE `deleted_at` > 0 ' . + 'AND `deleted_at` < ? ' . + 'AND `user_id` = ?'; + $this->setMapperResult($sql, array($deleteOlderThan, $this->user), $rows); + $result = $this->mapper->getToDelete($deleteOlderThan, $this->user); + + $this->assertEquals($this->feeds, $result); + } + + } \ No newline at end of file diff --git a/tests/unit/db/FolderMapperTest.php b/tests/unit/db/FolderMapperTest.php index 57eff63a4..87726b893 100644 --- a/tests/unit/db/FolderMapperTest.php +++ b/tests/unit/db/FolderMapperTest.php @@ -32,6 +32,7 @@ class FolderMapperTest extends \OCA\AppFramework\Utility\MapperTestUtility { private $folderMapper; private $folders; + private $user; protected function setUp(){ $this->beforeEach(); @@ -46,6 +47,7 @@ class FolderMapperTest extends \OCA\AppFramework\Utility\MapperTestUtility { $folder1, $folder2 ); + $this->user = 'hh'; } @@ -107,7 +109,8 @@ class FolderMapperTest extends \OCA\AppFramework\Utility\MapperTestUtility { array('id' => $this->folders[1]->getId()) ); $sql = 'SELECT * FROM `*PREFIX*news_folders` ' . - 'WHERE `user_id` = ?'; + 'WHERE `user_id` = ? ' . + 'AND `deleted_at` = 0'; $this->setMapperResult($sql, array($userId), $rows); @@ -117,17 +120,19 @@ class FolderMapperTest extends \OCA\AppFramework\Utility\MapperTestUtility { public function testFindByName(){ + $folderName = 'heheh'; $userId = 'john'; $rows = array( array('id' => $this->folders[0]->getId()), array('id' => $this->folders[1]->getId()) ); $sql = 'SELECT * FROM `*PREFIX*news_folders` ' . - 'WHERE `user_id` = ?'; + 'WHERE `name` = ? ' . + 'AND `user_id` = ?'; - $this->setMapperResult($sql, array($userId), $rows); + $this->setMapperResult($sql, array($folderName, $userId), $rows); - $result = $this->folderMapper->findAllFromUser($userId); + $result = $this->folderMapper->findByName($folderName, $userId); $this->assertEquals($this->folders, $result); } @@ -173,4 +178,39 @@ class FolderMapperTest extends \OCA\AppFramework\Utility\MapperTestUtility { $this->folderMapper->delete($folder); } + + public function testGetPurgeDeleted(){ + $rows = array( + array('id' => $this->folders[0]->getId()), + array('id' => $this->folders[1]->getId()) + ); + $deleteOlderThan = 110; + $sql = 'SELECT * FROM `*PREFIX*news_folders` ' . + 'WHERE `deleted_at` > 0 ' . + 'AND `deleted_at` < ?'; + $this->setMapperResult($sql, array($deleteOlderThan), $rows); + $result = $this->folderMapper->getToDelete($deleteOlderThan); + + $this->assertEquals($this->folders, $result); + } + + + + public function testGetPurgeDeletedUser(){ + $rows = array( + array('id' => $this->folders[0]->getId()), + array('id' => $this->folders[1]->getId()) + ); + $deleteOlderThan = 110; + $sql = 'SELECT * FROM `*PREFIX*news_folders` ' . + 'WHERE `deleted_at` > 0 ' . + 'AND `deleted_at` < ? ' . + 'AND `user_id` = ?'; + $this->setMapperResult($sql, array($deleteOlderThan, $this->user), $rows); + $result = $this->folderMapper->getToDelete($deleteOlderThan, $this->user); + + $this->assertEquals($this->folders, $result); + } + + } \ No newline at end of file diff --git a/tests/unit/external/FeedAPITest.php b/tests/unit/external/FeedAPITest.php index 8e0d78731..c6d8247f5 100644 --- a/tests/unit/external/FeedAPITest.php +++ b/tests/unit/external/FeedAPITest.php @@ -209,7 +209,9 @@ class FeedAPITest extends \PHPUnit_Framework_TestCase { $this->itemBusinessLayer ); - + $this->feedBusinessLayer->expects($this->once()) + ->method('purgeDeleted') + ->with($this->equalTo($this->user)); $this->feedBusinessLayer->expects($this->once()) ->method('create') ->with( @@ -249,7 +251,9 @@ class FeedAPITest extends \PHPUnit_Framework_TestCase { $this->itemBusinessLayer ); - + $this->feedBusinessLayer->expects($this->once()) + ->method('purgeDeleted') + ->with($this->equalTo($this->user)); $this->feedBusinessLayer->expects($this->once()) ->method('create') ->with( @@ -274,6 +278,9 @@ class FeedAPITest extends \PHPUnit_Framework_TestCase { public function testCreateExists() { + $this->feedBusinessLayer->expects($this->once()) + ->method('purgeDeleted') + ->with($this->equalTo($this->user)); $this->feedBusinessLayer->expects($this->once()) ->method('create') ->will($this->throwException(new BusinessLayerExistsException($this->msg))); diff --git a/tests/unit/external/FolderAPITest.php b/tests/unit/external/FolderAPITest.php index 654a4fbae..aaae2d084 100644 --- a/tests/unit/external/FolderAPITest.php +++ b/tests/unit/external/FolderAPITest.php @@ -112,6 +112,9 @@ class FolderAPITest extends \PHPUnit_Framework_TestCase { $this->itemBusinessLayer ); + $this->folderBusinessLayer->expects($this->once()) + ->method('purgeDeleted') + ->with($this->equalTo($this->user)); $this->folderBusinessLayer->expects($this->once()) ->method('create') ->with($this->equalTo($folderName), $this->equalTo($this->user)) @@ -127,6 +130,9 @@ class FolderAPITest extends \PHPUnit_Framework_TestCase { public function testCreateAlreadyExists() { $msg = 'exists'; + $this->folderBusinessLayer->expects($this->once()) + ->method('purgeDeleted') + ->with($this->equalTo($this->user)); $this->folderBusinessLayer->expects($this->once()) ->method('create') ->will($this->throwException(new BusinessLayerExistsException($msg))); -- cgit v1.2.3