From 7171a3dba0f932bffa2b7bba6e84edf02712cea9 Mon Sep 17 00:00:00 2001 From: Bernhard Posselt Date: Fri, 5 Apr 2013 10:27:28 +0200 Subject: moved tests into unit directory --- tests/unit/bl/BlTest.php | 110 +++++++ tests/unit/bl/FeedBlTest.php | 309 ++++++++++++++++++ tests/unit/bl/FolderBlTest.php | 151 +++++++++ tests/unit/bl/ItemBlTest.php | 275 ++++++++++++++++ tests/unit/bl/StatusFlagTest.php | 74 +++++ tests/unit/controller/ExportControllerTest.php | 61 ++++ tests/unit/controller/FeedControllerTest.php | 350 +++++++++++++++++++++ tests/unit/controller/FolderControllerTest.php | 260 +++++++++++++++ tests/unit/controller/ItemControllerTest.php | 299 ++++++++++++++++++ tests/unit/controller/PageControllerTest.php | 68 ++++ tests/unit/controller/TwitterFetcherTest.php | 77 +++++ .../unit/controller/UserSettingsControllerTest.php | 112 +++++++ tests/unit/db/FeedMapperTest.php | 258 +++++++++++++++ tests/unit/db/FolderMapperTest.php | 176 +++++++++++ tests/unit/db/ItemMapperTest.php | 299 ++++++++++++++++++ tests/unit/db/ItemTest.php | 69 ++++ tests/unit/utility/FeedFetcherTest.php | 47 +++ tests/unit/utility/FetcherTest.php | 108 +++++++ 18 files changed, 3103 insertions(+) create mode 100644 tests/unit/bl/BlTest.php create mode 100644 tests/unit/bl/FeedBlTest.php create mode 100644 tests/unit/bl/FolderBlTest.php create mode 100644 tests/unit/bl/ItemBlTest.php create mode 100644 tests/unit/bl/StatusFlagTest.php create mode 100644 tests/unit/controller/ExportControllerTest.php create mode 100644 tests/unit/controller/FeedControllerTest.php create mode 100644 tests/unit/controller/FolderControllerTest.php create mode 100644 tests/unit/controller/ItemControllerTest.php create mode 100644 tests/unit/controller/PageControllerTest.php create mode 100644 tests/unit/controller/TwitterFetcherTest.php create mode 100644 tests/unit/controller/UserSettingsControllerTest.php create mode 100644 tests/unit/db/FeedMapperTest.php create mode 100644 tests/unit/db/FolderMapperTest.php create mode 100644 tests/unit/db/ItemMapperTest.php create mode 100644 tests/unit/db/ItemTest.php create mode 100644 tests/unit/utility/FeedFetcherTest.php create mode 100644 tests/unit/utility/FetcherTest.php diff --git a/tests/unit/bl/BlTest.php b/tests/unit/bl/BlTest.php new file mode 100644 index 000000000..05d78794a --- /dev/null +++ b/tests/unit/bl/BlTest.php @@ -0,0 +1,110 @@ +. +* +*/ + +namespace OCA\News\Bl; + +require_once(__DIR__ . "/../../classloader.php"); + + +use \OCA\AppFramework\Db\DoesNotExistException; +use \OCA\AppFramework\Db\MultipleObjectsReturnedException; +use \OCA\News\Db\Folder; + + +class TestBl extends BL { + public function __construct($mapper){ + parent::__construct($mapper); + } +} + +class BlTest extends \OCA\AppFramework\Utility\TestUtility { + + protected $api; + protected $mapper; + protected $newsBl; + + protected function setUp(){ + $this->api = $this->getAPIMock(); + $this->mapper = $this->getMockBuilder('\OCA\News\Db\ItemMapper') + ->disableOriginalConstructor() + ->getMock(); + $this->newsBl = new TestBl($this->mapper); + } + + + public function testDelete(){ + $id = 5; + $user = 'ken'; + $folder = new Folder(); + $folder->setId($id); + + $this->mapper->expects($this->once()) + ->method('delete') + ->with($this->equalTo($folder)); + $this->mapper->expects($this->once()) + ->method('find') + ->with($this->equalTo($id), $this->equalTo($user)) + ->will($this->returnValue($folder)); + + $result = $this->newsBl->delete($id, $user); + } + + + public function testFind(){ + $id = 3; + $user = 'ken'; + + $this->mapper->expects($this->once()) + ->method('find') + ->with($this->equalTo($id), $this->equalTo($user)); + + $result = $this->newsBl->find($id, $user); + } + + + public function testFindDoesNotExist(){ + $ex = new DoesNotExistException('hi'); + + $this->mapper->expects($this->once()) + ->method('find') + ->will($this->throwException($ex)); + + $this->setExpectedException('\OCA\News\Bl\BLException'); + $this->newsBl->find(1, ''); + } + + + public function testFindMultiple(){ + $ex = new MultipleObjectsReturnedException('hi'); + + $this->mapper->expects($this->once()) + ->method('find') + ->will($this->throwException($ex)); + + $this->setExpectedException('\OCA\News\Bl\BLException'); + $this->newsBl->find(1, ''); + } + +} diff --git a/tests/unit/bl/FeedBlTest.php b/tests/unit/bl/FeedBlTest.php new file mode 100644 index 000000000..312bd7cfe --- /dev/null +++ b/tests/unit/bl/FeedBlTest.php @@ -0,0 +1,309 @@ +. +* +*/ + +namespace { + class DatabaseException extends Exception{}; +} + + +namespace OCA\News\Bl { + +require_once(__DIR__ . "/../../classloader.php"); + +use \OCA\AppFramework\Db\DoesNotExistException; + +use \OCA\News\Db\Feed; +use \OCA\News\Db\Item; +use \OCA\News\Utility\Fetcher; +use \OCA\News\Utility\FetcherException; + +class FeedBlTest extends \OCA\AppFramework\Utility\TestUtility { + + private $mapper; + private $bl; + private $user; + private $response; + private $fetcher; + private $itemMapper; + private $threshold; + + protected function setUp(){ + $this->api = $this->getAPIMock(); + $this->mapper = $this->getMockBuilder('\OCA\News\Db\FeedMapper') + ->disableOriginalConstructor() + ->getMock(); + $this->fetcher = $this->getMockBuilder('\OCA\News\Utility\Fetcher') + ->disableOriginalConstructor() + ->getMock(); + $this->itemMapper = $this->getMockBuilder('\OCA\News\Db\ItemMapper') + ->disableOriginalConstructor() + ->getMock(); + $this->bl = new FeedBl($this->mapper, + $this->fetcher, $this->itemMapper, $this->api); + $this->user = 'jack'; + $response = 'hi'; + + } + + + public function testFindAllFromUser(){ + $this->mapper->expects($this->once()) + ->method('findAllFromUser') + ->with($this->equalTo($this->user)) + ->will($this->returnValue($this->response)); + + $result = $this->bl->findAllFromUser($this->user); + $this->assertEquals($this->response, $result); + } + + + public function testCreateDoesNotFindFeed(){ + $ex = new FetcherException('hi'); + $url = 'test'; + $this->mapper->expects($this->once()) + ->method('findByUrlHash') + ->with($this->equalTo(md5($url)), $this->equalTo($this->user)) + ->will($this->throwException(new DoesNotExistException('yo'))); + $this->fetcher->expects($this->once()) + ->method('fetch') + ->with($this->equalTo($url)) + ->will($this->throwException($ex)); + $this->setExpectedException('\OCA\News\Bl\BLException'); + $this->bl->create($url, 1, $this->user); + } + + public function testCreate(){ + $url = 'test'; + $folderId = 10; + $createdFeed = new Feed(); + $ex = new DoesNotExistException('yo'); + $createdFeed->setUrl($url); + $return = array( + $createdFeed, + array(new Item(), new Item()) + ); + + $this->mapper->expects($this->once()) + ->method('findByUrlHash') + ->with($this->equalTo(md5($url)), $this->equalTo($this->user)) + ->will($this->throwException($ex)); + $this->fetcher->expects($this->once()) + ->method('fetch') + ->with($this->equalTo($url)) + ->will($this->returnValue($return)); + $this->mapper->expects($this->once()) + ->method('insert') + ->with($this->equalTo($createdFeed)) + ->will($this->returnValue($createdFeed)); + $this->itemMapper->expects($this->at(0)) + ->method('insert') + ->with($this->equalTo($return[1][1])); + $this->itemMapper->expects($this->at(1)) + ->method('insert') + ->with($this->equalTo($return[1][0])); + + $feed = $this->bl->create($url, $folderId, $this->user); + + $this->assertEquals($feed->getFolderId(), $folderId); + $this->assertEquals($feed->getUrl(), $url); + } + + public function testCreateFeedExistsAlready(){ + $url = 'test'; + $this->mapper->expects($this->once()) + ->method('findByUrlHash') + ->with($this->equalTo(md5($url)), $this->equalTo($this->user)); + $this->setExpectedException('\OCA\News\Bl\BLException'); + $this->bl->create($url, 1, $this->user); + } + + + public function testUpdateCreatesNewEntry(){ + $feed = new Feed(); + $feed->setId(3); + $feed->getUrl('test'); + + $item = new Item(); + $item->setGuidHash(md5('hi')); + $item->setFeedId(3); + $items = array( + $item + ); + + $ex = new DoesNotExistException('hi'); + + $fetchReturn = array($feed, $items); + + $this->mapper->expects($this->once()) + ->method('find') + ->with($this->equalTo($feed->getId()), + $this->equalTo($this->user)) + ->will($this->returnValue($feed)); + $this->fetcher->expects($this->once()) + ->method('fetch') + ->will($this->returnValue($fetchReturn)); + $this->itemMapper->expects($this->once()) + ->method('findByGuidHash') + ->with($this->equalTo($items[0]->getGuidHash()), + $this->equalTo($items[0]->getFeedId()), + $this->equalTo($this->user)) + ->will($this->throwException($ex)); + $this->itemMapper->expects($this->once()) + ->method('insert') + ->with($this->equalTo($items[0])); + + $this->bl->update($feed->getId(), $this->user); + } + + + public function testUpdateUpdatesEntryNotWhenPubDateSame(){ + $feed = new Feed(); + $feed->setId(3); + $feed->getUrl('test'); + $ex = new \DatabaseException(''); + + $item = new Item(); + $item->setGuidHash(md5('hi')); + $item->setPubDate(3333); + $items = array( + $item + ); + + $fetchReturn = array($feed, $items); + + $this->mapper->expects($this->once()) + ->method('find') + ->with($this->equalTo($feed->getId()), + $this->equalTo($this->user)) + ->will($this->returnValue($feed)); + $this->fetcher->expects($this->once()) + ->method('fetch') + ->will($this->returnValue($fetchReturn)); + $this->itemMapper->expects($this->once()) + ->method('findByGuidHash') + ->with($this->equalTo($item->getGuidHash()), + $this->equalTo($feed->getId()), + $this->equalTo($this->user)) + ->will($this->returnValue($item)); + $this->itemMapper->expects($this->never()) + ->method('insert'); + $this->itemMapper->expects($this->never()) + ->method('delete'); + + $this->bl->update($feed->getId(), $this->user); + } + + + + public function testUpdateUpdatesEntry(){ + $feed = new Feed(); + $feed->setId(3); + $feed->getUrl('test'); + $ex = new \DatabaseException(''); + + $item = new Item(); + $item->setGuidHash(md5('hi')); + $item->setPubDate(3333); + $items = array( + $item + ); + + $item2 = new Item(); + $item2->setPubDate(111); + + $fetchReturn = array($feed, $items); + + $this->mapper->expects($this->once()) + ->method('find') + ->with($this->equalTo($feed->getId()), + $this->equalTo($this->user)) + ->will($this->returnValue($feed)); + $this->fetcher->expects($this->once()) + ->method('fetch') + ->will($this->returnValue($fetchReturn)); + $this->itemMapper->expects($this->once()) + ->method('findByGuidHash') + ->with($this->equalTo($item->getGuidHash()), + $this->equalTo($feed->getId()), + $this->equalTo($this->user)) + ->will($this->returnValue($item2)); + $this->itemMapper->expects($this->at(1)) + ->method('delete') + ->with($this->equalTo($item2)); + $this->itemMapper->expects($this->at(2)) + ->method('insert') + ->with($this->equalTo($item)); + + $this->bl->update($feed->getId(), $this->user); + $this->assertTrue($item->isUnread()); + } + + + public function testCreateUpdateFails(){ + $feed = new Feed(); + $feed->setId(3); + $feed->getUrl('test'); + $ex = new FetcherException(''); + + $this->mapper->expects($this->once()) + ->method('find') + ->with($this->equalTo($feed->getId()), + $this->equalTo($this->user)) + ->will($this->returnValue($feed)); + $this->fetcher->expects($this->once()) + ->method('fetch') + ->will($this->throwException($ex)); + $this->api->expects($this->once()) + ->method('log'); + + $this->bl->update($feed->getId(), $this->user); + } + + public function testMove(){ + $feedId = 3; + $folderId = 4; + $feed = new Feed(); + $feed->setFolderId(16); + $feed->setId($feedId); + + $this->mapper->expects($this->once()) + ->method('find') + ->with($this->equalTo($feedId), $this->equalTo($this->user)) + ->will($this->returnValue($feed)); + + $this->mapper->expects($this->once()) + ->method('update') + ->with($this->equalTo($feed)); + + $this->bl->move($feedId, $folderId, $this->user); + + $this->assertEquals($folderId, $feed->getFolderId()); + } + + + +} + +} \ No newline at end of file diff --git a/tests/unit/bl/FolderBlTest.php b/tests/unit/bl/FolderBlTest.php new file mode 100644 index 000000000..1bf2cd3ce --- /dev/null +++ b/tests/unit/bl/FolderBlTest.php @@ -0,0 +1,151 @@ +. +* +*/ + +namespace OCA\News\Bl; + +require_once(__DIR__ . "/../../classloader.php"); + + +use \OCA\News\Db\Folder; + + +class FolderBlTest extends \OCA\AppFramework\Utility\TestUtility { + + protected $api; + protected $folderMapper; + protected $folderBl; + + protected function setUp(){ + $this->api = $this->getAPIMock(); + $this->folderMapper = $this->getMockBuilder( + '\OCA\News\Db\FolderMapper') + ->disableOriginalConstructor() + ->getMock(); + $this->folderBl = new FolderBl($this->folderMapper); + } + + + function testFindAll(){ + $userId = 'jack'; + $return = 'hi'; + $this->folderMapper->expects($this->once()) + ->method('findAllFromUser') + ->with($this->equalTo($userId)) + ->will($this->returnValue($return)); + + $result = $this->folderBl->findAll($userId); + + $this->assertEquals($return, $result); + } + + + public function testCreate(){ + $folder = new Folder(); + $folder->setName('hey'); + $folder->setParentId(5); + $folder->setUserId('john'); + + $this->folderMapper->expects($this->once()) + ->method('insert') + ->with($this->equalTo($folder)) + ->will($this->returnValue($folder)); + + $result = $this->folderBl->create('hey', 'john', 5); + + $this->assertEquals($folder, $result); + } + + + public function testCreateThrowsExWhenFolderNameExists(){ + $folderName = 'hihi'; + $rows = array( + array('id' => 1) + ); + + $this->folderMapper->expects($this->once()) + ->method('findByName') + ->with($this->equalTo($folderName)) + ->will($this->returnValue($rows)); + + $this->setExpectedException('\OCA\News\Bl\BLException'); + $result = $this->folderBl->create($folderName, 'john', 3); + } + + + public function testOpen(){ + $folder = new Folder(); + + $this->folderMapper->expects($this->once()) + ->method('find') + ->with($this->equalTo(3)) + ->will($this->returnValue($folder)); + + $this->folderMapper->expects($this->once()) + ->method('update') + ->with($this->equalTo($folder)); + + $this->folderBl->open(3, false, ''); + + $this->assertFalse($folder->getOpened()); + + } + + + public function testRename(){ + $folder = new Folder(); + $folder->setName('jooohn'); + + $this->folderMapper->expects($this->once()) + ->method('find') + ->with($this->equalTo(3)) + ->will($this->returnValue($folder)); + + $this->folderMapper->expects($this->once()) + ->method('update') + ->with($this->equalTo($folder)); + + $this->folderBl->rename(3, 'bogus', ''); + + $this->assertEquals('bogus', $folder->getName()); + } + + + public function testRenameThrowsExWhenFolderNameExists(){ + $folderName = 'hihi'; + $rows = array( + array('id' => 1) + ); + + $this->folderMapper->expects($this->once()) + ->method('findByName') + ->with($this->equalTo($folderName)) + ->will($this->returnValue($rows)); + + $this->setExpectedException('\OCA\News\Bl\BLException'); + $result = $this->folderBl->rename(3, $folderName, 'john'); + } + + +} diff --git a/tests/unit/bl/ItemBlTest.php b/tests/unit/bl/ItemBlTest.php new file mode 100644 index 000000000..1c2dedfb6 --- /dev/null +++ b/tests/unit/bl/ItemBlTest.php @@ -0,0 +1,275 @@ +. +* +*/ + +namespace OCA\News\Bl; + +require_once(__DIR__ . "/../../classloader.php"); + + +use \OCA\News\Db\Item; +use \OCA\News\Db\StatusFlag; +use \OCA\News\Db\FeedType; + + +class ItemBlTest extends \OCA\AppFramework\Utility\TestUtility { + + private $api; + private $mapper; + private $bl; + private $user; + private $response; + private $status; + + + protected function setUp(){ + $this->api = $this->getAPIMock(); + $this->mapper = $this->getMockBuilder('\OCA\News\Db\ItemMapper') + ->disableOriginalConstructor() + ->getMock(); + $statusFlag = $this->getMockBuilder('\OCA\News\Db\StatusFlag') + ->disableOriginalConstructor() + ->getMock(); + $this->status = StatusFlag::STARRED; + $statusFlag->expects($this->any()) + ->method('typeToStatus') + ->will($this->returnValue($this->status)); + $this->threshold = 2; + $this->bl = new ItemBl($this->mapper, $statusFlag, $this->threshold); + $this->user = 'jack'; + $response = 'hi'; + $this->id = 3; + $this->updatedSince = 20333; + $this->showAll = true; + $this->offset = 5; + $this->limit = 20; + } + + + public function testFindAllNewFeed(){ + $type = FeedType::FEED; + $this->mapper->expects($this->once()) + ->method('findAllNewFeed') + ->with($this->equalTo($this->id), + $this->equalTo($this->updatedSince), + $this->equalTo($this->status), + $this->equalTo($this->user)) + ->will($this->returnValue($this->response)); + + $result = $this->bl->findAllNew( + $this->id, $type, $this->updatedSince, $this->showAll, + $this->user); + $this->assertEquals($this->response, $result); + } + + + public function testFindAllNewFolder(){ + $type = FeedType::FOLDER; + $this->mapper->expects($this->once()) + ->method('findAllNewFolder') + ->with($this->equalTo($this->id), + $this->equalTo($this->updatedSince), + $this->equalTo($this->status), + $this->equalTo($this->user)) + ->will($this->returnValue($this->response)); + + $result = $this->bl->findAllNew( + $this->id, $type, $this->updatedSince, $this->showAll, + $this->user); + $this->assertEquals($this->response, $result); + } + + + public function testFindAllNew(){ + $type = FeedType::STARRED; + $this->mapper->expects($this->once()) + ->method('findAllNew') + ->with( $this->equalTo($this->updatedSince), + $this->equalTo($this->status), + $this->equalTo($this->user)) + ->will($this->returnValue($this->response)); + + $result = $this->bl->findAllNew( + $this->id, $type, $this->updatedSince, $this->showAll, + $this->user); + $this->assertEquals($this->response, $result); + } + + + public function testFindAllFeed(){ + $type = FeedType::FEED; + $this->mapper->expects($this->once()) + ->method('findAllFeed') + ->with($this->equalTo($this->id), + $this->equalTo($this->limit), + $this->equalTo($this->offset), + $this->equalTo($this->status), + $this->equalTo($this->user)) + ->will($this->returnValue($this->response)); + + $result = $this->bl->findAll( + $this->id, $type, $this->limit, + $this->offset, $this->showAll, + $this->user); + $this->assertEquals($this->response, $result); + } + + + public function testFindAllFolder(){ + $type = FeedType::FOLDER; + $this->mapper->expects($this->once()) + ->method('findAllFolder') + ->with($this->equalTo($this->id), + $this->equalTo($this->limit), + $this->equalTo($this->offset), + $this->equalTo($this->status), + $this->equalTo($this->user)) + ->will($this->returnValue($this->response)); + + $result = $this->bl->findAll( + $this->id, $type, $this->limit, + $this->offset, $this->showAll, + $this->user); + $this->assertEquals($this->response, $result); + } + + + public function testFindAll(){ + $type = FeedType::STARRED; + $this->mapper->expects($this->once()) + ->method('findAll') + ->with( $this->equalTo($this->limit), + $this->equalTo($this->offset), + $this->equalTo($this->status), + $this->equalTo($this->user)) + ->will($this->returnValue($this->response)); + + $result = $this->bl->findAll( + $this->id, $type, $this->limit, + $this->offset, $this->showAll, + $this->user); + $this->assertEquals($this->response, $result); + } + + + public function testStarredCount(){ + $star = 18; + + $this->mapper->expects($this->once()) + ->method('starredCount') + ->with($this->equalTo($this->user)) + ->will($this->returnValue($star)); + + $result = $this->bl->starredCount($this->user); + + $this->assertEquals($star, $result); + } + + + public function testStar(){ + $feedId = 3; + $guidHash = md5('hihi'); + + $item = new Item(); + $item->setStatus(128); + $item->setId($feedId); + + $this->mapper->expects($this->once()) + ->method('findByGuidHash') + ->with($this->equalTo($feedId), + $this->equalTo($guidHash), + $this->equalTo($this->user)) + ->will($this->returnValue($item)); + + $this->mapper->expects($this->once()) + ->method('update') + ->with($this->equalTo($item)); + + $this->bl->star($feedId, $guidHash, false, $this->user); + + $this->assertTrue($item->isUnstarred()); + } + + + public function testRead(){ + $itemId = 3; + $item = new Item(); + $item->setStatus(128); + $item->setId($itemId); + + $this->mapper->expects($this->once()) + ->method('find') + ->with($this->equalTo($itemId), $this->equalTo($this->user)) + ->will($this->returnValue($item)); + + $this->mapper->expects($this->once()) + ->method('update') + ->with($this->equalTo($item)); + + $this->bl->read($itemId, false, $this->user); + + $this->assertTrue($item->isUnread()); + } + + + public function testReadFeed(){ + $feedId = 3; + $highestItemId = 6; + + $this->mapper->expects($this->once()) + ->method('readFeed') + ->with($this->equalTo($feedId), + $this->equalTo($highestItemId), + $this->equalTo($this->user)); + + $this->bl->readFeed($feedId, $highestItemId, $this->user); + } + + + public function testAutoPurgeOldWillPurgeOld(){ + $item = new Item(); + $item->setId(3); + $unread = array( + new Item(), $item + ); + $this->mapper->expects($this->once()) + ->method('getReadOlderThanThreshold') + ->with($this->equalTo($this->threshold)) + ->will($this->returnValue($unread)); + $this->mapper->expects($this->once()) + ->method('deleteReadOlderThanId') + ->with($this->equalTo($item->getId())); + + $result = $this->bl->autoPurgeOld(); + + } + + +} + + + + + + diff --git a/tests/unit/bl/StatusFlagTest.php b/tests/unit/bl/StatusFlagTest.php new file mode 100644 index 000000000..12e78d8cc --- /dev/null +++ b/tests/unit/bl/StatusFlagTest.php @@ -0,0 +1,74 @@ +. +* +*/ + +namespace OCA\News\Db; + +use \OCA\AppFramework\Utility\TestUtility; + + +require_once(__DIR__ . "/../../classloader.php"); + + +class StatusFlagTest extends TestUtility { + + private $statusFlag; + + protected function setUp(){ + $this->statusFlag = new StatusFlag(); + } + + + public function testTypeToStatusUnreadStarred(){ + $expected = StatusFlag::UNREAD | StatusFlag::STARRED; + $status = $this->statusFlag->typeToStatus(FeedType::STARRED, false); + + $this->assertEquals($expected, $status); + } + + + public function testTypeToStatusUnread(){ + $expected = StatusFlag::UNREAD; + $status = $this->statusFlag->typeToStatus(FeedType::FEED, false); + + $this->assertEquals($expected, $status); + } + + + public function testTypeToStatusReadStarred(){ + $expected = (~StatusFlag::UNREAD) & StatusFlag::STARRED; + $status = $this->statusFlag->typeToStatus(FeedType::STARRED, true); + + $this->assertEquals($expected, $status); + } + + + public function testTypeToStatusRead(){ + $expected = (~StatusFlag::UNREAD) & 0; + $status = $this->statusFlag->typeToStatus(FeedType::FEED, true); + + $this->assertEquals($expected, $status); + } + +} \ 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..913b091c6 --- /dev/null +++ b/tests/unit/controller/ExportControllerTest.php @@ -0,0 +1,61 @@ +. +* +*/ + +namespace OCA\News\Controller; + +use \OCA\AppFramework\Http\Request; +use \OCA\AppFramework\Http\JSONResponse; +use \OCA\AppFramework\Utility\ControllerTestUtility; +use \OCA\AppFramework\Db\DoesNotExistException; +use \OCA\AppFramework\Db\MultipleObjectsReturnedException; + + +require_once(__DIR__ . "/../../classloader.php"); + + +class ExportControllerTest extends ControllerTestUtility { + + private $api; + private $request; + private $controller; + + + /** + * Gets run before each test + */ + public function setUp(){ + $this->api = $this->getAPIMock(); + $this->request = new Request(); + $this->controller = new ExportController($this->api, $this->request); + } + + + public function testOpmlAnnotations(){ + $annotations = array('IsAdminExemption', 'IsSubAdminExemption', 'Ajax'); + $this->assertAnnotations($this->controller, 'opml', $annotations); + } + + +} \ No newline at end of file diff --git a/tests/unit/controller/FeedControllerTest.php b/tests/unit/controller/FeedControllerTest.php new file mode 100644 index 000000000..e3dc60a28 --- /dev/null +++ b/tests/unit/controller/FeedControllerTest.php @@ -0,0 +1,350 @@ +. +* +*/ + +namespace OCA\News\Controller; + +use \OCA\AppFramework\Http\Request; +use \OCA\AppFramework\Http\JSONResponse; +use \OCA\AppFramework\Utility\ControllerTestUtility; +use \OCA\AppFramework\Db\DoesNotExistException; +use \OCA\AppFramework\Db\MultipleObjectsReturnedException; + +use \OCA\News\Db\Feed; +use \OCA\News\Db\FeedType; +use \OCA\News\Bl\BLException; + + +require_once(__DIR__ . "/../../classloader.php"); + + +class FeedControllerTest extends ControllerTestUtility { + + private $api; + private $bl; + private $request; + private $controller; + private $folderBl; + + + /** + * Gets run before each test + */ + public function setUp(){ + $this->api = $this->getAPIMock(); + $this->bl = $this->getMockBuilder('\OCA\News\Bl\FeedBl') + ->disableOriginalConstructor() + ->getMock(); + $this->folderBl = $this->getMockBuilder('\OCA\News\Bl\FolderBl') + ->disableOriginalConstructor() + ->getMock(); + $this->request = new Request(); + $this->controller = new FeedController($this->api, $this->request, + $this->bl, $this->folderBl); + $this->user = 'jack'; + } + + private function assertFeedControllerAnnotations($methodName){ + $annotations = array('IsAdminExemption', 'IsSubAdminExemption', 'Ajax'); + $this->assertAnnotations($this->controller, $methodName, $annotations); + } + + + private function getPostController($postValue, $url=array()){ + $post = array( + 'post' => $postValue, + 'urlParams' => $url + ); + + $request = $this->getRequest($post); + return new FeedController($this->api, $request, $this->bl, $this->folderBl); + } + + + public function testFeedsAnnotations(){ + $this->assertFeedControllerAnnotations('feeds'); + } + + + public function testActiveAnnotations(){ + $this->assertFeedControllerAnnotations('active'); + } + + + public function testCreateAnnotations(){ + $this->assertFeedControllerAnnotations('create'); + } + + + public function testDeleteAnnotations(){ + $this->assertFeedControllerAnnotations('delete'); + } + + + public function testUpdateAnnotations(){ + $this->assertFeedControllerAnnotations('update'); + } + + + public function testMoveAnnotations(){ + $this->assertFeedControllerAnnotations('move'); + } + + + public function testFeeds(){ + $result = array( + 'feeds' => array( + array('a feed') + ) + ); + $this->api->expects($this->once()) + ->method('getUserId') + ->will($this->returnValue($this->user)); + $this->bl->expects($this->once()) + ->method('findAllFromUser') + ->with($this->equalTo($this->user)) + ->will($this->returnValue($result['feeds'])); + + $response = $this->controller->feeds(); + + $this->assertEquals($result, $response->getParams()); + $this->assertTrue($response instanceof JSONResponse); + } + + + private function activeInitMocks($id, $type){ + $this->api->expects($this->at(0)) + ->method('getUserId') + ->will($this->returnValue($this->user)); + $this->api->expects($this->at(1)) + ->method('getUserValue') + ->with($this->equalTo('lastViewedFeedId')) + ->will($this->returnValue($id)); + $this->api->expects($this->at(2)) + ->method('getUserValue') + ->with($this->equalTo('lastViewedFeedType')) + ->will($this->returnValue($type)); + } + + + public function testActive(){ + $id = 3; + $type = FeedType::STARRED; + $result = array( + 'activeFeed' => array( + 'id' => $id, + 'type' => $type + ) + ); + + $this->activeInitMocks($id, $type); + + $response = $this->controller->active(); + + $this->assertEquals($result, $response->getParams()); + $this->assertTrue($response instanceof JSONResponse); + } + + + public function testActiveFeedDoesNotExist(){ + $id = 3; + $type = FeedType::FEED; + $ex = new BLException('hiu'); + $result = array( + 'activeFeed' => array( + 'id' => 0, + 'type' => FeedType::SUBSCRIPTIONS + ) + ); + $this->bl->expects($this->once()) + ->method('find') + ->with($this->equalTo($id), $this->equalTo($this->user)) + ->will($this->throwException($ex)); + + $this->activeInitMocks($id, $type); + + $response = $this->controller->active(); + + $this->assertEquals($result, $response->getParams()); + $this->assertTrue($response instanceof JSONResponse); + } + + + public function testActiveFolderDoesNotExist(){ + $id = 3; + $type = FeedType::FOLDER; + $ex = new BLException('hiu'); + $result = array( + 'activeFeed' => array( + 'id' => 0, + 'type' => FeedType::SUBSCRIPTIONS + ) + ); + $this->folderBl->expects($this->once()) + ->method('find') + ->with($this->equalTo($id), $this->equalTo($this->user)) + ->will($this->throwException($ex)); + + $this->activeInitMocks($id, $type); + + $response = $this->controller->active(); + + $this->assertEquals($result, $response->getParams()); + $this->assertTrue($response instanceof JSONResponse); + } + + + public function testActiveActiveIsNull(){ + $id = 3; + $type = null; + $result = array( + 'activeFeed' => array( + 'id' => 0, + 'type' => FeedType::SUBSCRIPTIONS + ) + ); + + $this->activeInitMocks($id, $type); + + $response = $this->controller->active(); + + $this->assertEquals($result, $response->getParams()); + $this->assertTrue($response instanceof JSONResponse); + } + + + public function testCreate(){ + $result = array( + 'feeds' => array(new Feed()) + ); + + $post = array( + 'url' => 'hi', + 'parentFolderId' => 4 + ); + $this->controller = $this->getPostController($post); + + $this->api->expects($this->once()) + ->method('getUserId') + ->will($this->returnValue($this->user)); + + $this->bl->expects($this->once()) + ->method('create') + ->with($this->equalTo($post['url']), + $this->equalTo($post['parentFolderId']), + $this->equalTo($this->user)) + ->will($this->returnValue($result['feeds'][0])); + + $response = $this->controller->create(); + + $this->assertEquals($result, $response->getParams()); + $this->assertTrue($response instanceof JSONResponse); + } + + + public function testCreateReturnsErrorForInvalidCreate(){ + $msg = 'except'; + $ex = new BLException($msg); + $this->bl->expects($this->once()) + ->method('create') + ->will($this->throwException($ex)); + + $response = $this->controller->create(); + $params = json_decode($response->render(), true); + + $this->assertEquals('error', $params['status']); + $this->assertEquals($msg, $params['msg']); + $this->assertTrue($response instanceof JSONResponse); + } + + + public function testDelete(){ + $url = array( + 'feedId' => 4 + ); + $this->controller = $this->getPostController(array(), $url); + + $this->api->expects($this->once()) + ->method('getUserId') + ->will($this->returnValue($this->user)); + $this->bl->expects($this->once()) + ->method('delete') + ->with($this->equalTo($url['feedId'])); + + $response = $this->controller->delete(); + $this->assertTrue($response instanceof JSONResponse); + } + + + public function testUpdate(){ + $result = array( + 'feeds' => array( + new Feed() + ) + ); + + $url = array( + 'feedId' => 4 + ); + $this->controller = $this->getPostController(array(), $url); + + $this->api->expects($this->once()) + ->method('getUserId') + ->will($this->returnValue($this->user)); + $this->bl->expects($this->once()) + ->method('update') + ->with($this->equalTo($url['feedId']), $this->equalTo($this->user)) + ->will($this->returnValue($result['feeds'][0])); + + $response = $this->controller->update(); + + $this->assertEquals($result, $response->getParams()); + $this->assertTrue($response instanceof JSONResponse); + } + + + public function testMove(){ + $post = array( + 'parentFolderId' => 3 + ); + $url = array( + 'feedId' => 4 + ); + $this->controller = $this->getPostController($post, $url); + + $this->api->expects($this->once()) + ->method('getUserId') + ->will($this->returnValue($this->user)); + $this->bl->expects($this->once()) + ->method('move') + ->with($this->equalTo($url['feedId']), + $this->equalTo($post['parentFolderId']), + $this->equalTo($this->user)); + + $response = $this->controller->move(); + + $this->assertTrue($response instanceof JSONResponse); + } + +} \ No newline at end of file diff --git a/tests/unit/controller/FolderControllerTest.php b/tests/unit/controller/FolderControllerTest.php new file mode 100644 index 000000000..231cc9187 --- /dev/null +++ b/tests/unit/controller/FolderControllerTest.php @@ -0,0 +1,260 @@ +. +* +*/ + +namespace OCA\News\Controller; + +use \OCA\AppFramework\Http\Request; +use \OCA\AppFramework\Http\JSONResponse; +use \OCA\AppFramework\Utility\ControllerTestUtility; +use \OCA\AppFramework\Db\DoesNotExistException; +use \OCA\AppFramework\Db\MultipleObjectsReturnedException; + +use \OCA\News\Db\Folder; +use \OCA\News\Bl\BLException; + +require_once(__DIR__ . "/../../classloader.php"); + + +class FolderControllerTest extends ControllerTestUtility { + + private $api; + private $bl; + private $request; + private $controller; + + + /** + * Gets run before each test + */ + public function setUp(){ + $this->api = $this->getAPIMock(); + $this->bl = $this->getMockBuilder('\OCA\News\Bl\FolderBl') + ->disableOriginalConstructor() + ->getMock(); + $this->request = new Request(); + $this->controller = new FolderController($this->api, $this->request, + $this->bl); + $this->user = 'jack'; + } + + + private function assertFolderControllerAnnotations($methodName){ + $annotations = array('IsAdminExemption', 'IsSubAdminExemption', 'Ajax'); + $this->assertAnnotations($this->controller, $methodName, $annotations); + } + + + private function getPostController($postValue, $url=array()){ + $post = array( + 'post' => $postValue, + 'urlParams' => $url + ); + + $request = $this->getRequest($post); + return new FolderController($this->api, $request, $this->bl); + } + + public function testFoldersAnnotations(){ + $this->assertFolderControllerAnnotations('folders'); + } + + + public function testOpenAnnotations(){ + $this->assertFolderControllerAnnotations('open'); + } + + + public function testCollapseAnnotations(){ + $this->assertFolderControllerAnnotations('collapse'); + } + + + public function testCreateAnnotations(){ + $this->assertFolderControllerAnnotations('create'); + } + + + public function testDeleteAnnotations(){ + $this->assertFolderControllerAnnotations('delete'); + } + + + public function testRenameAnnotations(){ + $this->assertFolderControllerAnnotations('rename'); + } + + + + public function testFolders(){ + $return = array( + new Folder(), + new Folder(), + ); + $this->bl->expects($this->once()) + ->method('findAll') + ->will($this->returnValue($return)); + + $response = $this->controller->folders(); + $expected = array( + 'folders' => $return + ); + $this->assertEquals($expected, $response->getParams()); + $this->assertTrue($response instanceof JSONResponse); + } + + + public function testOpen(){ + $url = array('folderId' => 5); + $this->controller = $this->getPostController(array(), $url); + + $this->api->expects($this->once()) + ->method('getUserId') + ->will($this->returnValue($this->user)); + $this->bl->expects($this->once()) + ->method('open') + ->with($this->equalTo($url['folderId']), + $this->equalTo(true), $this->equalTo($this->user)); + + $response = $this->controller->open(); + + $this->assertTrue($response instanceof JSONResponse); + } + + + public function testCollapse(){ + $url = array('folderId' => 5); + $this->controller = $this->getPostController(array(), $url); + + $this->api->expects($this->once()) + ->method('getUserId') + ->will($this->returnValue($this->user)); + $this->bl->expects($this->once()) + ->method('open') + ->with($this->equalTo($url['folderId']), + $this->equalTo(false), $this->equalTo($this->user)); + + $response = $this->controller->collapse(); + + $this->assertTrue($response instanceof JSONResponse); + } + + + public function testCreate(){ + $post = array('folderName' => 'tech'); + $this->controller = $this->getPostController($post); + $result = array( + 'folders' => array(new Folder()) + ); + + $this->api->expects($this->once()) + ->method('getUserId') + ->will($this->returnValue($this->user)); + $this->bl->expects($this->once()) + ->method('create') + ->with($this->equalTo($post['folderName']), + $this->equalTo($this->user)) + ->will($this->returnValue($result['folders'][0])); + + $response = $this->controller->create(); + + $this->assertEquals($result, $response->getParams()); + $this->assertTrue($response instanceof JSONResponse); + } + + + public function testCreateReturnsErrorForInvalidCreate(){ + $msg = 'except'; + $ex = new BLException($msg); + $this->bl->expects($this->once()) + ->method('create') + ->will($this->throwException($ex)); + + $response = $this->controller->create(); + $params = json_decode($response->render(), true); + + $this->assertEquals('error', $params['status']); + $this->assertEquals($msg, $params['msg']); + $this->assertTrue($response instanceof JSONResponse); + } + + + public function testDelete(){ + $url = array('folderId' => 5); + $this->controller = $this->getPostController(array(), $url); + + $this->api->expects($this->once()) + ->method('getUserId') + ->will($this->returnValue($this->user)); + $this->bl->expects($this->once()) + ->method('delete') + ->with($this->equalTo($url['folderId']), + $this->equalTo($this->user)); + + $response = $this->controller->delete(); + + $this->assertTrue($response instanceof JSONResponse); + } + + + public function testRename(){ + $post = array('folderName' => 'tech'); + $url = array('folderId' => 4); + $this->controller = $this->getPostController($post, $url); + $result = array( + 'folders' => array(new Folder()) + ); + + $this->api->expects($this->once()) + ->method('getUserId') + ->will($this->returnValue($this->user)); + $this->bl->expects($this->once()) + ->method('rename') + ->with($this->equalTo($url['folderId']), + $this->equalTo($post['folderName']), + $this->equalTo($this->user)) + ->will($this->returnValue($result['folders'][0])); + + $response = $this->controller->rename(); + + $this->assertEquals($result, $response->getParams()); + $this->assertTrue($response instanceof JSONResponse); + } + + + public function testRenameReturnsErrorForInvalidCreate(){ + $msg = 'except'; + $ex = new BLException($msg); + $this->bl->expects($this->once()) + ->method('rename') + ->will($this->throwException($ex)); + + $response = $this->controller->rename(); + $params = json_decode($response->render(), true); + + $this->assertEquals('error', $params['status']); + $this->assertEquals($msg, $params['msg']); + $this->assertTrue($response instanceof JSONResponse); + } +} \ No newline at end of file diff --git a/tests/unit/controller/ItemControllerTest.php b/tests/unit/controller/ItemControllerTest.php new file mode 100644 index 000000000..96a28da81 --- /dev/null +++ b/tests/unit/controller/ItemControllerTest.php @@ -0,0 +1,299 @@ +. +* +*/ + +namespace OCA\News\Controller; + +use \OCA\AppFramework\Http\Request; +use \OCA\AppFramework\Http\JSONResponse; +use \OCA\AppFramework\Utility\ControllerTestUtility; + +use \OCA\News\Db\Item; +use \OCA\News\Db\FeedType; + +require_once(__DIR__ . "/../../classloader.php"); + + +class ItemControllerTest extends ControllerTestUtility { + + private $api; + private $bl; + private $request; + private $controller; + + + /** + * Gets run before each test + */ + public function setUp(){ + $this->api = $this->getAPIMock(); + $this->bl = $this->getMockBuilder('\OCA\News\Bl\ItemBl') + ->disableOriginalConstructor() + ->getMock(); + $this->request = new Request(); + $this->controller = new ItemController($this->api, $this->request, + $this->bl); + $this->user = 'jackob'; + } + + private function getPostController($postValue, $url=array()){ + $post = array( + 'post' => $postValue, + 'urlParams' => $url + ); + + $request = $this->getRequest($post); + return new ItemController($this->api, $request, $this->bl); + } + + + private function assertItemControllerAnnotations($methodName){ + $annotations = array('IsAdminExemption', 'IsSubAdminExemption', 'Ajax'); + $this->assertAnnotations($this->controller, $methodName, $annotations); + } + + public function testItemsAnnotations(){ + $this->assertItemControllerAnnotations('items'); + } + + + public function testStarredAnnotations(){ + $this->assertItemControllerAnnotations('starred'); + } + + + public function testStarAnnotations(){ + $this->assertItemControllerAnnotations('star'); + } + + + public function testUnstarAnnotations(){ + $this->assertItemControllerAnnotations('unstar'); + } + + + public function testReadAnnotations(){ + $this->assertItemControllerAnnotations('read'); + } + + + public function testUnreadAnnotations(){ + $this->assertItemControllerAnnotations('unread'); + } + + + public function testReadFeedAnnotations(){ + $this->assertItemControllerAnnotations('readFeed'); + } + + + public function testRead(){ + $url = array( + 'itemId' => 4 + ); + $this->controller = $this->getPostController(array(), $url); + + $this->api->expects($this->once()) + ->method('getUserId') + ->will($this->returnValue($this->user)); + $this->bl->expects($this->once()) + ->method('read') + ->with($url['itemId'], true, $this->user); + + + $this->controller->read(); + } + + + public function testUnread(){ + $url = array( + 'itemId' => 4 + ); + $this->controller = $this->getPostController(array(), $url); + + $this->api->expects($this->once()) + ->method('getUserId') + ->will($this->returnValue($this->user)); + $this->bl->expects($this->once()) + ->method('read') + ->with($url['itemId'], false, $this->user); + + $this->controller->unread(); + } + + + public function testStar(){ + $url = array( + 'feedId' => 4, + 'guidHash' => md5('test') + ); + $this->controller = $this->getPostController(array(), $url); + + $this->api->expects($this->once()) + ->method('getUserId') + ->will($this->returnValue($this->user)); + $this->bl->expects($this->once()) + ->method('star') + ->with( + $this->equalTo($url['feedId']), + $this->equalTo($url['guidHash']), + $this->equalTo(true), + $this->equalTo($this->user)); + + $this->controller->star(); + } + + + public function testUnstar(){ + $url = array( + 'feedId' => 4, + 'guidHash' => md5('test') + ); + $this->controller = $this->getPostController(array(), $url); + + $this->api->expects($this->once()) + ->method('getUserId') + ->will($this->returnValue($this->user)); + $this->bl->expects($this->once()) + ->method('star') + ->with( + $this->equalTo($url['feedId']), + $this->equalTo($url['guidHash']), + $this->equalTo(false), + $this->equalTo($this->user)); + + $this->controller->unstar(); + } + + + public function testReadFeed(){ + $url = array( + 'feedId' => 4 + ); + $post = array( + 'highestItemId' => 5 + ); + $this->controller = $this->getPostController($post, $url); + + $this->api->expects($this->once()) + ->method('getUserId') + ->will($this->returnValue($this->user)); + $this->bl->expects($this->once()) + ->method('readFeed') + ->with($url['feedId'], $post['highestItemId'], $this->user); + + $this->controller->readFeed(); + } + + + public function testStarred(){ + $result = array( + 'starred' => 3 + ); + $this->api->expects($this->once()) + ->method('getUserId') + ->will($this->returnValue($this->user)); + $this->bl->expects($this->once()) + ->method('starredCount') + ->with($this->user) + ->will($this->returnValue($result['starred'])); + $response = $this->controller->starred(); + + $this->assertEquals($result, $response->getParams()); + $this->assertTrue($response instanceof JSONResponse); + } + + + + private function itemsApiExpects($id, $type){ + $this->api->expects($this->once()) + ->method('getUserValue') + ->with($this->equalTo($this->user), + $this->equalTo('showAll')) + ->will($this->returnValue('true')); + $this->api->expects($this->once()) + ->method('getUserId') + ->will($this->returnValue($this->user)); + $this->api->expects($this->at(2)) + ->method('setUserValue') + ->with($this->equalTo('lastViewedFeedId'), + $this->equalTo($id)); + $this->api->expects($this->at(3)) + ->method('setUserValue') + ->with($this->equalTo('lastViewedFeedType'), + $this->equalTo($type)); + } + + + public function testItems(){ + $result = array( + 'items' => array(new Item()) + ); + $post = array( + 'limit' => 3, + 'type' => FeedType::FEED, + 'id' => 2, + 'offset' => 0 + ); + $this->controller = $this->getPostController($post); + + $this->itemsApiExpects($post['id'], $post['type']); + + $this->bl->expects($this->once()) + ->method('findAll') + ->with($post['id'], $post['type'], $post['limit'], + $post['offset'], true, $this->user) + ->will($this->returnValue($result['items'])); + + $response = $this->controller->items(); + $this->assertEquals($result, $response->getParams()); + $this->assertTrue($response instanceof JSONResponse); + } + + + public function testItemsNew(){ + $result = array( + 'items' => array(new Item()) + ); + $post = array( + 'type' => FeedType::FEED, + 'id' => 2, + 'updatedSince' => 3333 + ); + $this->controller = $this->getPostController($post); + + $this->itemsApiExpects($post['id'], $post['type']); + + $this->bl->expects($this->once()) + ->method('findAllNew') + ->with($post['id'], $post['type'], $post['updatedSince'], + true, $this->user) + ->will($this->returnValue($result['items'])); + + $response = $this->controller->items(); + $this->assertEquals($result, $response->getParams()); + $this->assertTrue($response instanceof JSONResponse); + } + +} \ No newline at end of file diff --git a/tests/unit/controller/PageControllerTest.php b/tests/unit/controller/PageControllerTest.php new file mode 100644 index 000000000..d4f7e1097 --- /dev/null +++ b/tests/unit/controller/PageControllerTest.php @@ -0,0 +1,68 @@ +. +* +*/ + +namespace OCA\News\Controller; + +use \OCA\AppFramework\Http\Request; +use \OCA\AppFramework\Http\TemplateResponse; +use \OCA\AppFramework\Utility\ControllerTestUtility; +use \OCA\AppFramework\Db\DoesNotExistException; +use \OCA\AppFramework\Db\MultipleObjectsReturnedException; + + +require_once(__DIR__ . "/../../classloader.php"); + + +class PageControllerTest extends ControllerTestUtility { + + private $api; + private $request; + private $controller; + + + /** + * Gets run before each test + */ + public function setUp(){ + $this->api = $this->getAPIMock(); + $this->request = new Request(); + $this->controller = new PageController($this->api, $this->request); + } + + + public function testOpmlAnnotations(){ + $annotations = array('IsAdminExemption', 'IsSubAdminExemption', + 'CSRFExemption'); + $this->assertAnnotations($this->controller, 'index', $annotations); + } + + public function testIndex(){ + $response = $this->controller->index(); + $this->assertEquals('main', $response->getTemplateName()); + $this->assertTrue($response instanceof TemplateResponse); + } + + +} \ No newline at end of file diff --git a/tests/unit/controller/TwitterFetcherTest.php b/tests/unit/controller/TwitterFetcherTest.php new file mode 100644 index 000000000..b75cc4db4 --- /dev/null +++ b/tests/unit/controller/TwitterFetcherTest.php @@ -0,0 +1,77 @@ +. +* +*/ + +namespace OCA\News\Utility; + +require_once(__DIR__ . "/../../classloader.php"); + + +class TwitterFetcherTest extends \OCA\AppFramework\Utility\TestUtility { + + private $fetcher; + private $twitter; + + protected function setUp(){ + $this->fetcher = $this->getMockBuilder('\OCA\News\Utility\FeedFetcher') + ->disableOriginalConstructor() + ->getMock(); + $this->twitter = new TwitterFetcher($this->fetcher); + } + + + public function testCanHandle(){ + $urls = array( + 'https://twitter.com/GeorgeTakei', + 'https://www.twitter.com/GeorgeTakei', + 'http://twitter.com/GeorgeTakei', + 'http://www.twitter.com/GeorgeTakei', + 'www.twitter.com/GeorgeTakei', + 'twitter.com/GeorgeTakei' + ); + foreach($urls as $url){ + $this->assertTrue($this->twitter->canHandle($url), $url); + } + } + + + public function testCanHandleDoesNotUseApiUrls(){ + $url = 'https://api.twitter.com/1/statuses/user_timeline.rss?screen_name=GeorgeTakei'; + $this->assertFalse($this->twitter->canHandle($url)); + } + + + public function testFetch(){ + $inUrl = 'https://www.twitter.com/GeorgeTakei'; + $outUrl = 'https://api.twitter.com/1/statuses/user_timeline.rss?screen_name=GeorgeTakei'; + $out = 'hi'; + $this->fetcher->expects($this->once()) + ->method('fetch') + ->with($this->equalTo($outUrl)) + ->will($this->returnValue($out)); + + $return = $this->twitter->fetch($inUrl); + $this->assertEquals($out, $return); + } +} \ No newline at end of file diff --git a/tests/unit/controller/UserSettingsControllerTest.php b/tests/unit/controller/UserSettingsControllerTest.php new file mode 100644 index 000000000..82b4001fc --- /dev/null +++ b/tests/unit/controller/UserSettingsControllerTest.php @@ -0,0 +1,112 @@ +. +* +*/ + +namespace OCA\News\Controller; + +use \OCA\AppFramework\Http\Request; +use \OCA\AppFramework\Http\JSONResponse; +use \OCA\AppFramework\Utility\ControllerTestUtility; +use \OCA\AppFramework\Db\DoesNotExistException; +use \OCA\AppFramework\Db\MultipleObjectsReturnedException; + + +require_once(__DIR__ . "/../../classloader.php"); + + +class UserSettingsControllerTest extends ControllerTestUtility { + + private $api; + private $request; + private $controller; + + + /** + * Gets run before each test + */ + public function setUp(){ + $this->api = $this->getAPIMock(); + $this->request = new Request(); + $this->controller = new UserSettingsController($this->api, $this->request); + $this->user = 'becka'; + } + + + private function assertUserSettingsControllerAnnotations($methodName){ + $annotations = array('IsAdminExemption', 'IsSubAdminExemption', 'Ajax'); + $this->assertAnnotations($this->controller, $methodName, $annotations); + } + + + public function testFoldersAnnotations(){ + $this->assertUserSettingsControllerAnnotations('read'); + } + + + public function testOpenAnnotations(){ + $this->assertUserSettingsControllerAnnotations('show'); + } + + + public function testCollapseAnnotations(){ + $this->assertUserSettingsControllerAnnotations('hide'); + } + + + public function testShow(){ + $this->api->expects($this->once()) + ->method('setUserValue') + ->with($this->equalTo('showAll'), + $this->equalTo(true)); + $result = $this->controller->show(); + } + + + public function testHide(){ + $this->api->expects($this->once()) + ->method('setUserValue') + ->with($this->equalTo('showAll'), + $this->equalTo(false)); + $result = $this->controller->hide(); + } + + + public function testRead(){ + $result = array( + 'showAll' => true + ); + $this->api->expects($this->once()) + ->method('getUserValue') + ->with($this->equalTo('showAll')) + ->will($this->returnValue('1')); + + $response = $this->controller->read(); + $this->assertEquals($result, $response->getParams()); + $this->assertTrue($response instanceof JSONResponse); + } + + + + +} \ No newline at end of file diff --git a/tests/unit/db/FeedMapperTest.php b/tests/unit/db/FeedMapperTest.php new file mode 100644 index 000000000..58ea8c4ed --- /dev/null +++ b/tests/unit/db/FeedMapperTest.php @@ -0,0 +1,258 @@ +. +* +*/ + +namespace OCA\News\Db; + +require_once(__DIR__ . "/../../classloader.php"); + + +class FeedMapperTest extends \OCA\AppFramework\Utility\MapperTestUtility { + + private $mapper; + private $feeds; + + protected function setUp(){ + $this->beforeEach(); + + $this->mapper = new FeedMapper($this->api); + + // create mock feeds + $feed1 = new Feed(); + $feed2 = new Feed(); + + $this->feeds = array( + $feed1, + $feed2 + ); + $this->user = 'herman'; + } + + + public function testFind(){ + $userId = 'john'; + $id = 3; + $rows = array( + array('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` & ?) = ? ' . + 'WHERE `feeds`.`id` = ? ' . + 'AND `feeds`.`user_id` = ? ' . + 'GROUP BY `feeds`.`id`'; + $params = array(StatusFlag::UNREAD, StatusFlag::UNREAD, $id, $userId); + $this->setMapperRes