1
0

NotificationTest.php 18 KB

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