ManagerTest.php 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421
  1. <?php
  2. /**
  3. * @author Joas Schilling <nickvergessen@owncloud.com>
  4. *
  5. * @copyright Copyright (c) 2015, ownCloud, Inc.
  6. * @license AGPL-3.0
  7. *
  8. * This code is free software: you can redistribute it and/or modify
  9. * it under the terms of the GNU Affero General Public License, version 3,
  10. * as published by the Free Software Foundation.
  11. *
  12. * This program is distributed in the hope that it will be useful,
  13. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  15. * GNU Affero General Public License for more details.
  16. *
  17. * You should have received a copy of the GNU Affero General Public License, version 3,
  18. * along with this program. If not, see <http://www.gnu.org/licenses/>
  19. *
  20. */
  21. namespace Test\Notification;
  22. use OC\Notification\Manager;
  23. use OCP\Notification\IManager;
  24. use OCP\RichObjectStrings\IValidator;
  25. use Test\TestCase;
  26. class ManagerTest extends TestCase {
  27. /** @var IManager */
  28. protected $manager;
  29. public function setUp() {
  30. parent::setUp();
  31. $validator = $this->createMock(IValidator::class);
  32. $this->manager = new Manager($validator);
  33. }
  34. public function testRegisterApp() {
  35. $app = $this->getMockBuilder('OCP\Notification\IApp')
  36. ->disableOriginalConstructor()
  37. ->getMock();
  38. $closure = function() use ($app) {
  39. return $app;
  40. };
  41. $this->assertEquals([], $this->invokePrivate($this->manager, 'getApps'));
  42. $this->manager->registerApp($closure);
  43. $this->assertEquals([$app], $this->invokePrivate($this->manager, 'getApps'));
  44. $this->assertEquals([$app], $this->invokePrivate($this->manager, 'getApps'));
  45. $this->manager->registerApp($closure);
  46. $this->assertEquals([$app, $app], $this->invokePrivate($this->manager, 'getApps'));
  47. }
  48. /**
  49. * @expectedException \InvalidArgumentException
  50. */
  51. public function testRegisterAppInvalid() {
  52. $notifier = $this->getMockBuilder('OCP\Notification\INotifier')
  53. ->disableOriginalConstructor()
  54. ->getMock();
  55. $closure = function() use ($notifier) {
  56. return $notifier;
  57. };
  58. $this->manager->registerApp($closure);
  59. $this->invokePrivate($this->manager, 'getApps');
  60. }
  61. public function testRegisterNotifier() {
  62. $notifier = $this->getMockBuilder('OCP\Notification\INotifier')
  63. ->disableOriginalConstructor()
  64. ->getMock();
  65. $closure = function() use ($notifier) {
  66. return $notifier;
  67. };
  68. $this->assertEquals([], $this->invokePrivate($this->manager, 'getNotifiers'));
  69. $this->assertEquals([], $this->invokePrivate($this->manager, 'listNotifiers'));
  70. $this->manager->registerNotifier($closure, function() {
  71. return ['id' => 'test1', 'name' => 'Test One'];
  72. });
  73. $this->assertEquals([$notifier], $this->invokePrivate($this->manager, 'getNotifiers'));
  74. $this->assertEquals(['test1' => 'Test One'], $this->invokePrivate($this->manager, 'listNotifiers'));
  75. $this->assertEquals([$notifier], $this->invokePrivate($this->manager, 'getNotifiers'));
  76. $this->assertEquals(['test1' => 'Test One'], $this->invokePrivate($this->manager, 'listNotifiers'));
  77. $this->manager->registerNotifier($closure, function() {
  78. return ['id' => 'test2', 'name' => 'Test Two'];
  79. });
  80. $this->assertEquals([$notifier, $notifier], $this->invokePrivate($this->manager, 'getNotifiers'));
  81. $this->assertEquals(['test1' => 'Test One', 'test2' => 'Test Two'], $this->invokePrivate($this->manager, 'listNotifiers'));
  82. }
  83. /**
  84. * @expectedException \InvalidArgumentException
  85. */
  86. public function testRegisterNotifierInvalid() {
  87. $app = $this->getMockBuilder('OCP\Notification\IApp')
  88. ->disableOriginalConstructor()
  89. ->getMock();
  90. $closure = function() use ($app) {
  91. return $app;
  92. };
  93. $this->manager->registerNotifier($closure, function() {
  94. return ['id' => 'test1', 'name' => 'Test One'];
  95. });
  96. $this->invokePrivate($this->manager, 'getNotifiers');
  97. }
  98. public function dataRegisterNotifierInfoInvalid() {
  99. return [
  100. [null],
  101. ['No array'],
  102. [['id' => 'test1', 'name' => 'Test One', 'size' => 'Invalid']],
  103. [['no-id' => 'test1', 'name' => 'Test One']],
  104. [['id' => 'test1', 'no-name' => 'Test One']],
  105. ];
  106. }
  107. /**
  108. * @dataProvider dataRegisterNotifierInfoInvalid
  109. * @expectedException \InvalidArgumentException
  110. * @param mixed $data
  111. */
  112. public function testRegisterNotifierInfoInvalid($data) {
  113. $app = $this->getMockBuilder('OCP\Notification\IApp')
  114. ->disableOriginalConstructor()
  115. ->getMock();
  116. $closure = function() use ($app) {
  117. return $app;
  118. };
  119. $this->manager->registerNotifier($closure, function() use ($data) {
  120. return $data;
  121. });
  122. $this->manager->listNotifiers();
  123. }
  124. /**
  125. * @expectedException \InvalidArgumentException
  126. * @expectedExceptionMessage The given notifier ID test1 is already in use
  127. */
  128. public function testRegisterNotifierInfoDuplicate() {
  129. $app = $this->getMockBuilder('OCP\Notification\IApp')
  130. ->disableOriginalConstructor()
  131. ->getMock();
  132. $closure = function() use ($app) {
  133. return $app;
  134. };
  135. $this->manager->registerNotifier($closure, function() {
  136. return ['id' => 'test1', 'name' => 'Test One'];
  137. });
  138. $this->manager->registerNotifier($closure, function() {
  139. return ['id' => 'test1', 'name' => 'Test One'];
  140. });
  141. $this->manager->listNotifiers();
  142. }
  143. public function testCreateNotification() {
  144. $action = $this->manager->createNotification();
  145. $this->assertInstanceOf('OCP\Notification\INotification', $action);
  146. }
  147. public function testNotify() {
  148. /** @var \OCP\Notification\INotification|\PHPUnit_Framework_MockObject_MockObject $notification */
  149. $notification = $this->getMockBuilder('OCP\Notification\INotification')
  150. ->disableOriginalConstructor()
  151. ->getMock();
  152. $notification->expects($this->once())
  153. ->method('isValid')
  154. ->willReturn(true);
  155. /** @var \OCP\Notification\IApp|\PHPUnit_Framework_MockObject_MockObject $app */
  156. $app = $this->getMockBuilder('OCP\Notification\IApp')
  157. ->disableOriginalConstructor()
  158. ->getMock();
  159. $app->expects($this->once())
  160. ->method('notify')
  161. ->with($notification);
  162. /** @var \OCP\Notification\IApp|\PHPUnit_Framework_MockObject_MockObject $app2 */
  163. $app2 = $this->getMockBuilder('OCP\Notification\IApp')
  164. ->disableOriginalConstructor()
  165. ->getMock();
  166. $app2->expects($this->once())
  167. ->method('notify')
  168. ->with($notification);
  169. $this->manager->registerApp(function() use ($app) {
  170. return $app;
  171. });
  172. $this->manager->registerApp(function() use ($app2) {
  173. return $app2;
  174. });
  175. $this->manager->notify($notification);
  176. }
  177. /**
  178. * @expectedException \InvalidArgumentException
  179. */
  180. public function testNotifyInvalid() {
  181. /** @var \OCP\Notification\INotification|\PHPUnit_Framework_MockObject_MockObject $notification */
  182. $notification = $this->getMockBuilder('OCP\Notification\INotification')
  183. ->disableOriginalConstructor()
  184. ->getMock();
  185. $notification->expects($this->once())
  186. ->method('isValid')
  187. ->willReturn(false);
  188. $this->manager->notify($notification);
  189. }
  190. public function testPrepare() {
  191. /** @var \OCP\Notification\INotification|\PHPUnit_Framework_MockObject_MockObject $notification */
  192. $notification = $this->getMockBuilder('OCP\Notification\INotification')
  193. ->disableOriginalConstructor()
  194. ->getMock();
  195. $notification->expects($this->once())
  196. ->method('isValidParsed')
  197. ->willReturn(true);
  198. /** @var \OCP\Notification\INotification|\PHPUnit_Framework_MockObject_MockObject $notification2 */
  199. $notification2 = $this->getMockBuilder('OCP\Notification\INotification')
  200. ->disableOriginalConstructor()
  201. ->getMock();
  202. $notification2->expects($this->exactly(2))
  203. ->method('isValidParsed')
  204. ->willReturn(true);
  205. /** @var \OCP\Notification\IApp|\PHPUnit_Framework_MockObject_MockObject $notifier */
  206. $notifier = $this->getMockBuilder('OCP\Notification\INotifier')
  207. ->disableOriginalConstructor()
  208. ->getMock();
  209. $notifier->expects($this->once())
  210. ->method('prepare')
  211. ->with($notification, 'en')
  212. ->willReturnArgument(0);
  213. /** @var \OCP\Notification\IApp|\PHPUnit_Framework_MockObject_MockObject $notifier2 */
  214. $notifier2 = $this->getMockBuilder('OCP\Notification\INotifier')
  215. ->disableOriginalConstructor()
  216. ->getMock();
  217. $notifier2->expects($this->once())
  218. ->method('prepare')
  219. ->with($notification, 'en')
  220. ->willReturn($notification2);
  221. $this->manager->registerNotifier(function() use ($notifier) {
  222. return $notifier;
  223. }, function() {
  224. return ['id' => 'test1', 'name' => 'Test One'];
  225. });
  226. $this->manager->registerNotifier(function() use ($notifier2) {
  227. return $notifier2;
  228. }, function() {
  229. return ['id' => 'test2', 'name' => 'Test Two'];
  230. });
  231. $this->assertEquals($notification2, $this->manager->prepare($notification, 'en'));
  232. }
  233. /**
  234. * @expectedException \InvalidArgumentException
  235. */
  236. public function testPrepareInvalid() {
  237. /** @var \OCP\Notification\INotification|\PHPUnit_Framework_MockObject_MockObject $notification */
  238. $notification = $this->getMockBuilder('OCP\Notification\INotification')
  239. ->disableOriginalConstructor()
  240. ->getMock();
  241. $notification->expects($this->once())
  242. ->method('isValidParsed')
  243. ->willReturn(false);
  244. /** @var \OCP\Notification\IApp|\PHPUnit_Framework_MockObject_MockObject $notifier */
  245. $notifier = $this->getMockBuilder('OCP\Notification\INotifier')
  246. ->disableOriginalConstructor()
  247. ->getMock();
  248. $notifier->expects($this->once())
  249. ->method('prepare')
  250. ->with($notification, 'de')
  251. ->willReturnArgument(0);
  252. $this->manager->registerNotifier(function() use ($notifier) {
  253. return $notifier;
  254. }, function() {
  255. return ['id' => 'test1', 'name' => 'Test One'];
  256. });
  257. $this->manager->prepare($notification, 'de');
  258. }
  259. public function testPrepareNotifierThrows() {
  260. /** @var \OCP\Notification\INotification|\PHPUnit_Framework_MockObject_MockObject $notification */
  261. $notification = $this->getMockBuilder('OCP\Notification\INotification')
  262. ->disableOriginalConstructor()
  263. ->getMock();
  264. $notification->expects($this->once())
  265. ->method('isValidParsed')
  266. ->willReturn(true);
  267. /** @var \OCP\Notification\IApp|\PHPUnit_Framework_MockObject_MockObject $notifier */
  268. $notifier = $this->getMockBuilder('OCP\Notification\INotifier')
  269. ->disableOriginalConstructor()
  270. ->getMock();
  271. $notifier->expects($this->once())
  272. ->method('prepare')
  273. ->with($notification, 'de')
  274. ->willThrowException(new \InvalidArgumentException);
  275. $this->manager->registerNotifier(function() use ($notifier) {
  276. return $notifier;
  277. }, function() {
  278. return ['id' => 'test1', 'name' => 'Test One'];
  279. });
  280. $this->assertEquals($notification, $this->manager->prepare($notification, 'de'));
  281. }
  282. /**
  283. * @expectedException \InvalidArgumentException
  284. */
  285. public function testPrepareNoNotifier() {
  286. /** @var \OCP\Notification\INotification|\PHPUnit_Framework_MockObject_MockObject $notification */
  287. $notification = $this->getMockBuilder('OCP\Notification\INotification')
  288. ->disableOriginalConstructor()
  289. ->getMock();
  290. $notification->expects($this->once())
  291. ->method('isValidParsed')
  292. ->willReturn(false);
  293. $this->manager->prepare($notification, 'en');
  294. }
  295. public function testMarkProcessed() {
  296. /** @var \OCP\Notification\INotification|\PHPUnit_Framework_MockObject_MockObject $notification */
  297. $notification = $this->getMockBuilder('OCP\Notification\INotification')
  298. ->disableOriginalConstructor()
  299. ->getMock();
  300. /** @var \OCP\Notification\IApp|\PHPUnit_Framework_MockObject_MockObject $app */
  301. $app = $this->getMockBuilder('OCP\Notification\IApp')
  302. ->disableOriginalConstructor()
  303. ->getMock();
  304. $app->expects($this->once())
  305. ->method('markProcessed')
  306. ->with($notification);
  307. /** @var \OCP\Notification\IApp|\PHPUnit_Framework_MockObject_MockObject $app2 */
  308. $app2 = $this->getMockBuilder('OCP\Notification\IApp')
  309. ->disableOriginalConstructor()
  310. ->getMock();
  311. $app2->expects($this->once())
  312. ->method('markProcessed')
  313. ->with($notification);
  314. $this->manager->registerApp(function() use ($app) {
  315. return $app;
  316. });
  317. $this->manager->registerApp(function() use ($app2) {
  318. return $app2;
  319. });
  320. $this->manager->markProcessed($notification);
  321. }
  322. public function testGetCount() {
  323. /** @var \OCP\Notification\INotification|\PHPUnit_Framework_MockObject_MockObject $notification */
  324. $notification = $this->getMockBuilder('OCP\Notification\INotification')
  325. ->disableOriginalConstructor()
  326. ->getMock();
  327. /** @var \OCP\Notification\IApp|\PHPUnit_Framework_MockObject_MockObject $app */
  328. $app = $this->getMockBuilder('OCP\Notification\IApp')
  329. ->disableOriginalConstructor()
  330. ->getMock();
  331. $app->expects($this->once())
  332. ->method('getCount')
  333. ->with($notification)
  334. ->willReturn(21);
  335. /** @var \OCP\Notification\IApp|\PHPUnit_Framework_MockObject_MockObject $app2 */
  336. $app2 = $this->getMockBuilder('OCP\Notification\IApp')
  337. ->disableOriginalConstructor()
  338. ->getMock();
  339. $app2->expects($this->once())
  340. ->method('getCount')
  341. ->with($notification)
  342. ->willReturn(42);
  343. $this->manager->registerApp(function() use ($app) {
  344. return $app;
  345. });
  346. $this->manager->registerApp(function() use ($app2) {
  347. return $app2;
  348. });
  349. $this->assertSame(63, $this->manager->getCount($notification));
  350. }
  351. }