diff options
Diffstat (limited to 'tests/Unit/Controller')
-rw-r--r-- | tests/Unit/Controller/AdminControllerTest.php | 161 | ||||
-rw-r--r-- | tests/Unit/Controller/EntityApiSerializerTest.php | 116 | ||||
-rw-r--r-- | tests/Unit/Controller/ExportControllerTest.php | 131 | ||||
-rw-r--r-- | tests/Unit/Controller/FeedApiControllerTest.php | 353 | ||||
-rw-r--r-- | tests/Unit/Controller/FeedControllerTest.php | 484 | ||||
-rw-r--r-- | tests/Unit/Controller/FolderApiControllerTest.php | 255 | ||||
-rw-r--r-- | tests/Unit/Controller/FolderControllerTest.php | 306 | ||||
-rw-r--r-- | tests/Unit/Controller/ItemApiControllerTest.php | 372 | ||||
-rw-r--r-- | tests/Unit/Controller/ItemControllerTest.php | 384 | ||||
-rw-r--r-- | tests/Unit/Controller/JSONHttpErrorTest.php | 34 | ||||
-rw-r--r-- | tests/Unit/Controller/PageControllerTest.php | 322 | ||||
-rw-r--r-- | tests/Unit/Controller/UserApiControllerTest.php | 141 | ||||
-rw-r--r-- | tests/Unit/Controller/UtilityApiControllerTest.php | 90 |
13 files changed, 3149 insertions, 0 deletions
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 @@ +<?php +/** + * ownCloud - News + * + * This file is licensed under the Affero General Public License version 3 or + * later. See the COPYING file. + * + * @author Alessandro Cosentino <cosenal@gmail.com> + * @author Bernhard Posselt <dev@bernhard-posselt.com> + * @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 @@ +<?php +/** + * ownCloud - News + * + * This file is licensed under the Affero General Public License version 3 or + * later. See the COPYING file. + * + * @author Alessandro Cosentino <cosenal@gmail.com> + * @author Bernhard Posselt <dev@bernhard-posselt.com> + * @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 @@ +<?php +/** + * ownCloud - News + * + * This file is licensed under the Affero General Public License version 3 or + * later. See the COPYING file. + * + * @author Alessandro Cosentino <cosenal@gmail.com> + * @author Bernhard Posselt <dev@bernhard-posselt.com> + * @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 = + "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n" . + "<opml version=\"2.0\">\n" . + " <head>\n" . + " <title>Subscriptions</title>\n" . + " </head>\n" . + " <body/>\n" . + "</opml>\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 @@ +<?php +/** + * ownCloud - News + * + * This file is licensed under the Affero General Public License version 3 or + * later. See the COPYING file. + * + * @author Alessandro Cosentino <cosenal@gmail.com> + * @author Bernhard Posselt <dev@bernhard-posselt.com> + * @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, + 'newestItemId' => $newestItemId + ], $response); + } + + + public function testIndexNoNewestItemId() { + $feeds = [new Feed()]; + $starredCount = 3; + + $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->throwException(new ServiceNotFoundException(''))); + $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, + ], $response); + } + + + public function testDelete() { + $this->feedService->expects($this->once()) + ->method('delete') + ->with( + $this->equalTo(2), + $this->equalTo($this->user)); + + $this->feedAPI->delete(2); + } + + + public function testDeleteDoesNotExist() { + $this->feedService->expects($this->once()) + ->method('delete') + ->will($this->throwException( + new ServiceNotFoundException($this->msg)) + ); + + $response = $this->feedAPI->delete(2); + + $data = $response->getData(); + $this->assertEquals($this->msg, $data['message']); + $this->assertEquals(Http::STATUS_NOT_FOUND, $response->getStatus()); + } + + + public function testCreate() { + $feeds = [new Feed()]; + + $this->feedService->expects($this->once()) + ->method('purgeDeleted') + ->with($this->equalTo($this->user), $this->equalTo(false)); + $this->feedService->expects($this->once()) + ->method('create') + ->with( + $this->equalTo('url'), + $this->equalTo(3), + $this->equalTo($this->user)) + ->will($this->returnValue($feeds[0])); + $this->itemService->expects($this->once()) + ->method('getNewestItemId') + ->will($this->returnValue(3)); + + $response = $this->feedAPI->create('url', 3); + + $this->assertEquals([ + 'feeds' => [$feeds[0]->toAPI()], + 'newestItemId' => 3 + ], $response); + } + + + public function testCreateNoItems() { + $feeds = [new Feed()]; + + $this->feedService->expects($this->once()) + ->method('purgeDeleted') + ->with($this->equalTo($this->user), $this->equalTo(false)); + $this->feedService->expects($this->once()) + ->method('create') + ->with( + $this->equalTo('ho'), + $this->equalTo(3), + $this->equalTo($this->user)) + ->will($this->returnValue($feeds[0])); + $this->itemService->expects($this->once()) + ->method('getNewestItemId') + ->will($this->throwException(new ServiceNotFoundException(''))); + + $response = $this->feedAPI->create('ho', 3); + + $this->assertEquals([ + 'feeds' => [$feeds[0]->toAPI()] + ], $response); + } + + + + public function testCreateExists() { + $this->feedService->expects($this->once()) + ->method('purgeDeleted') + ->with($this->equalTo($this->user), $this->equalTo(false)); + $this->feedService->expects($this->once()) + ->method('create') + ->will( + $this->throwException(new ServiceConflictException($this->msg)) + ); + + $response = $this->feedAPI->create('ho', 3); + + $data = $response->getData(); + $this->assertEquals($this->msg, $data['message']); + $this->assertEquals(Http::STATUS_CONFLICT, $response->getStatus()); + } + + + public function testCreateError() { + $this->feedService->expects($this->once()) + ->method('create') + ->will( + $this->throwException(new ServiceNotFoundException($this->msg)) + ); + + $response = $this->feedAPI->create('ho', 3); + + $data = $response->getData(); + $this->assertEquals($this->msg, $data['message']); + $this->assertEquals(Http::STATUS_NOT_FOUND, $response->getStatus()); + } + + + public function testRead() { + $this->itemService->expects($this->once()) + ->method('readFeed') + ->with( + $this->equalTo(3), + $this->equalTo(30), + $this->equalTo($this->user)); + + $this->feedAPI->read(3, 30); + } + + + public function testMove() { + $this->feedService->expects($this->once()) + ->method('patch') + ->with( + $this->equalTo(3), + $this->equalTo($this->user), + $this->equalTo(['folderId' => 30])); + + $this->feedAPI->move(3, 30); + } + + + public function testMoveDoesNotExist() { + $this->feedService->expects($this->once()) + ->method('patch') + ->will( + $this->throwException(new ServiceNotFoundException($this->msg)) + ); + + $response = $this->feedAPI->move(3, 4); + + $data = $response->getData(); + $this->assertEquals($this->msg, $data['message']); + $this->assertEquals(Http::STATUS_NOT_FOUND, $response->getStatus()); + } + + + public function testRename() { + $feedId = 3; + $feedTitle = 'test'; + + $this->feedService->expects($this->once()) + ->method('patch') + ->with( + $this->equalTo($feedId), + $this->equalTo($this->user), + $this->equalTo(['title' => $feedTitle])); + + $this->feedAPI->rename($feedId, $feedTitle); + } + + + public function testRenameError() { + $feedId = 3; + $feedTitle = 'test'; + + $this->feedService->expects($this->once()) + ->method('patch') + ->with( + $this->equalTo($feedId), + $this->equalTo($this->user), + $this->equalTo(['title' => $feedTitle])) + ->will($this->throwException(new ServiceNotFoundException('hi'))); + + $result = $this->feedAPI->rename($feedId, $feedTitle); + $data = $result->getData(); + $code = $result->getStatus(); + + $this->assertSame(Http::STATUS_NOT_FOUND, $code); + $this->assertSame('hi', $data['message']); + } + + + public function testfromAllUsers(){ + $feed = new Feed(); + $feed->setUrl(3); + $feed->setId(1); + $feed->setUserId('john'); + $feeds = [$feed]; + $this->feedService->expects($this->once()) + ->method('findAllFromAllUsers') + ->will($this->returnValue($feeds)); + $response = json_encode($this->feedAPI->fromAllUsers()); + $this->assertEquals('{"feeds":[{"id":1,"userId":"john"}]}', $response); + } + + + public function testUpdate() { + $feedId = 3; + $userId = 'hi'; + + $this->feedService->expects($this->once()) + ->method('update') + ->with($this->equalTo($feedId), $this->equalTo($userId)); + + $this->feedAPI->update($userId, $feedId); + } + + + public function testUpdateError() { + $feedId = 3; + $userId = 'hi'; + $this->feedService->expects($this->once()) + ->method('update') + ->will($this->throwException(new \Exception($this->msg))); + $this->logger->expects($this->once()) + ->method('debug') + ->with($this->equalTo('Could not update feed ' . $this->msg), + $this->equalTo($this->loggerParams)); + + $this->feedAPI->update($userId, $feedId); + + + } + + +} diff --git a/tests/Unit/Controller/FeedControllerTest.php b/tests/Unit/Controller/FeedControllerTest.php new file mode 100644 index 000000000..3630ad2f8 --- /dev/null +++ b/tests/Unit/Controller/FeedControllerTest.php @@ -0,0 +1,484 @@ +<?php +/** + * ownCloud - News + * + * This file is licensed under the Affero General Public License version 3 or + * later. See the COPYING file. + * + * @author Alessandro Cosentino <cosenal@gmail.com> + * @author Bernhard Posselt <dev@bernhard-posselt.com> + * @copyright Alessandro Cosentino 2012 + * @copyright Bernhard Posselt 2012, 2014 + */ + +namespace OCA\News\Controller; + +use OCP\AppFramework\Http; + +use OCA\News\Db\Feed; +use OCA\News\Db\FeedType; +use OCA\News\Service\ServiceNotFoundException; +use OCA\News\Service\ServiceConflictException; + + +class FeedControllerTest extends \PHPUnit_Framework_TestCase { + + private $appNa |