ManagerTest.php 13 KB

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