Fix update notification tests

Signed-off-by: Joas Schilling <coding@schilljs.com>
This commit is contained in:
Joas Schilling 2017-01-09 12:09:47 +01:00
parent bb84db02cb
commit b29149402c
No known key found for this signature in database
GPG key ID: E166FD8976B3BAC8
3 changed files with 27 additions and 45 deletions

View file

@ -30,8 +30,6 @@ use OCP\Http\Client\IClientService;
use OCP\IConfig; use OCP\IConfig;
use OCP\IGroup; use OCP\IGroup;
use OCP\IGroupManager; use OCP\IGroupManager;
use OCP\IURLGenerator;
use OCP\IUser;
use OCP\Notification\IManager; use OCP\Notification\IManager;
class BackgroundJob extends TimedJob { class BackgroundJob extends TimedJob {
@ -51,9 +49,6 @@ class BackgroundJob extends TimedJob {
/** @var IClientService */ /** @var IClientService */
protected $client; protected $client;
/** @var IURLGenerator */
protected $urlGenerator;
/** @var string[] */ /** @var string[] */
protected $users; protected $users;
@ -65,9 +60,8 @@ class BackgroundJob extends TimedJob {
* @param IGroupManager $groupManager * @param IGroupManager $groupManager
* @param IAppManager $appManager * @param IAppManager $appManager
* @param IClientService $client * @param IClientService $client
* @param IURLGenerator $urlGenerator
*/ */
public function __construct(IConfig $config, IManager $notificationManager, IGroupManager $groupManager, IAppManager $appManager, IClientService $client, IURLGenerator $urlGenerator) { public function __construct(IConfig $config, IManager $notificationManager, IGroupManager $groupManager, IAppManager $appManager, IClientService $client) {
// Run once a day // Run once a day
$this->setInterval(60 * 60 * 24); $this->setInterval(60 * 60 * 24);
@ -76,7 +70,6 @@ class BackgroundJob extends TimedJob {
$this->groupManager = $groupManager; $this->groupManager = $groupManager;
$this->appManager = $appManager; $this->appManager = $appManager;
$this->client = $client; $this->client = $client;
$this->urlGenerator = $urlGenerator;
} }
protected function run($argument) { protected function run($argument) {

View file

@ -45,18 +45,15 @@ class BackgroundJobTest extends TestCase {
protected $appManager; protected $appManager;
/** @var IClientService|\PHPUnit_Framework_MockObject_MockObject */ /** @var IClientService|\PHPUnit_Framework_MockObject_MockObject */
protected $client; protected $client;
/** @var IURLGenerator|\PHPUnit_Framework_MockObject_MockObject */
protected $urlGenerator;
public function setUp() { public function setUp() {
parent::setUp(); parent::setUp();
$this->config = $this->getMockBuilder('OCP\IConfig')->getMock(); $this->config = $this->createMock(\OCP\IConfig::class);
$this->notificationManager = $this->getMockBuilder('OCP\Notification\IManager')->getMock(); $this->notificationManager = $this->createMock(\OCP\Notification\IManager::class);
$this->groupManager = $this->getMockBuilder('OCP\IGroupManager')->getMock(); $this->groupManager = $this->createMock(\OCP\IGroupManager::class);
$this->appManager = $this->getMockBuilder('OCP\App\IAppManager')->getMock(); $this->appManager = $this->createMock(\OCP\App\IAppManager::class);
$this->client = $this->getMockBuilder('OCP\Http\Client\IClientService')->getMock(); $this->client = $this->createMock(\OCP\Http\Client\IClientService::class);
$this->urlGenerator = $this->getMockBuilder('OCP\IURLGenerator')->getMock();
} }
/** /**
@ -70,8 +67,7 @@ class BackgroundJobTest extends TestCase {
$this->notificationManager, $this->notificationManager,
$this->groupManager, $this->groupManager,
$this->appManager, $this->appManager,
$this->client, $this->client
$this->urlGenerator
); );
} { } {
return $this->getMockBuilder('OCA\UpdateNotification\Notification\BackgroundJob') return $this->getMockBuilder('OCA\UpdateNotification\Notification\BackgroundJob')
@ -81,7 +77,6 @@ class BackgroundJobTest extends TestCase {
$this->groupManager, $this->groupManager,
$this->appManager, $this->appManager,
$this->client, $this->client,
$this->urlGenerator,
]) ])
->setMethods($methods) ->setMethods($methods)
->getMock(); ->getMock();
@ -160,20 +155,12 @@ class BackgroundJobTest extends TestCase {
} }
if ($notification === null) { if ($notification === null) {
$this->urlGenerator->expects($this->never())
->method('linkToRouteAbsolute');
$job->expects($this->never()) $job->expects($this->never())
->method('createNotifications'); ->method('createNotifications');
} else { } else {
$this->urlGenerator->expects($this->once())
->method('linkToRouteAbsolute')
->with('settings.AdminSettings.index')
->willReturn('admin-url');
$job->expects($this->once()) $job->expects($this->once())
->method('createNotifications') ->method('createNotifications')
->willReturn('core', $notification, 'admin-url#updater', $readableVersion); ->willReturn('core', $notification, $readableVersion);
} }
$this->invokePrivate($job, 'checkCoreUpdate'); $this->invokePrivate($job, 'checkCoreUpdate');
@ -188,7 +175,7 @@ class BackgroundJobTest extends TestCase {
['app2', '1.9.2'], ['app2', '1.9.2'],
], ],
[ [
['app2', '1.9.2', 'apps-url#app-app2'], ['app2', '1.9.2'],
], ],
], ],
]; ];
@ -215,11 +202,6 @@ class BackgroundJobTest extends TestCase {
->method('isUpdateAvailable') ->method('isUpdateAvailable')
->willReturnMap($isUpdateAvailable); ->willReturnMap($isUpdateAvailable);
$this->urlGenerator->expects($this->exactly(sizeof($notifications)))
->method('linkToRouteAbsolute')
->with('settings.AppSettings.viewApps')
->willReturn('apps-url');
$mockedMethod = $job->expects($this->exactly(sizeof($notifications))) $mockedMethod = $job->expects($this->exactly(sizeof($notifications)))
->method('createNotifications'); ->method('createNotifications');
call_user_func_array([$mockedMethod, 'withConsecutive'], $notifications); call_user_func_array([$mockedMethod, 'withConsecutive'], $notifications);
@ -229,9 +211,9 @@ class BackgroundJobTest extends TestCase {
public function dataCreateNotifications() { public function dataCreateNotifications() {
return [ return [
['app1', '1.0.0', 'link1', '1.0.0', false, false, null, null], ['app1', '1.0.0', '1.0.0', false, false, null, null],
['app2', '1.0.1', 'link2', '1.0.0', '1.0.0', true, ['user1'], [['user1']]], ['app2', '1.0.1', '1.0.0', '1.0.0', true, ['user1'], [['user1']]],
['app3', '1.0.1', 'link3', false, false, true, ['user2', 'user3'], [['user2'], ['user3']]], ['app3', '1.0.1', false, false, true, ['user2', 'user3'], [['user2'], ['user3']]],
]; ];
} }
@ -240,14 +222,13 @@ class BackgroundJobTest extends TestCase {
* *
* @param string $app * @param string $app
* @param string $version * @param string $version
* @param string $url
* @param string|false $lastNotification * @param string|false $lastNotification
* @param string|false $callDelete * @param string|false $callDelete
* @param bool $createNotification * @param bool $createNotification
* @param string[]|null $users * @param string[]|null $users
* @param array|null $userNotifications * @param array|null $userNotifications
*/ */
public function testCreateNotifications($app, $version, $url, $lastNotification, $callDelete, $createNotification, $users, $userNotifications) { public function testCreateNotifications($app, $version, $lastNotification, $callDelete, $createNotification, $users, $userNotifications) {
$job = $this->getJob([ $job = $this->getJob([
'deleteOutdatedNotifications', 'deleteOutdatedNotifications',
'getUsersToNotify', 'getUsersToNotify',
@ -299,10 +280,6 @@ class BackgroundJobTest extends TestCase {
->method('setSubject') ->method('setSubject')
->with('update_available') ->with('update_available')
->willReturnSelf(); ->willReturnSelf();
$notification->expects($this->once())
->method('setLink')
->with($url)
->willReturnSelf();
if ($userNotifications !== null) { if ($userNotifications !== null) {
$mockedMethod = $notification->expects($this->exactly(sizeof($userNotifications))) $mockedMethod = $notification->expects($this->exactly(sizeof($userNotifications)))
@ -323,7 +300,7 @@ class BackgroundJobTest extends TestCase {
->method('createNotification'); ->method('createNotification');
} }
$this->invokePrivate($job, 'createNotifications', [$app, $version, $url]); $this->invokePrivate($job, 'createNotifications', [$app, $version]);
} }
public function dataGetUsersToNotify() { public function dataGetUsersToNotify() {

View file

@ -24,7 +24,9 @@ namespace OCA\UpdateNotification\Tests\Notification;
use OCA\UpdateNotification\Notification\Notifier; use OCA\UpdateNotification\Notification\Notifier;
use OCP\IGroupManager;
use OCP\IURLGenerator; use OCP\IURLGenerator;
use OCP\IUserSession;
use OCP\L10N\IFactory; use OCP\L10N\IFactory;
use OCP\Notification\IManager; use OCP\Notification\IManager;
use OCP\Notification\INotification; use OCP\Notification\INotification;
@ -38,6 +40,10 @@ class NotifierTest extends TestCase {
protected $notificationManager; protected $notificationManager;
/** @var IFactory|\PHPUnit_Framework_MockObject_MockObject */ /** @var IFactory|\PHPUnit_Framework_MockObject_MockObject */
protected $l10nFactory; protected $l10nFactory;
/** @var IUserSession|\PHPUnit_Framework_MockObject_MockObject */
protected $userSession;
/** @var IGroupManager|\PHPUnit_Framework_MockObject_MockObject */
protected $groupManager;
public function setUp() { public function setUp() {
parent::setUp(); parent::setUp();
@ -45,6 +51,8 @@ class NotifierTest extends TestCase {
$this->urlGenerator = $this->createMock(IURLGenerator::class); $this->urlGenerator = $this->createMock(IURLGenerator::class);
$this->notificationManager = $this->createMock(IManager::class); $this->notificationManager = $this->createMock(IManager::class);
$this->l10nFactory = $this->createMock(IFactory::class); $this->l10nFactory = $this->createMock(IFactory::class);
$this->userSession = $this->createMock(IUserSession::class);
$this->groupManager = $this->createMock(IGroupManager::class);
} }
/** /**
@ -56,7 +64,9 @@ class NotifierTest extends TestCase {
return new Notifier( return new Notifier(
$this->urlGenerator, $this->urlGenerator,
$this->notificationManager, $this->notificationManager,
$this->l10nFactory $this->l10nFactory,
$this->userSession,
$this->groupManager
); );
} { } {
return $this->getMockBuilder(Notifier::class) return $this->getMockBuilder(Notifier::class)
@ -64,6 +74,8 @@ class NotifierTest extends TestCase {
$this->urlGenerator, $this->urlGenerator,
$this->notificationManager, $this->notificationManager,
$this->l10nFactory, $this->l10nFactory,
$this->userSession,
$this->groupManager,
]) ])
->setMethods($methods) ->setMethods($methods)
->getMock(); ->getMock();