summaryrefslogtreecommitdiffstats
path: root/tests
diff options
context:
space:
mode:
authorMarco Nassabain <marco.nassabain@hotmail.com>2021-03-04 23:27:37 +0100
committerSean Molenaar <SMillerDev@users.noreply.github.com>2021-04-08 22:31:21 +0200
commite6eb878c4fbd7e798b17e881f6f1fc9e0267c41e (patch)
treef1b5d0548d24678cc32e1fe85965ae7aaa1341fc /tests
parent25e2efd54a2d77adcefdebdbbbc8d00ab3bb3109 (diff)
✅ WIP add ShareServiceTest
Signed-off-by: Marco Nassabain <marco.nassabain@hotmail.com>
Diffstat (limited to 'tests')
-rw-r--r--tests/Unit/Service/ShareServiceTest.php218
1 files changed, 218 insertions, 0 deletions
diff --git a/tests/Unit/Service/ShareServiceTest.php b/tests/Unit/Service/ShareServiceTest.php
new file mode 100644
index 000000000..4c74e39a5
--- /dev/null
+++ b/tests/Unit/Service/ShareServiceTest.php
@@ -0,0 +1,218 @@
+<?php
+/**
+ * Nextcloud - News
+ *
+ * This file is licensed under the Affero General Public License version 3 or
+ * later. See the COPYING file.
+ *
+ * @author Marco Nassabain <marco.nassabain@hotmail.com>
+ */
+
+
+namespace OCA\News\Tests\Unit\Service;
+
+use FeedIo\Explorer;
+use FeedIo\Reader\ReadErrorException;
+
+use OC\L10N\L10N;
+use OCA\News\Service\Exceptions\ServiceConflictException;
+use OCA\News\Service\Exceptions\ServiceNotFoundException;
+use OCP\AppFramework\Db\DoesNotExistException;
+use OCA\News\Service\FeedServiceV2;
+use OCA\News\Service\ItemServiceV2;
+use OCA\News\Service\ShareService;
+use OCA\News\Utility\Time;
+
+use OCA\News\Db\Feed;
+use OCA\News\Db\Item;
+use OCP\IConfig;
+use OCP\IL10N;
+
+use PHPUnit\Framework\MockObject\MockObject;
+use PHPUnit\Framework\TestCase;
+use Psr\Log\LoggerInterface;
+
+
+class ShareServiceTest extends TestCase
+{
+ /**
+ * @var MockObject|ItemServiceV2
+ */
+ private $itemService;
+
+ /**
+ * @var MockObject|FeedServiceV2
+ */
+ private $feedService;
+
+ /**
+ * @var MockObject|LoggerInterface
+ */
+ private $logger;
+
+ /** @var ShareService */
+ private $class;
+
+ /**
+ * @var string
+ */
+ private $uid;
+
+ /**
+ * @var string
+ */
+ private $recipient;
+
+ protected function setUp(): void
+ {
+ $this->logger = $this->getMockBuilder(LoggerInterface::class)
+ ->disableOriginalConstructor()
+ ->getMock();
+ $this->itemService = $this
+ ->getMockBuilder(ItemServiceV2::class)
+ ->disableOriginalConstructor()
+ ->getMock();
+ $this->feedService = $this
+ ->getMockBuilder(FeedServiceV2::class)
+ ->disableOriginalConstructor()
+ ->getMock();
+
+ $this->time = 333333;
+
+ $this->class = new ShareService(
+ $this->feedService,
+ $this->itemService,
+ $this->logger
+ );
+
+ $this->uid = 'sender';
+ $this->recipient = 'recipient';
+ }
+
+ public function testShareItemWithUser()
+ {
+ $feedUrl = 'http://nextcloud/sharedwithme';
+ $itemId = 3;
+
+ // Item to be shared
+ $item = new Item();
+ $item->setGuid('_guid_')
+ ->setGuidHash(md5('_guid_'))
+ ->setUrl('_url_')
+ ->setTitle('_title_')
+ ->setAuthor('_author_')
+ ->setUnread(0)
+ ->setStarred(1)
+ ->setFeedId(10);
+
+ // Shared item
+ $sharedItem = clone $item;
+ $sharedItem
+ ->setUnread(1) // A newly shared item is unread, ...
+ ->setStarred(0) // ... not starred, ...
+ ->setFeedId(100) // ... placed in the 'Shared with me' feed, ...
+ ->setSharedBy($this->uid); // ... and contains the senders user ID
+
+ // Dummy feed 'Shared with me'
+ $feed = new Feed();
+ $feed->setId(100);
+ $feed->setUserId($this->recipient)
+ ->setUrl($feedUrl)
+ ->setLink($feedUrl)
+ ->setTitle('Shared with me')
+ ->setAdded($this->time)
+ ->setFolderId(null)
+ ->setPreventUpdate(true);
+
+
+ $this->itemService->expects($this->once())
+ ->method('find')
+ ->with($this->uid, $itemId)
+ ->will($this->returnValue($item));
+
+ $this->feedService->expects($this->once())
+ ->method('findByUrl')
+ ->with($this->recipient, $feedUrl)
+ ->will($this->returnValue($feed));
+
+ // Here we test if the setters worked properly using 'with()'
+ $this->itemService->expects($this->once())
+ ->method('insertOrUpdate')
+ ->with($sharedItem)
+ ->will($this->returnValue($sharedItem));
+
+
+ $this->class->shareItemWithUser($this->uid, $itemId, $this->recipient);
+ }
+
+ public function testShareItemWithUserCreatesOwnFeedWhenNotFound()
+ {
+ $feedUrl = 'http://nextcloud/sharedwithme';
+ $itemId = 3;
+
+ // Item to be shared
+ $item = new Item();
+ $item->setGuid('_guid_')
+ ->setGuidHash(md5('_guid_'))
+ ->setUrl('_url_')
+ ->setTitle('_title_')
+ ->setAuthor('_author_')
+ ->setUnread(0)
+ ->setStarred(1)
+ ->setFeedId(10);
+
+ // Shared item
+ $sharedItem = clone $item;
+ $sharedItem->setUnread(1) // A newly shared item is unread, ...
+ ->setStarred(0) // ... not starred, ...
+ ->setFeedId(100) // ... placed in the 'Shared with me' feed, ...
+ ->setSharedBy($this->uid); // ... and contains the senders user ID
+
+ // Dummy feed 'Shared with me'
+ $feed = new Feed();
+ $feed->setId(100);
+ $feed->setUserId($this->recipient)
+ ->setUrl($feedUrl)
+ ->setLink($feedUrl)
+ ->setTitle('Shared with me')
+ ->setAdded($this->time)
+ ->setFolderId(null)
+ ->setPreventUpdate(true);
+
+
+ $this->itemService->expects($this->once())
+ ->method('find')
+ ->with($this->uid, $itemId)
+ ->will($this->returnValue($item));
+
+ $this->feedService->expects($this->once())
+ ->method('findByUrl')
+ ->with($this->recipient, $feedUrl)
+ ->will($this->returnValue(null));
+
+ // TODO: Test if the feed is properly created
+ $this->feedService->expects($this->once())
+ ->method('insert')
+ ->will($this->returnValue($feed));
+
+ // Here we test if the setters worked properly using 'with()'
+ $this->itemService->expects($this->once())
+ ->method('insertOrUpdate')
+ ->with($sharedItem)
+ ->will($this->returnValue($sharedItem));
+
+
+ $this->class->shareItemWithUser($this->uid, $itemId, $this->recipient);
+ }
+
+
+ public function testShareItemWithUserItemDoesNotExist()
+ {
+ $this->expectException(ServiceNotFoundException::class);
+ $this->itemService->expects($this->once())
+ ->method('find')
+ ->will($this->throwException(new ServiceNotFoundException('')));
+
+ $this->class->shareItemWithUser('sender', 1, 'recipient');
+ }
+}