NotificationTest.php 18 KB

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