2015-09-03 13:24:33 +00: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;
|
2016-01-14 13:35:24 +00:00
|
|
|
use OCP\Notification\IManager;
|
2015-09-03 13:24:33 +00:00
|
|
|
use Test\TestCase;
|
|
|
|
|
|
|
|
class ManagerTest extends TestCase {
|
|
|
|
/** @var IManager */
|
|
|
|
protected $manager;
|
|
|
|
|
|
|
|
public function setUp() {
|
|
|
|
parent::setUp();
|
|
|
|
$this->manager = new Manager();
|
|
|
|
}
|
|
|
|
|
|
|
|
public function testRegisterApp() {
|
2016-01-14 13:35:24 +00:00
|
|
|
$app = $this->getMockBuilder('OCP\Notification\IApp')
|
2015-09-03 13:24:33 +00:00
|
|
|
->disableOriginalConstructor()
|
|
|
|
->getMock();
|
|
|
|
|
|
|
|
$closure = function() use ($app) {
|
|
|
|
return $app;
|
|
|
|
};
|
|
|
|
|
|
|
|
$this->assertEquals([], $this->invokePrivate($this->manager, 'getApps'));
|
|
|
|
|
|
|
|
$this->manager->registerApp($closure);
|
|
|
|
|
|
|
|
$this->assertEquals([$app], $this->invokePrivate($this->manager, 'getApps'));
|
|
|
|
$this->assertEquals([$app], $this->invokePrivate($this->manager, 'getApps'));
|
|
|
|
|
|
|
|
$this->manager->registerApp($closure);
|
|
|
|
|
|
|
|
$this->assertEquals([$app, $app], $this->invokePrivate($this->manager, 'getApps'));
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @expectedException \InvalidArgumentException
|
|
|
|
*/
|
|
|
|
public function testRegisterAppInvalid() {
|
2016-01-14 13:35:24 +00:00
|
|
|
$notifier = $this->getMockBuilder('OCP\Notification\INotifier')
|
2015-09-03 13:24:33 +00:00
|
|
|
->disableOriginalConstructor()
|
|
|
|
->getMock();
|
|
|
|
|
|
|
|
$closure = function() use ($notifier) {
|
|
|
|
return $notifier;
|
|
|
|
};
|
|
|
|
|
|
|
|
$this->manager->registerApp($closure);
|
|
|
|
|
|
|
|
$this->invokePrivate($this->manager, 'getApps');
|
|
|
|
}
|
|
|
|
|
|
|
|
public function testRegisterNotifier() {
|
2016-01-14 13:35:24 +00:00
|
|
|
$notifier = $this->getMockBuilder('OCP\Notification\INotifier')
|
2015-09-03 13:24:33 +00:00
|
|
|
->disableOriginalConstructor()
|
|
|
|
->getMock();
|
|
|
|
|
|
|
|
$closure = function() use ($notifier) {
|
|
|
|
return $notifier;
|
|
|
|
};
|
|
|
|
|
|
|
|
$this->assertEquals([], $this->invokePrivate($this->manager, 'getNotifiers'));
|
2016-01-21 08:49:37 +00:00
|
|
|
$this->assertEquals([], $this->invokePrivate($this->manager, 'listNotifiers'));
|
2015-09-03 13:24:33 +00:00
|
|
|
|
2016-01-21 08:49:37 +00:00
|
|
|
$this->manager->registerNotifier($closure, function() {
|
|
|
|
return ['id' => 'test1', 'name' => 'Test One'];
|
|
|
|
});
|
2015-09-03 13:24:33 +00:00
|
|
|
|
|
|
|
$this->assertEquals([$notifier], $this->invokePrivate($this->manager, 'getNotifiers'));
|
2016-01-21 08:49:37 +00:00
|
|
|
$this->assertEquals(['test1' => 'Test One'], $this->invokePrivate($this->manager, 'listNotifiers'));
|
2015-09-03 13:24:33 +00:00
|
|
|
$this->assertEquals([$notifier], $this->invokePrivate($this->manager, 'getNotifiers'));
|
2016-01-21 08:49:37 +00:00
|
|
|
$this->assertEquals(['test1' => 'Test One'], $this->invokePrivate($this->manager, 'listNotifiers'));
|
2015-09-03 13:24:33 +00:00
|
|
|
|
2016-01-21 08:49:37 +00:00
|
|
|
$this->manager->registerNotifier($closure, function() {
|
|
|
|
return ['id' => 'test2', 'name' => 'Test Two'];
|
|
|
|
});
|
2015-09-03 13:24:33 +00:00
|
|
|
|
|
|
|
$this->assertEquals([$notifier, $notifier], $this->invokePrivate($this->manager, 'getNotifiers'));
|
2016-01-21 08:49:37 +00:00
|
|
|
$this->assertEquals(['test1' => 'Test One', 'test2' => 'Test Two'], $this->invokePrivate($this->manager, 'listNotifiers'));
|
2015-09-03 13:24:33 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @expectedException \InvalidArgumentException
|
|
|
|
*/
|
|
|
|
public function testRegisterNotifierInvalid() {
|
2016-01-14 13:35:24 +00:00
|
|
|
$app = $this->getMockBuilder('OCP\Notification\IApp')
|
2015-09-03 13:24:33 +00:00
|
|
|
->disableOriginalConstructor()
|
|
|
|
->getMock();
|
|
|
|
|
|
|
|
$closure = function() use ($app) {
|
|
|
|
return $app;
|
|
|
|
};
|
|
|
|
|
2016-01-21 08:49:37 +00:00
|
|
|
$this->manager->registerNotifier($closure, function() {
|
|
|
|
return ['id' => 'test1', 'name' => 'Test One'];
|
|
|
|
});
|
2015-09-03 13:24:33 +00:00
|
|
|
|
|
|
|
$this->invokePrivate($this->manager, 'getNotifiers');
|
|
|
|
}
|
|
|
|
|
2016-01-21 08:49:37 +00:00
|
|
|
public function dataRegisterNotifierInfoInvalid() {
|
|
|
|
return [
|
|
|
|
[null],
|
|
|
|
['No array'],
|
|
|
|
[['id' => 'test1', 'name' => 'Test One', 'size' => 'Invalid']],
|
|
|
|
[['no-id' => 'test1', 'name' => 'Test One']],
|
|
|
|
[['id' => 'test1', 'no-name' => 'Test One']],
|
|
|
|
];
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @dataProvider dataRegisterNotifierInfoInvalid
|
|
|
|
* @expectedException \InvalidArgumentException
|
|
|
|
* @param mixed $data
|
|
|
|
*/
|
|
|
|
public function testRegisterNotifierInfoInvalid($data) {
|
2016-01-14 13:35:24 +00:00
|
|
|
$app = $this->getMockBuilder('OCP\Notification\IApp')
|
2016-01-21 08:49:37 +00:00
|
|
|
->disableOriginalConstructor()
|
|
|
|
->getMock();
|
|
|
|
|
|
|
|
$closure = function() use ($app) {
|
|
|
|
return $app;
|
|
|
|
};
|
|
|
|
|
|
|
|
$this->manager->registerNotifier($closure, function() use ($data) {
|
|
|
|
return $data;
|
|
|
|
});
|
|
|
|
|
|
|
|
$this->manager->listNotifiers();
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @expectedException \InvalidArgumentException
|
|
|
|
* @expectedExceptionMessage The given notifier ID test1 is already in use
|
|
|
|
*/
|
|
|
|
public function testRegisterNotifierInfoDuplicate() {
|
2016-01-14 13:35:24 +00:00
|
|
|
$app = $this->getMockBuilder('OCP\Notification\IApp')
|
2016-01-21 08:49:37 +00:00
|
|
|
->disableOriginalConstructor()
|
|
|
|
->getMock();
|
|
|
|
|
|
|
|
$closure = function() use ($app) {
|
|
|
|
return $app;
|
|
|
|
};
|
|
|
|
|
|
|
|
$this->manager->registerNotifier($closure, function() {
|
|
|
|
return ['id' => 'test1', 'name' => 'Test One'];
|
|
|
|
});
|
|
|
|
|
|
|
|
$this->manager->registerNotifier($closure, function() {
|
|
|
|
return ['id' => 'test1', 'name' => 'Test One'];
|
|
|
|
});
|
|
|
|
|
|
|
|
$this->manager->listNotifiers();
|
|
|
|
}
|
|
|
|
|
2015-09-03 13:24:33 +00:00
|
|
|
public function testCreateNotification() {
|
|
|
|
$action = $this->manager->createNotification();
|
2016-01-14 13:35:24 +00:00
|
|
|
$this->assertInstanceOf('OCP\Notification\INotification', $action);
|
2015-09-03 13:24:33 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
public function testNotify() {
|
2016-01-14 13:35:24 +00:00
|
|
|
/** @var \OCP\Notification\INotification|\PHPUnit_Framework_MockObject_MockObject $notification */
|
|
|
|
$notification = $this->getMockBuilder('OCP\Notification\INotification')
|
2015-09-03 13:24:33 +00:00
|
|
|
->disableOriginalConstructor()
|
|
|
|
->getMock();
|
|
|
|
$notification->expects($this->once())
|
|
|
|
->method('isValid')
|
|
|
|
->willReturn(true);
|
|
|
|
|
2016-01-14 13:35:24 +00:00
|
|
|
/** @var \OCP\Notification\IApp|\PHPUnit_Framework_MockObject_MockObject $app */
|
|
|
|
$app = $this->getMockBuilder('OCP\Notification\IApp')
|
2015-09-03 13:24:33 +00:00
|
|
|
->disableOriginalConstructor()
|
|
|
|
->getMock();
|
|
|
|
$app->expects($this->once())
|
|
|
|
->method('notify')
|
|
|
|
->with($notification);
|
|
|
|
|
2016-01-14 13:35:24 +00:00
|
|
|
/** @var \OCP\Notification\IApp|\PHPUnit_Framework_MockObject_MockObject $app2 */
|
|
|
|
$app2 = $this->getMockBuilder('OCP\Notification\IApp')
|
2015-09-03 13:24:33 +00:00
|
|
|
->disableOriginalConstructor()
|
|
|
|
->getMock();
|
|
|
|
$app2->expects($this->once())
|
|
|
|
->method('notify')
|
|
|
|
->with($notification);
|
|
|
|
|
|
|
|
$this->manager->registerApp(function() use ($app) {
|
|
|
|
return $app;
|
|
|
|
});
|
|
|
|
$this->manager->registerApp(function() use ($app2) {
|
|
|
|
return $app2;
|
|
|
|
});
|
|
|
|
|
|
|
|
$this->manager->notify($notification);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @expectedException \InvalidArgumentException
|
|
|
|
*/
|
|
|
|
public function testNotifyInvalid() {
|
2016-01-14 13:35:24 +00:00
|
|
|
/** @var \OCP\Notification\INotification|\PHPUnit_Framework_MockObject_MockObject $notification */
|
|
|
|
$notification = $this->getMockBuilder('OCP\Notification\INotification')
|
2015-09-03 13:24:33 +00:00
|
|
|
->disableOriginalConstructor()
|
|
|
|
->getMock();
|
|
|
|
$notification->expects($this->once())
|
|
|
|
->method('isValid')
|
|
|
|
->willReturn(false);
|
|
|
|
|
|
|
|
$this->manager->notify($notification);
|
|
|
|
}
|
|
|
|
|
|
|
|
public function testPrepare() {
|
2016-01-14 13:35:24 +00:00
|
|
|
/** @var \OCP\Notification\INotification|\PHPUnit_Framework_MockObject_MockObject $notification */
|
|
|
|
$notification = $this->getMockBuilder('OCP\Notification\INotification')
|
2015-09-03 13:24:33 +00:00
|
|
|
->disableOriginalConstructor()
|
|
|
|
->getMock();
|
|
|
|
$notification->expects($this->once())
|
|
|
|
->method('isValidParsed')
|
|
|
|
->willReturn(true);
|
2016-01-14 13:35:24 +00:00
|
|
|
/** @var \OCP\Notification\INotification|\PHPUnit_Framework_MockObject_MockObject $notification2 */
|
|
|
|
$notification2 = $this->getMockBuilder('OCP\Notification\INotification')
|
2015-09-03 13:24:33 +00:00
|
|
|
->disableOriginalConstructor()
|
|
|
|
->getMock();
|
|
|
|
$notification2->expects($this->exactly(2))
|
|
|
|
->method('isValidParsed')
|
|
|
|
->willReturn(true);
|
|
|
|
|
2016-01-14 13:35:24 +00:00
|
|
|
/** @var \OCP\Notification\IApp|\PHPUnit_Framework_MockObject_MockObject $notifier */
|
|
|
|
$notifier = $this->getMockBuilder('OCP\Notification\INotifier')
|
2015-09-03 13:24:33 +00:00
|
|
|
->disableOriginalConstructor()
|
|
|
|
->getMock();
|
|
|
|
$notifier->expects($this->once())
|
|
|
|
->method('prepare')
|
|
|
|
->with($notification, 'en')
|
|
|
|
->willReturnArgument(0);
|
|
|
|
|
2016-01-14 13:35:24 +00:00
|
|
|
/** @var \OCP\Notification\IApp|\PHPUnit_Framework_MockObject_MockObject $notifier2 */
|
|
|
|
$notifier2 = $this->getMockBuilder('OCP\Notification\INotifier')
|
2015-09-03 13:24:33 +00:00
|
|
|
->disableOriginalConstructor()
|
|
|
|
->getMock();
|
|
|
|
$notifier2->expects($this->once())
|
|
|
|
->method('prepare')
|
|
|
|
->with($notification, 'en')
|
|
|
|
->willReturn($notification2);
|
|
|
|
|
|
|
|
$this->manager->registerNotifier(function() use ($notifier) {
|
|
|
|
return $notifier;
|
2016-01-21 08:49:37 +00:00
|
|
|
}, function() {
|
|
|
|
return ['id' => 'test1', 'name' => 'Test One'];
|
2015-09-03 13:24:33 +00:00
|
|
|
});
|
|
|
|
$this->manager->registerNotifier(function() use ($notifier2) {
|
|
|
|
return $notifier2;
|
2016-01-21 08:49:37 +00:00
|
|
|
}, function() {
|
|
|
|
return ['id' => 'test2', 'name' => 'Test Two'];
|
2015-09-03 13:24:33 +00:00
|
|
|
});
|
|
|
|
|
|
|
|
$this->assertEquals($notification2, $this->manager->prepare($notification, 'en'));
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @expectedException \InvalidArgumentException
|
|
|
|
*/
|
|
|
|
public function testPrepareInvalid() {
|
2016-01-14 13:35:24 +00:00
|
|
|
/** @var \OCP\Notification\INotification|\PHPUnit_Framework_MockObject_MockObject $notification */
|
|
|
|
$notification = $this->getMockBuilder('OCP\Notification\INotification')
|
2015-09-03 13:24:33 +00:00
|
|
|
->disableOriginalConstructor()
|
|
|
|
->getMock();
|
|
|
|
$notification->expects($this->once())
|
|
|
|
->method('isValidParsed')
|
|
|
|
->willReturn(false);
|
|
|
|
|
2016-01-14 13:35:24 +00:00
|
|
|
/** @var \OCP\Notification\IApp|\PHPUnit_Framework_MockObject_MockObject $notifier */
|
|
|
|
$notifier = $this->getMockBuilder('OCP\Notification\INotifier')
|
2015-09-03 13:24:33 +00:00
|
|
|
->disableOriginalConstructor()
|
|
|
|
->getMock();
|
|
|
|
$notifier->expects($this->once())
|
|
|
|
->method('prepare')
|
|
|
|
->with($notification, 'de')
|
|
|
|
->willReturnArgument(0);
|
|
|
|
|
|
|
|
$this->manager->registerNotifier(function() use ($notifier) {
|
|
|
|
return $notifier;
|
2016-01-21 08:49:37 +00:00
|
|
|
}, function() {
|
|
|
|
return ['id' => 'test1', 'name' => 'Test One'];
|
2015-09-03 13:24:33 +00:00
|
|
|
});
|
|
|
|
|
|
|
|
$this->manager->prepare($notification, 'de');
|
|
|
|
}
|
|
|
|
|
|
|
|
public function testPrepareNotifierThrows() {
|
2016-01-14 13:35:24 +00:00
|
|
|
/** @var \OCP\Notification\INotification|\PHPUnit_Framework_MockObject_MockObject $notification */
|
|
|
|
$notification = $this->getMockBuilder('OCP\Notification\INotification')
|
2015-09-03 13:24:33 +00:00
|
|
|
->disableOriginalConstructor()
|
|
|
|
->getMock();
|
|
|
|
$notification->expects($this->once())
|
|
|
|
->method('isValidParsed')
|
|
|
|
->willReturn(true);
|
|
|
|
|
2016-01-14 13:35:24 +00:00
|
|
|
/** @var \OCP\Notification\IApp|\PHPUnit_Framework_MockObject_MockObject $notifier */
|
|
|
|
$notifier = $this->getMockBuilder('OCP\Notification\INotifier')
|
2015-09-03 13:24:33 +00:00
|
|
|
->disableOriginalConstructor()
|
|
|
|
->getMock();
|
|
|
|
$notifier->expects($this->once())
|
|
|
|
->method('prepare')
|
|
|
|
->with($notification, 'de')
|
|
|
|
->willThrowException(new \InvalidArgumentException);
|
|
|
|
|
|
|
|
$this->manager->registerNotifier(function() use ($notifier) {
|
|
|
|
return $notifier;
|
2016-01-21 08:49:37 +00:00
|
|
|
}, function() {
|
|
|
|
return ['id' => 'test1', 'name' => 'Test One'];
|
2015-09-03 13:24:33 +00:00
|
|
|
});
|
|
|
|
|
|
|
|
$this->assertEquals($notification, $this->manager->prepare($notification, 'de'));
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @expectedException \InvalidArgumentException
|
|
|
|
*/
|
|
|
|
public function testPrepareNoNotifier() {
|
2016-01-14 13:35:24 +00:00
|
|
|
/** @var \OCP\Notification\INotification|\PHPUnit_Framework_MockObject_MockObject $notification */
|
|
|
|
$notification = $this->getMockBuilder('OCP\Notification\INotification')
|
2015-09-03 13:24:33 +00:00
|
|
|
->disableOriginalConstructor()
|
|
|
|
->getMock();
|
|
|
|
$notification->expects($this->once())
|
|
|
|
->method('isValidParsed')
|
|
|
|
->willReturn(false);
|
|
|
|
|
|
|
|
$this->manager->prepare($notification, 'en');
|
|
|
|
}
|
|
|
|
|
|
|
|
public function testMarkProcessed() {
|
2016-01-14 13:35:24 +00:00
|
|
|
/** @var \OCP\Notification\INotification|\PHPUnit_Framework_MockObject_MockObject $notification */
|
|
|
|
$notification = $this->getMockBuilder('OCP\Notification\INotification')
|
2015-09-03 13:24:33 +00:00
|
|
|
->disableOriginalConstructor()
|
|
|
|
->getMock();
|
|
|
|
|
2016-01-14 13:35:24 +00:00
|
|
|
/** @var \OCP\Notification\IApp|\PHPUnit_Framework_MockObject_MockObject $app */
|
|
|
|
$app = $this->getMockBuilder('OCP\Notification\IApp')
|
2015-09-03 13:24:33 +00:00
|
|
|
->disableOriginalConstructor()
|
|
|
|
->getMock();
|
|
|
|
$app->expects($this->once())
|
|
|
|
->method('markProcessed')
|
|
|
|
->with($notification);
|
|
|
|
|
2016-01-14 13:35:24 +00:00
|
|
|
/** @var \OCP\Notification\IApp|\PHPUnit_Framework_MockObject_MockObject $app2 */
|
|
|
|
$app2 = $this->getMockBuilder('OCP\Notification\IApp')
|
2015-09-03 13:24:33 +00:00
|
|
|
->disableOriginalConstructor()
|
|
|
|
->getMock();
|
|
|
|
$app2->expects($this->once())
|
|
|
|
->method('markProcessed')
|
|
|
|
->with($notification);
|
|
|
|
|
|
|
|
$this->manager->registerApp(function() use ($app) {
|
|
|
|
return $app;
|
|
|
|
});
|
|
|
|
$this->manager->registerApp(function() use ($app2) {
|
|
|
|
return $app2;
|
|
|
|
});
|
|
|
|
|
|
|
|
$this->manager->markProcessed($notification);
|
|
|
|
}
|
|
|
|
|
|
|
|
public function testGetCount() {
|
2016-01-14 13:35:24 +00:00
|
|
|
/** @var \OCP\Notification\INotification|\PHPUnit_Framework_MockObject_MockObject $notification */
|
|
|
|
$notification = $this->getMockBuilder('OCP\Notification\INotification')
|
2015-09-03 13:24:33 +00:00
|
|
|
->disableOriginalConstructor()
|
|
|
|
->getMock();
|
|
|
|
|
2016-01-14 13:35:24 +00:00
|
|
|
/** @var \OCP\Notification\IApp|\PHPUnit_Framework_MockObject_MockObject $app */
|
|
|
|
$app = $this->getMockBuilder('OCP\Notification\IApp')
|
2015-09-03 13:24:33 +00:00
|
|
|
->disableOriginalConstructor()
|
|
|
|
->getMock();
|
|
|
|
$app->expects($this->once())
|
|
|
|
->method('getCount')
|
|
|
|
->with($notification)
|
|
|
|
->willReturn(21);
|
|
|
|
|
2016-01-14 13:35:24 +00:00
|
|
|
/** @var \OCP\Notification\IApp|\PHPUnit_Framework_MockObject_MockObject $app2 */
|
|
|
|
$app2 = $this->getMockBuilder('OCP\Notification\IApp')
|
2015-09-03 13:24:33 +00:00
|
|
|
->disableOriginalConstructor()
|
|
|
|
->getMock();
|
|
|
|
$app2->expects($this->once())
|
|
|
|
->method('getCount')
|
|
|
|
->with($notification)
|
|
|
|
->willReturn(42);
|
|
|
|
|
|
|
|
$this->manager->registerApp(function() use ($app) {
|
|
|
|
return $app;
|
|
|
|
});
|
|
|
|
$this->manager->registerApp(function() use ($app2) {
|
|
|
|
return $app2;
|
|
|
|
});
|
|
|
|
|
|
|
|
$this->assertSame(63, $this->manager->getCount($notification));
|
|
|
|
}
|
|
|
|
}
|