NotificationTest.php 17 KB

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