2015-09-03 16:24:33 +03:00
|
|
|
<?php
|
|
|
|
/**
|
|
|
|
* @author Joas Schilling <nickvergessen@owncloud.com>
|
|
|
|
*
|
|
|
|
* @copyright Copyright (c) 2015, ownCloud, Inc.
|
|
|
|
* @license AGPL-3.0
|
|
|
|
*
|
|
|
|
* This code is free software: you can redistribute it and/or modify
|
|
|
|
* it under the terms of the GNU Affero General Public License, version 3,
|
|
|
|
* as published by the Free Software Foundation.
|
|
|
|
*
|
|
|
|
* This program is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
* GNU Affero General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU Affero General Public License, version 3,
|
|
|
|
* along with this program. If not, see <http://www.gnu.org/licenses/>
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
|
|
|
|
namespace Test\Notification;
|
|
|
|
|
|
|
|
use OC\Notification\Manager;
|
2019-07-16 12:36:32 +03:00
|
|
|
use OCP\ILogger;
|
2017-10-24 16:26:53 +03:00
|
|
|
use OCP\Notification\IApp;
|
2016-01-14 16:35:24 +03:00
|
|
|
use OCP\Notification\IManager;
|
2017-10-24 16:26:53 +03:00
|
|
|
use OCP\Notification\INotification;
|
|
|
|
use OCP\Notification\INotifier;
|
2016-11-08 17:56:39 +03:00
|
|
|
use OCP\RichObjectStrings\IValidator;
|
2019-07-16 12:36:32 +03:00
|
|
|
use PHPUnit\Framework\MockObject\MockObject;
|
2015-09-03 16:24:33 +03:00
|
|
|
use Test\TestCase;
|
|
|
|
|
|
|
|
class ManagerTest extends TestCase {
|
|
|
|
/** @var IManager */
|
|
|
|
protected $manager;
|
|
|
|
|
2019-07-16 12:36:32 +03:00
|
|
|
/** @var IValidator|MockObject */
|
|
|
|
protected $validator;
|
|
|
|
/** @var ILogger|MockObject */
|
|
|
|
protected $logger;
|
|
|
|
|
2019-11-27 17:27:18 +03:00
|
|
|
protected function setUp(): void {
|
2015-09-03 16:24:33 +03:00
|
|
|
parent::setUp();
|
2019-07-16 12:36:32 +03:00
|
|
|
$this->validator = $this->createMock(IValidator::class);
|
|
|
|
$this->logger = $this->createMock(ILogger::class);
|
|
|
|
$this->manager = new Manager($this->validator, $this->logger);
|
2015-09-03 16:24:33 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
public function testRegisterApp() {
|
|
|
|
|
2019-07-16 12:36:32 +03:00
|
|
|
$this->assertEquals([], self::invokePrivate($this->manager, 'getApps'));
|
2015-09-03 16:24:33 +03:00
|
|
|
|
2019-07-16 12:36:32 +03:00
|
|
|
$this->manager->registerApp(DummyApp::class);
|
2015-09-03 16:24:33 +03:00
|
|
|
|
2019-07-16 12:36:32 +03:00
|
|
|
$this->assertCount(1, self::invokePrivate($this->manager, 'getApps'));
|
|
|
|
$this->assertCount(1, self::invokePrivate($this->manager, 'getApps'));
|
2015-09-03 16:24:33 +03:00
|
|
|
|
2019-07-16 12:36:32 +03:00
|
|
|
$this->manager->registerApp(DummyApp::class);
|
2015-09-03 16:24:33 +03:00
|
|
|
|
2019-07-16 12:36:32 +03:00
|
|
|
$this->assertCount(2, self::invokePrivate($this->manager, 'getApps'));
|
2015-09-03 16:24:33 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
public function testRegisterAppInvalid() {
|
2019-07-16 12:36:32 +03:00
|
|
|
$this->manager->registerApp(DummyNotifier::class);
|
2015-09-03 16:24:33 +03:00
|
|
|
|
2019-07-16 12:36:32 +03:00
|
|
|
$this->logger->expects($this->once())
|
|
|
|
->method('error');
|
|
|
|
self::invokePrivate($this->manager, 'getApps');
|
2015-09-03 16:24:33 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
public function testRegisterNotifier() {
|
2019-07-16 12:36:32 +03:00
|
|
|
$this->assertEquals([], self::invokePrivate($this->manager, 'getNotifiers'));
|
2015-09-03 16:24:33 +03:00
|
|
|
|
2019-07-16 12:36:32 +03:00
|
|
|
$this->manager->registerNotifierService(DummyNotifier::class);
|
2015-09-03 16:24:33 +03:00
|
|
|
|
2019-07-16 12:36:32 +03:00
|
|
|
$this->assertCount(1, self::invokePrivate($this->manager, 'getNotifiers'));
|
|
|
|
$this->assertCount(1, self::invokePrivate($this->manager, 'getNotifiers'));
|
2015-09-03 16:24:33 +03:00
|
|
|
|
2019-07-16 12:36:32 +03:00
|
|
|
$this->manager->registerNotifierService(DummyNotifier::class);
|
2015-09-03 16:24:33 +03:00
|
|
|
|
2019-07-16 12:36:32 +03:00
|
|
|
$this->assertCount(2, self::invokePrivate($this->manager, 'getNotifiers'));
|
2015-09-03 16:24:33 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
public function testRegisterNotifierInvalid() {
|
2019-07-16 12:36:32 +03:00
|
|
|
$this->manager->registerNotifierService(DummyApp::class);
|
2016-01-21 11:49:37 +03:00
|
|
|
|
2019-07-16 12:36:32 +03:00
|
|
|
$this->logger->expects($this->once())
|
|
|
|
->method('error');
|
|
|
|
self::invokePrivate($this->manager, 'getNotifiers');
|
2016-01-21 11:49:37 +03:00
|
|
|
}
|
|
|
|
|
2015-09-03 16:24:33 +03:00
|
|
|
public function testCreateNotification() {
|
|
|
|
$action = $this->manager->createNotification();
|
2016-01-14 16:35:24 +03:00
|
|
|
$this->assertInstanceOf('OCP\Notification\INotification', $action);
|
2015-09-03 16:24:33 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
public function testNotify() {
|
2016-01-14 16:35:24 +03:00
|
|
|
/** @var \OCP\Notification\INotification|\PHPUnit_Framework_MockObject_MockObject $notification */
|
2017-10-24 16:26:53 +03:00
|
|
|
$notification = $this->getMockBuilder(INotification::class)
|
2015-09-03 16:24:33 +03:00
|
|
|
->disableOriginalConstructor()
|
|
|
|
->getMock();
|
|
|
|
$notification->expects($this->once())
|
|
|
|
->method('isValid')
|
|
|
|
->willReturn(true);
|
|
|
|
|
2019-07-16 12:36:32 +03:00
|
|
|
$manager = $this->getMockBuilder(Manager::class)
|
|
|
|
->setConstructorArgs([
|
|
|
|
$this->validator,
|
|
|
|
$this->logger,
|
|
|
|
])
|
|
|
|
->setMethods(['getApps'])
|
2015-09-03 16:24:33 +03:00
|
|
|
->getMock();
|
|
|
|
|
2019-07-16 12:36:32 +03:00
|
|
|
$manager->expects($this->once())
|
|
|
|
->method('getApps')
|
|
|
|
->willReturn([]);
|
|
|
|
|
|
|
|
$manager->notify($notification);
|
2015-09-03 16:24:33 +03:00
|
|
|
}
|
|
|
|
|
2019-11-27 17:27:18 +03:00
|
|
|
|
2015-09-03 16:24:33 +03:00
|
|
|
public function testNotifyInvalid() {
|
2019-11-27 17:27:18 +03:00
|
|
|
$this->expectException(\InvalidArgumentException::class);
|
|
|
|
|
2016-01-14 16:35:24 +03:00
|
|
|
/** @var \OCP\Notification\INotification|\PHPUnit_Framework_MockObject_MockObject $notification */
|
2017-10-24 16:26:53 +03:00
|
|
|
$notification = $this->getMockBuilder(INotification::class)
|
2015-09-03 16:24:33 +03:00
|
|
|
->disableOriginalConstructor()
|
|
|
|
->getMock();
|
|
|
|
$notification->expects($this->once())
|
|
|
|
->method('isValid')
|
|
|
|
->willReturn(false);
|
|
|
|
|
2019-07-16 12:36:32 +03:00
|
|
|
$manager = $this->getMockBuilder(Manager::class)
|
|
|
|
->setConstructorArgs([
|
|
|
|
$this->validator,
|
|
|
|
$this->logger,
|
|
|
|
])
|
|
|
|
->setMethods(['getApps'])
|
2015-09-03 16:24:33 +03:00
|
|
|
->getMock();
|
|
|
|
|
2019-07-16 12:36:32 +03:00
|
|
|
$manager->expects($this->never())
|
|
|
|
->method('getApps');
|
2015-09-03 16:24:33 +03:00
|
|
|
|
2019-07-16 12:36:32 +03:00
|
|
|
$manager->notify($notification);
|
2015-09-03 16:24:33 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
public function testMarkProcessed() {
|
2016-01-14 16:35:24 +03:00
|
|
|
/** @var \OCP\Notification\INotification|\PHPUnit_Framework_MockObject_MockObject $notification */
|
2017-10-24 16:26:53 +03:00
|
|
|
$notification = $this->getMockBuilder(INotification::class)
|
2015-09-03 16:24:33 +03:00
|
|
|
->disableOriginalConstructor()
|
|
|
|
->getMock();
|
|
|
|
|
2019-07-16 12:36:32 +03:00
|
|
|
$manager = $this->getMockBuilder(Manager::class)
|
|
|
|
->setConstructorArgs([
|
|
|
|
$this->validator,
|
|
|
|
$this->logger,
|
|
|
|
])
|
|
|
|
->setMethods(['getApps'])
|
2015-09-03 16:24:33 +03:00
|
|
|
->getMock();
|
|
|
|
|
2019-07-16 12:36:32 +03:00
|
|
|
$manager->expects($this->once())
|
|
|
|
->method('getApps')
|
|
|
|
->willReturn([]);
|
|
|
|
|
|
|
|
$manager->markProcessed($notification);
|
2015-09-03 16:24:33 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
public function testGetCount() {
|
2016-01-14 16:35:24 +03:00
|
|
|
/** @var \OCP\Notification\INotification|\PHPUnit_Framework_MockObject_MockObject $notification */
|
2017-10-24 16:26:53 +03:00
|
|
|
$notification = $this->getMockBuilder(INotification::class)
|
2015-09-03 16:24:33 +03:00
|
|
|
->disableOriginalConstructor()
|
|
|
|
->getMock();
|
|
|
|
|
2019-07-16 12:36:32 +03:00
|
|
|
$manager = $this->getMockBuilder(Manager::class)
|
|
|
|
->setConstructorArgs([
|
|
|
|
$this->validator,
|
|
|
|
$this->logger,
|
|
|
|
])
|
|
|
|
->setMethods(['getApps'])
|
2015-09-03 16:24:33 +03:00
|
|
|
->getMock();
|
|
|
|
|
2019-07-16 12:36:32 +03:00
|
|
|
$manager->expects($this->once())
|
|
|
|
->method('getApps')
|
|
|
|
->willReturn([]);
|
|
|
|
|
|
|
|
$manager->getCount($notification);
|
2015-09-03 16:24:33 +03:00
|
|
|
}
|
|
|
|
}
|