summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorBernhard Posselt <nukeawhale@gmail.com>2013-04-16 23:52:23 +0200
committerBernhard Posselt <nukeawhale@gmail.com>2013-04-16 23:52:23 +0200
commitcdbf8ca56d427b835ad675c92d57fe5a3140a2f5 (patch)
tree04367f8eda65cbc78de23ac830f693fe9cbca0ce
parente95c1af89093f74e2ba95864b99e3df63dc5dce1 (diff)
dont add items if they are already in the database
-rw-r--r--businesslayer/feedbusinesslayer.php15
-rw-r--r--tests/unit/businesslayer/FeedBusinessLayerTest.php79
2 files changed, 80 insertions, 14 deletions
diff --git a/businesslayer/feedbusinesslayer.php b/businesslayer/feedbusinesslayer.php
index f7666fc1c..ec6d7283a 100644
--- a/businesslayer/feedbusinesslayer.php
+++ b/businesslayer/feedbusinesslayer.php
@@ -72,14 +72,25 @@ class FeedBusinessLayer extends BusinessLayer {
// insert items in reverse order because the first one is usually the
// newest item
+ $unreadCount = 0;
for($i=count($items)-1; $i>=0; $i--){
$item = $items[$i];
$item->setFeedId($feed->getId());
- $this->itemMapper->insert($item);
+
+ // check if item exists (guidhash is the same)
+ // and ignore it if it does
+ try {
+ $this->itemMapper->findByGuidHash(
+ $item->getGuidHash(), $item->getFeedId(), $userId);
+ continue;
+ } catch(DoesNotExistException $ex){
+ $unreadCount += 1;
+ $this->itemMapper->insert($item);
+ }
}
// set unread count
- $feed->setUnreadCount(count($items));
+ $feed->setUnreadCount($unreadCount);
return $feed;
} catch(FetcherException $ex){
diff --git a/tests/unit/businesslayer/FeedBusinessLayerTest.php b/tests/unit/businesslayer/FeedBusinessLayerTest.php
index 3f1ed9a56..b17a19136 100644
--- a/tests/unit/businesslayer/FeedBusinessLayerTest.php
+++ b/tests/unit/businesslayer/FeedBusinessLayerTest.php
@@ -102,9 +102,13 @@ class FeedBusinessLayerTest extends \OCA\AppFramework\Utility\TestUtility {
$createdFeed = new Feed();
$ex = new DoesNotExistException('yo');
$createdFeed->setUrl($url);
+ $item1 = new Item();
+ $item1->setGuidHash('hi');
+ $item2 = new Item();
+ $item2->setGuidHash('yo');
$return = array(
$createdFeed,
- array(new Item(), new Item())
+ array($item1, $item2)
);
$this->mapper->expects($this->once())
@@ -120,9 +124,23 @@ class FeedBusinessLayerTest extends \OCA\AppFramework\Utility\TestUtility {
->with($this->equalTo($createdFeed))
->will($this->returnValue($createdFeed));
$this->itemMapper->expects($this->at(0))
+ ->method('findByGuidHash')
+ ->with(
+ $this->equalTo($item2->getGuidHash()),
+ $this->equalTo($item2->getFeedId()),
+ $this->equalTo($this->user))
+ ->will($this->throwException($ex));
+ $this->itemMapper->expects($this->at(1))
->method('insert')
->with($this->equalTo($return[1][1]));
- $this->itemMapper->expects($this->at(1))
+ $this->itemMapper->expects($this->at(2))
+ ->method('findByGuidHash')
+ ->with(
+ $this->equalTo($item1->getGuidHash()),
+ $this->equalTo($item1->getFeedId()),
+ $this->equalTo($this->user))
+ ->will($this->throwException($ex));
+ $this->itemMapper->expects($this->at(3))
->method('insert')
->with($this->equalTo($return[1][0]));
@@ -132,19 +150,56 @@ class FeedBusinessLayerTest extends \OCA\AppFramework\Utility\TestUtility {
$this->assertEquals($feed->getUrl(), $url);
}
- public function testCreateFeedExistsAlready(){
+
+ public function testCreateItemGuidExistsAlready(){
$url = 'test';
- $trans = $this->getMock('Trans', array('t'));
- $trans->expects($this->once())
- ->method('t');
- $this->api->expects($this->once())
- ->method('getTrans')
- ->will($this->returnValue($trans));
+ $folderId = 10;
+ $createdFeed = new Feed();
+ $ex = new DoesNotExistException('yo');
+ $createdFeed->setUrl($url);
+ $item1 = new Item();
+ $item1->setGuidHash('hi');
+ $item2 = new Item();
+ $item2->setGuidHash('yo');
+ $return = array(
+ $createdFeed,
+ array($item1, $item2)
+ );
+
$this->mapper->expects($this->once())
->method('findByUrlHash')
- ->with($this->equalTo(md5($url)), $this->equalTo($this->user));
- $this->setExpectedException('\OCA\News\BusinessLayer\BusinessLayerException');
- $this->businessLayer->create($url, 1, $this->user);
+ ->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('findByGuidHash')
+ ->with(
+ $this->equalTo($item2->getGuidHash()),
+ $this->equalTo($item2->getFeedId()),
+ $this->equalTo($this->user))
+ ->will($this->throwException($ex));
+ $this->itemMapper->expects($this->at(1))
+ ->method('insert')
+ ->with($this->equalTo($return[1][1]));
+ $this->itemMapper->expects($this->at(2))
+ ->method('findByGuidHash')
+ ->with(
+ $this->equalTo($item1->getGuidHash()),
+ $this->equalTo($item1->getFeedId()),
+ $this->equalTo($this->user));
+
+ $feed = $this->businessLayer->create($url, $folderId, $this->user);
+
+ $this->assertEquals($feed->getFolderId(), $folderId);
+ $this->assertEquals($feed->getUrl(), $url);
+ $this->assertEquals(1, $feed->getUnreadCount());
}