From ec8394eda1b1769b3cf1032fc03a107092b222a4 Mon Sep 17 00:00:00 2001 From: Bernhard Posselt Date: Thu, 2 May 2013 21:03:33 +0200 Subject: [PATCH] add read button --- external/itemapi.php | 30 +++++++++++-- tests/unit/external/ItemAPITest.php | 66 +++++++++++++++++++++++++++-- 2 files changed, 89 insertions(+), 7 deletions(-) diff --git a/external/itemapi.php b/external/itemapi.php index e4d8ab904..b1b75ca35 100644 --- a/external/itemapi.php +++ b/external/itemapi.php @@ -75,17 +75,41 @@ class ItemAPI extends Controller { public function getUpdated() { + $result = array( + 'items' => array() + ); + $userId = $this->api->getUserId(); + $lastModified = (int) $this->params('lastModified', 0); + $type = (int) $this->params('type'); + $id = (int) $this->params('id'); + + $items = $this->itemBusinessLayer->findAllNew( + $id, + $type, + $lastModified, + true, + $userId + ); + + foreach ($items as $item) { + array_push($result['items'], $item->toAPI()); + } + + return new NewsAPIResult($result); } - public function get() { - + private function setRead($isRead) { + $userId = $this->api->getUserId(); + $itemId = $this->params('itemId'); + $this->itemBusinessLayer->read($itemId, $isRead, $userId); } public function read() { - + $this->setRead(true); + return new NewsAPIResult(); } diff --git a/tests/unit/external/ItemAPITest.php b/tests/unit/external/ItemAPITest.php index e8fe747d5..c91b06db7 100644 --- a/tests/unit/external/ItemAPITest.php +++ b/tests/unit/external/ItemAPITest.php @@ -54,12 +54,15 @@ class ItemAPITest extends \PHPUnit_Framework_TestCase { '\OCA\News\BusinessLayer\ItemBusinessLayer') ->disableOriginalConstructor() ->getMock(); + $this->user = 'tom'; + $this->api->expects($this->once()) + ->method('getUserId') + ->will($this->returnValue($this->user)); $this->itemAPI = new ItemAPI( $this->api, $this->request, $this->itemBusinessLayer ); - $this->user = 'tom'; } @@ -80,9 +83,6 @@ class ItemAPITest extends \PHPUnit_Framework_TestCase { $this->itemBusinessLayer ); - $this->api->expects($this->once()) - ->method('getUserId') - ->will($this->returnValue($this->user)); $this->itemBusinessLayer->expects($this->once()) ->method('findAll') ->with( @@ -103,5 +103,63 @@ class ItemAPITest extends \PHPUnit_Framework_TestCase { } + public function testGetUpdated() { + $items = array( + new Item() + ); + $request = new Request(array('params' => array( + 'lastModified' => 30, + 'type' => 1, + 'id' => 2, + ))); + $this->itemAPI = new ItemAPI( + $this->api, + $request, + $this->itemBusinessLayer + ); + + $this->itemBusinessLayer->expects($this->once()) + ->method('findAllNew') + ->with( + $this->equalTo(2), + $this->equalTo(1), + $this->equalTo(30), + $this->equalTo(true), + $this->equalTo($this->user) + ) + ->will($this->returnValue($items)); + + $response = $this->itemAPI->getUpdated(); + + $this->assertEquals(array( + 'items' => array($items[0]->toAPI()) + ), $response->getData()); + } + + + public function testRead() { + $request = new Request(array('urlParams' => array( + 'itemId' => 2 + ))); + $this->itemAPI = new ItemAPI( + $this->api, + $request, + $this->itemBusinessLayer + ); + + $this->itemBusinessLayer->expects($this->once()) + ->method('read') + ->with( + $this->equalTo(2), + $this->equalTo(true), + $this->equalTo($this->user) + ); + + $response = $this->itemAPI->read(); + + $this->assertNull($response->getData()); + $this->assertNull($response->getMessage()); + $this->assertEquals(NewsAPIResult::OK, $response->getStatusCode()); + } } \ No newline at end of file