You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

NotificationTest.php 18KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646
  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. public function setUp() {
  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. * @expectedException \InvalidArgumentException
  78. */
  79. public function testSetAppInvalid($app) {
  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. * @expectedException \InvalidArgumentException
  102. */
  103. public function testSetUserInvalid($user) {
  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. * @expectedException \InvalidArgumentException
  139. * @expectedMessage 'The given date time is invalid'
  140. */
  141. public function testSetDateTimeZero($dateTime) {
  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. * @expectedException \InvalidArgumentException
  176. * @expectedMessage 'The given object id is invalid'
  177. */
  178. public function testSetObjectIdInvalid($id) {
  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. * @expectedException \InvalidArgumentException
  208. */
  209. public function testSetSubjectInvalidSubject($subject) {
  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. * @expectedException \InvalidArgumentException
  232. */
  233. public function testSetParsedSubjectInvalid($subject) {
  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. * @expectedException \InvalidArgumentException
  263. */
  264. public function testSetMessageInvalidMessage($message) {
  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. * @expectedException \InvalidArgumentException
  287. */
  288. public function testSetParsedMessageInvalid($message) {
  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. * @expectedException \InvalidArgumentException
  311. */
  312. public function testSetLinkInvalid($link) {
  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. * @expectedException \InvalidArgumentException
  335. */
  336. public function testSetIconInvalid($icon) {
  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. /**
  356. * @expectedException \InvalidArgumentException
  357. */
  358. public function testAddActionInvalid() {
  359. /** @var \OCP\Notification\IAction|\PHPUnit_Framework_MockObject_MockObject $action */
  360. $action = $this->createMock(IAction::class);
  361. $action->expects($this->once())
  362. ->method('isValid')
  363. ->willReturn(false);
  364. $action->expects($this->never())
  365. ->method('isValidParsed');
  366. $this->notification->addAction($action);
  367. }
  368. public function testAddActionSecondPrimary() {
  369. /** @var \OCP\Notification\IAction|\PHPUnit_Framework_MockObject_MockObject $action */
  370. $action = $this->createMock(IAction::class);
  371. $action->expects($this->exactly(2))
  372. ->method('isValid')
  373. ->willReturn(true);
  374. $action->expects($this->exactly(2))
  375. ->method('isPrimary')
  376. ->willReturn(true);
  377. $this->assertSame($this->notification, $this->notification->addAction($action));
  378. $this->expectException(\InvalidArgumentException::class);
  379. $this->notification->addAction($action);
  380. }
  381. public function testAddParsedAction() {
  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(true);
  387. $action->expects($this->never())
  388. ->method('isValid');
  389. $this->assertSame($this->notification, $this->notification->addParsedAction($action));
  390. $this->assertEquals([$action], $this->notification->getParsedActions());
  391. $this->assertEquals([], $this->notification->getActions());
  392. }
  393. /**
  394. * @expectedException \InvalidArgumentException
  395. */
  396. public function testAddParsedActionInvalid() {
  397. /** @var \OCP\Notification\IAction|\PHPUnit_Framework_MockObject_MockObject $action */
  398. $action = $this->createMock(IAction::class);
  399. $action->expects($this->once())
  400. ->method('isValidParsed')
  401. ->willReturn(false);
  402. $action->expects($this->never())
  403. ->method('isValid');
  404. $this->notification->addParsedAction($action);
  405. }
  406. public function testAddActionSecondParsedPrimary() {
  407. /** @var \OCP\Notification\IAction|\PHPUnit_Framework_MockObject_MockObject $action */
  408. $action = $this->createMock(IAction::class);
  409. $action->expects($this->exactly(2))
  410. ->method('isValidParsed')
  411. ->willReturn(true);
  412. $action->expects($this->exactly(2))
  413. ->method('isPrimary')
  414. ->willReturn(true);
  415. $this->assertSame($this->notification, $this->notification->addParsedAction($action));
  416. $this->expectException(\InvalidArgumentException::class);
  417. $this->notification->addParsedAction($action);
  418. }
  419. public function testAddActionParsedPrimaryEnd() {
  420. /** @var \OCP\Notification\IAction|\PHPUnit_Framework_MockObject_MockObject $action */
  421. $action1 = $this->createMock(IAction::class);
  422. $action1->expects($this->exactly(2))
  423. ->method('isValidParsed')
  424. ->willReturn(true);
  425. $action1->expects($this->exactly(2))
  426. ->method('isPrimary')
  427. ->willReturn(false);
  428. /** @var \OCP\Notification\IAction|\PHPUnit_Framework_MockObject_MockObject $action */
  429. $action2 = $this->createMock(IAction::class);
  430. $action2->expects($this->once())
  431. ->method('isValidParsed')
  432. ->willReturn(true);
  433. $action2->expects($this->once())
  434. ->method('isPrimary')
  435. ->willReturn(true);
  436. $this->assertSame($this->notification, $this->notification->addParsedAction($action1));
  437. $this->assertSame($this->notification, $this->notification->addParsedAction($action2));
  438. $this->assertSame($this->notification, $this->notification->addParsedAction($action1));
  439. $this->assertEquals([$action2, $action1, $action1], $this->notification->getParsedActions());
  440. }
  441. public function dataIsValid() {
  442. return [
  443. [false, '', false],
  444. [true, '', false],
  445. [false, 'a', false],
  446. [true, 'a', true],
  447. ];
  448. }
  449. /**
  450. * @dataProvider dataIsValid
  451. *
  452. * @param bool $isValidCommon
  453. * @param string $subject
  454. * @param bool $expected
  455. */
  456. public function testIsValid($isValidCommon, $subject, $expected) {
  457. /** @var \OCP\Notification\INotification|\PHPUnit_Framework_MockObject_MockObject $notification */
  458. $notification = $this->getMockBuilder(Notification::class)
  459. ->setMethods([
  460. 'isValidCommon',
  461. 'getSubject',
  462. 'getParsedSubject',
  463. ])
  464. ->setConstructorArgs([$this->validator])
  465. ->getMock();
  466. $notification->expects($this->once())
  467. ->method('isValidCommon')
  468. ->willReturn($isValidCommon);
  469. $notification->expects(!$isValidCommon ? $this->never() : $this->once())
  470. ->method('getSubject')
  471. ->willReturn($subject);
  472. $notification->expects($this->never())
  473. ->method('getParsedSubject')
  474. ->willReturn($subject);
  475. $this->assertEquals($expected, $notification->isValid());
  476. }
  477. /**
  478. * @dataProvider dataIsValid
  479. *
  480. * @param bool $isValidCommon
  481. * @param string $subject
  482. * @param bool $expected
  483. */
  484. public function testIsParsedValid($isValidCommon, $subject, $expected) {
  485. /** @var \OCP\Notification\INotification|\PHPUnit_Framework_MockObject_MockObject $notification */
  486. $notification = $this->getMockBuilder(Notification::class)
  487. ->setMethods([
  488. 'isValidCommon',
  489. 'getParsedSubject',
  490. 'getSubject',
  491. ])
  492. ->setConstructorArgs([$this->validator])
  493. ->getMock();
  494. $notification->expects($this->once())
  495. ->method('isValidCommon')
  496. ->willReturn($isValidCommon);
  497. $notification->expects(!$isValidCommon ? $this->never() : $this->once())
  498. ->method('getParsedSubject')
  499. ->willReturn($subject);
  500. $notification->expects($this->never())
  501. ->method('getSubject')
  502. ->willReturn($subject);
  503. $this->assertEquals($expected, $notification->isValidParsed());
  504. }
  505. public function dataIsValidCommon() {
  506. return [
  507. ['', '', 0, '', '', false],
  508. ['app', '', 0, '', '', false],
  509. ['app', 'user', 0, '', '', false],
  510. ['app', 'user', time(), '', '', false],
  511. ['app', 'user', time(), 'type', '', false],
  512. ['app', 'user', time(), 'type', '42', true],
  513. ];
  514. }
  515. /**
  516. * @dataProvider dataIsValidCommon
  517. *
  518. * @param string $app
  519. * @param string $user
  520. * @param int $timestamp
  521. * @param string $objectType
  522. * @param string $objectId
  523. * @param bool $expected
  524. */
  525. public function testIsValidCommon($app, $user, $timestamp, $objectType, $objectId, $expected) {
  526. /** @var \OCP\Notification\INotification|\PHPUnit_Framework_MockObject_MockObject $notification */
  527. $notification = $this->getMockBuilder(Notification::class)
  528. ->setMethods([
  529. 'getApp',
  530. 'getUser',
  531. 'getDateTime',
  532. 'getObjectType',
  533. 'getObjectId',
  534. ])
  535. ->setConstructorArgs([$this->validator])
  536. ->getMock();
  537. $notification->expects($this->any())
  538. ->method('getApp')
  539. ->willReturn($app);
  540. $notification->expects($this->any())
  541. ->method('getUser')
  542. ->willReturn($user);
  543. $dateTime = new \DateTime();
  544. $dateTime->setTimestamp($timestamp);
  545. $notification->expects($this->any())
  546. ->method('getDateTime')
  547. ->willReturn($dateTime);
  548. $notification->expects($this->any())
  549. ->method('getObjectType')
  550. ->willReturn($objectType);
  551. $notification->expects($this->any())
  552. ->method('getObjectId')
  553. ->willReturn($objectId);
  554. $this->assertEquals($expected, $this->invokePrivate($notification, 'isValidCommon'));
  555. }
  556. }