From 004fcbbcc7609ca83807f2e38967ef54f469bf72 Mon Sep 17 00:00:00 2001 From: Bernhard Posselt Date: Sat, 23 Jul 2016 21:24:54 +0200 Subject: Move to new directory structure --- tests/Integration/Db/FeedMapperTest.php | 54 + tests/Integration/Db/FolderMapperTest.php | 51 + tests/Integration/Db/ItemMapperTest.php | 297 ++++++ tests/Integration/Fixtures/FeedFixture.php | 50 + tests/Integration/Fixtures/Fixture.php | 25 + tests/Integration/Fixtures/FolderFixture.php | 35 + tests/Integration/Fixtures/ItemFixture.php | 49 + tests/Integration/Fixtures/data/default.php | 76 ++ tests/Integration/Fixtures/data/readitem.php | 33 + tests/Integration/IntegrationTest.php | 206 ++++ tests/Unit/Config/ConfigTest.php | 230 +++++ tests/Unit/Controller/AdminControllerTest.php | 161 +++ tests/Unit/Controller/EntityApiSerializerTest.php | 116 +++ tests/Unit/Controller/ExportControllerTest.php | 131 +++ tests/Unit/Controller/FeedApiControllerTest.php | 353 +++++++ tests/Unit/Controller/FeedControllerTest.php | 484 +++++++++ tests/Unit/Controller/FolderApiControllerTest.php | 255 +++++ tests/Unit/Controller/FolderControllerTest.php | 306 ++++++ tests/Unit/Controller/ItemApiControllerTest.php | 372 +++++++ tests/Unit/Controller/ItemControllerTest.php | 384 +++++++ tests/Unit/Controller/JSONHttpErrorTest.php | 34 + tests/Unit/Controller/PageControllerTest.php | 322 ++++++ tests/Unit/Controller/UserApiControllerTest.php | 141 +++ tests/Unit/Controller/UtilityApiControllerTest.php | 90 ++ tests/Unit/Db/FeedMapperTest.php | 306 ++++++ tests/Unit/Db/FeedTest.php | 116 +++ tests/Unit/Db/FolderMapperTest.php | 196 ++++ tests/Unit/Db/FolderTest.php | 50 + tests/Unit/Db/ItemMapperTest.php | 550 ++++++++++ tests/Unit/Db/ItemTest.php | 316 ++++++ tests/Unit/Db/MapperFactoryTest.php | 50 + tests/Unit/Db/MapperTestUtility.php | 208 ++++ tests/Unit/Db/Mysql/ItemMapperTest.php | 119 +++ tests/Unit/Fetcher/FeedFetcherTest.php | 386 +++++++ tests/Unit/Fetcher/FetcherTest.php | 137 +++ tests/Unit/Fetcher/YoutubeFetcherTest.php | 68 ++ tests/Unit/Http/TextDownloadResponseTest.php | 32 + tests/Unit/Http/TextResponseTest.php | 44 + tests/Unit/Service/FeedServiceTest.php | 1060 ++++++++++++++++++++ tests/Unit/Service/FolderServiceTest.php | 282 ++++++ tests/Unit/Service/ItemServiceTest.php | 443 ++++++++ tests/Unit/Service/ServiceTest.php | 98 ++ tests/Unit/Service/StatusFlagTest.php | 57 ++ tests/Unit/Service/StatusServiceTest.php | 89 ++ tests/Unit/Upgrade/UpgradeTest.php | 72 ++ tests/Unit/Utility/OPMLExporterTest.php | 123 +++ tests/Unit/Utility/ProxyConfigParserTest.php | 93 ++ tests/Unit/Utility/UpdaterTest.php | 62 ++ tests/integration/db/FeedMapperTest.php | 54 - tests/integration/db/FolderMapperTest.php | 51 - tests/integration/db/ItemMapperTest.php | 297 ------ tests/integration/fixtures/data/default.php | 76 -- tests/integration/fixtures/data/readitem.php | 33 - tests/integration/fixtures/feedfixture.php | 50 - tests/integration/fixtures/fixture.php | 25 - tests/integration/fixtures/folderfixture.php | 35 - tests/integration/fixtures/itemfixture.php | 49 - tests/integration/integrationtest.php | 206 ---- tests/unit/config/ConfigTest.php | 230 ----- tests/unit/controller/AdminControllerTest.php | 161 --- tests/unit/controller/EntityApiSerializerTest.php | 116 --- tests/unit/controller/ExportControllerTest.php | 131 --- tests/unit/controller/FeedApiControllerTest.php | 353 ------- tests/unit/controller/FeedControllerTest.php | 484 --------- tests/unit/controller/FolderApiControllerTest.php | 255 ----- tests/unit/controller/FolderControllerTest.php | 306 ------ tests/unit/controller/ItemApiControllerTest.php | 372 ------- tests/unit/controller/ItemControllerTest.php | 384 ------- tests/unit/controller/JSONHttpErrorTest.php | 34 - tests/unit/controller/PageControllerTest.php | 322 ------ tests/unit/controller/UserApiControllerTest.php | 141 --- tests/unit/controller/UtilityApiControllerTest.php | 90 -- tests/unit/db/FeedMapperTest.php | 306 ------ tests/unit/db/FeedTest.php | 116 --- tests/unit/db/FolderMapperTest.php | 196 ---- tests/unit/db/FolderTest.php | 50 - tests/unit/db/ItemMapperTest.php | 550 ---------- tests/unit/db/ItemTest.php | 316 ------ tests/unit/db/MapperFactoryTest.php | 50 - tests/unit/db/mappertestutility.php | 208 ---- tests/unit/db/mysql/ItemMapperTest.php | 119 --- tests/unit/fetcher/FeedFetcherTest.php | 386 ------- tests/unit/fetcher/FetcherTest.php | 137 --- tests/unit/fetcher/YoutubeFetcherTest.php | 68 -- tests/unit/http/TextDownloadResponseTest.php | 32 - tests/unit/http/TextResponseTest.php | 44 - tests/unit/service/FeedServiceTest.php | 1060 -------------------- tests/unit/service/FolderServiceTest.php | 282 ------ tests/unit/service/ItemServiceTest.php | 443 -------- tests/unit/service/ServiceTest.php | 98 -- tests/unit/service/StatusFlagTest.php | 57 -- tests/unit/service/StatusServiceTest.php | 89 -- tests/unit/upgrade/UpgradeTest.php | 72 -- tests/unit/utility/OPMLExporterTest.php | 123 --- tests/unit/utility/ProxyConfigParserTest.php | 93 -- tests/unit/utility/UpdaterTest.php | 62 -- 96 files changed, 9212 insertions(+), 9212 deletions(-) create mode 100644 tests/Integration/Db/FeedMapperTest.php create mode 100644 tests/Integration/Db/FolderMapperTest.php create mode 100644 tests/Integration/Db/ItemMapperTest.php create mode 100644 tests/Integration/Fixtures/FeedFixture.php create mode 100644 tests/Integration/Fixtures/Fixture.php create mode 100644 tests/Integration/Fixtures/FolderFixture.php create mode 100644 tests/Integration/Fixtures/ItemFixture.php create mode 100644 tests/Integration/Fixtures/data/default.php create mode 100644 tests/Integration/Fixtures/data/readitem.php create mode 100644 tests/Integration/IntegrationTest.php create mode 100644 tests/Unit/Config/ConfigTest.php create mode 100644 tests/Unit/Controller/AdminControllerTest.php create mode 100644 tests/Unit/Controller/EntityApiSerializerTest.php create mode 100644 tests/Unit/Controller/ExportControllerTest.php create mode 100644 tests/Unit/Controller/FeedApiControllerTest.php create mode 100644 tests/Unit/Controller/FeedControllerTest.php create mode 100644 tests/Unit/Controller/FolderApiControllerTest.php create mode 100644 tests/Unit/Controller/FolderControllerTest.php create mode 100644 tests/Unit/Controller/ItemApiControllerTest.php create mode 100644 tests/Unit/Controller/ItemControllerTest.php create mode 100644 tests/Unit/Controller/JSONHttpErrorTest.php create mode 100644 tests/Unit/Controller/PageControllerTest.php create mode 100644 tests/Unit/Controller/UserApiControllerTest.php create mode 100644 tests/Unit/Controller/UtilityApiControllerTest.php create mode 100644 tests/Unit/Db/FeedMapperTest.php create mode 100644 tests/Unit/Db/FeedTest.php create mode 100644 tests/Unit/Db/FolderMapperTest.php create mode 100644 tests/Unit/Db/FolderTest.php create mode 100644 tests/Unit/Db/ItemMapperTest.php create mode 100644 tests/Unit/Db/ItemTest.php create mode 100644 tests/Unit/Db/MapperFactoryTest.php create mode 100644 tests/Unit/Db/MapperTestUtility.php create mode 100644 tests/Unit/Db/Mysql/ItemMapperTest.php create mode 100644 tests/Unit/Fetcher/FeedFetcherTest.php create mode 100644 tests/Unit/Fetcher/FetcherTest.php create mode 100644 tests/Unit/Fetcher/YoutubeFetcherTest.php create mode 100644 tests/Unit/Http/TextDownloadResponseTest.php create mode 100644 tests/Unit/Http/TextResponseTest.php create mode 100644 tests/Unit/Service/FeedServiceTest.php create mode 100644 tests/Unit/Service/FolderServiceTest.php create mode 100644 tests/Unit/Service/ItemServiceTest.php create mode 100644 tests/Unit/Service/ServiceTest.php create mode 100644 tests/Unit/Service/StatusFlagTest.php create mode 100644 tests/Unit/Service/StatusServiceTest.php create mode 100644 tests/Unit/Upgrade/UpgradeTest.php create mode 100644 tests/Unit/Utility/OPMLExporterTest.php create mode 100644 tests/Unit/Utility/ProxyConfigParserTest.php create mode 100644 tests/Unit/Utility/UpdaterTest.php delete mode 100644 tests/integration/db/FeedMapperTest.php delete mode 100644 tests/integration/db/FolderMapperTest.php delete mode 100644 tests/integration/db/ItemMapperTest.php delete mode 100644 tests/integration/fixtures/data/default.php delete mode 100644 tests/integration/fixtures/data/readitem.php delete mode 100644 tests/integration/fixtures/feedfixture.php delete mode 100644 tests/integration/fixtures/fixture.php delete mode 100644 tests/integration/fixtures/folderfixture.php delete mode 100644 tests/integration/fixtures/itemfixture.php delete mode 100644 tests/integration/integrationtest.php delete mode 100644 tests/unit/config/ConfigTest.php delete mode 100644 tests/unit/controller/AdminControllerTest.php delete mode 100644 tests/unit/controller/EntityApiSerializerTest.php delete mode 100644 tests/unit/controller/ExportControllerTest.php delete mode 100644 tests/unit/controller/FeedApiControllerTest.php delete mode 100644 tests/unit/controller/FeedControllerTest.php delete mode 100644 tests/unit/controller/FolderApiControllerTest.php delete mode 100644 tests/unit/controller/FolderControllerTest.php delete mode 100644 tests/unit/controller/ItemApiControllerTest.php delete mode 100644 tests/unit/controller/ItemControllerTest.php delete mode 100644 tests/unit/controller/JSONHttpErrorTest.php delete mode 100644 tests/unit/controller/PageControllerTest.php delete mode 100644 tests/unit/controller/UserApiControllerTest.php delete mode 100644 tests/unit/controller/UtilityApiControllerTest.php delete mode 100644 tests/unit/db/FeedMapperTest.php delete mode 100644 tests/unit/db/FeedTest.php delete mode 100644 tests/unit/db/FolderMapperTest.php delete mode 100644 tests/unit/db/FolderTest.php delete mode 100644 tests/unit/db/ItemMapperTest.php delete mode 100644 tests/unit/db/ItemTest.php delete mode 100644 tests/unit/db/MapperFactoryTest.php delete mode 100644 tests/unit/db/mappertestutility.php delete mode 100644 tests/unit/db/mysql/ItemMapperTest.php delete mode 100644 tests/unit/fetcher/FeedFetcherTest.php delete mode 100644 tests/unit/fetcher/FetcherTest.php delete mode 100644 tests/unit/fetcher/YoutubeFetcherTest.php delete mode 100644 tests/unit/http/TextDownloadResponseTest.php delete mode 100644 tests/unit/http/TextResponseTest.php delete mode 100644 tests/unit/service/FeedServiceTest.php delete mode 100644 tests/unit/service/FolderServiceTest.php delete mode 100644 tests/unit/service/ItemServiceTest.php delete mode 100644 tests/unit/service/ServiceTest.php delete mode 100644 tests/unit/service/StatusFlagTest.php delete mode 100644 tests/unit/service/StatusServiceTest.php delete mode 100644 tests/unit/upgrade/UpgradeTest.php delete mode 100644 tests/unit/utility/OPMLExporterTest.php delete mode 100644 tests/unit/utility/ProxyConfigParserTest.php delete mode 100644 tests/unit/utility/UpdaterTest.php (limited to 'tests') diff --git a/tests/Integration/Db/FeedMapperTest.php b/tests/Integration/Db/FeedMapperTest.php new file mode 100644 index 000000000..b72cb1203 --- /dev/null +++ b/tests/Integration/Db/FeedMapperTest.php @@ -0,0 +1,54 @@ + + * @copyright Bernhard Posselt 2015 + */ + +namespace OCA\News\Db; + +use \OCA\News\Tests\Integration\IntegrationTest; + +class FeedMapperTest extends IntegrationTest { + + + public function testFind () { + } + + + /* TBD + public function testFindAll () { + + } + + + public function testFindAllFromUser () { + + } + + + public function testFindByUrlHash () { + + } + + + public function testDelete () { + + } + + + public function testGetToDelete () { + + } + + + public function testDeleteUser () { + + }*/ + + +} diff --git a/tests/Integration/Db/FolderMapperTest.php b/tests/Integration/Db/FolderMapperTest.php new file mode 100644 index 000000000..21b6ddf41 --- /dev/null +++ b/tests/Integration/Db/FolderMapperTest.php @@ -0,0 +1,51 @@ + + * @copyright Bernhard Posselt 2015 + */ + +namespace OCA\News\Db; + +use \OCA\News\Tests\Integration\IntegrationTest; + +class FolderMapperTest extends IntegrationTest { + + + public function testFind () { + + } + + + /* TBD + public function testFindByName () { + + } + + + public function testFindAllFromUser () { + + } + + + public function testDelete () { + + } + + + public function testGetToDelete () { + + } + + + public function testDeleteUser () { + + }*/ + + + +} diff --git a/tests/Integration/Db/ItemMapperTest.php b/tests/Integration/Db/ItemMapperTest.php new file mode 100644 index 000000000..14c989cbf --- /dev/null +++ b/tests/Integration/Db/ItemMapperTest.php @@ -0,0 +1,297 @@ + + * @copyright Bernhard Posselt 2015 + */ + +namespace OCA\News\Db; + +use OCA\News\Tests\Integration\Fixtures\FeedFixture; +use OCA\News\Tests\Integration\Fixtures\ItemFixture; +use OCA\News\Tests\Integration\IntegrationTest; + +class ItemMapperTest extends IntegrationTest { + + public function testFind() { + $feed = new FeedFixture(); + $feed = $this->feedMapper->insert($feed); + + $item = new ItemFixture(['feedId' => $feed->getId()]); + + $item = $this->itemMapper->insert($item); + + $fetched = $this->itemMapper->find($item->getId(), $this->user); + + $this->assertEquals($item->getTitle(), $fetched->getTitle()); + } + + /** + * Same as whereId with easier title search + * @param $title + * @return mixed + */ + private function whereTitleId($title) { + return $this->findItemByTitle($title)->getId(); + } + + /** + * @expectedException OCP\AppFramework\Db\DoesNotExistException + */ + public function testFindNotFoundWhenDeletedFeed() { + $this->loadFixtures('default'); + + $id = $this->whereTitleId('not found feed'); + $this->itemMapper->find($id, $this->user); + } + + + /** + * @expectedException OCP\AppFramework\Db\DoesNotExistException + */ + public function testFindNotFoundWhenDeletedFolder() { + $this->loadFixtures('default'); + + + $id = $this->whereTitleId('not found folder'); + $this->itemMapper->find($id, $this->user); + } + + + private function deleteReadOlderThanThreshold() { + $this->loadFixtures('default'); + + $this->itemMapper->deleteReadOlderThanThreshold(1); + + $this->itemMapper->find($this->whereTitleId('a title1'), $this->user); + $this->itemMapper->find($this->whereTitleId('a title2'), $this->user); + $this->itemMapper->find($this->whereTitleId('a title3'), $this->user); + $this->itemMapper->find($this->whereTitleId('del3'), $this->user); + $this->itemMapper->find($this->whereTitleId('del4'), $this->user); + } + + /** + * @expectedException OCP\AppFramework\Db\DoesNotExistException + */ + public function testDeleteOlderThanThresholdOne() { + $this->loadFixtures('default'); + $id = $this->whereTitleId('del1'); + + $this->deleteReadOlderThanThreshold(); + + $this->itemMapper->find($id, $this->user); + } + + /** + * @expectedException OCP\AppFramework\Db\DoesNotExistException + */ + public function testDeleteOlderThanThresholdTwo() { + $this->loadFixtures('default'); + $id = $this->whereTitleId('del2'); + + $this->deleteReadOlderThanThreshold(); + + $this->itemMapper->find($id, $this->user); + } + + + public function testStarredCount () { + $this->loadFixtures('default'); + + $count = $this->itemMapper->starredCount($this->user); + $this->assertEquals(2, $count); + } + + + public function testReadAll () { + $this->loadFixtures('default'); + + $this->itemMapper->readAll(PHP_INT_MAX, 10, $this->user); + + $status = StatusFlag::UNREAD; + $items = $this->itemMapper->findAll( + 30, 0, $status, false, $this->user + ); + + $this->assertEquals(0, count($items)); + + $itemId = $this->whereTitleId('a title1'); + $item = $this->itemMapper->find($itemId, $this->user); + + $this->assertEquals(10, $item->getLastModified()); + + $itemId = $this->whereTitleId('a title3'); + $item = $this->itemMapper->find($itemId, $this->user); + + $this->assertEquals(10, $item->getLastModified()); + + $itemId = $this->whereTitleId('a title9'); + $item = $this->itemMapper->find($itemId, $this->user); + + $this->assertEquals(10, $item->getLastModified()); + } + + + public function testReadFolder () { + $this->loadFixtures('default'); + + $folderId = $this->findFolderByName('first folder')->getId(); + $this->itemMapper->readFolder( + $folderId, PHP_INT_MAX, 10, $this->user + ); + + $status = StatusFlag::UNREAD; + $items = $this->itemMapper->findAll( + 30, 0, $status, false, $this->user + ); + + $this->assertEquals(1, count($items)); + + $item = $this->findItemByTitle('a title1'); + $item = $this->itemMapper->find($item->getId(), $this->user); + + $this->assertEquals(10, $item->getLastModified()); + + $item = $this->findItemByTitle('a title3'); + $item = $this->itemMapper->find($item->getId(), $this->user); + + $this->assertEquals(10, $item->getLastModified()); + + $item = $this->findItemByTitle('a title9'); + $item = $this->itemMapper->find($item->getId(), $this->user); + + $this->assertTrue($item->isUnread()); + } + + + public function testReadFeed () { + $this->loadFixtures('default'); + + $feedId = $this->findFeedByTitle('third feed')->getId(); + $this->itemMapper->readFeed( + $feedId, PHP_INT_MAX, 10, $this->user + ); + + $status = StatusFlag::UNREAD; + $items = $this->itemMapper->findAll( + 30, 0, $status, false, $this->user + ); + + $this->assertEquals(2, count($items)); + + $item = $this->findItemByTitle('a title9'); + $item = $this->itemMapper->find($item->getId(), $this->user); + + $this->assertEquals(10, $item->getLastModified()); + + $item = $this->findItemByTitle('a title3'); + $item = $this->itemMapper->find($item->getId(), $this->user); + $this->assertTrue($item->isUnread()); + + + $item = $this->findItemByTitle('a title1'); + $item = $this->itemMapper->find($item->getId(), $this->user); + $this->assertTrue($item->isUnread()); + } + + + public function testDeleteUser () { + $this->loadFixtures('default'); + + $this->itemMapper->deleteUser($this->user); + $id = $this->itemMapper->getNewestItemId($this->user); + + $this->assertEquals(0, $id); + } + + public function testGetNewestItemId () { + $this->loadFixtures('default'); + + $id = $this->itemMapper->getNewestItemId($this->user); + + $itemId = $this->whereTitleId('no folder'); + $this->assertEquals($itemId, $id); + } + + public function testFindAllUnreadOrStarred () { + $this->loadFixtures('default'); + + $items = $this->itemMapper->findAllUnreadOrStarred($this->user); + $this->assertEquals(4, count($items)); + } + + + public function testReadItem() { + $this->loadFixtures('readitem'); + // assert that all items are unread + $feed = $this->feedMapper->where(['userId' => 'john'])[0]; + $items = $this->itemMapper->where(['feedId' => $feed->getId()]); + foreach ($items as $item) { + $this->assertTrue($item->isUnread()); + } + $feed = $this->feedMapper->where(['userId' => 'test'])[0]; + $items = $this->itemMapper->where(['feedId' => $feed->getId()]); + foreach ($items as $item) { + $this->assertTrue($item->isUnread()); + } + + // read an item + $duplicateItem = $this->itemMapper->where(['feedId' => $feed->getId()])[0]; + $this->itemMapper->readItem($duplicateItem->getId(), true, 1000, $this->user); + + // 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()); + } + + // assert that a different item is not read + $items = $this->itemMapper->where(['feedId' => $feed->getId(), 'title' => 'blubbs']); + foreach ($items as $item) { + $this->assertTrue($item->isUnread()); + } + + // assert that other user's same items stayed the same + $johnsFeed = $this->feedMapper->where(['userId' => 'john'])[0]; + $items = $this->itemMapper->where(['feedId' => $johnsFeed->getId()]); + foreach ($items as $item) { + $this->assertTrue($item->isUnread()); + } + } + + public function testUnreadItem() { + $this->loadFixtures('readitem'); + // unread an item + $feed = $this->feedMapper->where(['userId' => 'test'])[0]; + $duplicateItem = $this->itemMapper->where(['feedId' => $feed->getId()])[0]; + $this->itemMapper->readItem($duplicateItem->getId(), true, 1000, $this->user); + $this->itemMapper->readItem($duplicateItem->getId(), false, 1000, $this->user); + + // assert that only one item is now unread + $items = $this->itemMapper->where(['feedId' => $feed->getId(), 'title' => 'blubb']); + foreach ($items as $item) { + if ($item->getId() === $duplicateItem->getId()) { + $this->assertTrue($item->isUnread()); + } else { + $this->assertTrue($item->isRead()); + } + } + + // assert that other user's same items stayed the same + $johnsFeed = $this->feedMapper->where(['userId' => 'john'])[0]; + $items = $this->itemMapper->where(['feedId' => $johnsFeed->getId()]); + foreach ($items as $item) { + $this->assertTrue($item->isUnread()); + } + } + + protected function tearDown() { + parent::tearDown(); + $this->clearUserNewsDatabase('john'); + } + +} diff --git a/tests/Integration/Fixtures/FeedFixture.php b/tests/Integration/Fixtures/FeedFixture.php new file mode 100644 index 000000000..eafcab906 --- /dev/null +++ b/tests/Integration/Fixtures/FeedFixture.php @@ -0,0 +1,50 @@ + + * @copyright Bernhard Posselt 2015 + */ + + +namespace OCA\News\Tests\Integration\Fixtures; + + +use OCA\News\Db\Feed; + +class FeedFixture extends Feed { + + use Fixture; + + public function __construct(array $defaults=[]) { + parent::__construct(); + $defaults = array_merge([ + 'userId' => 'test', + 'urlHash' => 'urlHash', + 'url' => 'http://the.url.com', + 'title' => 'title', + 'faviconLink' => 'http://feed.com/favicon.ico', + 'added' => 3000, + 'folderId' => 0, + 'link' => 'http://feed.com/rss', + 'preventUpdate' => false, + 'deletedAt' => 0, + 'articlesPerUpdate' => 40, + 'httpLastModified' => 10, + 'httpEtag' => '', + 'location' => 'http://feed.com/rss', + 'ordering' => 0, + 'fullTextEnabled' => false, + 'pinned' => false, + 'updateMode' => 0, + 'updateErrorCount' => 0, + 'lastUpdateError' => '', + ], $defaults); + unset($defaults['items']); + $this->fillDefaults($defaults); + } + +} diff --git a/tests/Integration/Fixtures/Fixture.php b/tests/Integration/Fixtures/Fixture.php new file mode 100644 index 000000000..a5bceda17 --- /dev/null +++ b/tests/Integration/Fixtures/Fixture.php @@ -0,0 +1,25 @@ + + * @copyright Bernhard Posselt 2015 + */ + + +namespace OCA\News\Tests\Integration\Fixtures; + + +trait Fixture { + + protected function fillDefaults(array $defaults=[]) { + foreach ($defaults as $key => $value) { + $method = 'set' . ucfirst($key); + $this->$method($value); + } + } + +} \ No newline at end of file diff --git a/tests/Integration/Fixtures/FolderFixture.php b/tests/Integration/Fixtures/FolderFixture.php new file mode 100644 index 000000000..2dc6b3a44 --- /dev/null +++ b/tests/Integration/Fixtures/FolderFixture.php @@ -0,0 +1,35 @@ + + * @copyright Bernhard Posselt 2015 + */ + + +namespace OCA\News\Tests\Integration\Fixtures; + + +use OCA\News\Db\Folder; + +class FolderFixture extends Folder { + use Fixture; + + public function __construct(array $defaults=[]) { + parent::__construct(); + $defaults = array_merge([ + 'parentId' => 0, + 'name' => 'folder', + 'userId' => 'test', + 'opened' => true, + 'deletedAt' => 0, + 'lastModified' => 9 + ], $defaults); + unset($defaults['feeds']); + $this->fillDefaults($defaults); + } + +} \ No newline at end of file diff --git a/tests/Integration/Fixtures/ItemFixture.php b/tests/Integration/Fixtures/ItemFixture.php new file mode 100644 index 000000000..9e6e07fe2 --- /dev/null +++ b/tests/Integration/Fixtures/ItemFixture.php @@ -0,0 +1,49 @@ + + * @copyright Bernhard Posselt 2015 + */ + + +namespace OCA\News\Tests\Integration\Fixtures; + + +use OCA\News\Db\Item; + +class ItemFixture extends Item { + use Fixture; + + public function __construct(array $defaults=[]) { + parent::__construct(); + $defaults = array_merge([ + 'url' => 'http://google.de', + 'title' => 'title', + 'author' => 'my author', + 'pubDate' => 2323, + 'body' => 'this is a body', + 'enclosureMime' => 'video/mpeg', + 'enclosureLink' => 'http://google.de/web.webm', + 'feedId' => 0, + 'status' => 2, + 'lastModified' => 113, + 'rtl' => false, + ], $defaults); + + if (!array_key_exists('guid', $defaults)) { + $defaults['guid'] = $defaults['title']; + } + + if (!array_key_exists('guidHash', $defaults)) { + $defaults['guidHash'] = $defaults['guid']; + } + + $this->fillDefaults($defaults); + $this->generateSearchIndex(); + } + +} diff --git a/tests/Integration/Fixtures/data/default.php b/tests/Integration/Fixtures/data/default.php new file mode 100644 index 000000000..d1383c3f7 --- /dev/null +++ b/tests/Integration/Fixtures/data/default.php @@ -0,0 +1,76 @@ + + * @copyright Bernhard Posselt 2015 + */ + +return [ + 'folders' => [ + [ + 'name' => 'first folder', + 'feeds' => [ + [ + 'title' => 'first feed', + 'url' => 'http://google.de', + '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' => 'second feed', + 'url' => 'http://golem.de', + 'items' => [] + ], + ], + ], + [ + 'name' => 'second folder', + 'opened' => false, + 'feeds' => [ + [ + 'title' => 'third feed', + 'url' => 'http://heise.de', + 'items' => [['title' => 'a title9']] + ], + [ + 'title' => 'sixth feed', + 'url' => 'http://gremlins.de', + 'deletedAt' => 999999999, + 'items' => [['title' => 'not found feed', 'guid' => 'not found']] + ] + ], + ], + [ + 'name' => 'third folder', + 'deletedAt' => 9999999999, + 'feeds' => [ + [ + 'title' => 'fifth feed', + 'url' => 'http://prolinux.de', + 'items' => [['title' => 'not found folder', 'guid' => 'not found']] + ] + ], + ] + ], + 'feeds' => [ + [ + 'title' => 'fourth feed', + 'url' => 'http://blog.fefe.de', + 'items' => [ + ['title' => 'no folder', 'status' => 0] + ] + ] + ] +]; \ No newline at end of file diff --git a/tests/Integration/Fixtures/data/readitem.php b/tests/Integration/Fixtures/data/readitem.php new file mode 100644 index 000000000..7e3d68fad --- /dev/null +++ b/tests/Integration/Fixtures/data/readitem.php @@ -0,0 +1,33 @@ + + * @copyright Bernhard Posselt 2015 + */ + +return [ + 'feeds' => [ + [ + 'title' => 'john feed', + 'userId' => 'john', + 'items' => [ + ['title' => 'blubb', 'status' => 2], + ['title' => 'blubb', 'status' => 2] + ] + ], + [ + 'title' => 'test feed', + 'userId' => 'test', + 'items' => [ + ['title' => 'blubb', 'status' => 2], + ['title' => 'blubbs', 'status' => 2], + ['title' => 'blubb', 'status' => 2], + ['title' => 'blubb', 'status' => 2] + ] + ] + ] +]; diff --git a/tests/Integration/IntegrationTest.php b/tests/Integration/IntegrationTest.php new file mode 100644 index 000000000..1e89bd8c2 --- /dev/null +++ b/tests/Integration/IntegrationTest.php @@ -0,0 +1,206 @@ + + * @copyright Bernhard Posselt 2015 + */ + + +namespace OCA\News\Tests\Integration; + +use PHPUnit_Framework_TestCase; + +use OCA\News\Db\Feed; +use OCA\News\Db\Item; +use OCP\AppFramework\Db\Entity; +use OCP\AppFramework\IAppContainer; + +use OCP\IDb; +use OCP\IUserSession; +use OCP\IUserManager; + +use OCA\News\AppInfo\Application; +use OCA\News\Tests\Integration\Fixtures\ItemFixture; +use OCA\News\Tests\Integration\Fixtures\FeedFixture; +use OCA\News\Tests\Integration\Fixtures\FolderFixture; +use OCA\News\Db\FeedMapper; +use OCA\News\Db\ItemMapper; +use OCA\News\Db\FolderMapper; + + +abstract class IntegrationTest extends PHPUnit_Framework_TestCase { + + protected $user = 'test'; + protected $userPassword = 'test'; + + /** @var ItemMapper */ + protected $itemMapper; + + /** @var FeedMapper */ + protected $feedMapper; + + /** @var FolderMapper */ + protected $folderMapper; + + /** @var IAppContainer */ + protected $container; + + protected function setUp() { + parent::setUp(); + $app = new Application(); + $this->container = $app->getContainer(); + $this->tearDownUser($this->user); + $this->setupUser($this->user, $this->userPassword); + + // set up database layers + $this->itemMapper = $this->container->query(ItemMapper::class); + $this->feedMapper = $this->container->query(FeedMapper::class); + $this->folderMapper = $this->container->query(FolderMapper::class); + } + + protected function findItemByTitle($title) { + // db logic in app code, negligible since its a test + $items = $this->itemMapper->where(['title' => $title]); + $feeds = $this->feedMapper->where(['userId' => $this->user]); + + $feedIds = []; + foreach ($feeds as $feed) { + $feedIds[$feed->getId()] = true; + } + + $result = array_filter($items, + function (Item $item) use ($feedIds) { + return array_key_exists($item->getFeedId(), $feedIds); + }); + + // ok so this is funny: array_filter preserves indices, meaning that + // you can't use 0 as key for the first element return from it :D + $result = array_values($result)[0]; + + return $result; + } + + protected function findFolderByName($name) { + return $this->folderMapper->where([ + 'userId' => $this->user, + 'name' => $name + ])[0]; + } + + protected function findFeedByTitle($title) { + return $this->feedMapper->where([ + 'userId' => $this->user, + 'title' => $title + ])[0]; + } + + /** + * @param string $name loads fixtures from a given file + */ + protected function loadFixtures($name) { + $fixtures = include __DIR__ . '/Fixtures/data/' . $name . '.php'; + if (array_key_exists('folders', $fixtures)) { + $this->loadFolderFixtures($fixtures['folders']); + } + if (array_key_exists('feeds', $fixtures)) { + $this->loadFeedFixtures($fixtures['feeds']); + } + } + + protected function loadFolderFixtures(array $folderFixtures=[]) { + foreach ($folderFixtures as $folderFixture) { + $folder = new FolderFixture($folderFixture); + $folderId = $this->loadFixture($folder); + $this->loadFeedFixtures($folderFixture['feeds'], $folderId); + } + } + + protected function loadFeedFixtures(array $feedFixtures=[], $folderId=0) { + foreach ($feedFixtures as $feedFixture) { + $feed = new FeedFixture($feedFixture); + $feed->setFolderId($folderId); + $feedId = $this->loadFixture($feed); + $this->loadItemFixtures($feedFixture['items'], $feedId); + } + } + + protected function loadItemFixtures(array $itemFixtures=[], $feedId) { + foreach ($itemFixtures as $itemFixture) { + $item = new ItemFixture($itemFixture); + $item->setFeedId($feedId); + $this->loadFixture($item); + } + } + + /** + * Saves a fixture in a database and returns the saved result + * @param Entity $fixture + * @return int the id + */ + protected function loadFixture(Entity $fixture) { + if ($fixture instanceof FeedFixture) { + return $this->feedMapper->insert($fixture)->getId(); + } elseif ($fixture instanceof ItemFixture) { + return $this->itemMapper->insert($fixture)->getId(); + } elseif ($fixture instanceof FolderFixture) { + return $this->folderMapper->insert($fixture)->getId(); + } + + throw new \InvalidArgumentException('Invalid fixture class given'); + } + + /** + * Creates and logs in a new ownCloud user + * @param $user + * @param $password + */ + protected function setupUser($user, $password) { + $userManager = $this->container->query(IUserManager::class); + $userManager->createUser($user, $password); + + $session = $this->container->query(IUserSession::class); + $session->login($user, $password); + } + + /** + * Removes a user and his News app database entries from the database + * @param $user + */ + protected function tearDownUser($user) { + $userManager = $this->container->query(IUserManager::class); + + if ($userManager->userExists($user)) { + $userManager->get($user)->delete(); + } + + $this->clearUserNewsDatabase($user); + } + + /** + * Deletes all news entries of a given user + * @param string $user + */ + protected function clearUserNewsDatabase($user) { + $sql = [ + 'DELETE FROM `*PREFIX*news_items` WHERE `feed_id` IN + (SELECT `id` FROM `*PREFIX*news_feeds` WHERE `user_id` = ?)', + 'DELETE FROM `*PREFIX*news_feeds` WHERE `user_id` = ?', + 'DELETE FROM `*PREFIX*news_folders` WHERE `user_id` = ?' + ]; + + $db = $this->container->query(IDb::class); + foreach ($sql as $query) { + $db->prepareQuery($query)->execute([$user]); + } + } + + protected function tearDown() { + parent::tearDown(); + $this->tearDownUser($this->user); + } + +} \ No newline at end of file diff --git a/tests/Unit/Config/ConfigTest.php b/tests/Unit/Config/ConfigTest.php new file mode 100644 index 000000000..a30601403 --- /dev/null +++ b/tests/Unit/Config/ConfigTest.php @@ -0,0 +1,230 @@ + + * @author Bernhard Posselt + * @copyright Alessandro Cosentino 2012 + * @copyright Bernhard Posselt 2012, 2014 + */ + +namespace OCA\News\Config; + +use PHPUnit_Framework_TestCase; + + +class ConfigTest extends PHPUnit_Framework_TestCase { + + private $fileSystem; + private $config; + private $configPath; + private $loggerParams; + + public function setUp() { + $this->logger = $this->getMockBuilder( + 'OCP\ILogger') + ->disableOriginalConstructor() + ->getMock(); + $this->fileSystem = $this->getMockBuilder('OCP\Files\Folder')->getMock(); + $this->loggerParams = ['hi']; + $this->config = new Config( + $this->fileSystem, $this->logger, $this->loggerParams + ); + $this->configPath = 'config.json'; + } + + + public function testDefaults() { + $this->assertEquals(60, $this->config->getAutoPurgeMinimumInterval()); + $this->assertEquals(200, $this->config->getAutoPurgeCount()); + $this->assertEquals(10, $this->config->getMaxRedirects()); + $this->assertEquals(60, $this->config->getFeedFetcherTimeout()); + $this->assertEquals(true, $this->config->getUseCronUpdates()); + $this->assertEquals('', $this->config->getExploreUrl()); + $this->assertEquals(1024*1024*100, $this->config->getMaxSize()); + } + + + public function testRead () { + $file = $this->getMockBuilder('OCP\Files\File')->getMock(); + $this->fileSystem->expects($this->once()) + ->method('get') + ->with($this->equalTo($this->configPath)) + ->will($this->returnValue($file)); + $file->expects($this->once()) + ->method('getContent') + ->will($this->returnValue( + 'autoPurgeCount = 3' . "\n" . 'useCronUpdates = true' + )); + + + $this->config->read($this->configPath); + + $this->assertSame(3, $this->config->getAutoPurgeCount()); + $this->assertSame(true, $this->config->getUseCronUpdates()); + } + + + public function testReadIgnoresVeryLowPurgeInterval () { + $file = $this->getMockBuilder('OCP\Files\File')->getMock(); + $this->fileSystem->expects($this->once()) + ->method('get') + ->with($this->equalTo($this->configPath)) + ->will($this->returnValue($file)); + $file->expects($this->once()) + ->method('getContent') + ->will($this->returnValue('autoPurgeMinimumInterval = 59')); + + $this->config->read($this->configPath); + + $this->assertSame(60, $this->config->getAutoPurgeMinimumInterval()); + } + + + + public function testReadBool () { + $file = $this->getMockBuilder('OCP\Files\File')->getMock(); + $this->fileSystem->expects($this->once()) + ->method('get') + ->with($this->equalTo($this->configPath)) + ->will($this->returnValue($file)); + $file->expects($this->once()) + ->method('getContent') + ->will($this->returnValue( + 'autoPurgeCount = 3' . "\n" . 'useCronUpdates = false') + ); + + $this->config->read($this->configPath); + + $this->assertSame(3, $this->config->getAutoPurgeCount()); + $this->assertSame(false, $this->config->getUseCronUpdates()); + } + + + public function testReadLogsInvalidValue() { + $file = $this->getMockBuilder('OCP\Files\File')->getMock(); + $this->fileSystem->expects($this->once()) + ->method('get') + ->with($this->equalTo($this->configPath)) + ->will($this->returnValue($file)); + $file->expects($this->once()) + ->method('getContent') + ->will($this->returnValue('autoPurgeCounts = 3')); + $this->logger->expects($this->once()) + ->method('warning') + ->with($this->equalTo('Configuration value "autoPurgeCounts" ' . + 'does not exist. Ignored value.'), + $this->equalTo($this->loggerParams)); + + $this->config->read($this->configPath); + } + + + public function testReadLogsInvalidINI() { + $file = $this->getMockBuilder('OCP\Files\File')->getMock(); + $this->fileSystem->expects($this->once()) + ->method('get') + ->with($this->equalTo($this->configPath)) + ->will($this->returnValue($file)); + $file->expects($this->once()) + ->method('getContent') + ->will($this->returnValue('')); + $this->logger->expects($this->once()) + ->method('warning') + ->with($this->equalTo('Configuration invalid. Ignoring values.'), + $this->equalTo($this->loggerParams)); + + $this->config->read($this->configPath); + } + + + public function testWrite () { + $json = 'autoPurgeMinimumInterval = 60' . "\n" . + 'autoPurgeCount = 3' . "\n" . + 'maxRedirects = 10' . "\n" . + 'maxSize = 399' . "\n" . + 'exploreUrl = http://google.de' . "\n" . + 'feedFetcherTimeout = 60' . "\n" . + 'useCronUpdates = true'; + $this->config->setAutoPurgeCount(3); + $this->config->setMaxSize(399); + $this->config->setExploreUrl('http://google.de'); + + $file = $this->getMockBuilder('OCP\Files\File')->getMock(); + $this->fileSystem->expects($this->once()) + ->method('get') + ->with($this->equalTo($this->configPath)) + ->will($this->returnValue($file)); + $file->expects($this->once()) + ->method('putContent') + ->with($this->equalTo($json)); + + $this->config->write($this->configPath); + } + + + + public function testReadingNonExistentConfigWillWriteDefaults() { + $this->fileSystem->expects($this->once()) + ->method('nodeExists') + ->with($this->equalTo($this->configPath)) + ->will($this->returnValue(false)); + + $this->config->setUseCronUpdates(false); + + $json = 'autoPurgeMinimumInterval = 60' . "\n" . + 'autoPurgeCount = 200' . "\n" . + 'maxRedirects = 10' . "\n" . + 'maxSize = 104857600' . "\n" . + 'exploreUrl = ' . "\n" . + 'feedFetcherTimeout = 60' . "\n" . + 'useCronUpdates = false'; + + $this->fileSystem->expects($this->once()) + ->method('newFile') + ->with($this->equalTo($this->configPath)); + $file = $this->getMockBuilder('OCP\Files\File')->getMock(); + $this->fileSystem->expects($this->once()) + ->method('get') + ->with($this->equalTo($this->configPath)) + ->will($this->returnValue($file)); + $file->expects($this->once()) + ->method('putContent') + ->with($this->equalTo($json)); + + $this->config->read($this->configPath, true); + } + + + public function testNoLowMinimumAutoPurgeInterval() { + $this->config->setAutoPurgeMinimumInterval(59); + $interval = $this->config->getAutoPurgeMinimumInterval(); + + $this->assertSame(60, $interval); + } + + + public function testMinimumAutoPurgeInterval() { + $this->config->setAutoPurgeMinimumInterval(61); + $interval = $this->config->getAutoPurgeMinimumInterval(); + + $this->assertSame(61, $interval); + } + + public function testMaxRedirects() { + $this->config->setMaxRedirects(21); + $redirects = $this->config->getMaxRedirects(); + + $this->assertSame(21, $redirects); + } + + public function testFeedFetcherTimeout() { + $this->config->setFeedFetcherTimeout(2); + $timout = $this->config->getFeedFetcherTimeout(); + + $this->assertSame(2, $timout); + } +} diff --git a/tests/Unit/Controller/AdminControllerTest.php b/tests/Unit/Controller/AdminControllerTest.php new file mode 100644 index 000000000..9d5014636 --- /dev/null +++ b/tests/Unit/Controller/AdminControllerTest.php @@ -0,0 +1,161 @@ + + * @author Bernhard Posselt + * @copyright Alessandro Cosentino 2012 + * @copyright Bernhard Posselt 2012, 2014 + */ + +namespace OCA\News\Controller; + + +class AdminControllerTest extends \PHPUnit_Framework_TestCase { + + private $appName; + private $request; + private $controller; + private $config; + private $configPath; + private $itemService; + + /** + * Gets run before each test + */ + public function setUp(){ + $this->appName = 'news'; + $this->request = $this->getMockBuilder( + '\OCP\IRequest') + ->disableOriginalConstructor() + ->getMock(); + $this->config = $this->getMockBuilder( + '\OCA\News\Config\Config') + ->disableOriginalConstructor() + ->getMock(); + $this->itemService = $this->getMockBuilder( + '\OCA\News\Service\ItemService') + ->disableOriginalConstructor() + ->getMock(); + + $this->configPath = 'my.ini'; + $this->controller = new AdminController($this->appName, $this->request, + $this->config, $this->itemService, $this->configPath); + } + + + public function testIndex() { + $expected = [ + 'autoPurgeMinimumInterval' => 1, + 'autoPurgeCount' => 2, + 'maxRedirects' => 3, + 'feedFetcherTimeout' => 4, + 'useCronUpdates' => 5, + 'maxSize' => 7, + 'exploreUrl' => 'test' + ]; + $this->config->expects($this->once()) + ->method('getAutoPurgeMinimumInterval') + ->will($this->returnValue($expected['autoPurgeMinimumInterval'])); + $this->config->expects($this->once()) + ->method('getAutoPurgeCount') + ->will($this->returnValue($expected['autoPurgeCount'])); + $this->config->expects($this->once()) + ->method('getMaxRedirects') + ->will($this->returnValue($expected['maxRedirects'])); + $this->config->expects($this->once()) + ->method('getFeedFetcherTimeout') + ->will($this->returnValue($expected['feedFetcherTimeout'])); + $this->config->expects($this->once()) + ->method('getUseCronUpdates') + ->will($this->returnValue($expected['useCronUpdates'])); + $this->config->expects($this->once()) + ->method('getMaxSize') + ->will($this->returnValue($expected['maxSize'])); + $this->config->expects($this->once()) + ->method('getExploreUrl') + ->will($this->returnValue($expected['exploreUrl'])); + + $response = $this->controller->index(); + $data = $response->getParams(); + $name = $response->getTemplateName(); + $type = $response->getRenderAs(); + + $this->assertEquals($type, 'blank'); + $this->assertEquals($name, 'admin'); + $this->assertEquals($expected, $data); + } + + + public function testUpdate() { + $expected = [ + 'autoPurgeMinimumInterval' => 1, + 'autoPurgeCount' => 2, + 'maxRedirects' => 3, + 'feedFetcherTimeout' => 4, + 'useCronUpdates' => 5, + 'maxSize' => 7, + 'exploreUrl' => 'test' + ]; + + $this->config->expects($this->once()) + ->method('setAutoPurgeMinimumInterval') + ->with($this->equalTo($expected['autoPurgeMinimumInterval'])); + $this->config->expects($this->once()) + ->method('setAutoPurgeCount') + ->with($this->equalTo($expected['autoPurgeCount'])); + $this->config->expects($this->once()) + ->method('setMaxRedirects') + ->with($this->equalTo($expected['maxRedirects'])); + $this->config->expects($this->once()) + ->method('setFeedFetcherTimeout') + ->with($this->equalTo($expected['feedFetcherTimeout'])); + $this->config->expects($this->once()) + ->method('setUseCronUpdates') + ->with($this->equalTo($expected['useCronUpdates'])); + $this->config->expects($this->once()) + ->method('setExploreUrl') + ->with($this->equalTo($expected['exploreUrl'])); + $this->config->expects($this->once()) + ->method('write') + ->with($this->equalTo($this->configPath)); + + $this->config->expects($this->once()) + ->method('getAutoPurgeMinimumInterval') + ->will($this->returnValue($expected['autoPurgeMinimumInterval'])); + $this->config->expects($this->once()) + ->method('getAutoPurgeCount') + ->will($this->returnValue($expected['autoPurgeCount'])); + $this->config->expects($this->once()) + ->method('getMaxRedirects') + ->will($this->returnValue($expected['maxRedirects'])); + $this->config->expects($this->once()) + ->method('getFeedFetcherTimeout') + ->will($this->returnValue($expected['feedFetcherTimeout'])); + $this->config->expects($this->once()) + ->method('getUseCronUpdates') + ->will($this->returnValue($expected['useCronUpdates'])); + $this->config->expects($this->once()) + ->method('getMaxSize') + ->will($this->returnValue($expected['maxSize'])); + $this->config->expects($this->once()) + ->method('getExploreUrl') + ->will($this->returnValue($expected['exploreUrl'])); + + $response = $this->controller->update( + $expected['autoPurgeMinimumInterval'], + $expected['autoPurgeCount'], + $expected['maxRedirects'], + $expected['feedFetcherTimeout'], + $expected['maxSize'], + $expected['useCronUpdates'], + $expected['exploreUrl'] + ); + + $this->assertEquals($expected, $response); + } + +} diff --git a/tests/Unit/Controller/EntityApiSerializerTest.php b/tests/Unit/Controller/EntityApiSerializerTest.php new file mode 100644 index 000000000..80752889d --- /dev/null +++ b/tests/Unit/Controller/EntityApiSerializerTest.php @@ -0,0 +1,116 @@ + + * @author Bernhard Posselt + * @copyright Alessandro Cosentino 2012 + * @copyright Bernhard Posselt 2012, 2014 + */ + +namespace OCA\News\Controller; + + +use \OCP\AppFramework\Http\Response; +use \OCP\AppFramework\Db\Entity; + +use \OCA\News\Db\Item; + +class TestEntity extends Entity { + +} + + +class EntityApiSerializerTest extends \PHPUnit_Framework_TestCase { + + + public function testSerializeSingle() { + $item = new Item(); + $item->setUnread(); + + $serializer = new EntityApiSerializer('items'); + $result = $serializer->serialize($item); + + $this->assertTrue($result['items'][0]['unread']); + } + + + public function testSerializeMultiple() { + $item = new Item(); + $item->setUnread(); + + $item2 = new Item(); + $item2->setRead(); + + $serializer = new EntityApiSerializer('items'); + + $result = $serializer->serialize([$item, $item2]); + + $this->assertTrue($result['items'][0]['unread']); + $this->assertFalse($result['items'][1]['unread']); + } + + + public function testResponseNoChange() { + $response = new Response(); + $serializer = new EntityApiSerializer('items'); + + $result = $serializer->serialize($response); + + $this->assertEquals($response, $result); + } + + + public function testCompleteArraysTransformed() { + $item = new Item(); + $item->setUnread(); + + $item2 = new Item(); + $item2->setRead(); + + $serializer = new EntityApiSerializer('items'); + + $in = [ + 'items' => [$item, $item2], + 'test' => 1 + ]; + + $result = $serializer->serialize($in); + + $this->assertTrue($result['items'][0]['unread']); + $this->assertFalse($result['items'][1]['unread']); + $this->assertEquals(1, $result['test']); + } + + + public function testNoEntityNoChange() { + $serializer = new EntityApiSerializer('items'); + + $in = [ + 'items' => ['hi', '2'], + 'test' => 1 + ]; + + $result = $serializer->serialize($in); + + $this->assertEquals('hi', $result['items'][0]); + $this->assertEquals('2', $result['items'][1]); + $this->assertEquals(1, $result['test']); + } + + + public function testEntitiesNoChange() { + $serializer = new EntityApiSerializer('items'); + + $in = [ + 'items' => [new TestEntity()] + ]; + + $result = $serializer->serialize($in); + + $this->assertEquals($in, $result); + } +} \ No newline at end of file diff --git a/tests/Unit/Controller/ExportControllerTest.php b/tests/Unit/Controller/ExportControllerTest.php new file mode 100644 index 000000000..6e7df683e --- /dev/null +++ b/tests/Unit/Controller/ExportControllerTest.php @@ -0,0 +1,131 @@ + + * @author Bernhard Posselt + * @copyright Alessandro Cosentino 2012 + * @copyright Bernhard Posselt 2012, 2014 + */ + +namespace OCA\News\Controller; + +use \OCP\AppFramework\Http; + +use \OCA\News\Http\TextDownloadResponse; +use \OCA\News\Utility\OPMLExporter; +use \OCA\News\Db\Item; +use \OCA\News\Db\Feed; + + +class ExportControllerTest extends \PHPUnit_Framework_TestCase { + + private $appName; + private $request; + private $controller; + private $user; + private $feedService; + private $folderService; + private $itemService; + private $opmlExporter; + + /** + * Gets run before each test + */ + public function setUp(){ + $this->appName = 'news'; + $this->user = 'john'; + $this->itemService = $this->getMockBuilder( + '\OCA\News\Service\ItemService') + ->disableOriginalConstructor() + ->getMock(); + $this->feedService = $this->getMockBuilder( + '\OCA\News\Service\FeedService') + ->disableOriginalConstructor() + ->getMock(); + $this->folderService = $this->getMockBuilder( + '\OCA\News\Service\FolderService') + ->disableOriginalConstructor() + ->getMock(); + $this->request = $this->getMockBuilder('\OCP\IRequest') + ->disableOriginalConstructor() + ->getMock(); + $this->opmlExporter = new OPMLExporter(); + $this->controller = new ExportController($this->appName, $this->request, + $this->folderService, $this->feedService, + $this->itemService, $this->opmlExporter, $this->user); + } + + + public function testOpmlExportNoFeeds(){ + $opml = + "\n" . + "\n" . + " \n" . + " Subscriptions\n" . + " \n" . + " \n" . + "\n"; + + $this->feedService->expects($this->once()) + ->method('findAll') + ->with($this->equalTo($this->user)) + ->will($this->returnValue([])); + $this->folderService->expects($this->once()) + ->method('findAll') + ->with($this->equalTo($this->user)) + ->will($this->returnValue([])); + + $return = $this->controller->opml(); + $this->assertTrue($return instanceof TextDownloadResponse); + $this->assertEquals($opml, $return->render()); + } + + + public function testGetAllArticles(){ + $item1 = new Item(); + $item1->setFeedId(3); + $item2 = new Item(); + $item2->setFeedId(5); + + $feed1 = new Feed(); + $feed1->setId(3); + $feed1->setLink('http://goo'); + $feed2 = new Feed(); + $feed2->setId(5); + $feed2->setLink('http://gee'); + $feeds = [$feed1, $feed2]; + + $articles = [$item1, $item2]; + + $this->feedService->expects($this->once()) + ->method('findAll') + ->with($this->equalTo($this->user)) + ->will($this->returnValue($feeds)); + $this->itemService->expects($this->once()) + ->method('getUnreadOrStarred') + ->with($this->equalTo($this->user)) + ->will($this->returnValue($articles)); + + + $return = $this->controller->articles(); + $headers = $return->getHeaders(); + $this->assertEquals( + 'attachment; filename="articles.json"', + $headers ['Content-Disposition'] + ); + + $this->assertEquals('[{"guid":null,"url":null,"title":null,' . + '"author":null,"pubDate":null,"body":null,"enclosureMime":null,' . + '"enclosureLink":null,"unread":false,"starred":false,' . + '"feedLink":"http:\/\/goo","rtl":null},{"guid":null,"url":null,' . + '"title":null,"author":null,"pubDate":null,"body":null,' . + '"enclosureMime":null,"enclosureLink":null,"unread":false,' . + '"starred":false,"feedLink":"http:\/\/gee","rtl":null}]', + $return->render()); + } + +} diff --git a/tests/Unit/Controller/FeedApiControllerTest.php b/tests/Unit/Controller/FeedApiControllerTest.php new file mode 100644 index 000000000..f13e96660 --- /dev/null +++ b/tests/Unit/Controller/FeedApiControllerTest.php @@ -0,0 +1,353 @@ + + * @author Bernhard Posselt + * @copyright Alessandro Cosentino 2012 + * @copyright Bernhard Posselt 2012, 2014 + */ + +namespace OCA\News\Controller; + +use \OCP\AppFramework\Http; + +use \OCA\News\Service\ServiceNotFoundException; +use \OCA\News\Service\ServiceConflictException; +use \OCA\News\Db\Folder; +use \OCA\News\Db\Feed; +use \OCA\News\Db\Item; + + +class FeedApiControllerTest extends \PHPUnit_Framework_TestCase { + + private $feedService; + private $itemService; + private $feedAPI; + private $appName; + private $user; + private $request; + private $msg; + private $logger; + private $loggerParams; + + protected function setUp() { + $this->user = 'tom'; + $this->loggerParams = ['hi']; + $this->logger = $this->getMockBuilder( + '\OCP\ILogger') + ->disableOriginalConstructor() + ->getMock(); + $this->appName = 'news'; + $this->request = $this->getMockBuilder( + '\OCP\IRequest') + ->disableOriginalConstructor() + ->getMock(); + $this->feedService = $this->getMockBuilder( + '\OCA\News\Service\FeedService') + ->disableOriginalConstructor() + ->getMock(); + $this->itemService = $this->getMockBuilder( + '\OCA\News\Service\ItemService') + ->disableOriginalConstructor() + ->getMock(); + $this->feedAPI = new FeedApiController( + $this->appName, + $this->request, + $this->feedService, + $this->itemService, + $this->logger, + $this->user, + $this->loggerParams + ); + $this->msg = 'hohoho'; + } + + + public function testIndex() { + $feeds = [new Feed()]; + $starredCount = 3; + $newestItemId = 2; + + $this->itemService->expects($this->once()) + ->method('starredCount') + ->with($this->equalTo($this->user)) + ->will($this->returnValue($starredCount)); + $this->itemService->expects($this->once()) + ->method('getNewestItemId') + ->with($this->equalTo($this->user)) + ->will($this->returnValue($newestItemId)); + $this->feedService->expects($this->once()) + ->method('findAll') + ->with($this->equalTo($this->user)) + ->will($this->returnValue($feeds)); + + $response = $this->feedAPI->index(); + + $this->assertEquals([ + 'feeds' => [$feeds[0]->toAPI()], + 'starredCount' => $starredCount, + 'ne