From a97dd58e3b499b60ac8b37786d402d7f2e371a88 Mon Sep 17 00:00:00 2001 From: Daniel Opitz Date: Mon, 14 Aug 2017 10:34:53 +0200 Subject: Split binary to booleans (#203) * replaced old status with 2 flags for unread and starred * add fields to db, replace int(1,0) with booleans in sql queries, removed StatusFlags class + refactor code relying to it * add repair step for migration * again use integer(1,0) instead of bool in sql queries, because of sqlite doesn't support true/false * add/fix unit tests for new boolean status * set unread/starred flags as statements in sql * fixed mysql unknown column items.unread, fixed marking of read items on repair step * remove unnecessary bool casts * add empty checks to Items::is* methods * update migration to use native sql instead of the querybuilder * don't cast the flags manually, let the api do the work --- tests/Integration/Db/FeedMapperTest.php | 192 +++++++- tests/Integration/Db/ItemMapperTest.php | 13 +- tests/Integration/Fixtures/ItemFixture.php | 3 +- tests/Integration/Fixtures/data/default.php | 14 +- tests/Integration/Fixtures/data/readitem.php | 12 +- tests/Integration/IntegrationTest.php | 5 +- tests/Unit/Controller/EntityApiSerializerTest.php | 10 +- tests/Unit/Db/FeedMapperTest.php | 303 ------------ tests/Unit/Db/ItemMapperTest.php | 550 ---------------------- tests/Unit/Db/ItemTest.php | 31 +- tests/Unit/Db/Mysql/ItemMapperTest.php | 119 ----- tests/Unit/Fetcher/FeedFetcherTest.php | 2 +- tests/Unit/Migration/MigrateStatusFlagsTest.php | 99 ++++ tests/Unit/Service/FeedServiceTest.php | 14 +- tests/Unit/Service/ItemServiceTest.php | 44 +- tests/Unit/Service/StatusFlagTest.php | 57 --- 16 files changed, 359 insertions(+), 1109 deletions(-) delete mode 100644 tests/Unit/Db/FeedMapperTest.php delete mode 100644 tests/Unit/Db/ItemMapperTest.php delete mode 100644 tests/Unit/Db/Mysql/ItemMapperTest.php create mode 100644 tests/Unit/Migration/MigrateStatusFlagsTest.php delete mode 100644 tests/Unit/Service/StatusFlagTest.php (limited to 'tests') diff --git a/tests/Integration/Db/FeedMapperTest.php b/tests/Integration/Db/FeedMapperTest.php index 0c13280f5..b429149a4 100644 --- a/tests/Integration/Db/FeedMapperTest.php +++ b/tests/Integration/Db/FeedMapperTest.php @@ -6,49 +6,231 @@ * later. See the COPYING file. * * @author Bernhard Posselt + * @author Daniel Opitz * @copyright Bernhard Posselt 2015 + * @copyright Daniel Opitz 2017 */ namespace OCA\News\Db; -use \OCA\News\Tests\Integration\IntegrationTest; +use OCA\News\Tests\Integration\Fixtures\FeedFixture; +use OCA\News\Tests\Integration\IntegrationTest; class FeedMapperTest extends IntegrationTest { - public function testFind () { + $feed = new FeedFixture(); + $feed = $this->feedMapper->insert($feed); + + $fetched = $this->feedMapper->find($feed->getId(), $this->user); + + $this->assertInstanceOf(Feed::class, $fetched); + $this->assertEquals($feed->getLink(), $fetched->getLink()); + } + + /** + * @expectedException OCP\AppFramework\Db\DoesNotExistException + */ + public function testFindNotExisting () { + $this->feedMapper->find(0, $this->user); } - /* TBD public function testFindAll () { + $feeds = [ + [ + 'userId' => $this->user, + 'items' => [] + ], + [ + 'userId' => 'john', + 'items' => [] + ] + ]; + $this->loadFeedFixtures($feeds); + + $fetched = $this->feedMapper->findAll(); + + $this->assertInternalType('array', $fetched); + $this->assertCount(2, $fetched); + $this->assertContainsOnlyInstancesOf(Feed::class, $fetched); + + $this->tearDownUser('john'); + } + public function testFindAllEmpty () { + $feeds = $this->feedMapper->findAll(); + + $this->assertInternalType('array', $feeds); + $this->assertCount(0, $feeds); } public function testFindAllFromUser () { + $feeds = [ + [ + 'userId' => $this->user, + 'items' => [] + ], + [ + 'userId' => 'john', + 'items' => [] + ] + ]; + $this->loadFeedFixtures($feeds); + + $fetched = $this->feedMapper->findAllFromUser($this->user); + + $this->assertInternalType('array', $fetched); + $this->assertCount(1, $fetched); + $this->assertContainsOnlyInstancesOf(Feed::class, $fetched); + + $this->tearDownUser('john'); + } + + public function testFindAllFromUserNotExisting () { + $fetched = $this->feedMapper->findAllFromUser('notexistinguser'); + + $this->assertInternalType('array', $fetched); + $this->assertCount(0, $fetched); } public function testFindByUrlHash () { + $feed = new FeedFixture([ + 'urlHash' => 'someTestHash', + 'title' => 'Some Test Title' + ]); + $feed = $this->feedMapper->insert($feed); + + $fetched = $this->feedMapper->findByUrlHash($feed->getUrlHash(), $this->user); + $this->assertInstanceOf(Feed::class, $fetched); + $this->assertEquals($feed->getTitle(), $fetched->getTitle()); } + /** + * @expectedException OCP\AppFramework\Db\MultipleObjectsReturnedException + */ + public function testFindByUrlHashMoreThanOneResult () { + $feed1 = $this->feedMapper->insert(new FeedFixture([ + 'urlHash' => 'someTestHash' + ])); + $feed2 = $this->feedMapper->insert(new FeedFixture([ + 'urlHash' => 'someTestHash' + ])); + + $this->feedMapper->findByUrlHash($feed1->getUrlHash(), $this->user); + } - public function testDelete () { + /** + * @expectedException OCP\AppFramework\Db\DoesNotExistException + */ + public function testFindByUrlHashNotExisting () { + $this->feedMapper->findByUrlHash('some random hash', $this->user); } + public function testDelete () { + $this->loadFixtures('default'); + + $feeds = $this->feedMapper->findAllFromUser($this->user); + $this->assertCount(4, $feeds); + + $feed = reset($feeds); + + $items = $this->itemMapper->findAllFeed( + $feed->getId(), 100, 0, true, false, $this->user + ); + $this->assertCount(7, $items); + + $this->feedMapper->delete($feed); + + $this->assertCount(3, $this->feedMapper->findAllFromUser($this->user)); + + $items = $this->itemMapper->findAllFeed( + $feed->getId(), 100, 0, true, false, $this->user + ); + $this->assertCount(0, $items); + } + public function testGetToDelete () { + $this->loadFeedFixtures([ + ['deletedAt' => 1], + ['deletedAt' => 0], + ['deletedAt' => 1, 'userId' => 'john'], + ['deletedAt' => 1000] + ]); + + $fetched = $this->feedMapper->getToDelete(); + + $this->assertInternalType('array', $fetched); + $this->assertCount(3, $fetched); + $this->assertContainsOnlyInstancesOf(Feed::class, $fetched); + + $this->tearDownUser('john'); + } + + public function testGetToDeleteOlderThan () { + $this->loadFeedFixtures([ + ['deletedAt' => 1], + ['deletedAt' => 0], + ['deletedAt' => 1, 'userId' => 'john'], + ['deletedAt' => 1000] + ]); + + $fetched = $this->feedMapper->getToDelete(1000); + + $this->assertInternalType('array', $fetched); + $this->assertCount(2, $fetched); + $this->assertContainsOnlyInstancesOf(Feed::class, $fetched); + + $this->tearDownUser('john'); + } + public function testGetToDeleteUser () { + $this->loadFeedFixtures([ + ['deletedAt' => 1], + ['deletedAt' => 0], + ['deletedAt' => 1, 'userId' => 'john'], + ['deletedAt' => 1000] + ]); + + $fetched = $this->feedMapper->getToDelete(2000, $this->user); + + $this->assertInternalType('array', $fetched); + $this->assertCount(2, $fetched); + $this->assertContainsOnlyInstancesOf(Feed::class, $fetched); + + $this->tearDownUser('john'); } + public function testGetToDeleteEmpty () { + $fetched = $this->feedMapper->getToDelete(); + + $this->assertInternalType('array', $fetched); + $this->assertCount(0, $fetched); + } public function testDeleteUser () { + $this->loadFixtures('default'); - }*/ + $this->assertCount(4, $this->feedMapper->findAllFromUser($this->user)); + $items = $this->itemMapper->findAll(100, 0, 0, true, false, $this->user); + $this->assertCount(9, $items); + $this->feedMapper->deleteUser($this->user); + + $this->assertCount(0, $this->feedMapper->findAllFromUser($this->user)); + + $items = $this->itemMapper->findAll(100, 0, 0, true, false, $this->user); + $this->assertCount(0, $items); + } + + public function testDeleteUserNotExisting () { + $this->feedMapper->deleteUser('notexistinguser'); + } } diff --git a/tests/Integration/Db/ItemMapperTest.php b/tests/Integration/Db/ItemMapperTest.php index 6b621e070..fa9cc7d25 100644 --- a/tests/Integration/Db/ItemMapperTest.php +++ b/tests/Integration/Db/ItemMapperTest.php @@ -112,9 +112,8 @@ class ItemMapperTest extends IntegrationTest { $this->itemMapper->readAll(PHP_INT_MAX, 10, $this->user); - $status = StatusFlag::UNREAD; $items = $this->itemMapper->findAll( - 30, 0, $status, false, $this->user + 30, 0, 0, false, false, $this->user ); $this->assertEquals(0, count($items)); @@ -144,9 +143,8 @@ class ItemMapperTest extends IntegrationTest { $folderId, PHP_INT_MAX, 10, $this->user ); - $status = StatusFlag::UNREAD; $items = $this->itemMapper->findAll( - 30, 0, $status, false, $this->user + 30, 0, 0, false, false, $this->user ); $this->assertEquals(1, count($items)); @@ -176,9 +174,8 @@ class ItemMapperTest extends IntegrationTest { $feedId, PHP_INT_MAX, 10, $this->user ); - $status = StatusFlag::UNREAD; $items = $this->itemMapper->findAll( - 30, 0, $status, false, $this->user + 30, 0, 0, false, false, $this->user ); $this->assertEquals(2, count($items)); @@ -246,7 +243,7 @@ class ItemMapperTest extends IntegrationTest { // assert that all test user's same items are read $items = $this->itemMapper->where(['feedId' => $feed->getId(), 'title' => 'blubb']); foreach ($items as $item) { - $this->assertTrue($item->isRead()); + $this->assertFalse($item->isUnread()); } // assert that a different item is not read @@ -277,7 +274,7 @@ class ItemMapperTest extends IntegrationTest { if ($item->getId() === $duplicateItem->getId()) { $this->assertTrue($item->isUnread()); } else { - $this->assertTrue($item->isRead()); + $this->assertFalse($item->isUnread()); } } diff --git a/tests/Integration/Fixtures/ItemFixture.php b/tests/Integration/Fixtures/ItemFixture.php index 2dfe79c28..0832b0ef4 100644 --- a/tests/Integration/Fixtures/ItemFixture.php +++ b/tests/Integration/Fixtures/ItemFixture.php @@ -29,7 +29,8 @@ class ItemFixture extends Item { 'enclosureMime' => 'video/mpeg', 'enclosureLink' => 'http://google.de/web.webm', 'feedId' => 0, - 'status' => 2, + 'unread' => true, + 'starred' => false, 'lastModified' => 113, 'rtl' => false, ], $defaults); diff --git a/tests/Integration/Fixtures/data/default.php b/tests/Integration/Fixtures/data/default.php index d87bb1e6f..862515b12 100644 --- a/tests/Integration/Fixtures/data/default.php +++ b/tests/Integration/Fixtures/data/default.php @@ -20,12 +20,12 @@ return [ 'articlesPerUpdate' => 1, 'items' => [ ['title' => 'a title1', 'guid' => 'abc'], - ['title' => 'a title2', 'status' => 4, 'guid' => 'def'], - ['title' => 'a title3', 'status' => 6, 'guid' => 'gih'], - ['title' => 'del1', 'status' => 0], - ['title' => 'del2', 'status' => 0], - ['title' => 'del3', 'status' => 0], - ['title' => 'del4', 'status' => 0] + ['title' => 'a title2', 'unread' => false, 'starred' => true, 'guid' => 'def'], + ['title' => 'a title3', 'unread' => true, 'starred' => true, 'guid' => 'gih'], + ['title' => 'del1', 'unread' => false, 'starred' => false], + ['title' => 'del2', 'unread' => false, 'starred' => false], + ['title' => 'del3', 'unread' => false, 'starred' => false], + ['title' => 'del4', 'unread' => false, 'starred' => false] ] ], [ @@ -69,7 +69,7 @@ return [ 'title' => 'fourth feed', 'url' => 'http://blog.fefe.de', 'items' => [ - ['title' => 'no folder', 'status' => 0] + ['title' => 'no folder', 'unread' => false, 'starred' => false] ] ] ] diff --git a/tests/Integration/Fixtures/data/readitem.php b/tests/Integration/Fixtures/data/readitem.php index 0a587bad7..8f953a845 100644 --- a/tests/Integration/Fixtures/data/readitem.php +++ b/tests/Integration/Fixtures/data/readitem.php @@ -15,18 +15,18 @@ return [ 'title' => 'john feed', 'userId' => 'john', 'items' => [ - ['title' => 'blubb', 'status' => 2], - ['title' => 'blubb', 'status' => 2] + ['title' => 'blubb', 'unread' => true, 'starred' => false], + ['title' => 'blubb', 'unread' => true, 'starred' => false] ] ], [ 'title' => 'test feed', 'userId' => 'test', 'items' => [ - ['title' => 'blubb', 'status' => 2], - ['title' => 'blubbs', 'status' => 2], - ['title' => 'blubb', 'status' => 2], - ['title' => 'blubb', 'status' => 2] + ['title' => 'blubb', 'unread' => true, 'starred' => false], + ['title' => 'blubbs', 'unread' => true, 'starred' => false], + ['title' => 'blubb', 'unread' => true, 'starred' => false], + ['title' => 'blubb', 'unread' => true, 'starred' => false] ] ] ] diff --git a/tests/Integration/IntegrationTest.php b/tests/Integration/IntegrationTest.php index c14d3007a..f14a1263b 100644 --- a/tests/Integration/IntegrationTest.php +++ b/tests/Integration/IntegrationTest.php @@ -124,7 +124,10 @@ abstract class IntegrationTest extends \Test\TestCase { $feed = new FeedFixture($feedFixture); $feed->setFolderId($folderId); $feedId = $this->loadFixture($feed); - $this->loadItemFixtures($feedFixture['items'], $feedId); + + if (!empty($feedFixture['items'])) { + $this->loadItemFixtures($feedFixture['items'], $feedId); + } } } diff --git a/tests/Unit/Controller/EntityApiSerializerTest.php b/tests/Unit/Controller/EntityApiSerializerTest.php index ec357e7f2..63de1ed7e 100644 --- a/tests/Unit/Controller/EntityApiSerializerTest.php +++ b/tests/Unit/Controller/EntityApiSerializerTest.php @@ -29,7 +29,7 @@ class EntityApiSerializerTest extends \PHPUnit_Framework_TestCase { public function testSerializeSingle() { $item = new Item(); - $item->setUnread(); + $item->setUnread(true); $serializer = new EntityApiSerializer('items'); $result = $serializer->serialize($item); @@ -40,10 +40,10 @@ class EntityApiSerializerTest extends \PHPUnit_Framework_TestCase { public function testSerializeMultiple() { $item = new Item(); - $item->setUnread(); + $item->setUnread(true); $item2 = new Item(); - $item2->setRead(); + $item2->setUnread(false); $serializer = new EntityApiSerializer('items'); @@ -66,10 +66,10 @@ class EntityApiSerializerTest extends \PHPUnit_Framework_TestCase { public function testCompleteArraysTransformed() { $item = new Item(); - $item->setUnread(); + $item->setUnread(true); $item2 = new Item(); - $item2->setRead(); + $item2->setUnread(false); $serializer = new EntityApiSerializer('items'); diff --git a/tests/Unit/Db/FeedMapperTest.php b/tests/Unit/Db/FeedMapperTest.php deleted file mode 100644 index ed74b235e..000000000 --- a/tests/Unit/Db/FeedMapperTest.php +++ /dev/null @@ -1,303 +0,0 @@ - - * @author Bernhard Posselt - * @copyright Alessandro Cosentino 2012 - * @copyright Bernhard Posselt 2012, 2014 - */ - -namespace OCA\News\Db; - - -use OCA\News\Utility\Time; - -class FeedMapperTest extends \OCA\News\Tests\Unit\Db\MapperTestUtility { - - private $mapper; - private $feeds; - - protected function setUp(){ - parent::setUp(); - - $this->mapper = new FeedMapper($this->db, new Time()); - - // 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`'; - $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`'; - $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`'; - $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` ' . - '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`'; - - $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` ' . - '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`'; - $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`'; - $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`'; - $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`'; - $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()]; - - $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/ItemMapperTest.php b/tests/Unit/Db/ItemMapperTest.php deleted file mode 100644 index 082ff6ff9..000000000 --- a/tests/Unit/Db/ItemMapperTest.php +++ /dev/null @@ -1,550 +0,0 @@ - - * @author Bernhard Posselt - * @copyright Alessandro Cosentino 2012 - * @copyright Bernhard Posselt 2012, 2014 - */ - -namespace OCA\News\Db; - - -use OCA\News\Utility\Time; - -class ItemMapperTest extends \OCA\News\Tests\Unit\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, new Time()); - - // create mock items - $item1 = new Item(); - $item2 = new Item(); - - $this->items = [ - $item1, - $item2 - ]; - - $this->userId = 'john'; - $this->id = 3; - $this->folderId = 2; - - $this->row = [ - ['id' => $this->items[0]->getId()], - ]; - - $this->rows = [ - ['id' => $this->items[0]->getId()], - ['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, $search=[]) { - $status = (int) $status; - - // WARNING: Potential SQL injection if you change this carelessly - $sql = 'AND ((`items`.`status` & ' . $status . ') = ' . $status . ') '; - - foreach ($search as $_) { - $sql .= 'AND `items`.`search_index` LIKE ? '; - } - - $sql .= $prependTo; - - return $this->makeSelectQuery($sql, $oldestFirst); - } - - - public function testFind(){ - $sql = $this->makeSelectQuery('AND `items`.`id` = ? '); - - $this->setMapperResult( - $sql, [$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 = [['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, [$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 = [~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 = [~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 = [ - ~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 = [$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 = [$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 = [$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 = [$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 = [$this->user, $this->id, $this->offset]; - $this->setMapperResult($sql, $params, $this->rows, $this->limit); - $result = $this->mapper->findAllFeed($this->id, $this->limit, - $this->offset, $this->status, false, $this->user); - - $this->assertEquals($this->items, $result); - } - - - public function testFindAllFeedSearch(){ - $sql = 'AND `items`.`feed_id` = ? ' . - 'AND `items`.`id` < ? '; - $search = ['%test_\\', 'a']; - $sql = $this->makeSelectQueryStatus($sql, $this->status, false, $search); - $params = [$this->user, '%\%test\\_\\\\%', '%a%', $this->id, $this->offset]; - $this->setMapperResult($sql, $params, $this->rows, $this->limit); - $result = $this->mapper->findAllFeed($this->id, $this->limit, - $this->offset, $this->status, false, $this->user, $search); - - $this->assertEquals($this->items, $result); - } - - - public function testFindAllFeedNegativeLimit(){ - $sql = 'AND `items`.`feed_id` = ? ' . - 'AND `items`.`id` < ? '; - $sql = $this->makeSelectQueryStatus($sql, $this->status); - $params = [$this->user, $this->id, $this->offset]; - $this->setMapperResult($sql, $params, $this->rows); - $result = $this->mapper->findAllFeed($this->id, -1, - $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 = [$this->user, $this->id, $this->offset]; - $this->setMapperResult($sql, $params, $this->rows, $this->limit); - $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 = [$this->user, $this->id]; - $this->setMapperResult($sql, $params, $this->rows, $this->limit); - $result = $this->mapper->findAllFeed($this->id, $this->limit, - 0, $this->status, false, $this->user); - - $this->assertEquals($this->items, $result); - } - - - public function testFindAllFolder(){ - $sql = 'AND `feeds`.`folder_id` = ? ' . - 'AND `items`.`id` < ? '; - $sql = $this->makeSelectQueryStatus($sql, $this->status); - $params = [$this->user, $this->id, $this->offset]; - $this->setMapperResult($sql, $params, $this->rows, $this->limit); - $result = $this->mapper->findAllFolder($this->id, $this->limit, - $this->offset, $this->status, false, $this->user); - - $this->assertEquals($this->items, $result); - } - - public function testFindAllFolderSearch(){ - $sql = 'AND `feeds`.`folder_id` = ? ' . - 'AND `items`.`id` < ? '; - $search = ['%test_\\', 'a']; - $sql = $this->makeSelectQueryStatus($sql, $this->status, false, $search); - $params = [$this->user, '%\%test\\_\\\\%', '%a%', $this->id, $this->offset]; - $this->setMapperResult($sql, $params, $this->rows, $this->limit); - $result = $this->mapper->findAllFolder($this->id, $this->limit, - $this->offset, $this->status, false, $this->user, $search); - - $this->assertEquals($this->items, $result); - } - - public function testFindAllFolderNegativeLimit(){ - $sql = 'AND `feeds`.`folder_id` = ? ' . - 'AND `items`.`id` < ? '; - $sql = $this->makeSelectQueryStatus($sql, $this->status); - $params = [$this->user, $this->id, $this->offset]; - $this->setMapperResult($sql, $params, $this->rows); - $result = $this->mapper->findAllFolder($this->id, -1, - $this->offset, $this->status, false, $this->user); - - $this->assertEquals($this->items, $result); - } - - - - public function testFindAllFolderOldestFirst(){ - $sql = 'AND `feeds`.`folder_id` = ? ' . - 'AND `items`.`id` > ? '; - $sql = $this->makeSelectQueryStatus($sql, $this->status, true); - $params = [$this->user, $this->id, $this->offset]; - $this->setMapperResult($sql, $params, $this->rows, $this->limit); - $result = $this->mapper->findAllFolder($this->id, $this->limit, - $this->offset, $this->status, true, $this->user); - - $this->assertEquals($this->items, $result); - } - - - public function testFindAllFolderOffsetZero(){ - $sql = 'AND `feeds`.`folder_id` = ? '; - $sql = $this->makeSelectQueryStatus($sql, $this->status); - $params = [$this->user, $this->id]; - $this->setMapperResult($sql, $params, $this->rows, $this->limit); - $result = $this->mapper->findAllFolder($this->id, $this->limit, - 0, $this->status, false, $this->user); - - $this->assertEquals($this->items, $result); - } - - - public function testFindAll(){ - $sql = 'AND `items`.`id` < ? '; - $sql = $this->makeSelectQueryStatus($sql, $this->status); - $params = [$this->user, $this->offset]; - $this->setMapperResult($sql, $params, $this->rows, $this->limit); - $result = $this->mapper->findAll($this->limit, - $this->offset, $this->status, false, $this->user); - - $this->assertEquals($this->items, $result); - } - - - public function testFindAllSearch(){ - $sql = 'AND `items`.`id` < ? '; - $search = ['%tEst_\\', 'a']; - $params = [$this->user, '%\%test\\_\\\\%', '%a%', $this->offset]; - $sql = $this->makeSelectQueryStatus($sql, $this->status, false, $search); - $this->setMapperResult($sql, $params, $this->rows, $this->limit); - $result = $this->mapper->findAll($this->limit, - $this->offset, $this->status, false, $this->user, $search); - - $this->assertEquals($this->items, $result); - } - - - public function testFindAllNegativeLimit(){ - $sql = 'AND `items`.`id` < ? '; - $sql = $this->makeSelectQueryStatus($sql, $this->status); - $params = [$this->user, $this->offset]; - $this->setMapperResult($sql, $params, $this->rows, null); - $result = $this->mapper->findAll(-1, - $this->offset, $this->status, false, $this->user); - - $this->assertEquals($this->items, $result); - } - - - public function testFindAllOldestFirst(){ - $sql = 'AND `items`.`id` > ? '; - $sql = $this->makeSelectQueryStatus($sql, $this->status, true); - $params = [$this->user, $this->offset]; - $this->setMapperResult($sql, $params, $this->rows, $this->limit); - $result = $this->mapper->findAll($this->limit, - $this->offset, $this->status, true, $this->user); - - $this->assertEquals($this->items, $result); - } - - - public function testFindAllOffsetZero(){ - $sql = $this->makeSelectQueryStatus('', $this->status); - $params = [$this->user]; - $this->setMapperResult($sql, $params, $this->rows, $this->limit); - $result = $this->mapper->findAll($this->limit, - 0, $this->status, false, $this->user); - - $this->assertEquals($this->items, $result); - } - - - - - public function testFindByGuidHash(){ - $hash = md5('test'); - $feedId = 3; - $sql = $this->makeSelectQuery( - 'AND `items`.`guid_hash` = ? ' . - 'AND `feeds`.`id` = ? '); - - $this->setMapperResult( - $sql, [$this->userId, $hash, $feedId], $this->row); - - $result = $this->mapper->findByGuidHash($hash, $feedId, $this->userId); - $this->assertEquals($this->items[0], $result); - } - - - public function testDeleteReadOlderThanThresholdDoesNotDelete(){ - $status = StatusFlag::STARRED | StatusFlag::UNREAD; - $sql = 'SELECT (COUNT(*) - `feeds`.`articles_per_update`) AS `size`' . - ', `feeds`.`id` AS `feed_id`, `feeds`.`articles_per_update` ' . - 'FROM `*PREFIX*news_items` `items` ' . - 'JOIN `*PREFIX*news_feeds` `feeds` ' . - 'ON `feeds`.`id` = `items`.`feed_id` ' . - 'AND NOT ((`items`.`status` & ?) > 0) ' . - 'GROUP BY `feeds`.`id`, `feeds`.`articles_per_update` ' . - 'HAVING COUNT(*) > ?'; - - $threshold = 10; - $rows = [['feed_id' => 30, 'size' => 9]]; - $params = [$status, $threshold]; - - $this->setMapperResult($sql, $params, $rows); - $this->mapper->deleteReadOlderThanThreshold($threshold); - - - } - - - public function testDeleteReadOlderThanThreshold(){ - $threshold = 10; - $status = StatusFlag::STARRED | StatusFlag::UNREAD; - - $sql1 = 'SELECT (COUNT(*) - `feeds`.`articles_per_update`) AS `size`' . - ', `feeds`.`id` AS `feed_id`, `feeds`.`articles_per_update` ' . - 'FROM `*PREFIX*news_items` `items` ' . - 'JOIN `*PREFIX*news_feeds` `feeds` ' . - 'ON `feeds`.`id` = `items`.`feed_id` ' . - 'AND NOT ((`items`.`status` & ?) > 0) ' . - 'GROUP BY `feeds`.`id`, `feeds`.`articles_per_update` ' . - 'HAVING COUNT(*) > ?'; - $params1 = [$status, $threshold]; - - $sql2 = 'DELETE FROM `*PREFIX*news_items` ' . - 'WHERE `id` IN (' . - 'SELECT `id` FROM `*PREFIX*news_items` ' . - 'WHERE NOT ((`status` & ?) > 0) ' . - 'AND `feed_id` = ? ' . - 'ORDER BY `id` ASC ' . - 'LIMIT ?' . - ')'; - $params2 = [$status, 30, 1]; - - $row = ['feed_id' => 30, 'size' => 11]; - $this->setMapperResult($sql1, $params1, [$row]); - $this->setMapperResult($sql2, $params2); - - $this->mapper->deleteReadOlderThanThreshold($threshold); - } - - - public function testGetNewestItem() { - $sql = 'SELECT MAX(`items`.`id`) AS `max_id` ' . - 'FROM `*PREFIX*news_items` `items` '. - 'JOIN `*PREFIX*news_feeds` `feeds` ' . - 'ON `feeds`.`id` = `items`.`feed_id` '. - 'AND `feeds`.`user_id` = ?'; - $params = [$this->user]; - $rows = [['max_id' => 3]]; - - $this->setMapperResult($sql, $params, $rows); - - $result = $this->mapper->getNewestItemId($this->user); - $this->assertEquals(3, $result); - } - - - public function testGetNewestItemIdNotFound() { - $sql = 'SELECT MAX(`items`.`id`) AS `max_id` ' . - 'FROM `*PREFIX*news_items` `items` '. - 'JOIN `*PREFIX*news_feeds` `feeds` ' . - 'ON `feeds`.`id` = `items`.`feed_id` '. - 'AND `feeds`.`user_id` = ?'; - $params = [$this->user]; - $rows = []; - - $this->setMapperResult($sql, $params, $rows); - $this->setExpectedException( - '\OCP\AppFramework\Db\DoesNotExistException' - ); - - $this->mapper->getNewestItemId($this->user); - } - - - public function testDeleteFromUser(){ - $userId = 'john'; - $sql = 'DELETE FROM `*PREFIX*news_items` ' . - 'WHERE `feed_id` IN (' . - 'SELECT `feeds`.`id` FROM `*PREFIX*news_feeds` `feeds` ' . - 'WHERE `feeds`.`user_id` = ?' . - ')'; - - $this->setMapperResult($sql, [$userId]); - - $this->mapper->deleteUser($userId); - } - - -} \ No newline at end of file diff --git a/tests/Unit/Db/ItemTest.php b/tests/Unit/Db/ItemTest.php index f26cf6ff9..6f0b48e5a 100644 --- a/tests/Unit/Db/ItemTest.php +++ b/tests/Unit/Db/ItemTest.php @@ -16,6 +16,7 @@ namespace OCA\News\Db; class ItemTest extends \PHPUnit_Framework_TestCase { + /** @var Item */ private $item; protected function setUp(){ @@ -25,30 +26,30 @@ class ItemTest extends \PHPUnit_Framework_TestCase { public function testSetRead(){ - $this->item->setRead(); + $this->item->setUnread(false); - $this->assertTrue($this->item->isRead()); + $this->assertFalse($this->item->isUnread()); } public function testSetUnread(){ - $this->item->setUnread(); + $this->item->setUnread(true); $this->assertTrue($this->item->isUnread()); } public function testSetStarred(){ - $this->item->setStarred(); + $this->item->setStarred(true); $this->assertTrue($this->item->isStarred()); } public function testSetUnstarred(){ - $this->item->setUnstarred(); + $this->item->setStarred(false); - $this->assertTrue($this->item->isUnstarred()); + $this->assertFalse($this->item->isStarred()); } @@ -68,8 +69,8 @@ class ItemTest extends \PHPUnit_Framework_TestCase { $item->setRtl(true); $item->setFeedId(1); $item->setStatus(0); - $item->setUnread(); - $item->setStarred(); + $item->setUnread(true); + $item->setStarred(true); $item->setLastModified('1111111111234567'); $item->setFingerprint('fingerprint'); $item->setContentHash('contentHash'); @@ -113,9 +114,9 @@ class ItemTest extends \PHPUnit_Framework_TestCase { $item->setFeedId(1); $item->setStatus(0); $item->setRtl(true); - $item->setUnread(); + $item->setUnread(true); $item->setFingerprint('fingerprint'); - $item->setStarred(); + $item->setStarred(true); $item->setLastModified(321); $this->assertEquals([ @@ -156,8 +157,8 @@ class ItemTest extends \PHPUnit_Framework_TestCase { $item->setFeedId(1); $item->setRtl(true); $item->setStatus(0); - $item->setRead(); - $item->setStarred(); + $item->setUnread(false); + $item->setStarred(true); $item->setLastModified(321); $feed = new Feed(); @@ -194,13 +195,13 @@ class ItemTest extends \PHPUnit_Framework_TestCase { $item->setBody('body'); $item->setEnclosureMime('audio/ogg'); $item->setEnclosureLink('enclink'); - $item->setStarred(); + $item->setStarred(true); $item->setRtl(true); if ($isRead) { - $item->setUnread(); + $item->setUnread(true); } else { - $item->setRead(); + $item->setUnread(false); } return $item; diff --git a/tests/Unit/Db/Mysql/ItemMapperTest.php b/tests/Unit/Db/Mysql/ItemMapperTest.php deleted file mode 100644 index e058d80b4..000000000 --- a/tests/Unit/Db/Mysql/ItemMapperTest.php +++ /dev/null @@ -1,119 +0,0 @@ - - * @author Bernhard Posselt - * @copyright Alessandro Cosentino 2012 - * @copyright Bernhard Posselt 2012, 2014 - */ - -namespace OCA\News\Db\Mysql; - -use \OCA\News\Db\Item; -use \OCA\News\Db\StatusFlag; -use OCA\News\Utility\Time; - -class ItemMapperTest extends \OCA\News\Tests\Unit\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, new Time()); - - // create mock items - $item1 = new Item(); - $item2 = new Item(); - - $this->items = [$item1, $item2]; - - $this->userId = 'john'; - $this->id = 3; - $this->folderId = 2; - - $this->row = [['id' => $this->items[0]->getId()]]; - - $this->rows = [ - ['id' => $this->items[0]->getId()], - ['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; - - } - - - public function testDeleteReadOlderThanThresholdDoesNotDeleteBelow(){ - $status = StatusFlag::STARRED | StatusFlag::UNREAD; - $sql = 'SELECT (COUNT(*) - `feeds`.`articles_per_update`) AS `size`' . - ', `feeds`.`id` AS `feed_id`, `feeds`.`articles_per_update` ' . - 'FROM `*PREFIX*news_items` `items` ' . - 'JOIN `*PREFIX*news_feeds` `feeds` ' . - 'ON `feeds`.`id` = `items`.`feed_id` ' . - 'AND NOT ((`items`.`status` & ?) > 0) ' . - 'GROUP BY `feeds`.`id`, `feeds`.`articles_per_update` ' . - 'HAVING COUNT(*) > ?'; - - $threshold = 10; - $rows = [['feed_id' => 30, 'size' => 9]]; - $params = [$status, $threshold]; - - $this->setMapperResult($sql, $params, $rows); - $this->mapper->deleteReadOlderThanThreshold($threshold); - - - } - - - public function testDeleteReadOlderThanThreshold(){ - $threshold = 10; - $status = StatusFlag::STARRED | StatusFlag::UNREAD; - - $sql1 = 'SELECT (COUNT(*) - `feeds`.`articles_per_update`) AS `size`' . - ', `feeds`.`id` AS `feed_id`, `feeds`.`articles_per_update` ' . - 'FROM `*PREFIX*news_items` `items` ' . - 'JOIN `*PREFIX*news_feeds` `feeds` ' . - 'ON `feeds`.`id` = `items`.`feed_id` ' . - 'AND NOT ((`items`.`status` & ?) > 0) ' . - 'GROUP BY `feeds`.`id`, `feeds`.`articles_per_update` ' . - 'HAVING COUNT(*) > ?'; - $params1 = [$status, $threshold]; - - - $row = ['feed_id' => 30, 'size' => 11]; - - $sql2 = 'DELETE FROM `*PREFIX*news_items` ' . - 'WHERE NOT ((`status` & ?) > 0) ' . - 'AND `feed_id` = ? ' . - 'ORDER BY `id` ASC ' . - 'LIMIT ?'; - $params2 = [$status, 30, 1]; - - - $this->setMapperResult($sql1, $params1, [$row]); - $this->setMapperResult($sql2, $params2); - - $this->mapper->deleteReadOlderThanThreshold($threshold); - } - - -} \ No newline at end of file diff --git a/tests/Unit/Fetcher/FeedFetcherTest.php b/tests/Unit/Fetcher/FeedFetcherTest.php index ce09bb0e3..16d26cb66 100644 --- a/tests/Unit/Fetcher/FeedFetcherTest.php +++ b/tests/Unit/Fetcher/FeedFetcherTest.php @@ -231,7 +231,7 @@ class FeedFetcherTest extends \PHPUnit_Framework_TestCase { $item->setUpdatedDate($this->updated); $item->setStatus(0); - $item->setUnread(); + $item->setUnread(true); $item->setUrl($this->permalink); $item->setTitle('my<\' title'); $item->setGuid($this->guid); diff --git a/tests/Unit/Migration/MigrateStatusFlagsTest.php b/tests/Unit/Migration/MigrateStatusFlagsTest.php new file mode 100644 index 000000000..bee3532c1 --- /dev/null +++ b/tests/Unit/Migration/MigrateStatusFlagsTest.php @@ -0,0 +1,99 @@ + + * @copyright Daniel Opitz 2017 + */ + +namespace OCA\News\Migration; + +use Doctrine\DBAL\Driver\Statement; +use OCP\IConfig; +use OCP\IDBConnection; +use OCP\Migration\IOutput; +use Test\TestCase; + +class MigrateStatusFlagsTest extends TestCase { + + /** @var IDBConnection|\PHPUnit_Framework_MockObject_MockObject */ + protected $db; + /** @var IConfig|\PHPUnit_Framework_MockObject_MockObject */ + protected $config; + /** @var IOutput|\PHPUnit_Framework_MockObject_MockObject */ + protected $output; + + protected function setUp() { + $this->db = $this->createMock(IDBConnection::class); + $this->config = $this->createMock(IConfig::class); + $this->output = $this->createMock(IOutput::class); + } + + public function testRun() { + $statement = $this->createMock(Statement::class); + $statement->expects($this->exactly(1)) + ->method('execute') + ->with() + ->willReturn(true); + + $this->config->expects($this->exactly(1)) + ->method('getAppValue') + ->with('news', 'installed_version', '0.0.0') + ->willReturn('11.0.5'); + + $sql = 'UPDATE `*PREFIX*news_items` ' + . 'SET `unread` = ((`status` & 2) = 2), ' + . '`starred` = ((`status` & 4) = 4)'; + + $this->db->expects($this->exactly(1)) + ->method('prepare') + ->with($sql) + ->willReturn($statement); + + $migration = new MigrateStatusFlags($this->db, $this->config); + $migration->run($this->output); + } + + /** + * @expectedException \Exception + */ + public function testRunException() { + $statement = $this->createMock(Statement::class); + $statement->expects($this->exactly(1)) + ->method('execute') + ->with() + ->willReturn(false); + + $this->config->expects($this->exactly(1)) + ->method('getAppValue') + ->with('news', 'installed_version', '0.0.0') + ->willReturn('11.0.5'); + + $sql = 'UPDATE `*PREFIX*news_items` ' + . 'SET `unread` = ((`status` & 2) = 2), ' + . '`starred` = ((`status` & 4) = 4)'; + + $this->db->expects($this->exactly(1)) + ->method('prepare') + ->with($sql) + ->willReturn($statement); + + $migration = new MigrateStatusFlags($this->db, $this->config); + $migration->run($this->output); + } + + public function testRunNewerVersion() { + $this->config->expects($this->exactly(1)) + ->method('getAppValue') + ->with('news', 'installed_version', '0.0.0') + ->willReturn('11.1.0'); + $this->db->expects($this->exactly(0)) + ->method('prepare'); + + $migration = new MigrateStatusFlags($this->db, $this->config); + $migration->run($this->output); + } +} \ No newline at end of file diff --git a/tests/Unit/Service/FeedServiceTest.php b/tests/Unit/Service/FeedServiceTest.php index 68d25df9e..6df02e51c 100644 --- a/tests/Unit/Service/FeedServiceTest.php +++ b/tests/Unit/Service/FeedServiceTest.php @@ -388,7 +388,7 @@ class FeedServiceTest extends \PHPUnit_Framework_TestCase { $item->setTitle('hey'); $item->setAuthor('aut'); $item->setBody('new'); - $item->setRead(); + $item->setUnread(false); return $item; } @@ -401,7 +401,7 @@ class FeedServiceTest extends \PHPUnit_Framework_TestCase { $item->setTitle('ho'); $item->setAuthor('auto'); $item->setBody('old'); - $item->setRead(); + $item->setUnread(false); return $item; } @@ -448,7 +448,7 @@ class FeedServiceTest extends \PHPUnit_Framework_TestCase { $item = $this->createUpdateItem(); $item2 = $this->createUpdateItem2(); $item3 = $this->createUpdateItem(); - $item3->setUnread(); + $item3->setUnread(true); $items = [$item]; @@ -760,8 +760,8 @@ class FeedServiceTest extends \PHPUnit_Framework_TestCase { $item->setBody('come over'); $item->setEnclosureMime('mime'); $item->setEnclosureLink('lin'); - $item->setUnread(); - $item->setUnstarred(); + $item->setUnread(true); + $item->setStarred(false); $item->generateSearchIndex(); $json = $item->toExport(['feed3' => $feed]); @@ -816,8 +816,8 @@ class FeedServiceTest extends \PHPUnit_Framework_TestCase { $item->setBody('come over'); $item->setEnclosureMime('mime'); $item->setEnclosureLink('lin'); - $item->setUnread(); - $item->setUnstarred(); + $item->setUnread(true); + $item->setStarred(false); $item->generateSearchIndex(); $json = $item->toExport(['feed3' => $feed]); diff --git a/tests/Unit/Service/ItemServiceTest.php b/tests/Unit/Service/ItemServiceTest.php index 3b3197136..4beebbb63 100644 --- a/tests/Unit/Service/ItemServiceTest.php +++ b/tests/Unit/Service/ItemServiceTest.php @@ -16,13 +16,13 @@ namespace OCA\News\Service; use \OCP\AppFramework\Db\DoesNotExistException; use \OCA\News\Db\Item; -use \OCA\News\Db\StatusFlag; use \OCA\News\Db\FeedType; class ItemServiceTest extends \PHPUnit_Framework_TestCase { private $mapper; + /** @var ItemService */ private $itemService; private $user; private $response; @@ -46,13 +46,6 @@ class ItemServiceTest extends \PHPUnit_Framework_TestCase { $this->mapper = $this->getMockBuilder('\OCA\News\Db\ItemMapper') ->disableOriginalConstructor() ->getMock(); - $this->statusFlag = $this->getMockBuilder('\OCA\News\Db\StatusFlag') - ->disableOriginalConstructor() - ->getMock(); - $this->status = StatusFlag::STARRED; - $this->statusFlag->expects($this->any()) - ->method('typeToStatus') - ->will($this->returnValue($this->status)); $this->config = $this->getMockBuilder( '\OCA\News\Config\Config') ->disableOriginalConstructor() @@ -62,8 +55,7 @@ class ItemServiceTest extends \PHPUnit_Framework_TestCase { ->disableOriginalConstructor() ->getMock(); $this->itemService = new ItemService($this->mapper, - $this->statusFlag, $this->timeFactory, $this->config, - $this->systemConfig); + $this->timeFactory, $this->config, $this->systemConfig); $this->user = 'jack'; $this->id = 3; $this->updatedSince = 20333; @@ -80,7 +72,7 @@ class ItemServiceTest extends \PHPUnit_Framework_TestCase { ->method('findAllNewFeed') ->with($this->equalTo($this->id), $this->equalTo($this->updatedSince), - $this->equalTo($this->status), + $this->equalTo($this->showAll), $this->equalTo($this->user)) ->will($this->returnValue($this->response)); @@ -97,7 +89,7 @@ class ItemServiceTest extends \PHPUnit_Framework_TestCase { ->method('findAllNewFolder') ->with($this->equalTo($this->id), $this->equalTo($this->updatedSince), - $this->equalTo($this->status), + $this->equalTo($this->showAll), $this->equalTo($this->user)) ->will($this->returnValue($this->response)); @@ -113,7 +105,8 @@ class ItemServiceTest extends \PHPUnit_Framework_TestCase { $this->mapper->expects($this->once()) ->method('findAllNew') ->with( $this->equalTo($this->updatedSince), - $this->equalTo($this->status), + $this->equalTo($type), + $this->equalTo($this->showAll), $this->equalTo($this->user)) ->will($this->returnValue($this->response)); @@ -131,7 +124,7 @@ class ItemServiceTest extends \PHPUnit_Framework_TestCase { ->with($this->equalTo($this->id), $this->equalTo($this->limit), $this->equalTo($this->offset), - $this->equalTo($this->status), + $this->equalTo($this->showAll), $this->equalTo(false), $this->equalTo($this->user), $this->equalTo([])) @@ -152,7 +145,7 @@ class ItemServiceTest extends \PHPUnit_Framework_TestCase { ->with($this->equalTo($this->id), $this->equalTo($this->limit), $this->equalTo($this->offset), - $this->equalTo($this->status), + $this->equalTo($this->showAll), $this->equalTo(true), $this->equalTo($this->user), $this->equalTo([])) @@ -172,7 +165,8 @@ class ItemServiceTest extend