From 42d69a95f3276a2d6089ca68f635c4e2f6aa7a23 Mon Sep 17 00:00:00 2001 From: Bernhard Posselt Date: Tue, 21 Oct 2014 16:45:36 +0200 Subject: convert tabs indention to indention with 4 spaces because of mixing of both variants in code and better readability on github and websites because you cant set the indention width there and 8 spaces will be used for a tab --- tests/unit/db/FeedMapperTest.php | 614 ++++++++++++------------ tests/unit/db/FeedTest.php | 142 +++--- tests/unit/db/FolderMapperTest.php | 238 ++++----- tests/unit/db/FolderTest.php | 60 +-- tests/unit/db/ItemMapperTest.php | 768 +++++++++++++++--------------- tests/unit/db/ItemTest.php | 514 ++++++++++---------- tests/unit/db/MapperFactoryTest.php | 38 +- tests/unit/db/postgres/ItemMapperTest.php | 146 +++--- 8 files changed, 1260 insertions(+), 1260 deletions(-) (limited to 'tests/unit/db') diff --git a/tests/unit/db/FeedMapperTest.php b/tests/unit/db/FeedMapperTest.php index 93137d8d4..a1a570b87 100644 --- a/tests/unit/db/FeedMapperTest.php +++ b/tests/unit/db/FeedMapperTest.php @@ -16,313 +16,313 @@ namespace OCA\News\Db; class FeedMapperTest extends \Test\AppFramework\Db\MapperTestUtility { - private $mapper; - private $feeds; - - protected function setUp(){ - parent::setUp(); - - $this->mapper = new FeedMapper($this->db); - - // create mock feeds - $feed1 = new Feed(); - $feed2 = new Feed(); - - $this->feeds = [$feed1, $feed2]; - $this->user = 'herman'; - } - - - public function testFind(){ - $userId = 'john'; - $id = 3; - $rows = [ - ['id' => $this->feeds[0]->getId()], - ]; - $sql = 'SELECT `feeds`.*, COUNT(`items`.`id`) AS `unread_count` ' . - 'FROM `*PREFIX*news_feeds` `feeds` ' . - 'LEFT JOIN `*PREFIX*news_items` `items` ' . - 'ON `feeds`.`id` = `items`.`feed_id` ' . - 'AND (`items`.`status` & ' . StatusFlag::UNREAD . ') = ' . - StatusFlag::UNREAD . ' ' . - 'WHERE `feeds`.`id` = ? ' . - 'AND `feeds`.`user_id` = ? ' . - 'GROUP BY `feeds`.`id`, `feeds`.`user_id`, `feeds`.`url_hash`,'. - '`feeds`.`url`, `feeds`.`title`, `feeds`.`link`,'. - '`feeds`.`favicon_link`, `feeds`.`added`, `feeds`.`articles_per_update`,'. - '`feeds`.`folder_id`, `feeds`.`prevent_update`, `feeds`.`deleted_at`'; - $params = [$id, $userId]; - $this->setMapperResult($sql, $params, $rows); - - $result = $this->mapper->find($id, $userId); - $this->assertEquals($this->feeds[0], $result); - - } - - - public function testFindNotFound(){ - $userId = 'john'; - $id = 3; - $sql = 'SELECT `feeds`.*, COUNT(`items`.`id`) AS `unread_count` ' . - 'FROM `*PREFIX*news_feeds` `feeds` ' . - 'LEFT JOIN `*PREFIX*news_items` `items` ' . - 'ON `feeds`.`id` = `items`.`feed_id` ' . - 'AND (`items`.`status` & ' . StatusFlag::UNREAD . ') = ' . - StatusFlag::UNREAD . ' ' . - 'WHERE `feeds`.`id` = ? ' . - 'AND `feeds`.`user_id` = ? ' . - 'GROUP BY `feeds`.`id`, `feeds`.`user_id`, `feeds`.`url_hash`,'. - '`feeds`.`url`, `feeds`.`title`, `feeds`.`link`,'. - '`feeds`.`favicon_link`, `feeds`.`added`, `feeds`.`articles_per_update`,'. - '`feeds`.`folder_id`, `feeds`.`prevent_update`, `feeds`.`deleted_at`'; - $params = [$id, $userId]; - $this->setMapperResult($sql, $params); - - $this->setExpectedException('\OCP\AppFramework\Db\DoesNotExistException'); - $this->mapper->find($id, $userId); - } - - - public function testFindMoreThanOneResultFound(){ - $userId = 'john'; - $id = 3; - $rows = [ - ['id' => $this->feeds[0]->getId()], - ['id' => $this->feeds[1]->getId()] - ]; - $sql = 'SELECT `feeds`.*, COUNT(`items`.`id`) AS `unread_count` ' . - 'FROM `*PREFIX*news_feeds` `feeds` ' . - 'LEFT JOIN `*PREFIX*news_items` `items` ' . - 'ON `feeds`.`id` = `items`.`feed_id` ' . - 'AND (`items`.`status` & ' . StatusFlag::UNREAD . ') = ' . - StatusFlag::UNREAD . ' ' . - 'WHERE `feeds`.`id` = ? ' . - 'AND `feeds`.`user_id` = ? ' . - 'GROUP BY `feeds`.`id`, `feeds`.`user_id`, `feeds`.`url_hash`,'. - '`feeds`.`url`, `feeds`.`title`, `feeds`.`link`,'. - '`feeds`.`favicon_link`, `feeds`.`added`, `feeds`.`articles_per_update`,'. - '`feeds`.`folder_id`, `feeds`.`prevent_update`, `feeds`.`deleted_at`'; - $params = [$id, $userId]; - $this->setMapperResult($sql, $params, $rows); - - $this->setExpectedException('\OCP\AppFramework\Db\MultipleObjectsReturnedException'); - $this->mapper->find($id, $userId); - } - - - public function testFindAll(){ - $rows = [ - ['id' => $this->feeds[0]->getId()], - ['id' => $this->feeds[1]->getId()] - ]; - $sql = 'SELECT `feeds`.*, COUNT(`items`.`id`) AS `unread_count` ' . - 'FROM `*PREFIX*news_feeds` `feeds` ' . - 'LEFT OUTER JOIN `*PREFIX*news_folders` `folders` '. - 'ON `feeds`.`folder_id` = `folders`.`id` ' . - 'LEFT JOIN `*PREFIX*news_items` `items` ' . - 'ON `feeds`.`id` = `items`.`feed_id` ' . - // WARNING: this is a desperate attempt at making this query work - // because prepared statements dont work. This is a possible - // SQL INJECTION RISK WHEN MODIFIED WITHOUT THOUGHT. - // think twice when changing this - 'AND (`items`.`status` & ' . StatusFlag::UNREAD . ') = ' . - StatusFlag::UNREAD . ' ' . - 'WHERE (`feeds`.`folder_id` = 0 ' . - 'OR `folders`.`deleted_at` = 0' . - ')' . - 'AND `feeds`.`deleted_at` = 0 ' . - 'GROUP BY `feeds`.`id`, `feeds`.`user_id`, `feeds`.`url_hash`,'. - '`feeds`.`url`, `feeds`.`title`, `feeds`.`link`,'. - '`feeds`.`favicon_link`, `feeds`.`added`, `feeds`.`articles_per_update`,'. - '`feeds`.`folder_id`, `feeds`.`prevent_update`, `feeds`.`deleted_at`'; - - $this->setMapperResult($sql, [], $rows); - - $result = $this->mapper->findAll(); - $this->assertEquals($this->feeds, $result); - } - - - public function testFindAllFromUser(){ - $userId = 'john'; - $rows = [ - ['id' => $this->feeds[0]->getId()], - ['id' => $this->feeds[1]->getId()] - ]; - $sql = 'SELECT `feeds`.*, COUNT(`items`.`id`) AS `unread_count` ' . - 'FROM `*PREFIX*news_feeds` `feeds` ' . - 'LEFT OUTER JOIN `*PREFIX*news_folders` `folders` '. - 'ON `feeds`.`folder_id` = `folders`.`id` ' . - 'LEFT JOIN `*PREFIX*news_items` `items` ' . - 'ON `feeds`.`id` = `items`.`feed_id` ' . - // WARNING: this is a desperate attempt at making this query work - // because prepared statements dont work. This is a possible - // SQL INJECTION RISK WHEN MODIFIED WITHOUT THOUGHT. - // think twice when changing this - 'AND (`items`.`status` & ' . StatusFlag::UNREAD . ') = ' . - StatusFlag::UNREAD . ' ' . - 'WHERE `feeds`.`user_id` = ? ' . - 'AND (`feeds`.`folder_id` = 0 ' . - 'OR `folders`.`deleted_at` = 0' . - ')' . - 'AND `feeds`.`deleted_at` = 0 ' . - 'GROUP BY `feeds`.`id`, `feeds`.`user_id`, `feeds`.`url_hash`,'. - '`feeds`.`url`, `feeds`.`title`, `feeds`.`link`,'. - '`feeds`.`favicon_link`, `feeds`.`added`, `feeds`.`articles_per_update`,'. - '`feeds`.`folder_id`, `feeds`.`prevent_update`, `feeds`.`deleted_at`'; - $this->setMapperResult($sql, [$userId], $rows); - - $result = $this->mapper->findAllFromUser($userId); - $this->assertEquals($this->feeds, $result); - } - - - public function testFindByUrlHash(){ - $urlHash = md5('hihi'); - $row = [['id' => $this->feeds[0]->getId()]]; - $sql = 'SELECT `feeds`.*, COUNT(`items`.`id`) AS `unread_count` ' . - 'FROM `*PREFIX*news_feeds` `feeds` ' . - 'LEFT JOIN `*PREFIX*news_items` `items` ' . - 'ON `feeds`.`id` = `items`.`feed_id` ' . - 'AND (`items`.`status` & ' . StatusFlag::UNREAD . ') = ' . - StatusFlag::UNREAD . ' ' . - 'WHERE `feeds`.`url_hash` = ? ' . - 'AND `feeds`.`user_id` = ? ' . - 'GROUP BY `feeds`.`id`, `feeds`.`user_id`, `feeds`.`url_hash`,'. - '`feeds`.`url`, `feeds`.`title`, `feeds`.`link`,'. - '`feeds`.`favicon_link`, `feeds`.`added`, `feeds`.`articles_per_update`,'. - '`feeds`.`folder_id`, `feeds`.`prevent_update`, `feeds`.`deleted_at`'; - $this->setMapperResult($sql, [$urlHash, $this->user], $row); - - $result = $this->mapper->findByUrlHash($urlHash, $this->user); - $this->assertEquals($this->feeds[0], $result); - } - - - public function testFindByUrlHashNotFound(){ - $urlHash = md5('hihi'); - $sql = 'SELECT `feeds`.*, COUNT(`items`.`id`) AS `unread_count` ' . - 'FROM `*PREFIX*news_feeds` `feeds` ' . - 'LEFT JOIN `*PREFIX*news_items` `items` ' . - 'ON `feeds`.`id` = `items`.`feed_id` ' . - 'AND (`items`.`status` & ' . StatusFlag::UNREAD . ') = ' . - StatusFlag::UNREAD . ' ' . - 'WHERE `feeds`.`url_hash` = ? ' . - 'AND `feeds`.`user_id` = ? ' . - 'GROUP BY `feeds`.`id`, `feeds`.`user_id`, `feeds`.`url_hash`,'. - '`feeds`.`url`, `feeds`.`title`, `feeds`.`link`,'. - '`feeds`.`favicon_link`, `feeds`.`added`, `feeds`.`articles_per_update`,'. - '`feeds`.`folder_id`, `feeds`.`prevent_update`, `feeds`.`deleted_at`'; - $this->setMapperResult($sql, [$urlHash, $this->user]); - - $this->setExpectedException('\OCP\AppFramework\Db\DoesNotExistException'); - $this->mapper->findByUrlHash($urlHash, $this->user); - } - - - public function testFindByUrlHashMoreThanOneResultFound(){ - $urlHash = md5('hihi'); - $rows = [ - ['id' => $this->feeds[0]->getId()], - ['id' => $this->feeds[1]->getId()] - ]; - $sql = 'SELECT `feeds`.*, COUNT(`items`.`id`) AS `unread_count` ' . - 'FROM `*PREFIX*news_feeds` `feeds` ' . - 'LEFT JOIN `*PREFIX*news_items` `items` ' . - 'ON `feeds`.`id` = `items`.`feed_id` ' . - 'AND (`items`.`status` & ' . StatusFlag::UNREAD . ') = ' . - StatusFlag::UNREAD . ' ' . - 'WHERE `feeds`.`url_hash` = ? ' . - 'AND `feeds`.`user_id` = ? ' . - 'GROUP BY `feeds`.`id`, `feeds`.`user_id`, `feeds`.`url_hash`,'. - '`feeds`.`url`, `feeds`.`title`, `feeds`.`link`,'. - '`feeds`.`favicon_link`, `feeds`.`added`, `feeds`.`articles_per_update`,'. - '`feeds`.`folder_id`, `feeds`.`prevent_update`, `feeds`.`deleted_at`'; - $this->setMapperResult($sql, [$urlHash, $this->user], $rows); - - $this->setExpectedException('\OCP\AppFramework\Db\MultipleObjectsReturnedException'); - $this->mapper->findByUrlHash($urlHash, $this->user); - } - - - public function testDelete(){ - $feed = new Feed(); - $feed->setId(3); - - $sql = 'DELETE FROM `*PREFIX*news_feeds` WHERE `id` = ?'; - $arguments = [$feed->getId()]; - - $sql2 = 'DELETE FROM `*PREFIX*news_items` WHERE `feed_id` = ?'; - $arguments2 = [$feed->getId()]; - - $pdoResult = $this->getMock('Result', ['fetch']); - $pdoResult->expects($this->any())->method('fetch'); - - $this->setMapperResult($sql, $arguments); - $this->setMapperResult($sql2, $arguments2); - - $this->mapper->delete($feed); - - } - - - public function testGetPurgeDeleted(){ - $rows = [ - ['id' => $this->feeds[0]->getId()], - ['id' => $this->feeds[1]->getId()] - ]; - $deleteOlderThan = 110; - $sql = 'SELECT * FROM `*PREFIX*news_feeds` ' . - 'WHERE `deleted_at` > 0 ' . - 'AND `deleted_at` < ? '; - $this->setMapperResult($sql, [$deleteOlderThan], $rows); - $result = $this->mapper->getToDelete($deleteOlderThan); - - $this->assertEquals($this->feeds, $result); - } - - - public function testGetPurgeDeletedFromUser(){ - $rows = [ - ['id' => $this->feeds[0]->getId()], - ['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, [$deleteOlderThan, $this->user], $rows); - $result = $this->mapper->getToDelete($deleteOlderThan, $this->user); - - $this->assertEquals($this->feeds, $result); - } - - - public function testGetAllPurgeDeletedFromUser(){ - $rows = [ - ['id' => $this->feeds[0]->getId()], - ['id' => $this->feeds[1]->getId()] - ]; - - $sql = 'SELECT * FROM `*PREFIX*news_feeds` ' . - 'WHERE `deleted_at` > 0 ' . - 'AND `user_id` = ?'; - $this->setMapperResult($sql, [$this->user], $rows); - $result = $this->mapper->getToDelete(null, $this->user); - - $this->assertEquals($this->feeds, $result); - } - - - public function testDeleteFromUser(){ - $userId = 'john'; - $sql = 'DELETE FROM `*PREFIX*news_feeds` WHERE `user_id` = ?'; - - $this->setMapperResult($sql, [$userId]); - - $this->mapper->deleteUser($userId); - } + private $mapper; + private $feeds; + + protected function setUp(){ + parent::setUp(); + + $this->mapper = new FeedMapper($this->db); + + // create mock feeds + $feed1 = new Feed(); + $feed2 = new Feed(); + + $this->feeds = [$feed1, $feed2]; + $this->user = 'herman'; + } + + + public function testFind(){ + $userId = 'john'; + $id = 3; + $rows = [ + ['id' => $this->feeds[0]->getId()], + ]; + $sql = 'SELECT `feeds`.*, COUNT(`items`.`id`) AS `unread_count` ' . + 'FROM `*PREFIX*news_feeds` `feeds` ' . + 'LEFT JOIN `*PREFIX*news_items` `items` ' . + 'ON `feeds`.`id` = `items`.`feed_id` ' . + 'AND (`items`.`status` & ' . StatusFlag::UNREAD . ') = ' . + StatusFlag::UNREAD . ' ' . + 'WHERE `feeds`.`id` = ? ' . + 'AND `feeds`.`user_id` = ? ' . + 'GROUP BY `feeds`.`id`, `feeds`.`user_id`, `feeds`.`url_hash`,'. + '`feeds`.`url`, `feeds`.`title`, `feeds`.`link`,'. + '`feeds`.`favicon_link`, `feeds`.`added`, `feeds`.`articles_per_update`,'. + '`feeds`.`folder_id`, `feeds`.`prevent_update`, `feeds`.`deleted_at`'; + $params = [$id, $userId]; + $this->setMapperResult($sql, $params, $rows); + + $result = $this->mapper->find($id, $userId); + $this->assertEquals($this->feeds[0], $result); + + } + + + public function testFindNotFound(){ + $userId = 'john'; + $id = 3; + $sql = 'SELECT `feeds`.*, COUNT(`items`.`id`) AS `unread_count` ' . + 'FROM `*PREFIX*news_feeds` `feeds` ' . + 'LEFT JOIN `*PREFIX*news_items` `items` ' . + 'ON `feeds`.`id` = `items`.`feed_id` ' . + 'AND (`items`.`status` & ' . StatusFlag::UNREAD . ') = ' . + StatusFlag::UNREAD . ' ' . + 'WHERE `feeds`.`id` = ? ' . + 'AND `feeds`.`user_id` = ? ' . + 'GROUP BY `feeds`.`id`, `feeds`.`user_id`, `feeds`.`url_hash`,'. + '`feeds`.`url`, `feeds`.`title`, `feeds`.`link`,'. + '`feeds`.`favicon_link`, `feeds`.`added`, `feeds`.`articles_per_update`,'. + '`feeds`.`folder_id`, `feeds`.`prevent_update`, `feeds`.`deleted_at`'; + $params = [$id, $userId]; + $this->setMapperResult($sql, $params); + + $this->setExpectedException('\OCP\AppFramework\Db\DoesNotExistException'); + $this->mapper->find($id, $userId); + } + + + public function testFindMoreThanOneResultFound(){ + $userId = 'john'; + $id = 3; + $rows = [ + ['id' => $this->feeds[0]->getId()], + ['id' => $this->feeds[1]->getId()] + ]; + $sql = 'SELECT `feeds`.*, COUNT(`items`.`id`) AS `unread_count` ' . + 'FROM `*PREFIX*news_feeds` `feeds` ' . + 'LEFT JOIN `*PREFIX*news_items` `items` ' . + 'ON `feeds`.`id` = `items`.`feed_id` ' . + 'AND (`items`.`status` & ' . StatusFlag::UNREAD . ') = ' . + StatusFlag::UNREAD . ' ' . + 'WHERE `feeds`.`id` = ? ' . + 'AND `feeds`.`user_id` = ? ' . + 'GROUP BY `feeds`.`id`, `feeds`.`user_id`, `feeds`.`url_hash`,'. + '`feeds`.`url`, `feeds`.`title`, `feeds`.`link`,'. + '`feeds`.`favicon_link`, `feeds`.`added`, `feeds`.`articles_per_update`,'. + '`feeds`.`folder_id`, `feeds`.`prevent_update`, `feeds`.`deleted_at`'; + $params = [$id, $userId]; + $this->setMapperResult($sql, $params, $rows); + + $this->setExpectedException('\OCP\AppFramework\Db\MultipleObjectsReturnedException'); + $this->mapper->find($id, $userId); + } + + + public function testFindAll(){ + $rows = [ + ['id' => $this->feeds[0]->getId()], + ['id' => $this->feeds[1]->getId()] + ]; + $sql = 'SELECT `feeds`.*, COUNT(`items`.`id`) AS `unread_count` ' . + 'FROM `*PREFIX*news_feeds` `feeds` ' . + 'LEFT OUTER JOIN `*PREFIX*news_folders` `folders` '. + 'ON `feeds`.`folder_id` = `folders`.`id` ' . + 'LEFT JOIN `*PREFIX*news_items` `items` ' . + 'ON `feeds`.`id` = `items`.`feed_id` ' . + // WARNING: this is a desperate attempt at making this query work + // because prepared statements dont work. This is a possible + // SQL INJECTION RISK WHEN MODIFIED WITHOUT THOUGHT. + // think twice when changing this + 'AND (`items`.`status` & ' . StatusFlag::UNREAD . ') = ' . + StatusFlag::UNREAD . ' ' . + 'WHERE (`feeds`.`folder_id` = 0 ' . + 'OR `folders`.`deleted_at` = 0' . + ')' . + 'AND `feeds`.`deleted_at` = 0 ' . + 'GROUP BY `feeds`.`id`, `feeds`.`user_id`, `feeds`.`url_hash`,'. + '`feeds`.`url`, `feeds`.`title`, `feeds`.`link`,'. + '`feeds`.`favicon_link`, `feeds`.`added`, `feeds`.`articles_per_update`,'. + '`feeds`.`folder_id`, `feeds`.`prevent_update`, `feeds`.`deleted_at`'; + + $this->setMapperResult($sql, [], $rows); + + $result = $this->mapper->findAll(); + $this->assertEquals($this->feeds, $result); + } + + + public function testFindAllFromUser(){ + $userId = 'john'; + $rows = [ + ['id' => $this->feeds[0]->getId()], + ['id' => $this->feeds[1]->getId()] + ]; + $sql = 'SELECT `feeds`.*, COUNT(`items`.`id`) AS `unread_count` ' . + 'FROM `*PREFIX*news_feeds` `feeds` ' . + 'LEFT OUTER JOIN `*PREFIX*news_folders` `folders` '. + 'ON `feeds`.`folder_id` = `folders`.`id` ' . + 'LEFT JOIN `*PREFIX*news_items` `items` ' . + 'ON `feeds`.`id` = `items`.`feed_id` ' . + // WARNING: this is a desperate attempt at making this query work + // because prepared statements dont work. This is a possible + // SQL INJECTION RISK WHEN MODIFIED WITHOUT THOUGHT. + // think twice when changing this + 'AND (`items`.`status` & ' . StatusFlag::UNREAD . ') = ' . + StatusFlag::UNREAD . ' ' . + 'WHERE `feeds`.`user_id` = ? ' . + 'AND (`feeds`.`folder_id` = 0 ' . + 'OR `folders`.`deleted_at` = 0' . + ')' . + 'AND `feeds`.`deleted_at` = 0 ' . + 'GROUP BY `feeds`.`id`, `feeds`.`user_id`, `feeds`.`url_hash`,'. + '`feeds`.`url`, `feeds`.`title`, `feeds`.`link`,'. + '`feeds`.`favicon_link`, `feeds`.`added`, `feeds`.`articles_per_update`,'. + '`feeds`.`folder_id`, `feeds`.`prevent_update`, `feeds`.`deleted_at`'; + $this->setMapperResult($sql, [$userId], $rows); + + $result = $this->mapper->findAllFromUser($userId); + $this->assertEquals($this->feeds, $result); + } + + + public function testFindByUrlHash(){ + $urlHash = md5('hihi'); + $row = [['id' => $this->feeds[0]->getId()]]; + $sql = 'SELECT `feeds`.*, COUNT(`items`.`id`) AS `unread_count` ' . + 'FROM `*PREFIX*news_feeds` `feeds` ' . + 'LEFT JOIN `*PREFIX*news_items` `items` ' . + 'ON `feeds`.`id` = `items`.`feed_id` ' . + 'AND (`items`.`status` & ' . StatusFlag::UNREAD . ') = ' . + StatusFlag::UNREAD . ' ' . + 'WHERE `feeds`.`url_hash` = ? ' . + 'AND `feeds`.`user_id` = ? ' . + 'GROUP BY `feeds`.`id`, `feeds`.`user_id`, `feeds`.`url_hash`,'. + '`feeds`.`url`, `feeds`.`title`, `feeds`.`link`,'. + '`feeds`.`favicon_link`, `feeds`.`added`, `feeds`.`articles_per_update`,'. + '`feeds`.`folder_id`, `feeds`.`prevent_update`, `feeds`.`deleted_at`'; + $this->setMapperResult($sql, [$urlHash, $this->user], $row); + + $result = $this->mapper->findByUrlHash($urlHash, $this->user); + $this->assertEquals($this->feeds[0], $result); + } + + + public function testFindByUrlHashNotFound(){ + $urlHash = md5('hihi'); + $sql = 'SELECT `feeds`.*, COUNT(`items`.`id`) AS `unread_count` ' . + 'FROM `*PREFIX*news_feeds` `feeds` ' . + 'LEFT JOIN `*PREFIX*news_items` `items` ' . + 'ON `feeds`.`id` = `items`.`feed_id` ' . + 'AND (`items`.`status` & ' . StatusFlag::UNREAD . ') = ' . + StatusFlag::UNREAD . ' ' . + 'WHERE `feeds`.`url_hash` = ? ' . + 'AND `feeds`.`user_id` = ? ' . + 'GROUP BY `feeds`.`id`, `feeds`.`user_id`, `feeds`.`url_hash`,'. + '`feeds`.`url`, `feeds`.`title`, `feeds`.`link`,'. + '`feeds`.`favicon_link`, `feeds`.`added`, `feeds`.`articles_per_update`,'. + '`feeds`.`folder_id`, `feeds`.`prevent_update`, `feeds`.`deleted_at`'; + $this->setMapperResult($sql, [$urlHash, $this->user]); + + $this->setExpectedException('\OCP\AppFramework\Db\DoesNotExistException'); + $this->mapper->findByUrlHash($urlHash, $this->user); + } + + + public function testFindByUrlHashMoreThanOneResultFound(){ + $urlHash = md5('hihi'); + $rows = [ + ['id' => $this->feeds[0]->getId()], + ['id' => $this->feeds[1]->getId()] + ]; + $sql = 'SELECT `feeds`.*, COUNT(`items`.`id`) AS `unread_count` ' . + 'FROM `*PREFIX*news_feeds` `feeds` ' . + 'LEFT JOIN `*PREFIX*news_items` `items` ' . + 'ON `feeds`.`id` = `items`.`feed_id` ' . + 'AND (`items`.`status` & ' . StatusFlag::UNREAD . ') = ' . + StatusFlag::UNREAD . ' ' . + 'WHERE `feeds`.`url_hash` = ? ' . + 'AND `feeds`.`user_id` = ? ' . + 'GROUP BY `feeds`.`id`, `feeds`.`user_id`, `feeds`.`url_hash`,'. + '`feeds`.`url`, `feeds`.`title`, `feeds`.`link`,'. + '`feeds`.`favicon_link`, `feeds`.`added`, `feeds`.`articles_per_update`,'. + '`feeds`.`folder_id`, `feeds`.`prevent_update`, `feeds`.`deleted_at`'; + $this->setMapperResult($sql, [$urlHash, $this->user], $rows); + + $this->setExpectedException('\OCP\AppFramework\Db\MultipleObjectsReturnedException'); + $this->mapper->findByUrlHash($urlHash, $this->user); + } + + + public function testDelete(){ + $feed = new Feed(); + $feed->setId(3); + + $sql = 'DELETE FROM `*PREFIX*news_feeds` WHERE `id` = ?'; + $arguments = [$feed->getId()]; + + $sql2 = 'DELETE FROM `*PREFIX*news_items` WHERE `feed_id` = ?'; + $arguments2 = [$feed->getId()]; + + $pdoResult = $this->getMock('Result', ['fetch']); + $pdoResult->expects($this->any())->method('fetch'); + + $this->setMapperResult($sql, $arguments); + $this->setMapperResult($sql2, $arguments2); + + $this->mapper->delete($feed); + + } + + + public function testGetPurgeDeleted(){ + $rows = [ + ['id' => $this->feeds[0]->getId()], + ['id' => $this->feeds[1]->getId()] + ]; + $deleteOlderThan = 110; + $sql = 'SELECT * FROM `*PREFIX*news_feeds` ' . + 'WHERE `deleted_at` > 0 ' . + 'AND `deleted_at` < ? '; + $this->setMapperResult($sql, [$deleteOlderThan], $rows); + $result = $this->mapper->getToDelete($deleteOlderThan); + + $this->assertEquals($this->feeds, $result); + } + + + public function testGetPurgeDeletedFromUser(){ + $rows = [ + ['id' => $this->feeds[0]->getId()], + ['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, [$deleteOlderThan, $this->user], $rows); + $result = $this->mapper->getToDelete($deleteOlderThan, $this->user); + + $this->assertEquals($this->feeds, $result); + } + + + public function testGetAllPurgeDeletedFromUser(){ + $rows = [ + ['id' => $this->feeds[0]->getId()], + ['id' => $this->feeds[1]->getId()] + ]; + + $sql = 'SELECT * FROM `*PREFIX*news_feeds` ' . + 'WHERE `deleted_at` > 0 ' . + 'AND `user_id` = ?'; + $this->setMapperResult($sql, [$this->user], $rows); + $result = $this->mapper->getToDelete(null, $this->user); + + $this->assertEquals($this->feeds, $result); + } + + + public function testDeleteFromUser(){ + $userId = 'john'; + $sql = 'DELETE FROM `*PREFIX*news_feeds` WHERE `user_id` = ?'; + + $this->setMapperResult($sql, [$userId]); + + $this->mapper->deleteUser($userId); + } } diff --git a/tests/unit/db/FeedTest.php b/tests/unit/db/FeedTest.php index f7fe9b885..936948813 100644 --- a/tests/unit/db/FeedTest.php +++ b/tests/unit/db/FeedTest.php @@ -17,77 +17,77 @@ namespace OCA\News\Db; class FeedTest extends \PHPUnit_Framework_TestCase { - private function createFeed() { - $feed = new Feed(); - $feed->setId(3); - $feed->setUrl('http://google.com/some/weird/path'); - $feed->setTitle('title'); - $feed->setFaviconLink('favicon'); - $feed->setAdded(123); - $feed->setFolderId(1); - $feed->setUnreadCount(321); - $feed->setLink('https://www.google.com/some/weird/path'); - - return $feed; - } - - public function testToAPI() { - $feed = $this->createFeed(); - - $this->assertEquals([ - 'id' => 3, - 'url' => 'http://google.com/some/weird/path', - 'title' => 'title', - 'faviconLink' => 'favicon', - 'added' => 123, - 'folderId' => 1, - 'unreadCount' => 321, - 'link' => 'https://www.google.com/some/weird/path' - ], $feed->toAPI()); - } - - - public function testSerialize() { - $feed = $this->createFeed(); - - $this->assertEquals([ - 'id' => 3, - 'url' => 'http://google.com/some/weird/path', - 'title' => 'title', - 'faviconLink' => 'favicon', - 'added' => 123, - 'folderId' => 1, - 'unreadCount' => 321, - 'link' => 'https://www.google.com/some/weird/path', - 'userId' => null, - 'urlHash' => '44168618f55392b145629d6b3922e84b', - 'preventUpdate' => null, - 'deletedAt' => null, - 'articlesPerUpdate' => null, - 'cssClass' => 'custom-google-com', - ], $feed->jsonSerialize()); - } - - - public function testSetXSSUrl() { - $feed = new Feed(); - $feed->setUrl('javascript:alert()'); - $this->assertEquals('', $feed->getUrl()); - } - - - public function testSetUrlUpdatesHash() { - $feed = new Feed(); - $feed->setUrl('http://test'); - $this->assertEquals(md5('http://test'), $feed->getUrlHash()); - } - - - public function testSetXSSLink() { - $feed = new Feed(); - $feed->setLink('javascript:alert()'); - $this->assertEquals('', $feed->getLink()); - } + private function createFeed() { + $feed = new Feed(); + $feed->setId(3); + $feed->setUrl('http://google.com/some/weird/path'); + $feed->setTitle('title'); + $feed->setFaviconLink('favicon'); + $feed->setAdded(123); + $feed->setFolderId(1); + $feed->setUnreadCount(321); + $feed->setLink('https://www.google.com/some/weird/path'); + + return $feed; + } + + public function testToAPI() { + $feed = $this->createFeed(); + + $this->assertEquals([ + 'id' => 3, + 'url' => 'http://google.com/some/weird/path', + 'title' => 'title', + 'faviconLink' => 'favicon', + 'added' => 123, + 'folderId' => 1, + 'unreadCount' => 321, + 'link' => 'https://www.google.com/some/weird/path' + ], $feed->toAPI()); + } + + + public function testSerialize() { + $feed = $this->createFeed(); + + $this->assertEquals([ + 'id' => 3, + 'url' => 'http://google.com/some/weird/path', + 'title' => 'title', + 'faviconLink' => 'favicon', + 'added' => 123, + 'folderId' => 1, + 'unreadCount' => 321, + 'link' => 'https://www.google.com/some/weird/path', + 'userId' => null, + 'urlHash' => '44168618f55392b145629d6b3922e84b', + 'preventUpdate' => null, + 'deletedAt' => null, + 'articlesPerUpdate' => null, + 'cssClass' => 'custom-google-com', + ], $feed->jsonSerialize()); + } + + + public function testSetXSSUrl() { + $feed = new Feed(); + $feed->setUrl('javascript:alert()'); + $this->assertEquals('', $feed->getUrl()); + } + + + public function testSetUrlUpdatesHash() { + $feed = new Feed(); + $feed->setUrl('http://test'); + $this->assertEquals(md5('http://test'), $feed->getUrlHash()); + } + + + public function testSetXSSLink() { + $feed = new Feed(); + $feed->setLink('javascript:alert()'); + $this->assertEquals('', $feed->getLink()); + } } \ No newline at end of file diff --git a/tests/unit/db/FolderMapperTest.php b/tests/unit/db/FolderMapperTest.php index 6a2e94588..1078d205f 100644 --- a/tests/unit/db/FolderMapperTest.php +++ b/tests/unit/db/FolderMapperTest.php @@ -16,173 +16,173 @@ namespace OCA\News\Db; class FolderMapperTest extends \Test\AppFramework\Db\MapperTestUtility { - private $folderMapper; - private $folders; - private $user; + private $folderMapper; + private $folders; + private $user; - protected function setUp(){ - parent::setUp(); + protected function setUp(){ + parent::setUp(); - $this->folderMapper = new FolderMapper($this->db); + $this->folderMapper = new FolderMapper($this->db); - // create mock folders - $folder1 = new Folder(); - $folder2 = new Folder(); + // create mock folders + $folder1 = new Folder(); + $folder2 = new Folder(); - $this->folders = [$folder1, $folder2]; - $this->user = 'hh'; - $this->twoRows = [ - ['id' => $this->folders[0]->getId()], - ['id' => $this->folders[1]->getId()] - ]; - } + $this->folders = [$folder1, $folder2]; + $this->user = 'hh'; + $this->twoRows = [ + ['id' => $this->folders[0]->getId()], + ['id' => $this->folders[1]->getId()] + ]; + } - public function testFind(){ - $userId = 'john'; - $id = 3; - $rows = [['id' => $this->folders[0]->getId()]]; - $sql = 'SELECT * FROM `*PREFIX*news_folders` ' . - 'WHERE `id` = ? ' . - 'AND `user_id` = ?'; + public function testFind(){ + $userId = 'john'; + $id = 3; + $rows = [['id' => $this->folders[0]->getId()]]; + $sql = 'SELECT * FROM `*PREFIX*news_folders` ' . + 'WHERE `id` = ? ' . + 'AND `user_id` = ?'; - $this->setMapperResult($sql, [$id, $userId], $rows); + $this->setMapperResult($sql, [$id, $userId], $rows); - $result = $this->folderMapper->find($id, $userId); - $this->assertEquals($this->folders[0], $result); + $result = $this->folderMapper->find($id, $userId); + $this->assertEquals($this->folders[0], $result); - } + } - public function testFindNotFound(){ - $userId = 'john'; - $id = 3; - $sql = 'SELECT * FROM `*PREFIX*news_folders` ' . - 'WHERE `id` = ? ' . - 'AND `user_id` = ?'; + public function testFindNotFound(){ + $userId = 'john'; + $id = 3; + $sql = 'SELECT * FROM `*PREFIX*news_folders` ' . + 'WHERE `id` = ? ' . + 'AND `user_id` = ?'; - $this->setMapperResult($sql, [$id, $userId]); + $this->setMapperResult($sql, [$id, $userId]); - $this->setExpectedException('\OCP\AppFramework\Db\DoesNotExistException'); - $this->folderMapper->find($id, $userId); - } + $this->setExpectedException('\OCP\AppFramework\Db\DoesNotExistException'); + $this->folderMapper->find($id, $userId); + } - public function testFindMoreThanOneResultFound(){ - $userId = 'john'; - $id = 3; - $rows = $this->twoRows; - $sql = 'SELECT * FROM `*PREFIX*news_folders` ' . - 'WHERE `id` = ? ' . - 'AND `user_id` = ?'; + public function testFindMoreThanOneResultFound(){ + $userId = 'john'; + $id = 3; + $rows = $this->twoRows; + $sql = 'SELECT * FROM `*PREFIX*news_folders` ' . + 'WHERE `id` = ? ' . + 'AND `user_id` = ?'; - $this->setMapperResult($sql, [$id, $userId], $rows); + $this->setMapperResult($sql, [$id, $userId], $rows); - $this->setExpectedException('\OCP\AppFramework\Db\MultipleObjectsReturnedException'); - $this->folderMapper->find($id, $userId); - } + $this->setExpectedException('\OCP\AppFramework\Db\MultipleObjectsReturnedException'); + $this->folderMapper->find($id, $userId); + } - public function testFindAllFromUser(){ - $userId = 'john'; - $rows = $this->twoRows; - $sql = 'SELECT * FROM `*PREFIX*news_folders` ' . - 'WHERE `user_id` = ? ' . - 'AND `deleted_at` = 0'; + public function testFindAllFromUser(){ + $userId = 'john'; + $rows = $this->twoRows; + $sql = 'SELECT * FROM `*PREFIX*news_folders` ' . + 'WHERE `user_id` = ? ' . + 'AND `deleted_at` = 0'; - $this->setMapperResult($sql, [$userId], $rows); + $this->setMapperResult($sql, [$userId], $rows); - $result = $this->folderMapper->findAllFromUser($userId); - $this->assertEquals($this->folders, $result); - } + $result = $this->folderMapper->findAllFromUser($userId); + $this->assertEquals($this->folders, $result); + } - public function testFindByName(){ - $folderName = 'heheh'; - $userId = 'john'; - $rows = $this->twoRows; - $sql = 'SELECT * FROM `*PREFIX*news_folders` ' . - 'WHERE `name` = ? ' . - 'AND `user_id` = ?'; + public function testFindByName(){ + $folderName = 'heheh'; + $userId = 'john'; + $rows = $this->twoRows; + $sql = 'SELECT * FROM `*PREFIX*news_folders` ' . + 'WHERE `name` = ? ' . + 'AND `user_id` = ?'; - $this->setMapperResult($sql, [$folderName, $userId], $rows); + $this->setMapperResult($sql, [$folderName, $userId], $rows); - $result = $this->folderMapper->findByName($folderName, $userId); - $this->assertEquals($this->folders, $result); - } + $result = $this->folderMapper->findByName($folderName, $userId); + $this->assertEquals($this->folders, $result); + } - public function testDelete(){ - $folder = new Folder(); - $folder->setId(3); + public function testDelete(){ + $folder = new Folder(); + $folder->setId(3); - $sql = 'DELETE FROM `*PREFIX*news_folders` WHERE `id` = ?'; - $arguments = [$folder->getId()]; + $sql = 'DELETE FROM `*PREFIX*news_folders` WHERE `id` = ?'; + $arguments = [$folder->getId()]; - $sql2 = 'DELETE FROM `*PREFIX*news_feeds` WHERE `folder_id` = ?'; + $sql2 = 'DELETE FROM `*PREFIX*news_feeds` WHERE `folder_id` = ?'; - $sql3 = 'DELETE FROM `*PREFIX*news_items` WHERE `feed_id` NOT IN '. - '(SELECT `feeds`.`id` FROM `*PREFIX*news_feeds` `feeds`)'; - $arguments2 = [$folder->getId()]; + $sql3 = 'DELETE FROM `*PREFIX*news_items` WHERE `feed_id` NOT IN '. + '(SELECT `feeds`.`id` FROM `*PREFIX*news_feeds` `feeds`)'; + $arguments2 = [$folder->getId()]; - $this->setMapperResult($sql, $arguments); - $this->setMapperResult($sql2, $arguments2); - $this->setMapperResult($sql3); + $this->setMapperResult($sql, $arguments); + $this->setMapperResult($sql2, $arguments2); + $this->setMapperResult($sql3); - $this->folderMapper->delete($folder); - } + $this->folderMapper->delete($folder); + } - public function testGetPurgeDeleted(){ - $rows = $this->twoRows; - $deleteOlderThan = 110; - $sql = 'SELECT * FROM `*PREFIX*news_folders` ' . - 'WHERE `deleted_at` > 0 ' . - 'AND `deleted_at` < ? '; - $this->setMapperResult($sql, [$deleteOlderThan], $rows); - $result = $this->folderMapper->getToDelete($deleteOlderThan); + public function testGetPurgeDeleted(){ + $rows = $this->twoRows; + $deleteOlderThan = 110; + $sql = 'SELECT * FROM `*PREFIX*news_folders` ' . + 'WHERE `deleted_at` > 0 ' . + 'AND `deleted_at` < ? '; + $this->setMapperResult($sql, [$deleteOlderThan], $rows); + $result = $this->folderMapper->getToDelete($deleteOlderThan); - $this->assertEquals($this->folders, $result); - } + $this->assertEquals($this->folders, $result); + } - public function testGetPurgeDeletedUser(){ - $rows = $this->twoRows; - $deleteOlderThan = 110; - $sql = 'SELECT * FROM `*PREFIX*news_folders` ' . - 'WHERE `deleted_at` > 0 ' . - 'AND `deleted_at` < ? ' . - 'AND `user_id` = ?'; - $this->setMapperResult($sql, [$deleteOlderThan, $this->user], $rows); - $result = $this->folderMapper->getToDelete($deleteOlderThan, $this->user); + public function testGetPurgeDeletedUser(){ + $rows = $this->twoRows; + $deleteOlderThan = 110; + $sql = 'SELECT * FROM `*PREFIX*news_folders` ' . + 'WHERE `deleted_at` > 0 ' . + 'AND `deleted_at` < ? ' . + 'AND `user_id` = ?'; + $this->setMapperResult($sql, [$deleteOlderThan, $this->user], $rows); + $result = $this->folderMapper->getToDelete($deleteOlderThan, $this->user); - $this->assertEquals($this->folders, $result); - } + $this->assertEquals($this->folders, $result); + } - public function testGetAllPurgeDeletedUser(){ - $rows = $this->twoRows; + public function testGetAllPurgeDeletedUser(){ + $rows = $this->twoRows; - $sql = 'SELECT * FROM `*PREFIX*news_folders` ' . - 'WHERE `deleted_at` > 0 ' . - 'AND `user_id` = ?'; - $this->setMapperResult($sql, [$this->user], $rows); - $result = $this->folderMapper->getToDelete(null, $this->user); + $sql = 'SELECT * FROM `*PREFIX*news_folders` ' . + 'WHERE `deleted_at` > 0 ' . + 'AND `user_id` = ?'; + $this->setMapperResult($sql, [$this->user], $rows); + $result = $this->folderMapper->getToDelete(null, $this->user); - $this->assertEquals($this->folders, $result); - } + $this->assertEquals($this->folders, $result); + } - public function testDeleteFromUser(){ - $userId = 'john'; - $sql = 'DELETE FROM `*PREFIX*news_folders` WHERE `user_id` = ?'; + public function testDeleteFromUser(){ + $userId = 'john'; + $sql = 'DELETE FROM `*PREFIX*news_folders` WHERE `user_id` = ?'; - $this->setMapperResult($sql, [$userId]); + $this->setMapperResult($sql, [$userId]); - $this->folderMapper->deleteUser($userId); - } + $this->folderMapper->deleteUser($userId); + } } \ No newline at end of file diff --git a/tests/unit/db/FolderTest.php b/tests/unit/db/FolderTest.php index c4921b0f5..9d55035f1 100644 --- a/tests/unit/db/FolderTest.php +++ b/tests/unit/db/FolderTest.php @@ -17,34 +17,34 @@ namespace OCA\News\Db; class FolderTest extends \PHPUnit_Framework_TestCase { - public function testToAPI() { - $folder = new Folder(); - $folder->setId(3); - $folder->setName('name'); - - $this->assertEquals([ - 'id' => 3, - 'name' => 'name' - ], $folder->toAPI()); - } - - - public function testSerialize() { - $folder = new Folder(); - $folder->setId(3); - $folder->setName('john'); - $folder->setParentId(4); - $folder->setUserId('abc'); - $folder->setOpened(true); - $folder->setDeletedAt(9); - - $this->assertEquals([ - 'id' => 3, - 'parentId' => 4, - 'name' => 'john', - 'userId' => 'abc', - 'opened' => true, - 'deletedAt' => 9, - ], $folder->jsonSerialize()); - } + public function testToAPI() { + $folder = new Folder(); + $folder->setId(3); + $folder->setName('name'); + + $this->assertEquals([ + 'id' => 3, + 'name' => 'name' + ], $folder->toAPI()); + } + + + public function testSerialize() { + $folder = new Folder(); + $folder->setId(3); + $folder->setName('john'); + $folder->setParentId(4); + $folder->setUserId('abc'); + $folder->setOpened(true); + $folder->setDeletedAt(9); + + $this->assertEquals([ + 'id' => 3, + 'parentId' => 4, + 'name' => 'john', + 'userId' => 'abc', + 'opened' => true, + 'deletedAt' => 9, + ], $folder->jsonSerialize()); + } } \ No newline at end of file diff --git a/tests/unit/db/ItemMapperTest.php b/tests/unit/db/ItemMapperTest.php index daff20822..46ab2165a 100644 --- a/tests/unit/db/ItemMapperTest.php +++ b/tests/unit/db/ItemMapperTest.php @@ -16,443 +16,443 @@ namespace OCA\News\Db; class ItemMapperTest extends \Test\AppFramework\Db\MapperTestUtility { - private $mapper; - private $items; - private $newestItemId; - private $limit; - private $user; - private $offset; - private $updatedSince; - private $status; - - - public function setUp() { - parent::setup(); - - $this->mapper = new ItemMapper($this->db); - - // create mock items - $item1 = new Item(); - $item2 = new Item(); - - $this->items = array( - $item1, - $item2 - ); - - $this->userId = 'john'; - $this->id = 3; - $this->folderId = 2; - - $this->row = array( - array('id' => $this->items[0]->getId()), - ); - - $this->rows = array( - array('id' => $this->items[0]->getId()), - array('id' => $this->items[1]->getId()) - ); - - $this->user = 'john'; - $this->limit = 10; - $this->offset = 3; - $this->id = 11; - $this->status = 333; - $this->updatedSince = 323; - $this->newestItemId = 2; - - } - - - private function makeSelectQuery($prependTo, $oldestFirst=false){ - if ($oldestFirst) { - $ordering = 'ASC'; - } else { - $ordering = 'DESC'; - } - - return 'SELECT `items`.* FROM `*PREFIX*news_items` `items` '. - 'JOIN `*PREFIX*news_feeds` `feeds` ' . - 'ON `feeds`.`id` = `items`.`feed_id` '. - 'AND `feeds`.`deleted_at` = 0 ' . - 'AND `feeds`.`user_id` = ? ' . - $prependTo . - 'LEFT OUTER JOIN `*PREFIX*news_folders` `folders` ' . - 'ON `folders`.`id` = `feeds`.`folder_id` ' . - 'WHERE `feeds`.`folder_id` = 0 ' . - 'OR `folders`.`deleted_at` = 0 ' . - 'ORDER BY `items`.`id` ' . $ordering; - } - - private function makeSelectQueryStatus($prependTo, $status, - $oldestFirst=false) { - $status = (int) $status; - - return $this->makeSelectQuery( - 'AND ((`items`.`status` & ' . $status . ') = ' . $status . ') ' . - $prependTo, $oldestFirst - ); - } - - - public function testFind(){ - $sql = $this->makeSelectQuery('AND `items`.`id` = ? '); - - $this->setMapperResult($sql, array($this->userId, $this->id), $this->row); - - $result = $this->mapper->find($this->id, $this->userId); - $this->assertEquals($this->items[0], $result); - } - - - public function testGetStarredCount(){ - $userId = 'john'; - $row = array( - array('size' => 9) - ); - $sql = 'SELECT COUNT(*) AS size FROM `*PREFIX*news_items` `items` '. - 'JOIN `*PREFIX*news_feeds` `feeds` ' . - 'ON `feeds`.`id` = `items`.`feed_id` '. - 'AND `feeds`.`deleted_at` = 0 ' . - 'AND `feeds`.`user_id` = ? ' . - 'AND ((`items`.`status` & ' . StatusFlag::STARRED . ') = ' . - StatusFlag::STARRED . ')' . - 'LEFT OUTER JOIN `*PREFIX*news_folders` `folders` ' . - 'ON `folders`.`id` = `feeds`.`folder_id` ' . - 'WHERE `feeds`.`folder_id` = 0 ' . - 'OR `folders`.`deleted_at` = 0'; - - $this->setMapperResult($sql, array($userId), $row); - - $result = $this->mapper->starredCount($userId); - $this->assertEquals($row[0]['size'], $result); - } - - - public function testReadAll(){ - $sql = 'UPDATE `*PREFIX*news_items` ' . - 'SET `status` = `status` & ? ' . - ', `last_modified` = ? ' . - 'WHERE `feed_id` IN (' . - 'SELECT `id` FROM `*PREFIX*news_feeds` ' . - 'WHERE `user_id` = ? ' . - ') '. - 'AND `id` <= ?'; - $params = array(~StatusFlag::UNREAD, $this->updatedSince, $this->user, 3); - $this->setMapperResult($sql, $params); - $this->mapper->readAll(3, $this->updatedSince, $this->user); - } - - - public function testReadFolder(){ - $sql = 'UPDATE `*PREFIX*news_items` ' . - 'SET `status` = `status` & ? ' . - ', `last_modified` = ? ' . - 'WHERE `feed_id` IN (' . - 'SELECT `id` FROM `*PREFIX*news_feeds` ' . - 'WHERE `folder_id` = ? ' . - 'AND `user_id` = ? ' . - ') '. - 'AND `id` <= ?'; - $params = array(~StatusFlag::UNREAD, $this->updatedSince, 3, $this->user, 6); - $this->setMapperResult($sql, $params); - $this->mapper->readFolder(3, 6, $this->updatedSince, $this->user); - } - - - public function testReadFeed(){ - $sql = 'UPDATE `*PREFIX*news_items` ' . - 'SET `status` = `status` & ? ' . - ', `last_modified` = ? ' . - 'WHERE `feed_id` = ? ' . - 'AND `id` <= ? ' . - 'AND EXISTS (' . - 'SELECT * FROM `*PREFIX*news_feeds` ' . - 'WHERE `user_id` = ? ' . - 'AND `id` = ? ) '; - $params = array(~StatusFlag::UNREAD, $this->updatedSince, 3, 6, - $this->user, 3); - $this->setMapperResult($sql, $params); - $this->mapper->readFeed(3, 6, $this->updatedSince, $this->user); - } - - - public function testFindAllNew(){ - $sql = 'AND `items`.`last_modified` >= ? '; - $sql = $this->makeSelectQueryStatus($sql, $this->status); - $params = array($this->user, $this->updatedSince); - - $this->setMapperResult($sql, $params, $this->rows); - $result = $this->mapper->findAllNew($this->updatedSince, - $this->status, $this->user); - - $this->assertEquals($this->items, $result); - } - - - public function testFindAllNewFolder(){ - $sql = 'AND `feeds`.`folder_id` = ? ' . - 'AND `items`.`last_modified` >= ? '; - $sql = $this->makeSelectQueryStatus($sql, $this->status); - - $params = array($this->user, $this->id, $this->updatedSince); - $this->setMapperResult($sql, $params, $this->rows); - $result = $this->mapper->findAllNewFolder($this->id, $this->updatedSince, - $this->status, $this->user); - - $this->assertEquals($this->items, $result); - } - - - public function testFindAllNewFeed(){ - $sql = 'AND `items`.`feed_id` = ? ' . - 'AND `items`.`last_modified` >= ? '; - $sql = $this->makeSelectQueryStatus($sql, $this->status); - $params = array($this->user, $this->id, $this->updatedSince); - - $this->setMapperResult($sql, $params, $this->rows); - $result = $this->mapper->findAllNewFeed($this->id, $this->updatedSince, - $this->status, $this->user); - - $this->assertEquals($this->items, $result); - } - - - public function testFindAllUnreadOrStarred(){ - $status = StatusFlag::UNREAD | StatusFlag::STARRED; - $sql = 'AND ((`items`.`status` & ' . $status . ') > 0) '; - $sql = $this->makeSelectQuery($sql); - $params = array($this->user); - $this->setMapperResult($sql, $params, $this->rows); - $result = $this->mapper->findAllUnreadOrStarred($this->user); - - $this->assertEquals($this->items, $result); - } - - - public function testFindAllFeed(){ - $sql = 'AND `items`.`feed_id` = ? ' . - 'AND `items`.`id` < ? '; - $sql = $this->makeSelectQueryStatus($sql, $this->status); - $params = array($this->user, $this->id, $this->offset); - $this->setMapperResult($sql, $params, $this->rows); - $result = $this->mapper->findAllFeed($this->id, $this->limit, - $this->offset, $this->status, false, $this->user); - - $this->assertEquals($this->items, $result); - } - - - public function testFindAllFeedOldestFirst(){ - $sql = 'AND `items`.`feed_id` = ? ' . - 'AND `items`.`id` > ? '; - $sql = $this->makeSelectQueryStatus($sql, $this->status, true); - $params = array($this->user, $this->id, $this->offset); - $this->setMapperResult($sql, $params, $this->rows); - $result = $this->mapper->findAllFeed($this->id, $this->limit, - $this->offset, $this->status, true, $this->user); - - $this->assertEquals($this->items, $result); - } + private $mapper; + private $items; + private $newestItemId; + private $limit; + private $user; + private $offset; + private $updatedSince; + private $status; + + + public function setUp() { + parent::setup(); + + $this->mapper = new ItemMapper($this->db); + + // create mock items + $item1 = new Item(); + $item2 = new Item(); + + $this->items = array( + $item1, + $item2 + ); + + $this->userId = 'john'; + $this->id = 3; + $this->folderId = 2; + + $this->row = array( + array('id' => $this->items[0]->getId()), + ); + + $this->rows = array( + array('id' => $this->items[0]->getId()), + array('id' => $this->items[1]->getId()) + ); + + $this->user = 'john'; + $this->limit = 10; + $this->offset = 3; + $this->id = 11; + $this->status = 333; + $this->updatedSince = 323; + $this->newestItemId = 2; + + } + + + private function makeSelectQuery($prependTo, $oldestFirst=false){ + if ($oldestFirst) { + $ordering = 'ASC'; + } else { + $ordering = 'DESC'; + } + + return 'SELECT `items`.* FROM `*PREFIX*news_items` `items` '. + 'JOIN `*PREFIX*news_feeds` `feeds` ' . + 'ON `feeds`.`id` = `items`.`feed_id` '. + 'AND `feeds`.`deleted_at` = 0 ' . + 'AND `feeds`.`user_id` = ? ' . + $prependTo . + 'LEFT OUTER JOIN `*PREFIX*news_folders` `folders` ' . + 'ON `folders`.`id` = `feeds`.`folder_id` ' . + 'WHERE `feeds`.`folder_id` = 0 ' . + 'OR `folders`.`deleted_at` = 0 ' . + 'ORDER BY `items`.`id` ' . $ordering; + } + + private function makeSelectQueryStatus($prependTo, $status, + $oldestFirst=false) { + $status = (int) $status; + + return $this->makeSelectQuery( + 'AND ((`items`.`status` & ' . $status . ') = ' . $status . ') ' . + $prependTo, $oldestFirst + ); + } + + + public function testFind(){ + $sql = $this->makeSelectQuery('AND `items`.`id` = ? '); + + $this->setMapperResult($sql, array($this->userId, $this->id), $this->row); + + $result = $this->mapper->find($this->id, $this->userId); + $this->assertEquals($this->items[0], $result); + } + + + public function testGetStarredCount(){ + $userId = 'john'; + $row = array( + array('size' => 9) + ); + $sql = 'SELECT COUNT(*) AS size FROM `*PREFIX*news_items` `items` '. + 'JOIN `*PREFIX*news_feeds` `feeds` ' . + 'ON `feeds`.`id` = `items`.`feed_id` '. + 'AND `feeds`.`deleted_at` = 0 ' . + 'AND `feeds`.`user_id` = ? ' . + 'AND ((`items`.`status` & ' . StatusFlag::STARRED . ') = ' . + StatusFlag::STARRED . ')' . + 'LEFT OUTER JOIN `*PREFIX*news_folders` `folders` ' . + 'ON `folders`.`id` = `feeds`.`folder_id` ' . + 'WHERE `feeds`.`folder_id` = 0 ' . + 'OR `folders`.`deleted_at` = 0'; + + $this->setMapperResult($sql, array($userId), $row); + + $result = $this->mapper->starredCount($userId); + $this->assertEquals($row[0]['size'], $result); + } + + + public function testReadAll(){ + $sql = 'UPDATE `*PREFIX*news_items` ' . + 'SET `status` = `status` & ? ' . + ', `last_modified` = ? ' . + 'WHERE `feed_id` IN (' . + 'SELECT `id` FROM `*PREFIX*news_feeds` ' . + 'WHERE `user_id` = ? ' . + ') '. + 'AND `id` <= ?'; + $params = array(~StatusFlag::UNREAD, $this->updatedSince, $this->user, 3); + $this->setMapperResult($sql, $params); + $this->mapper->readAll(3, $this->updatedSince, $this->user); + } + + + public function testReadFolder(){ + $sql = 'UPDATE `*PREFIX*news_items` ' . + 'SET `status` = `status` & ? ' . + ', `last_modified` = ? ' . + 'WHERE `feed_id` IN (' . + 'SELECT `id` FROM `*PREFIX*news_feeds` ' . + 'WHERE `folder_id` = ? ' . + 'AND `user_id` = ? ' . + ') '. + 'AND `id` <= ?'; + $params = array(~StatusFlag::UNREAD, $this->updatedSince, 3, $this->user, 6); + $this->setMapperResult($sql, $params); + $this->mapper->readFolder(3, 6, $this->updatedSince, $this->user); + } + + + public function testReadFeed(){ + $sql = 'UPDATE `*PREFIX*news_items` ' . + 'SET `status` = `status` & ? ' . + ', `last_modified` = ? ' . + 'WHERE `feed_id` = ? ' . + 'AND `id` <= ? ' . + 'AND EXISTS (' . + 'SELECT * FROM `*PREFIX*news_feeds` ' . + 'WHERE `user_id` = ? ' . + 'AND `id` = ? ) '; + $params = array(~StatusFlag::UNREAD, $this->updatedSince, 3, 6, + $this->user, 3); + $this->setMapperResult($sql, $params); + $this->mapper->readFeed(3, 6, $this->updatedSince, $this->user); + } + + + public function testFindAllNew(){ + $sql = 'AND `items`.`last_modified` >= ? '; + $sql = $this->makeSelectQueryStatus($sql, $this->status); + $params = array($this->user, $this->updatedSince); + + $this->setMapperResult($sql, $params, $this->rows); + $result = $this->mapper->findAllNew($this->updatedSince, + $this->status, $this->user); + + $this->assertEquals($this->items, $result); + } + + + public function testFindAllNewFolder(){ + $sql = 'AND `feeds`.`folder_id` = ? ' . + 'AND `items`.`last_modified` >= ? '; + $sql = $this->makeSelectQueryStatus($sql, $this->status); + + $params = array($this->user, $this->id, $this->updatedSince); + $this->setMapperResult($sql, $params, $this->rows); + $result = $this->mapper->findAllNewFolder($this->id, $this->updatedSince, + $this->status, $this->user); + + $this->assertEquals($this->items, $result); + } + + + public function testFindAllNewFeed(){ + $sql = 'AND `items`.`feed_id` = ? ' . + 'AND `items`.`last_modified` >= ? '; + $sql = $this->makeSelectQueryStatus($sql, $this->status); + $params = array($this->user, $this->id, $this->updatedSince); + + $this->setMapperResult($sql, $params, $this->rows); + $result = $this->mapper->findAllNewFeed($this->id, $this->updatedSince, + $this->status, $this->user); + + $this->assertEquals($this->items, $result); + } + + + public function testFindAllUnreadOrStarred(){ + $status = StatusFlag::UNREAD | StatusFlag::STARRED; + $sql = 'AND ((`items`.`status` & ' . $status . ') > 0) '; + $sql = $this->makeSelectQuery($sql); + $params = array($this->user); + $this->setMapperResult($sql, $params, $this->rows); + $result = $this->mapper->findAllUnreadOrStarred($this->user); + + $this->assertEquals($this->items, $result); + } + + + public function testFindAllFeed(){ + $sql = 'AND `items`.`feed_id` = ? ' . + 'AND `items`.`id` < ? '; + $sql = $this->makeSelectQueryStatus($sql, $this->status); + $params = array($this->user, $this->id, $this->offset); + $this->setMapperResult($sql, $params, $this->rows); + $result = $this->mapper->findAllFeed($this->id, $this->limit, + $this->offset, $this->status, false, $this->user); + + $this->assertEquals($this->items, $result); + } + + + public function testFindAllFeedOldestFirst(){ + $sql = 'AND `items`.`feed_id` = ? ' . + 'AND `items`.`id` > ? '; + $sql = $this->makeSelectQueryStatus($sql, $this->status, true); + $params = array($this->user, $this->id, $this->offset); + $this->setMapperResult($sql, $params, $this->rows); + $result = $this->mapper->findAllFeed($this->id, $this->limit, + $this->offset, $this->status, true, $this->user); + + $this->assertEquals($this->items, $result); + } - public function testFindAllFeedOffsetZero(){ - $sql = 'AND `items`.`feed_id` = ? '; - $sql = $this->makeSelectQueryStatus($sql, $this->status); - $params = array($this->user, $this->id); - $this->setMapperResult($sql, $params, $this->rows); - $result = $this->mapper->findAllFeed($this->id, $this->limit, - 0, $this->status, false, $this->user); + public function testFindAllFeedOffsetZero(){ + $sql = 'AND `items`.`feed_id` = ? '; + $sql = $this->makeSelectQueryStatus($sql, $this->status); + $params = array($this->user, $this->id); + $this->setMapperResult($sql, $params, $this->rows); + $result = $this->mapper->findAllFeed($this->id, $this->limit, + 0, $this->status, false, $this->user); - $this->assertEquals($this->items, $result); - } + $this->assertEquals($this->items, $result); + } - public function testFindAllFolder(){ - $sql = 'AND `feeds`.`folder_id` = ? ' . - 'AND `items`.`id` < ? '; - $sql = $this->makeSelectQueryStatus($sql, $this->status); - $params = array($this->user, $this->id, - $this->offset); - $this->setMapperResult($sql, $params, $this->rows); - $result = $this->mapper->findAllFolder($this->id, $this->limit, - $this->offset, $this->status, false, $this->user); + public function testFindAllFolder(){ + $sql = 'AND `feeds`.`folder_id` = ? ' . + 'AND `items`.`id` < ? '; + $sql = $this->makeSelectQueryStatus($sql, $this->status); + $params = array($this->user, $this->id, + $this->offset); + $this->setMapperResult($sql, $params, $this->rows); + $result = $this->mapper->findAllFolder($this->id, $this->limit, + $this->offset, $this->status, false, $this->user); - $this->assertEquals($this->items, $result); - } + $this->assertEquals($this->items, $result); + } - public function testFindAllFolderOldestFirst(){ - $sql = 'AND `feeds`.`folder_id` = ? ' . - 'AND `items`.`id` > ? '; - $sql = $this->makeSelectQueryStatus($sql, $this->status, true); - $params = array($this->user, $this->id, - $this->offset); - $this->setMapperResult($sql, $params, $this->rows); - $result = $this->mapper->findAllFolder($this->id, $this->limit, - $this->offset, $this->status, true, $this->user); + public function testFindAllFolderOldestFirst(){ + $sql = 'AND `feeds`.`folder_id` = ? ' . + 'AND `items`.`id` > ? '; + $sql = $this->makeSelectQueryStatus($sql, $this->status, true); + $params = array($this->user, $this->id, + $this->offset); + $this->setMapperResult($sql, $params, $this->rows); + $result = $this->mapper->findAllFolder($this->id, $this->limit, + $this->offset, $this->status, true, $this->user); - $this->assertEquals($this->items, $result); - } + $this->assertEquals($this->items, $result); + } - public function testFindAllFolderOffsetZero(){ - $sql = 'AND `feeds`.`folder_id` = ? '; - $sql = $this->makeSelectQueryStatus($sql, $this->status); - $params = array($this->user, $this->id); - $this->setMapperResult($sql, $params, $this->rows); - $result = $this->mapper->findAllFolder($this->id, $this->limit, - 0, $this->status, false, $this->user); + public function testFindAllFolderOffsetZero(){ + $sql = 'AND `feeds`.`folder_id` = ? '; + $sql = $this->makeSelectQueryStatus($sql, $this->status); + $params = array($this->user, $this->id); + $this->setMapperResult($sql, $params, $this->rows); + $result = $this->mapper->findAllFolder($this->id, $this->limit, + 0, $this->status, false, $this->user); - $this->assertEquals($this->items, $result); - } + $this->assertEquals($this->items, $result); + } - public function testFindAll(){ - $sql = 'AND `items`.`id` < ? '; - $sql = $this->makeSelectQueryStatus($sql, $this->status); - $params = array($this->user, $this->offset); - $this->setMapperResult($sql, $params, $this->rows); - $result = $this->mapper->findAll($this->limit, - $this->offset, $this->status, false, $this->user); + public function testFindAll(){ + $sql = 'AND `items`.`id` < ? '; + $sql = $this->makeSelectQueryStatus($sql, $this->status); + $params = array($this->user, $this->offset); + $this->setMapperResult($sql, $params, $this->rows); + $result = $this->mapper->findAll($this->limit, + $this->offset, $this->status, false, $this->user); - $this->assertEquals($this->items, $result); - } + $this->assertEquals($this->items, $result); + } - public function testFindAllOldestFirst(){ - $sql = 'AND `items`.`id` > ? '; - $sql = $this->makeSelectQueryStatus($sql, $this->status, true); - $params = array($this->user, $this->offset); - $this->setMapperResult($sql, $params, $this->rows); - $result = $this->mapper->findAll($this->limit, - $this->offset, $this->status, true, $this->user); + public function testFindAllOldestFirst(){ + $sql = 'AND `items`.`id` > ? '; + $sql = $this->makeSelectQueryStatus($sql, $this->status, true); + $params = array($this->user, $this->offset); + $this->setMapperResult($sql, $params, $this->rows); + $result = $this->mapper->findAll($this->limit, + $this->offset, $this->status, true, $this->user); - $this->assertEquals($this->items, $result); - } + $this->assertEquals($this->items, $result); + } - public function testFindAllOffsetZero(){ - $sql = $this->makeSelectQueryStatus('', $this->status); - $params = array($this->user); - $this->setMapperResult($sql, $params, $this->rows); - $result = $this->mapper->findAll($this->limit, - 0, $this->status, false, $this->user); + public function testFindAllOffsetZero(){ + $sql = $this->makeSelectQueryStatus('', $this->status); + $params = array($this->user); + $this->setMapperResult($sql, $params, $this->rows); + $result = $this->mapper->findAll($this->limit, + 0, $this->status, false, $this->user); - $this->assertEquals($this->items, $result); - } + $this->assertEquals($this->items, $result); + } - public function testFindByGuidHash(){ - $hash = md5('test'); - $feedId = 3; - $sql = $this->makeSelectQuery( - 'AND `items`.`guid_hash` = ? ' . - 'AND `feeds`.`id` = ? '); + public function testFindByGuidHash(){ + $hash = md5('test'); + $feedId = 3; + $sql = $this->makeSelectQuery( + 'AND `items`.`guid_hash` = ? ' . + 'AND `feeds`.`id` = ? '); - $this->setMapperResult($sql, array($this->userId, $hash, $feedId), $this->row); + $this->setMapperResult($sql, array($this->userId, $hash, $feedId), $this->row); - $result = $this->mapper->findByGuidHash($hash, $feedId, $this->userId); - $this->assertEquals($this->items[0], $result); - } + $result = $this->mapper->findByGuidHash($hash, $feedId, $this->userId); + $this->assertEquals($this->items[0], $result); + } - public function testDeleteReadOlderThanThresholdDoesNotDeleteBelowThreshold(){ - $status = StatusFlag::STARRED | StatusFlag::UNREAD; - $sql = 'SELECT COUNT(*) - `feeds`.`articles_per_update` AS `size`, ' . - '`items`.`feed_id` AS `feed_id` ' . - 'FROM `*PREFIX*news_items` `items` ' . - 'JOIN `*PREFIX*news_feeds` `feeds` ' . - 'ON `feeds`.`id` = `items`.`feed_id` ' . - 'WHERE NOT ((`items`.`status` & ?) > 0) ' . - 'GROUP BY `items`.`feed_id`, `feeds`.`articles_per_update` ' . - 'HAVING COUNT(*) > ?'; + public function testDeleteReadOlderThanThresholdDoesNotDeleteBelowThreshold(){ + $status = StatusFlag::STARRED | StatusFlag::UNREAD; + $sql = 'SELECT COUNT(*) - `feeds`.`articles_per_update` AS `size`, ' . + '`items`.`feed_id` AS `feed_id` ' . + 'FROM `*