1
0

NotificationTest.php 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656
  1. <?php
  2. declare(strict_types = 1);
  3. /**
  4. * @author Joas Schilling <nickvergessen@owncloud.com>
  5. *
  6. * @copyright Copyright (c) 2015, ownCloud, Inc.
  7. * @license AGPL-3.0
  8. *
  9. * This code is free software: you can redistribute it and/or modify
  10. * it under the terms of the GNU Affero General Public License, version 3,
  11. * as published by the Free Software Foundation.
  12. *
  13. * This program is distributed in the hope that it will be useful,
  14. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  15. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  16. * GNU Affero General Public License for more details.
  17. *
  18. * You should have received a copy of the GNU Affero General Public License, version 3,
  19. * along with this program. If not, see <http://www.gnu.org/licenses/>
  20. *
  21. */
  22. namespace Test\Notification;
  23. use OC\Notification\Notification;
  24. use OCP\Notification\IAction;
  25. use OCP\Notification\INotification;
  26. use OCP\RichObjectStrings\IValidator;
  27. use Test\TestCase;
  28. class NotificationTest extends TestCase {
  29. /** @var INotification */
  30. protected $notification;
  31. /** @var IValidator|\PHPUnit\Framework\MockObject\MockObject */
  32. protected $validator;
  33. protected function setUp(): void {
  34. parent::setUp();
  35. $this->validator = $this->createMock(IValidator::class);
  36. $this->notification = new Notification($this->validator);
  37. }
  38. protected function dataValidString($maxLength) {
  39. $dataSets = [
  40. ['test1'],
  41. ['1564'],
  42. [str_repeat('a', 1)],
  43. ];
  44. if ($maxLength !== false) {
  45. $dataSets[] = [str_repeat('a', $maxLength)];
  46. }
  47. return $dataSets;
  48. }
  49. protected function dataInvalidString($maxLength) {
  50. $dataSets = [
  51. ['']
  52. ];
  53. if ($maxLength !== false) {
  54. $dataSets[] = [str_repeat('a', $maxLength + 1)];
  55. }
  56. return $dataSets;
  57. }
  58. public function dataSetApp() {
  59. return $this->dataValidString(32);
  60. }
  61. /**
  62. * @dataProvider dataSetApp
  63. * @param string $app
  64. */
  65. public function testSetApp($app) {
  66. $this->assertSame('', $this->notification->getApp());
  67. $this->assertSame($this->notification, $this->notification->setApp($app));
  68. $this->assertSame($app, $this->notification->getApp());
  69. }
  70. public function dataSetAppInvalid() {
  71. return $this->dataInvalidString(32);
  72. }
  73. /**
  74. * @dataProvider dataSetAppInvalid
  75. * @param mixed $app
  76. *
  77. */
  78. public function testSetAppInvalid($app) {
  79. $this->expectException(\InvalidArgumentException::class);
  80. $this->notification->setApp($app);
  81. }
  82. public function dataSetUser() {
  83. return $this->dataValidString(64);
  84. }
  85. /**
  86. * @dataProvider dataSetUser
  87. * @param string $user
  88. */
  89. public function testSetUser($user) {
  90. $this->assertSame('', $this->notification->getUser());
  91. $this->assertSame($this->notification, $this->notification->setUser($user));
  92. $this->assertSame($user, $this->notification->getUser());
  93. }
  94. public function dataSetUserInvalid() {
  95. return $this->dataInvalidString(64);
  96. }
  97. /**
  98. * @dataProvider dataSetUserInvalid
  99. * @param mixed $user
  100. *
  101. */
  102. public function testSetUserInvalid($user) {
  103. $this->expectException(\InvalidArgumentException::class);
  104. $this->notification->setUser($user);
  105. }
  106. public function dataSetDateTime() {
  107. $past = new \DateTime();
  108. $past->sub(new \DateInterval('P1Y'));
  109. $current = new \DateTime();
  110. $future = new \DateTime();
  111. $future->add(new \DateInterval('P1Y'));
  112. return [
  113. [$past],
  114. [$current],
  115. [$future],
  116. ];
  117. }
  118. /**
  119. * @dataProvider dataSetDateTime
  120. * @param \DateTime $dateTime
  121. */
  122. public function testSetDateTime(\DateTime $dateTime) {
  123. $this->assertSame(0, $this->notification->getDateTime()->getTimestamp());
  124. $this->assertSame($this->notification, $this->notification->setDateTime($dateTime));
  125. $this->assertSame($dateTime, $this->notification->getDateTime());
  126. }
  127. public function dataSetDateTimeZero() {
  128. $nineTeenSeventy = new \DateTime();
  129. $nineTeenSeventy->setTimestamp(0);
  130. return [
  131. [$nineTeenSeventy],
  132. ];
  133. }
  134. /**
  135. * @dataProvider dataSetDateTimeZero
  136. * @param \DateTime $dateTime
  137. *
  138. * @expectedMessage 'The given date time is invalid'
  139. */
  140. public function testSetDateTimeZero($dateTime) {
  141. $this->expectException(\InvalidArgumentException::class);
  142. $this->notification->setDateTime($dateTime);
  143. }
  144. public function dataSetObject() {
  145. return [
  146. ['a', '21'],
  147. [str_repeat('a', 64), '42'],
  148. ];
  149. }
  150. /**
  151. * @dataProvider dataSetObject
  152. * @param string $type
  153. * @param string $id
  154. */
  155. public function testSetObject($type, $id) {
  156. $this->assertSame('', $this->notification->getObjectType());
  157. $this->assertSame('', $this->notification->getObjectId());
  158. $this->assertSame($this->notification, $this->notification->setObject($type, $id));
  159. $this->assertSame($type, $this->notification->getObjectType());
  160. $this->assertSame($id, $this->notification->getObjectId());
  161. }
  162. public function dataSetObjectTypeInvalid() {
  163. return $this->dataInvalidString(64);
  164. }
  165. public function dataSetObjectIdInvalid() {
  166. return [
  167. [''],
  168. [str_repeat('a', 64 + 1)],
  169. ];
  170. }
  171. /**
  172. * @dataProvider dataSetObjectIdInvalid
  173. * @param mixed $id
  174. *
  175. * @expectedMessage 'The given object id is invalid'
  176. */
  177. public function testSetObjectIdInvalid($id) {
  178. $this->expectException(\InvalidArgumentException::class);
  179. $this->notification->setObject('object', $id);
  180. }
  181. public function dataSetSubject() {
  182. return [
  183. ['a', []],
  184. [str_repeat('a', 64), [str_repeat('a', 160)]],
  185. [str_repeat('a', 64), array_fill(0, 160, 'a')],
  186. ];
  187. }
  188. /**
  189. * @dataProvider dataSetSubject
  190. * @param string $subject
  191. * @param array $parameters
  192. */
  193. public function testSetSubject($subject, $parameters) {
  194. $this->assertSame('', $this->notification->getSubject());
  195. $this->assertSame([], $this->notification->getSubjectParameters());
  196. $this->assertSame($this->notification, $this->notification->setSubject($subject, $parameters));
  197. $this->assertSame($subject, $this->notification->getSubject());
  198. $this->assertSame($parameters, $this->notification->getSubjectParameters());
  199. }
  200. public function dataSetSubjectInvalidSubject() {
  201. return $this->dataInvalidString(64);
  202. }
  203. /**
  204. * @dataProvider dataSetSubjectInvalidSubject
  205. * @param mixed $subject
  206. *
  207. */
  208. public function testSetSubjectInvalidSubject($subject) {
  209. $this->expectException(\InvalidArgumentException::class);
  210. $this->notification->setSubject($subject, []);
  211. }
  212. public function dataSetParsedSubject() {
  213. return $this->dataValidString(false);
  214. }
  215. /**
  216. * @dataProvider dataSetParsedSubject
  217. * @param string $subject
  218. */
  219. public function testSetParsedSubject($subject) {
  220. $this->assertSame('', $this->notification->getParsedSubject());
  221. $this->assertSame($this->notification, $this->notification->setParsedSubject($subject));
  222. $this->assertSame($subject, $this->notification->getParsedSubject());
  223. }
  224. public function dataSetParsedSubjectInvalid() {
  225. return $this->dataInvalidString(false);
  226. }
  227. /**
  228. * @dataProvider dataSetParsedSubjectInvalid
  229. * @param mixed $subject
  230. *
  231. */
  232. public function testSetParsedSubjectInvalid($subject) {
  233. $this->expectException(\InvalidArgumentException::class);
  234. $this->notification->setParsedSubject($subject);
  235. }
  236. public function dataSetMessage() {
  237. return [
  238. ['a', []],
  239. [str_repeat('a', 64), [str_repeat('a', 160)]],
  240. [str_repeat('a', 64), array_fill(0, 160, 'a')],
  241. ];
  242. }
  243. /**
  244. * @dataProvider dataSetMessage
  245. * @param string $message
  246. * @param array $parameters
  247. */
  248. public function testSetMessage($message, $parameters) {
  249. $this->assertSame('', $this->notification->getMessage());
  250. $this->assertSame([], $this->notification->getMessageParameters());
  251. $this->assertSame($this->notification, $this->notification->setMessage($message, $parameters));
  252. $this->assertSame($message, $this->notification->getMessage());
  253. $this->assertSame($parameters, $this->notification->getMessageParameters());
  254. }
  255. public function dataSetMessageInvalidMessage() {
  256. return $this->dataInvalidString(64);
  257. }
  258. /**
  259. * @dataProvider dataSetMessageInvalidMessage
  260. * @param mixed $message
  261. *
  262. */
  263. public function testSetMessageInvalidMessage($message) {
  264. $this->expectException(\InvalidArgumentException::class);
  265. $this->notification->setMessage($message, []);
  266. }
  267. public function dataSetParsedMessage() {
  268. return $this->dataValidString(false);
  269. }
  270. /**
  271. * @dataProvider dataSetParsedMessage
  272. * @param string $message
  273. */
  274. public function testSetParsedMessage($message) {
  275. $this->assertSame('', $this->notification->getParsedMessage());
  276. $this->assertSame($this->notification, $this->notification->setParsedMessage($message));
  277. $this->assertSame($message, $this->notification->getParsedMessage());
  278. }
  279. public function dataSetParsedMessageInvalid() {
  280. return $this->dataInvalidString(false);
  281. }
  282. /**
  283. * @dataProvider dataSetParsedMessageInvalid
  284. * @param mixed $message
  285. *
  286. */
  287. public function testSetParsedMessageInvalid($message) {
  288. $this->expectException(\InvalidArgumentException::class);
  289. $this->notification->setParsedMessage($message);
  290. }
  291. public function dataSetLink() {
  292. return $this->dataValidString(4000);
  293. }
  294. /**
  295. * @dataProvider dataSetLink
  296. * @param string $link
  297. */
  298. public function testSetLink($link) {
  299. $this->assertSame('', $this->notification->getLink());
  300. $this->assertSame($this->notification, $this->notification->setLink($link));
  301. $this->assertSame($link, $this->notification->getLink());
  302. }
  303. public function dataSetLinkInvalid() {
  304. return $this->dataInvalidString(4000);
  305. }
  306. /**
  307. * @dataProvider dataSetLinkInvalid
  308. * @param mixed $link
  309. *
  310. */
  311. public function testSetLinkInvalid($link) {
  312. $this->expectException(\InvalidArgumentException::class);
  313. $this->notification->setLink($link);
  314. }
  315. public function dataSetIcon() {
  316. return $this->dataValidString(4000);
  317. }
  318. /**
  319. * @dataProvider dataSetIcon
  320. * @param string $icon
  321. */
  322. public function testSetIcon($icon) {
  323. $this->assertSame('', $this->notification->getIcon());
  324. $this->assertSame($this->notification, $this->notification->setIcon($icon));
  325. $this->assertSame($icon, $this->notification->getIcon());
  326. }
  327. public function dataSetIconInvalid() {
  328. return $this->dataInvalidString(4000);
  329. }
  330. /**
  331. * @dataProvider dataSetIconInvalid
  332. * @param mixed $icon
  333. *
  334. */
  335. public function testSetIconInvalid($icon) {
  336. $this->expectException(\InvalidArgumentException::class);
  337. $this->notification->setIcon($icon);
  338. }
  339. public function testCreateAction() {
  340. $action = $this->notification->createAction();
  341. $this->assertInstanceOf(IAction::class, $action);
  342. }
  343. public function testAddAction() {
  344. /** @var \OCP\Notification\IAction|\PHPUnit\Framework\MockObject\MockObject $action */
  345. $action = $this->createMock(IAction::class);
  346. $action->expects($this->once())
  347. ->method('isValid')
  348. ->willReturn(true);
  349. $action->expects($this->never())
  350. ->method('isValidParsed');
  351. $this->assertSame($this->notification, $this->notification->addAction($action));
  352. $this->assertEquals([$action], $this->notification->getActions());
  353. $this->assertEquals([], $this->notification->getParsedActions());
  354. }
  355. public function testAddActionInvalid() {
  356. $this->expectException(\InvalidArgumentException::class);
  357. /** @var \OCP\Notification\IAction|\PHPUnit\Framework\MockObject\MockObject $action */
  358. $action = $this->createMock(IAction::class);
  359. $action->expects($this->once())
  360. ->method('isValid')
  361. ->willReturn(false);
  362. $action->expects($this->never())
  363. ->method('isValidParsed');
  364. $this->notification->addAction($action);
  365. }
  366. public function testAddActionSecondPrimary() {
  367. /** @var \OCP\Notification\IAction|\PHPUnit\Framework\MockObject\MockObject $action */
  368. $action = $this->createMock(IAction::class);
  369. $action->expects($this->exactly(2))
  370. ->method('isValid')
  371. ->willReturn(true);
  372. $action->expects($this->exactly(2))
  373. ->method('isPrimary')
  374. ->willReturn(true);
  375. $this->assertSame($this->notification, $this->notification->addAction($action));
  376. $this->expectException(\InvalidArgumentException::class);
  377. $this->notification->addAction($action);
  378. }
  379. public function testAddParsedAction() {
  380. /** @var \OCP\Notification\IAction|\PHPUnit\Framework\MockObject\MockObject $action */
  381. $action = $this->createMock(IAction::class);
  382. $action->expects($this->once())
  383. ->method('isValidParsed')
  384. ->willReturn(true);
  385. $action->expects($this->never())
  386. ->method('isValid');
  387. $this->assertSame($this->notification, $this->notification->addParsedAction($action));
  388. $this->assertEquals([$action], $this->notification->getParsedActions());
  389. $this->assertEquals([], $this->notification->getActions());
  390. }
  391. public function testAddParsedActionInvalid() {
  392. $this->expectException(\InvalidArgumentException::class);
  393. /** @var \OCP\Notification\IAction|\PHPUnit\Framework\MockObject\MockObject $action */
  394. $action = $this->createMock(IAction::class);
  395. $action->expects($this->once())
  396. ->method('isValidParsed')
  397. ->willReturn(false);
  398. $action->expects($this->never())
  399. ->method('isValid');
  400. $this->notification->addParsedAction($action);
  401. }
  402. public function testAddActionSecondParsedPrimary() {
  403. /** @var \OCP\Notification\IAction|\PHPUnit\Framework\MockObject\MockObject $action */
  404. $action = $this->createMock(IAction::class);
  405. $action->expects($this->exactly(2))
  406. ->method('isValidParsed')
  407. ->willReturn(true);
  408. $action->expects($this->exactly(2))
  409. ->method('isPrimary')
  410. ->willReturn(true);
  411. $this->assertSame($this->notification, $this->notification->addParsedAction($action));
  412. $this->expectException(\InvalidArgumentException::class);
  413. $this->notification->addParsedAction($action);
  414. }
  415. public function testAddActionParsedPrimaryEnd() {
  416. /** @var \OCP\Notification\IAction|\PHPUnit\Framework\MockObject\MockObject $action */
  417. $action1 = $this->createMock(IAction::class);
  418. $action1->expects($this->exactly(2))
  419. ->method('isValidParsed')
  420. ->willReturn(true);
  421. $action1->expects($this->exactly(2))
  422. ->method('isPrimary')
  423. ->willReturn(false);
  424. /** @var \OCP\Notification\IAction|\PHPUnit\Framework\MockObject\MockObject $action */
  425. $action2 = $this->createMock(IAction::class);
  426. $action2->expects($this->once())
  427. ->method('isValidParsed')
  428. ->willReturn(true);
  429. $action2->expects($this->once())
  430. ->method('isPrimary')
  431. ->willReturn(true);
  432. $this->assertSame($this->notification, $this->notification->addParsedAction($action1));
  433. $this->assertSame($this->notification, $this->notification->addParsedAction($action2));
  434. $this->assertSame($this->notification, $this->notification->addParsedAction($action1));
  435. $this->assertEquals([$action2, $action1, $action1], $this->notification->getParsedActions());
  436. }
  437. public function dataIsValid() {
  438. return [
  439. [false, '', false],
  440. [true, '', false],
  441. [false, 'a', false],
  442. [true, 'a', true],
  443. ];
  444. }
  445. /**
  446. * @dataProvider dataIsValid
  447. *
  448. * @param bool $isValidCommon
  449. * @param string $subject
  450. * @param bool $expected
  451. */
  452. public function testIsValid($isValidCommon, $subject, $expected) {
  453. /** @var \OCP\Notification\INotification|\PHPUnit\Framework\MockObject\MockObject $notification */
  454. $notification = $this->getMockBuilder(Notification::class)
  455. ->setMethods([
  456. 'isValidCommon',
  457. 'getSubject',
  458. 'getParsedSubject',
  459. ])
  460. ->setConstructorArgs([$this->validator])
  461. ->getMock();
  462. $notification->expects($this->once())
  463. ->method('isValidCommon')
  464. ->willReturn($isValidCommon);
  465. $notification->expects(!$isValidCommon ? $this->never() : $this->once())
  466. ->method('getSubject')
  467. ->willReturn($subject);
  468. $notification->expects($this->never())
  469. ->method('getParsedSubject')
  470. ->willReturn($subject);
  471. $this->assertEquals($expected, $notification->isValid());
  472. }
  473. /**
  474. * @dataProvider dataIsValid
  475. *
  476. * @param bool $isValidCommon
  477. * @param string $subject
  478. * @param bool $expected
  479. */
  480. public function testIsParsedValid($isValidCommon, $subject, $expected) {
  481. /** @var \OCP\Notification\INotification|\PHPUnit\Framework\MockObject\MockObject $notification */
  482. $notification = $this->getMockBuilder(Notification::class)
  483. ->setMethods([
  484. 'isValidCommon',
  485. 'getParsedSubject',
  486. 'getSubject',
  487. ])
  488. ->setConstructorArgs([$this->validator])
  489. ->getMock();
  490. $notification->expects($this->once())
  491. ->method('isValidCommon')
  492. ->willReturn($isValidCommon);
  493. $notification->expects(!$isValidCommon ? $this->never() : $this->once())
  494. ->method('getParsedSubject')
  495. ->willReturn($subject);
  496. $notification->expects($this->never())
  497. ->method('getSubject')
  498. ->willReturn($subject);
  499. $this->assertEquals($expected, $notification->isValidParsed());
  500. }
  501. public function dataIsValidCommon() {
  502. return [
  503. ['', '', 0, '', '', false],
  504. ['app', '', 0, '', '', false],
  505. ['app', 'user', 0, '', '', false],
  506. ['app', 'user', time(), '', '', false],
  507. ['app', 'user', time(), 'type', '', false],
  508. ['app', 'user', time(), 'type', '42', true],
  509. ];
  510. }
  511. /**
  512. * @dataProvider dataIsValidCommon
  513. *
  514. * @param string $app
  515. * @param string $user
  516. * @param int $timestamp
  517. * @param string $objectType
  518. * @param string $objectId
  519. * @param bool $expected
  520. */
  521. public function testIsValidCommon($app, $user, $timestamp, $objectType, $objectId, $expected) {
  522. /** @var \OCP\Notification\INotification|\PHPUnit\Framework\MockObject\MockObject $notification */
  523. $notification = $this->getMockBuilder(Notification::class)
  524. ->setMethods([
  525. 'getApp',
  526. 'getUser',
  527. 'getDateTime',
  528. 'getObjectType',
  529. 'getObjectId',
  530. ])
  531. ->setConstructorArgs([$this->validator])
  532. ->getMock();
  533. $notification->expects($this->any())
  534. ->method('getApp')
  535. ->willReturn($app);
  536. $notification->expects($this->any())
  537. ->method('getUser')
  538. ->willReturn($user);
  539. $dateTime = new \DateTime();
  540. $dateTime->setTimestamp($timestamp);
  541. $notification->expects($this->any())
  542. ->method('getDateTime')
  543. ->willReturn($dateTime);
  544. $notification->expects($this->any())
  545. ->method('getObjectType')
  546. ->willReturn($objectType);
  547. $notification->expects($this->any())
  548. ->method('getObjectId')
  549. ->willReturn($objectId);
  550. $this->assertEquals($expected, $this->invokePrivate($notification, 'isValidCommon'));
  551. }
  552. }