From 5cc47b4f414026ecf16d8c7025571422367f7c58 Mon Sep 17 00:00:00 2001 From: Bernhard Posselt Date: Fri, 26 Apr 2013 10:46:32 +0200 Subject: use last_modified column for finding new items (so we also see if they were updated or starred), use offset to paginate rather than item id --- businesslayer/itembusinesslayer.php | 25 +++- controller/itemcontroller.php | 44 +++---- db/itemmapper.php | 80 ++++++------ templates/part.shortcuts.php | 0 tests/unit/businesslayer/ItemBusinessLayerTest.php | 33 ++++- tests/unit/controller/ItemControllerTest.php | 70 +++++++---- tests/unit/db/ItemMapperTest.php | 134 +++++++++++---------- 7 files changed, 234 insertions(+), 152 deletions(-) create mode 100644 templates/part.shortcuts.php diff --git a/businesslayer/itembusinesslayer.php b/businesslayer/itembusinesslayer.php index 4fbca4af0..794da73b7 100644 --- a/businesslayer/itembusinesslayer.php +++ b/businesslayer/itembusinesslayer.php @@ -26,6 +26,7 @@ namespace OCA\News\BusinessLayer; use \OCA\AppFramework\Utility\TimeFactory; +use \OCA\AppFramework\Db\DoesNotExistException; use \OCA\News\Db\Item; use \OCA\News\Db\ItemMapper; @@ -70,22 +71,25 @@ class ItemBusinessLayer extends BusinessLayer { } - public function findAll($id, $type, $limit, $offset, - $showAll, $userId){ + public function findAll($id, $type, $limit, $offset, $newestItemId, $showAll, + $userId){ + $status = $this->statusFlag->typeToStatus($type, $showAll); switch($type){ case FeedType::FEED: $items = $this->mapper->findAllFeed($id, $limit, $offset, - $status, $userId); + $newestItemId, $status, + $userId); break; case FeedType::FOLDER: $items = $this->mapper->findAllFolder($id, $limit, $offset, - $status, $userId); + $newestItemId, $status, + $userId); break; default: - $items = $this->mapper->findAll($limit, $offset, $status, - $userId); + $items = $this->mapper->findAll($limit, $offset, $newestItemId, + $status, $userId); } return $items; @@ -138,4 +142,13 @@ class ItemBusinessLayer extends BusinessLayer { } + public function getNewestItemId($userId) { + try { + return $this->mapper->getNewestItemId($userId); + } catch(DoesNotExistException $ex) { + throw new BusinessLayerException($ex->getMessage()); + } + } + + } diff --git a/controller/itemcontroller.php b/controller/itemcontroller.php index 84f177cd6..459435a7e 100644 --- a/controller/itemcontroller.php +++ b/controller/itemcontroller.php @@ -29,6 +29,7 @@ use \OCA\AppFramework\Controller\Controller; use \OCA\AppFramework\Core\API; use \OCA\AppFramework\Http\Request; +use \OCA\News\BusinessLayer\BusinessLayerException; use \OCA\News\BusinessLayer\ItemBusinessLayer; use \OCA\News\BusinessLayer\FeedBusinessLayer; @@ -59,32 +60,33 @@ class ItemController extends Controller { $limit = $this->params('limit'); $type = (int) $this->params('type'); $id = (int) $this->params('id'); + $offset = (int) $this->params('offset', 0); + $newestItemId = (int) $this->params('newestItemId'); $this->api->setUserValue('lastViewedFeedId', $id); $this->api->setUserValue('lastViewedFeedType', $type); - - if($limit !== null){ - $offset = (int) $this->params('offset', 0); - $items = $this->itemBusinessLayer->findAll($id, $type, (int) $limit, - $offset, $showAll, $userId); - if($offset === 0) { - $feeds = $this->feedBusinessLayer->findAll($userId); - } - } else { - $updatedSince = (int) $this->params('updatedSince'); - $items = $this->itemBusinessLayer->findAllNew($id, $type, - $updatedSince, $showAll, $userId); - } - $params = array( - 'items' => $items - ); + $params = array(); - // we need to pass the newest feeds to not let the unread count get out - // of sync - if(isset($feeds)) { - $params['feeds'] = $feeds; - } + try { + + // the offset is 0 if the user clicks on a new feed + // we need to pass the newest feeds to not let the unread count get + // out of sync + if($offset === 0) { + $params['newestItemId'] = + $this->itemBusinessLayer->getNewestItemId($userId); + $newestItemId = $params['newestItemId']; + $params['feeds'] = $this->feedBusinessLayer->findAll($userId); + } + + $params['items'] = $this->itemBusinessLayer->findAll( + $id, $type, $limit, + $offset, $newestItemId, + $showAll, $userId); + // this gets thrown if there are no items + // in that case just return an empty array + } catch(BusinessLayerException $ex) {} return $this->renderJSON($params); } diff --git a/db/itemmapper.php b/db/itemmapper.php index 59654e62a..bef13ef4f 100644 --- a/db/itemmapper.php +++ b/db/itemmapper.php @@ -139,67 +139,65 @@ class ItemMapper extends Mapper implements IMapper { } - public function findAllNewFeed($id, $updatedSince, $status, $userId){ - $sql = 'AND `items`.`feed_id` = ? ' . - 'AND `items`.`id` >= ?'; - $sql = $this->makeSelectQueryStatus($sql, $status); - $params = array($userId, $id, $updatedSince); + public function findAllNew($updatedSince, $status, $userId){ + $sql = $this->makeSelectQueryStatus('AND `items`.`last_modified` >= ?', $status); + $params = array($userId, $updatedSince); return $this->findAllRows($sql, $params); } public function findAllNewFolder($id, $updatedSince, $status, $userId){ $sql = 'AND `feeds`.`folder_id` = ? ' . - 'AND `items`.`id` >= ?'; + 'AND `items`.`last_modified` >= ?'; $sql = $this->makeSelectQueryStatus($sql, $status); $params = array($userId, $id, $updatedSince); return $this->findAllRows($sql, $params); } - public function findAllNew($updatedSince, $status, $userId){ - $sql = $this->makeSelectQueryStatus('AND `items`.`id` >= ?', $status); - $params = array($userId, $updatedSince); + public function findAllNewFeed($id, $updatedSince, $status, $userId){ + $sql = 'AND `items`.`feed_id` = ? ' . + 'AND `items`.`last_modified` >= ?'; + $sql = $this->makeSelectQueryStatus($sql, $status); + $params = array($userId, $id, $updatedSince); return $this->findAllRows($sql, $params); } - public function findAllFeed($id, $limit, $offset, $status, $userId){ - $params = array($userId, $id); - $sql = 'AND `items`.`feed_id` = ? '; - if($offset !== 0){ - $sql .= 'AND `items`.`id` < ? '; - array_push($params, $offset); - } - $sql .= 'ORDER BY `items`.`id` DESC '; + public function findAll($limit, $offset, $newestItemId, $status, $userId){ + $sql = 'AND `items`.`id` < ? '; + $sql .= 'ORDER BY `items`.`pub_date` DESC, `items`.`id` DESC '; + $params = array($userId, $newestItemId); + $sql = $this->makeSelectQueryStatus($sql, $status); - return $this->findAllRows($sql, $params, $limit); + return $this->findAllRows($sql, $params, $limit, $offset); } - public function findAllFolder($id, $limit, $offset, $status, $userId){ - $params = array($userId, $id); - $sql = 'AND `feeds`.`folder_id` = ? '; - if($offset !== 0){ - $sql .= 'AND `items`.`id` < ? '; - array_push($params, $offset); - } - $sql .= 'ORDER BY `items`.`id` DESC '; + public function findAllFolder($id, $limit, $offset, $newestItemId, $status, + $userId){ + + $sql = 'AND `feeds`.`folder_id` = ? ' . + 'AND `items`.`id` < ? ' . + 'ORDER BY `items`.`pub_date` DESC, `items`.`id` DESC '; + $params = array($userId, $id, $newestItemId); + $sql = $this->makeSelectQueryStatus($sql, $status); - return $this->findAllRows($sql, $params, $limit); + + return $this->findAllRows($sql, $params, $limit, $offset); } - public function findAll($limit, $offset, $status, $userId){ - $params = array($userId); - $sql = ''; - if($offset !== 0){ - $sql .= 'AND `items`.`id` < ? '; - array_push($params, $offset); - } - $sql .= 'ORDER BY `items`.`id` DESC '; + public function findAllFeed($id, $limit, $offset, $newestItemId, $status, + $userId){ + + $sql = 'AND `items`.`feed_id` = ? ' . + 'AND `items`.`id` < ? ' . + 'ORDER BY `items`.`pub_date` DESC, `items`.`id` DESC '; $sql = $this->makeSelectQueryStatus($sql, $status); - return $this->findAllRows($sql, $params, $limit); + $params = array($userId, $id, $newestItemId); + + return $this->findAllRows($sql, $params, $limit, $offset); } @@ -248,6 +246,16 @@ class ItemMapper extends Mapper implements IMapper { } + public function getNewestItemId($userId) { + $sql = 'SELECT MAX(`items`.`id`) AS `max_id` FROM `*PREFIX*news_items` `items` '. + 'JOIN `*PREFIX*news_feeds` `feeds` ' . + 'ON `feeds`.`id` = `items`.`feed_id` '. + 'AND `feeds`.`user_id` = ?'; + $params = array($userId); + + $result = $this->findOneQuery($sql, $params); + return $result['max_id']; + } } diff --git a/templates/part.shortcuts.php b/templates/part.shortcuts.php new file mode 100644 index 000000000..e69de29bb diff --git a/tests/unit/businesslayer/ItemBusinessLayerTest.php b/tests/unit/businesslayer/ItemBusinessLayerTest.php index 17c477398..5b5598abd 100644 --- a/tests/unit/businesslayer/ItemBusinessLayerTest.php +++ b/tests/unit/businesslayer/ItemBusinessLayerTest.php @@ -27,6 +27,7 @@ namespace OCA\News\BusinessLayer; require_once(__DIR__ . "/../../classloader.php"); +use \OCA\AppFramework\Db\DoesNotExistException; use \OCA\News\Db\Item; use \OCA\News\Db\StatusFlag; @@ -42,6 +43,7 @@ class ItemBusinessLayerTest extends \OCA\AppFramework\Utility\TestUtility { private $response; private $status; private $time; + private $newestItemId; protected function setUp(){ @@ -74,6 +76,7 @@ class ItemBusinessLayerTest extends \OCA\AppFramework\Utility\TestUtility { $this->showAll = true; $this->offset = 5; $this->limit = 20; + $this->newestItemId = 4; } @@ -134,13 +137,14 @@ class ItemBusinessLayerTest extends \OCA\AppFramework\Utility\TestUtility { ->with($this->equalTo($this->id), $this->equalTo($this->limit), $this->equalTo($this->offset), + $this->equalTo($this->newestItemId), $this->equalTo($this->status), $this->equalTo($this->user)) ->will($this->returnValue($this->response)); $result = $this->itemBusinessLayer->findAll( $this->id, $type, $this->limit, - $this->offset, $this->showAll, + $this->offset, $this->newestItemId, $this->showAll, $this->user); $this->assertEquals($this->response, $result); } @@ -153,13 +157,14 @@ class ItemBusinessLayerTest extends \OCA\AppFramework\Utility\TestUtility { ->with($this->equalTo($this->id), $this->equalTo($this->limit), $this->equalTo($this->offset), + $this->equalTo($this->newestItemId), $this->equalTo($this->status), $this->equalTo($this->user)) ->will($this->returnValue($this->response)); $result = $this->itemBusinessLayer->findAll( $this->id, $type, $this->limit, - $this->offset, $this->showAll, + $this->offset, $this->newestItemId, $this->showAll, $this->user); $this->assertEquals($this->response, $result); } @@ -171,13 +176,14 @@ class ItemBusinessLayerTest extends \OCA\AppFramework\Utility\TestUtility { ->method('findAll') ->with( $this->equalTo($this->limit), $this->equalTo($this->offset), + $this->equalTo($this->newestItemId), $this->equalTo($this->status), $this->equalTo($this->user)) ->will($this->returnValue($this->response)); $result = $this->itemBusinessLayer->findAll( $this->id, $type, $this->limit, - $this->offset, $this->showAll, + $this->offset, $this->newestItemId, $this->showAll, $this->user); $this->assertEquals($this->response, $result); } @@ -283,6 +289,27 @@ class ItemBusinessLayerTest extends \OCA\AppFramework\Utility\TestUtility { } + public function testGetNewestItemId() { + $this->mapper->expects($this->once()) + ->method('getNewestItemId') + ->with($this->equalTo($this->user)) + ->will($this->returnValue(12)); + + $result = $this->itemBusinessLayer->getNewestItemId($this->user); + $this->assertEquals(12, $result); + } + + + public function testGetNewestItemIdDoesNotExist() { + $this->mapper->expects($this->once()) + ->method('getNewestItemId') + ->with($this->equalTo($this->user)) + ->will($this->throwException(new DoesNotExistException('There are no items'))); + + $this->setExpectedException('\OCA\News\BusinessLayer\BusinessLayerException'); + $this->itemBusinessLayer->getNewestItemId($this->user); + } + } diff --git a/tests/unit/controller/ItemControllerTest.php b/tests/unit/controller/ItemControllerTest.php index 00d455830..451053317 100644 --- a/tests/unit/controller/ItemControllerTest.php +++ b/tests/unit/controller/ItemControllerTest.php @@ -32,6 +32,7 @@ use \OCA\AppFramework\Utility\ControllerTestUtility; use \OCA\News\Db\Item; use \OCA\News\Db\Feed; use \OCA\News\Db\FeedType; +use \OCA\News\BusinessLayer\BusinessLayerException; require_once(__DIR__ . "/../../classloader.php"); @@ -43,6 +44,7 @@ class ItemControllerTest extends ControllerTestUtility { private $feedBusinessLayer; private $request; private $controller; + private $newestItemId; /** @@ -62,6 +64,7 @@ class ItemControllerTest extends ControllerTestUtility { $this->controller = new ItemController($this->api, $this->request, $this->itemBusinessLayer, $this->feedBusinessLayer); $this->user = 'jackob'; + $this->newestItemId = 12312; } private function getPostController($postValue, $url=array()){ @@ -269,36 +272,49 @@ class ItemControllerTest extends ControllerTestUtility { $feeds = array(new Feed()); $result = array( 'items' => array(new Item()), - 'feeds' => $feeds + 'feeds' => $feeds, + 'newestItemId' => $this->newestItemId ); $post = array( 'limit' => 3, 'type' => FeedType::FEED, 'id' => 2, - 'offset' => 0 + 'offset' => 0, + 'newestItemId' => 3 ); $this->controller = $this->getPostController($post); $this->itemsApiExpects($post['id'], $post['type']); - $this->itemBusinessLayer->expects($this->once()) - ->method('findAll') - ->with($post['id'], $post['type'], $post['limit'], - $post['offset'], true, $this->user) - ->will($this->returnValue($result['items'])); - $this->feedBusinessLayer->expects($this->once()) ->method('findAll') ->with($this->equalTo($this->user)) ->will($this->returnValue($feeds)); + $this->itemBusinessLayer->expects($this->once()) + ->method('getNewestItemId') + ->with($this->equalTo($this->user)) + ->will($this->returnValue($this->newestItemId)); + + $this->itemBusinessLayer->expects($this->once()) + ->method('findAll') + ->with( + $this->equalTo($post['id']), + $this->equalTo($post['type']), + $this->equalTo($post['limit']), + $this->equalTo($post['offset']), + $this->equalTo($this->newestItemId), + $this->equalTo(true), + $this->equalTo($this->user)) + ->will($this->returnValue($result['items'])); + $response = $this->controller->items(); $this->assertEquals($result, $response->getParams()); $this->assertTrue($response instanceof JSONResponse); } - public function testItemsDoesNotReturnFeedsIfOffsetNotZero(){ + public function testItemsOffsetNotZero(){ $result = array( 'items' => array(new Item()) ); @@ -306,7 +322,8 @@ class ItemControllerTest extends ControllerTestUtility { 'limit' => 3, 'type' => FeedType::FEED, 'id' => 2, - 'offset' => 10 + 'offset' => 10, + 'newestItemId' => 3 ); $this->controller = $this->getPostController($post); @@ -314,8 +331,13 @@ class ItemControllerTest extends ControllerTestUtility { $this->itemBusinessLayer->expects($this->once()) ->method('findAll') - ->with($post['id'], $post['type'], $post['limit'], - $post['offset'], true, $this->user) + ->with($this->equalTo($post['id']), + $this->equalTo($post['type']), + $this->equalTo($post['limit']), + $this->equalTo($post['offset']), + $this->equalTo($post['newestItemId']), + $this->equalTo(true), + $this->equalTo($this->user)) ->will($this->returnValue($result['items'])); $this->feedBusinessLayer->expects($this->never()) @@ -326,28 +348,30 @@ class ItemControllerTest extends ControllerTestUtility { $this->assertTrue($response instanceof JSONResponse); } - public function testItemsNew(){ - $result = array( - 'items' => array(new Item()) - ); + + public function testGetItemsNoNewestItemsId(){ + $result = array(); $post = array( + 'limit' => 3, 'type' => FeedType::FEED, 'id' => 2, - 'updatedSince' => 3333 + 'offset' => 0, + 'newestItemId' => 3 ); $this->controller = $this->getPostController($post); $this->itemsApiExpects($post['id'], $post['type']); - + $this->itemBusinessLayer->expects($this->once()) - ->method('findAllNew') - ->with($post['id'], $post['type'], $post['updatedSince'], - true, $this->user) - ->will($this->returnValue($result['items'])); + ->method('getNewestItemId') + ->with($this->equalTo($this->user)) + ->will($this->throwException(new BusinessLayerException(''))); $response = $this->controller->items(); $this->assertEquals($result, $response->getParams()); - $this->assertTrue($response instanceof JSONResponse); + $this->assertTrue($response instanceof JSONResponse); } + + } \ No newline at end of file diff --git a/tests/unit/db/ItemMapperTest.php b/tests/unit/db/ItemMapperTest.php index 8619731c0..ff5bb1f4c 100644 --- a/tests/unit/db/ItemMapperTest.php +++ b/tests/unit/db/ItemMapperTest.php @@ -32,6 +32,13 @@ class ItemMapperTest extends \OCA\AppFramework\Utility\MapperTestUtility { private $mapper; private $items; + private $newestItemId; + private $limit; + private $user; + private $offset; + private $updatedSince; + private $status; + public function setUp() { @@ -67,6 +74,7 @@ class ItemMapperTest extends \OCA\AppFramework\Utility\MapperTestUtility { $this->id = 11; $this->status = 333; $this->updatedSince = 323; + $this->newestItemId = 2; } @@ -146,7 +154,7 @@ class ItemMapperTest extends \OCA\AppFramework\Utility\MapperTestUtility { } public function testFindAllNew(){ - $sql = 'AND `items`.`id` >= ?'; + $sql = 'AND `items`.`last_modified` >= ?'; $sql = $this->makeSelectQueryStatus($sql, $this->status); $params = array($this->user, $this->updatedSince); @@ -158,23 +166,9 @@ class ItemMapperTest extends \OCA\AppFramework\Utility\MapperTestUtility { } - public function testFindAllNewFeed(){ - $sql = 'AND `items`.`feed_id` = ? ' . - 'AND `items`.`id` >= ?'; - $sql = $this->makeSelectQueryStatus($sql, $this->status); - $params = array($this->user, $this->id, $this->updatedSince); - - $this->setMapperResult($sql, $params, $this->rows); - $result = $this->mapper->findAllNewFeed($this->id, $this->updatedSince, - $this->status, $this->user); - - $this->assertEquals($this->items, $result); - } - - public function testFindAllNewFolder(){ $sql = 'AND `feeds`.`folder_id` = ? ' . - 'AND `items`.`id` >= ?'; + 'AND `items`.`last_modified` >= ?'; $sql = $this->makeSelectQueryStatus($sql, $this->status); $params = array($this->user, $this->id, $this->updatedSince); @@ -186,28 +180,31 @@ class ItemMapperTest extends \OCA\AppFramework\Utility\MapperTestUtility { } - public function testFindAllFeed(){ + public function testFindAllNewFeed(){ $sql = 'AND `items`.`feed_id` = ? ' . - 'AND `items`.`id` < ? ' . - 'ORDER BY `items`.`id` DESC '; + 'AND `items`.`last_modified` >= ?'; $sql = $this->makeSelectQueryStatus($sql, $this->status); - $params = array($this->user, $this->id, $this->offset); + $params = array($this->user, $this->id, $this->updatedSince); + $this->setMapperResult($sql, $params, $this->rows); - $result = $this->mapper->findAllFeed($this->id, $this->limit, - $this->offset, $this->status, $this->user); + $result = $this->mapper->findAllNewFeed($this->id, $this->updatedSince, + $this->status, $this->user); $this->assertEquals($this->items, $result); } - public function testFindAllFeedOffsetZero(){ - $sql = 'AND `items`.`feed_id` = ? ' . - 'ORDER BY `items`.`id` DESC '; + public function testFindAll(){ + $sql = 'AND `items`.`id` < ? ' . + 'ORDER BY `items`.`pub_date` DESC, `items`.`id` DESC '; $sql = $this->makeSelectQueryStatus($sql, $this->status); - $params = array($this->user, $this->id); - $this->setMapperResult($sql, $params, $this->rows); - $result = $this->mapper->findAllFeed($this->id, $this->limit, - 0, $this->status, $this->user); + $params = array($this->user, $this->newestItemId); + $this->setMapperResult($sql, $params, $this->rows, + $this->limit, $this->offset); + + $result = $this->mapper->findAll($this->limit, + $this->offset, $this->newestItemId, + $this->status, $this->user); $this->assertEquals($this->items, $result); } @@ -216,56 +213,37 @@ class ItemMapperTest extends \OCA\AppFramework\Utility\MapperTestUtility { public function testFindAllFolder(){ $sql = 'AND `feeds`.`folder_id` = ? ' . 'AND `items`.`id` < ? ' . - 'ORDER BY `items`.`id` DESC '; + 'ORDER BY `items`.`pub_date` DESC, `items`.`id` DESC '; $sql = $this->makeSelectQueryStatus($sql, $this->status); - $params = array($this->user, $this->id, - $this->offset); - $this->setMapperResult($sql, $params, $this->rows); - $result = $this->mapper->findAllFolder($this->id, $this->limit, - $this->offset, $this->status, $this->user); - - $this->assertEquals($this->items, $result); - } + $params = array($this->user, $this->id, $this->newestItemId); + $this->setMapperResult($sql, $params, $this->rows, + $this->limit, $this->offset); - public function testFindAllFolderOffsetZero(){ - $sql = 'AND `feeds`.`folder_id` = ? ' . - 'ORDER BY `items`.`id` DESC '; - $sql = $this->makeSelectQueryStatus($sql, $this->status); - $params = array($this->user, $this->id); - $this->setMapperResult($sql, $params, $this->rows); $result = $this->mapper->findAllFolder($this->id, $this->limit, - 0, $this->status, $this->user); + $this->offset, $this->newestItemId, $this->status, $this->user); $this->assertEquals($this->items, $result); } - public function testFindAll(){ - $sql = 'AND `items`.`id` < ? ' . - 'ORDER BY `items`.`id` DESC '; + public function testFindAllFeed(){ + $sql = 'AND `items`.`feed_id` = ? ' . + 'AND `items`.`id` < ? ' . + 'ORDER BY `items`.`pub_date` DESC, `items`.`id` DESC '; $sql = $this->makeSelectQueryStatus($sql, $this->status); - $params = array($this->user, $this->offset); - $this->setMapperResult($sql, $params, $this->rows); - $result = $this->mapper->findAll($this->limit, - $this->offset, $this->status, $this->user); - - $this->assertEquals($this->items, $result); - } - + $params = array($this->user, $this->id, $this->newestItemId); + $this->setMapperResult($sql, $params, $this->rows, $this->limit, + $this->offset); - public function testFindAllOffsetZero(){ - $sql = 'ORDER BY `items`.`id` DESC '; - $sql = $this->makeSelectQueryStatus($sql, $this->status); - $params = array($this->user); - $this->setMapperResult($sql, $params, $this->rows); - $result = $this->mapper->findAll($this->limit, - 0, $this->status, $this->user); + $result = $this->mapper->findAllFeed($this->id, $this->limit, + $this->offset, $this->newestItemId, $this->status, $this->user); $this->assertEquals($this->items, $result); } + public function testFindByGuidHash(){ $hash = md5('test'); $feedId = 3; @@ -354,4 +332,34 @@ class ItemMapperTest extends \OCA\AppFramework\Utility\MapperTestUtility { $result = $this->mapper->deleteReadOlderThanThreshold($threshold); } + + public function testGetNewestItem() { + $sql = 'SELECT MAX(`items`.`id`) AS `max_id` FROM `*PREFIX*news_items` `items` '. + 'JOIN `*PREFIX*news_feeds` `feeds` ' . + 'ON `feeds`.`id` = `items`.`feed_id` '. + 'AND `feeds`.`user_id` = ?'; + $params = array($this->user); + $rows = array(array('max_id' => 3)); + + $this->setMapperResult($sql, $params, $rows); + + $result = $this->mapper->getNewestItemId($this->user); + $this->assertEquals(3, $result); + } + + + public function testGetNewestItemIdNotFound() { + $sql = 'SELECT MAX(`items`.`id`) AS `max_id` FROM `*PREFIX*news_items` `items` '. + 'JOIN `*PREFIX*news_feeds` `feeds` ' . + 'ON `feeds`.`id` = `items`.`feed_id` '. + 'AND `feeds`.`user_id` = ?'; + $params = array($this->user); + $rows = array(); + + $this->setMapperResult($sql, $params, $rows); + $this->setExpectedException('\OCA\AppFramework\Db\DoesNotExistException'); + + $result = $this->mapper->getNewestItemId($this->user); + } + } \ No newline at end of file -- cgit v1.2.3