diff options
author | Devlin Junker <devlin.junker@gmail.com> | 2022-11-09 21:52:11 -0800 |
---|---|---|
committer | Benjamin Brahmer <info@b-brahmer.de> | 2022-11-17 14:50:53 +0100 |
commit | c6b4bcc00e5202e85388a0624d149e8b1df386f8 (patch) | |
tree | 7063a29710dfd0a363f3462fee8a0e8185488e15 /tests/Unit | |
parent | 760c44288256b104b662ade7a892a6ec0b898851 (diff) |
upmerging master into vue-rewrite
Signed-off-by: Devlin Junker <devlin.junker@gmail.com>
Diffstat (limited to 'tests/Unit')
-rw-r--r-- | tests/Unit/Controller/AdminControllerTest.php | 156 | ||||
-rw-r--r-- | tests/Unit/Db/ItemMapperTest.php | 14 | ||||
-rw-r--r-- | tests/Unit/Db/ItemTest.php | 12 | ||||
-rw-r--r-- | tests/Unit/Db/NewsMapperTest.php | 8 | ||||
-rw-r--r-- | tests/Unit/Fetcher/FeedFetcherTest.php | 44 | ||||
-rw-r--r-- | tests/Unit/Service/FeedServiceTest.php | 50 | ||||
-rw-r--r-- | tests/Unit/Service/ItemServiceTest.php | 15 |
7 files changed, 90 insertions, 209 deletions
diff --git a/tests/Unit/Controller/AdminControllerTest.php b/tests/Unit/Controller/AdminControllerTest.php deleted file mode 100644 index a9d7bb127..000000000 --- a/tests/Unit/Controller/AdminControllerTest.php +++ /dev/null @@ -1,156 +0,0 @@ -<?php -/** - * Nextcloud - News - * - * This file is licensed under the Affero General Public License version 3 or - * later. See the COPYING file. - * - * @author Alessandro Cosentino <cosenal@gmail.com> - * @author Bernhard Posselt <dev@bernhard-posselt.com> - * @copyright 2012 Alessandro Cosentino - * @copyright 2012-2014 Bernhard Posselt - */ - -namespace OCA\News\Tests\Unit\Controller; - -use OCA\News\Controller\AdminController; -use OCA\News\Service\ItemService; -use OCP\IConfig; -use OCP\IRequest; -use PHPUnit\Framework\TestCase; - -class AdminControllerTest extends TestCase -{ - /** - * @var string - */ - private $appName; - - /** - * @var \PHPUnit\Framework\MockObject\MockObject|IRequest - */ - private $request; - - /** - * @var AdminController - */ - private $controller; - - /** - * @var \PHPUnit\Framework\MockObject\MockObject|IConfig - */ - private $config; - - /** - * @var \PHPUnit\Framework\MockObject\MockObject|ItemService - */ - private $itemService; - - /** - * Gets run before each test - */ - public function setUp(): void - { - $this->appName = 'news'; - $this->request = $this->getMockBuilder(IRequest::class) - ->disableOriginalConstructor() - ->getMock(); - $this->config = $this->getMockBuilder(IConfig::class) - ->disableOriginalConstructor() - ->getMock(); - $this->itemService = $this->getMockBuilder(ItemService::class) - ->disableOriginalConstructor() - ->getMock(); - - $this->controller = new AdminController($this->request, $this->config, $this->itemService); - } - - /** - * Test \OCA\News\Controller\AdminController::index - */ - public function testIndex() - { - $expected = [ - 'autoPurgeMinimumInterval' => 1, - 'autoPurgeCount' => 2, - 'maxRedirects' => 3, - 'feedFetcherTimeout' => 4, - 'useCronUpdates' => false, - 'exploreUrl' => 'test', - 'updateInterval' => 3601 - ]; - $map = [ - ['news','autoPurgeMinimumInterval', 60, 1], - ['news','autoPurgeCount', 200, 2], - ['news','maxRedirects', 10, 3], - ['news','feedFetcherTimeout', 60, 4], - ['news','useCronUpdates', true, false,], - ['news','exploreUrl', '', 'test'], - ['news','updateInterval', 3600, 3601] - ]; - $this->config->expects($this->exactly(count($map))) - ->method('getAppValue') - ->will($this->returnValueMap($map)); - - $response = $this->controller->index(); - $data = $response->getParams(); - $name = $response->getTemplateName(); - $type = $response->getRenderAs(); - - $this->assertEquals($type, 'blank'); - $this->assertEquals($name, 'admin'); - $this->assertEquals($expected, $data); - } - - - public function testUpdate() - { - $expected = [ - 'autoPurgeMinimumInterval' => 1, - 'autoPurgeCount' => 2, - 'maxRedirects' => 3, - 'feedFetcherTimeout' => 4, - 'useCronUpdates' => false, - 'exploreUrl' => 'test', - 'updateInterval' => 3601 - ]; - - $this->config->expects($this->exactly(count($expected))) - ->method('setAppValue') - ->withConsecutive( - ['news','autoPurgeMinimumInterval', 1], - ['news','autoPurgeCount', 2], - ['news','maxRedirects', 3], - ['news','feedFetcherTimeout', 4], - ['news','useCronUpdates', false], - ['news','exploreUrl', 'test'], - ['news','updateInterval', 3601] - ); - - $map = [ - ['news','autoPurgeMinimumInterval', 60, 1], - ['news','autoPurgeCount', 200, 2], - ['news','maxRedirects', 10, 3], - ['news','feedFetcherTimeout', 60, 4], - ['news','useCronUpdates', true, false,], - ['news','exploreUrl', '', 'test'], - ['news','updateInterval', 3600, 3601] - ]; - $this->config->expects($this->exactly(count($map))) - ->method('getAppValue') - ->will($this->returnValueMap($map)); - - $response = $this->controller->update( - $expected['autoPurgeMinimumInterval'], - $expected['autoPurgeCount'], - $expected['maxRedirects'], - $expected['feedFetcherTimeout'], - $expected['useCronUpdates'], - $expected['exploreUrl'], - $expected['updateInterval'] - ); - - $this->assertEquals($expected, $response); - } - -} diff --git a/tests/Unit/Db/ItemMapperTest.php b/tests/Unit/Db/ItemMapperTest.php index 7fc45018e..3a4a026d6 100644 --- a/tests/Unit/Db/ItemMapperTest.php +++ b/tests/Unit/Db/ItemMapperTest.php @@ -497,14 +497,14 @@ class ItemMapperTest extends MapperTestUtility ->with('items', 'news_feeds', 'feeds', 'items.feed_id = feeds.id') ->will($this->returnSelf()); - $selectbuilder->expects($this->exactly(2)) + $selectbuilder->expects($this->exactly(3)) ->method('andWhere') - ->withConsecutive(['feeds.user_id = :userId'], ['items.id <= :maxItemId']) + ->withConsecutive(['feeds.user_id = :userId'], ['items.id <= :maxItemId'], ['items.unread = :unread']) ->will($this->returnSelf()); - $selectbuilder->expects($this->exactly(2)) + $selectbuilder->expects($this->exactly(3)) ->method('setParameter') - ->withConsecutive(['userId', 'admin'], ['maxItemId', 4]) + ->withConsecutive(['userId', 'admin'], ['maxItemId', 4], ['unread', true]) ->will($this->returnSelf()); $selectbuilder->expects($this->exactly(1)) @@ -542,14 +542,14 @@ class ItemMapperTest extends MapperTestUtility ->with('unread', 'unread') ->will($this->returnSelf()); - $this->builder->expects($this->exactly(2)) + $this->builder->expects($this->exactly(1)) ->method('andWhere') - ->withConsecutive(['id IN (:idList)'], ['unread != :unread']) + ->withConsecutive(['id IN (:idList)']) ->will($this->returnSelf()); $this->builder->expects($this->exactly(2)) ->method('setParameter') - ->withConsecutive(['unread', false], ['idList', [1, 2]]) + ->withConsecutive(['idList', [1, 2]], ['unread', false]) ->will($this->returnSelf()); $this->builder->expects($this->exactly(1)) diff --git a/tests/Unit/Db/ItemTest.php b/tests/Unit/Db/ItemTest.php index 68b7b5ab8..11620b33c 100644 --- a/tests/Unit/Db/ItemTest.php +++ b/tests/Unit/Db/ItemTest.php @@ -317,6 +317,18 @@ class ItemTest extends TestCase $this->assertEquals($expected, $item->getSearchIndex()); } + public function testSearchIndexNull() + { + $item = new Item(); + $item->setBody('<a>somEthäng</a>'); + $item->setUrl('http://link'); + $item->setAuthor(null); + $item->setTitle('<a>tätle</a>'); + $item->setCategories(['food', 'travel']); + $item->generateSearchIndex(); + $expected = 'somethängtätlefoodtravelhttp://link'; + $this->assertEquals($expected, $item->getSearchIndex()); + } public function testFromImport() { diff --git a/tests/Unit/Db/NewsMapperTest.php b/tests/Unit/Db/NewsMapperTest.php index c7ce7342a..aa1be5989 100644 --- a/tests/Unit/Db/NewsMapperTest.php +++ b/tests/Unit/Db/NewsMapperTest.php @@ -169,7 +169,7 @@ class NewsMapperTest extends TestCase ->will($this->returnSelf()); $qb->expects($this->once()) - ->method('execute'); + ->method('executeStatement'); $result = $this->class->purgeDeleted(null, null); } @@ -202,7 +202,7 @@ class NewsMapperTest extends TestCase ->will($this->returnSelf()); $qb->expects($this->once()) - ->method('execute'); + ->method('executeStatement'); $result = $this->class->purgeDeleted('jack', null); } @@ -235,7 +235,7 @@ class NewsMapperTest extends TestCase ->will($this->returnSelf()); $qb->expects($this->once()) - ->method('execute'); + ->method('executeStatement'); $result = $this->class->purgeDeleted(null, 1); } @@ -268,7 +268,7 @@ class NewsMapperTest extends TestCase ->will($this->returnSelf()); $qb->expects($this->once()) - ->method('execute'); + ->method('executeStatement'); $result = $this->class->purgeDeleted('jack', 1); } diff --git a/tests/Unit/Fetcher/FeedFetcherTest.php b/tests/Unit/Fetcher/FeedFetcherTest.php index 42f518453..af8066171 100644 --- a/tests/Unit/Fetcher/FeedFetcherTest.php +++ b/tests/Unit/Fetcher/FeedFetcherTest.php @@ -459,6 +459,40 @@ class FeedFetcherTest extends TestCase } /** + * Test if the fetch function fetches a feed that specifies a guid. + */ + public function testFetchWithGuid() + { + $this->setUpReader($this->url); + $this->createItem(); + $feed = $this->createFeed(); + $this->mockIterator($this->feed_mock, [$this->item_mock]); + $result = $this->fetcher->fetch($this->url, false, null, null); + //Explicitly assert GUID value + $this->assertEquals(2, count($result)); + $this->assertEquals(1, count($result[1])); + $resultItem = $result[1][0]; + $this->assertEquals($this->guid, $resultItem->getGuid()); + } + + /** + * Test if the fetch function fetches a feed that does not specify a guid. + */ + public function testFetchWithoutGuid() + { + $this->setUpReader($this->url); + $this->guid = null; + $this->createItem(); + $feed = $this->createFeed(); + $this->mockIterator($this->feed_mock, [$this->item_mock]); + $result = $this->fetcher->fetch($this->url, false, null, null); + //Explicitly assert GUID value + $this->assertEquals(2, count($result)); + $this->assertEquals(1, count($result[1])); + $resultItem = $result[1][0]; + $this->assertEquals($this->permalink, $resultItem->getGuid()); + } + /** * Mock an iteration option on an existing mock * * @param object $iteratorMock The mock to enhance @@ -576,10 +610,10 @@ class FeedFetcherTest extends TestCase $this->item_mock->expects($this->exactly(1)) ->method('getLink') ->will($this->returnValue($this->permalink)); - $this->item_mock->expects($this->exactly(2)) + $this->item_mock->expects($this->exactly(1)) ->method('getTitle') ->will($this->returnValue($this->title)); - $this->item_mock->expects($this->exactly(2)) + $this->item_mock->expects($this->exactly(1)) ->method('getPublicId') ->will($this->returnValue($this->guid)); $this->item_mock->expects($this->exactly(1)) @@ -600,7 +634,6 @@ class FeedFetcherTest extends TestCase $item->setUnread(true) ->setUrl($this->permalink) ->setTitle('my<\' title') - ->setGuid($this->guid) ->setGuidHash($this->guid_hash) ->setBody($this->parsed_body) ->setRtl(false) @@ -609,6 +642,11 @@ class FeedFetcherTest extends TestCase ->setAuthor(html_entity_decode($this->author->getName())) ->setCategoriesJson($this->categoriesJson); + // some tests deliberately omit this, so leave default value if the guid is to be ignored + if ($this->guid !== null) { + $item->setGuid($this->guid); + } + if ($enclosureType === 'audio/ogg' || $enclosureType === 'video/ogg') { $media = $this->getMockbuilder(MediaInterface::class)->getMock(); $media->expects($this->once()) diff --git a/tests/Unit/Service/FeedServiceTest.php b/tests/Unit/Service/FeedServiceTest.php index 9f4743777..bcc367f0c 100644 --- a/tests/Unit/Service/FeedServiceTest.php +++ b/tests/Unit/Service/FeedServiceTest.php @@ -68,11 +68,6 @@ class FeedServiceTest extends TestCase private $time; /** - * @var int - */ - private $autoPurgeMinimumInterval; - - /** * @var \PHPUnit\Framework\MockObject\MockObject|\HTMLPurifier */ private $purifier; @@ -98,7 +93,6 @@ class FeedServiceTest extends TestCase ->disableOriginalConstructor() ->getMock(); $this->time = 222; - $this->autoPurgeMinimumInterval = 10; $timeFactory = $this->getMockBuilder(Time::class) ->disableOriginalConstructor() ->getMock(); @@ -131,10 +125,6 @@ class FeedServiceTest extends TestCase $config = $this->getMockBuilder(IConfig::class) ->disableOriginalConstructor() ->getMock(); - $config->expects($this->any()) - ->method('getAppValue') - ->with('news', 'autoPurgeMinimumInterval') - ->will($this->returnValue($this->autoPurgeMinimumInterval)); $this->class = new FeedServiceV2( $this->mapper, @@ -165,21 +155,14 @@ class FeedServiceTest extends TestCase public function testCreateDoesNotFindFeed() { - $ex = new ReadErrorException('hi'); $url = 'test'; - $this->mapper->expects($this->once()) - ->method('findByURL') - ->with($this->uid, $url) - ->willReturn(new Feed()); - - $this->fetcher->expects($this->never()) + $this->fetcher->expects($this->exactly(2)) ->method('fetch') ->with($url) - ->will($this->throwException($ex)); + ->will($this->throwException(new ReadErrorException('There is no feed'))); - $this->expectException(ServiceConflictException::class); - $this->expectExceptionMessage('Feed with this URL exists'); + $this->expectException(ServiceNotFoundException::class); $this->class->create($this->uid, $url, 1); } @@ -209,15 +192,16 @@ class FeedServiceTest extends TestCase ->method('findByURL') ->with($this->uid, $url) ->will($this->throwException(new DoesNotExistException('no'))); - $this->explorer->expects($this->once()) - ->method('discover') - ->with($url) - ->will($this->returnValue([])); + $this->explorer->expects($this->never()) + ->method('discover') + ->with($url) + ->will($this->returnValue([])); $this->fetcher->expects($this->once()) ->method('fetch') ->with($url) ->will($this->returnValue($return)); + $this->mapper->expects($this->once()) ->method('insert') ->with($createdFeed) @@ -268,7 +252,7 @@ class FeedServiceTest extends TestCase ->method('findByURL') ->with($this->uid, $url) ->will($this->throwException(new DoesNotExistException('no'))); - $this->explorer->expects($this->once()) + $this->explorer->expects($this->never()) ->method('discover') ->with($url) ->will($this->returnValue([])); @@ -326,16 +310,19 @@ class FeedServiceTest extends TestCase $this->mapper->expects($this->once()) ->method('findByURL') - ->with($this->uid, $url) + ->with($this->uid, 'http://discover.test') ->will($this->throwException(new DoesNotExistException('no'))); $this->explorer->expects($this->once()) ->method('discover') ->with($url) ->will($this->returnValue(['http://discover.test'])); - $this->fetcher->expects($this->once()) + $this->fetcher->expects($this->exactly(2)) ->method('fetch') - ->with('http://discover.test') - ->will($this->returnValue($return)); + ->withConsecutive( + ['http://test'], + ['http://discover.test'] + ) + ->willReturnOnConsecutiveCalls($this->throwException(new ReadErrorException('There is no feed')), $this->returnValue($return)); $this->mapper->expects($this->once()) ->method('insert') @@ -434,12 +421,12 @@ class FeedServiceTest extends TestCase $url = 'http://test'; $folderId = 10; - $this->fetcher->expects($this->once()) + $this->fetcher->expects($this->exactly(2)) ->method('fetch') ->with($url) ->will($this->throwException(new ReadErrorException('ERROR'))); - $this->mapper->expects($this->once()) + $this->mapper->expects($this->never()) ->method('findByURL') ->with($this->uid, $url) ->will($this->throwException(new DoesNotExistException('no'))); @@ -657,7 +644,6 @@ class FeedServiceTest extends TestCase $feed2->setId(5); $feeds = [$feed1, $feed2]; - $time = $this->time - $this->autoPurgeMinimumInterval; $this->mapper->expects($this->exactly(1)) ->method('purgeDeleted') diff --git a/tests/Unit/Service/ItemServiceTest.php b/tests/Unit/Service/ItemServiceTest.php index f91692cda..17a67674c 100644 --- a/tests/Unit/Service/ItemServiceTest.php +++ b/tests/Unit/Service/ItemServiceTest.php @@ -605,23 +605,24 @@ class ItemServiceTest extends TestCase public function testPurgeOverThresholdNull() { - $this->config->expects($this->once()) + $this->config->expects($this->exactly(2)) ->method('getAppValue') - ->with('news', 'autoPurgeCount', 200) - ->will($this->returnValue(200)); - + ->withConsecutive(['news', 'autoPurgeCount', 200], ['news', 'purgeUnread', false]) + ->willReturnOnConsecutiveCalls(200, false); + $this->mapper->expects($this->once()) ->method('deleteOverThreshold') - ->with(200); + ->with(200, false); $this->class->purgeOverThreshold(); } public function testPurgeOverThresholdSet() { - $this->config->expects($this->never()) + $this->config->expects($this->once()) ->method('getAppValue') - ->with('news', 'autoPurgeCount', 200); + ->with('news', 'purgeUnread', false) + ->will($this->returnValue(false)); $this->mapper->expects($this->once()) ->method('deleteOverThreshold') |