1
0
mirror of https://github.com/chylex/Nextcloud-News.git synced 2024-10-17 08:42:48 +02:00
Nextcloud-News/tests/Unit/Controller/ExportControllerTest.php
Sean Molenaar 66b4005b5e Controllers: Export Starred or unread instead of and
Fixes GH-1010

Signed-off-by: Sean Molenaar <sean@seanmolenaar.eu>
2021-02-02 16:07:38 +01:00

172 lines
5.4 KiB
PHP

<?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\ExportController;
use OCA\News\Service\FeedServiceV2;
use OCA\News\Service\FolderServiceV2;
use OCA\News\Service\ItemServiceV2;
use OCA\News\Service\OpmlService;
use \OCA\News\Utility\OPMLExporter;
use \OCA\News\Db\Item;
use \OCA\News\Db\Feed;
use OCP\AppFramework\Http\DataDownloadResponse;
use OCP\IRequest;
use OCP\IUser;
use OCP\IUserSession;
use PHPUnit\Framework\TestCase;
class ExportControllerTest extends TestCase
{
private $controller;
private $user;
/**
* @var \PHPUnit\Framework\MockObject\MockObject|IUserSession
*/
private $userSession;
/**
* @var \PHPUnit\Framework\MockObject\MockObject|FeedServiceV2
*/
private $feedService;
/**
* @var \PHPUnit\Framework\MockObject\MockObject|FolderServiceV2
*/
private $folderService;
/**
* @var \PHPUnit\Framework\MockObject\MockObject|ItemServiceV2
*/
private $itemService;
/**
* @var \PHPUnit\Framework\MockObject\MockObject|OpmlService
*/
private $opmlService;
/**
* Gets run before each test
*/
public function setUp(): void
{
$appName = 'news';
$this->itemService = $this->getMockBuilder(ItemServiceV2::class)
->disableOriginalConstructor()
->getMock();
$this->feedService = $this->getMockBuilder(FeedServiceV2::class)
->disableOriginalConstructor()
->getMock();
$this->folderService = $this->getMockBuilder(FolderServiceV2::class)
->disableOriginalConstructor()
->getMock();
$this->opmlService = $this->getMockBuilder(OpmlService::class)
->disableOriginalConstructor()
->getMock();
$this->user = $this->getMockBuilder(IUser::class)->getMock();
$this->user->expects($this->any())
->method('getUID')
->will($this->returnValue('user'));
$this->userSession = $this->getMockBuilder(IUserSession::class)
->getMock();
$this->userSession->expects($this->any())
->method('getUser')
->will($this->returnValue($this->user));
$request = $this->getMockBuilder(IRequest::class)
->disableOriginalConstructor()
->getMock();
$this->controller = new ExportController(
$request,
$this->folderService,
$this->feedService,
$this->itemService,
$this->opmlService,
$this->userSession
);
}
public function testOpmlExportNoFeeds()
{
$opml =
"<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n" .
"<opml version=\"2.0\">\n" .
" <head>\n" .
" <title>Subscriptions</title>\n" .
" </head>\n" .
" <body/>\n" .
"</opml>\n";
$this->opmlService->expects($this->once())
->method('export')
->with('user')
->will($this->returnValue($opml));
$return = $this->controller->opml();
$this->assertTrue($return instanceof DataDownloadResponse);
$this->assertEquals($opml, $return->render());
}
public function testGetAllArticles()
{
$item1 = new Item();
$item1->setFeedId(3);
$item1->setGuid('guid');
$item2 = new Item();
$item2->setFeedId(5);
$item2->setGuid('guid');
$feed1 = new Feed();
$feed1->setId(3);
$feed1->setLink('http://goo');
$feed2 = new Feed();
$feed2->setId(5);
$feed2->setLink('http://gee');
$feeds = [$feed1, $feed2];
$articles = [$item1, $item2];
$this->feedService->expects($this->once())
->method('findAllForUser')
->with('user')
->will($this->returnValue($feeds));
$this->itemService->expects($this->exactly(2))
->method('findAllForUser')
->withConsecutive(['user', ['unread' => false, 'starred' => true]], ['user', ['unread' => true]])
->willReturnOnConsecutiveCalls($articles, []);
$return = $this->controller->articles();
$headers = $return->getHeaders();
$this->assertEquals(
'attachment; filename="articles.json"',
$headers ['Content-Disposition']
);
$this->assertEquals(
'[{"guid":"guid","url":null,"title":null,' .
'"author":null,"pubDate":null,"updatedDate":null,"body":null,"enclosureMime":null,' .
'"enclosureLink":null,"mediaThumbnail":null,"mediaDescription":null,'.
'"unread":false,"starred":false,' .
'"feedLink":"http:\/\/goo","rtl":false},{"guid":"guid","url":null,' .
'"title":null,"author":null,"pubDate":null,"updatedDate":null,"body":null,' .
'"enclosureMime":null,"enclosureLink":null,"mediaThumbnail":null,'.
'"mediaDescription":null,"unread":false,' .
'"starred":false,"feedLink":"http:\/\/gee","rtl":false}]',
$return->render()
);
}
}