CommentsNodeTest.php 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548
  1. <?php
  2. /**
  3. * @copyright Copyright (c) 2016, ownCloud, Inc.
  4. *
  5. * @author Arthur Schiwon <blizzz@arthur-schiwon.de>
  6. * @author Christoph Wurst <christoph@winzerhof-wurst.at>
  7. * @author Joas Schilling <coding@schilljs.com>
  8. * @author Morris Jobke <hey@morrisjobke.de>
  9. * @author Roeland Jago Douma <roeland@famdouma.nl>
  10. * @author Vincent Petry <vincent@nextcloud.com>
  11. *
  12. * @license AGPL-3.0
  13. *
  14. * This code is free software: you can redistribute it and/or modify
  15. * it under the terms of the GNU Affero General Public License, version 3,
  16. * as published by the Free Software Foundation.
  17. *
  18. * This program is distributed in the hope that it will be useful,
  19. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  20. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  21. * GNU Affero General Public License for more details.
  22. *
  23. * You should have received a copy of the GNU Affero General Public License, version 3,
  24. * along with this program. If not, see <http://www.gnu.org/licenses/>
  25. *
  26. */
  27. namespace OCA\DAV\Tests\unit\Comments;
  28. use OCA\DAV\Comments\CommentNode;
  29. use OCP\Comments\IComment;
  30. use OCP\Comments\ICommentsManager;
  31. use OCP\Comments\MessageTooLongException;
  32. use OCP\IUser;
  33. use OCP\IUserManager;
  34. use OCP\IUserSession;
  35. use Psr\Log\LoggerInterface;
  36. use Sabre\DAV\PropPatch;
  37. class CommentsNodeTest extends \Test\TestCase {
  38. /** @var ICommentsManager|\PHPUnit\Framework\MockObject\MockObject */
  39. protected $commentsManager;
  40. protected $comment;
  41. protected $node;
  42. protected $userManager;
  43. protected $logger;
  44. protected $userSession;
  45. protected function setUp(): void {
  46. parent::setUp();
  47. $this->commentsManager = $this->getMockBuilder(ICommentsManager::class)
  48. ->disableOriginalConstructor()
  49. ->getMock();
  50. $this->comment = $this->getMockBuilder(IComment::class)
  51. ->disableOriginalConstructor()
  52. ->getMock();
  53. $this->userManager = $this->getMockBuilder(IUserManager::class)
  54. ->disableOriginalConstructor()
  55. ->getMock();
  56. $this->userSession = $this->getMockBuilder(IUserSession::class)
  57. ->disableOriginalConstructor()
  58. ->getMock();
  59. $this->logger = $this->getMockBuilder(LoggerInterface::class)
  60. ->disableOriginalConstructor()
  61. ->getMock();
  62. $this->node = new CommentNode(
  63. $this->commentsManager,
  64. $this->comment,
  65. $this->userManager,
  66. $this->userSession,
  67. $this->logger
  68. );
  69. }
  70. public function testDelete(): void {
  71. $user = $this->getMockBuilder(IUser::class)
  72. ->disableOriginalConstructor()
  73. ->getMock();
  74. $user->expects($this->once())
  75. ->method('getUID')
  76. ->willReturn('alice');
  77. $this->userSession->expects($this->once())
  78. ->method('getUser')
  79. ->willReturn($user);
  80. $this->comment->expects($this->once())
  81. ->method('getId')
  82. ->willReturn('19');
  83. $this->comment->expects($this->any())
  84. ->method('getActorType')
  85. ->willReturn('users');
  86. $this->comment->expects($this->any())
  87. ->method('getActorId')
  88. ->willReturn('alice');
  89. $this->commentsManager->expects($this->once())
  90. ->method('delete')
  91. ->with('19');
  92. $this->node->delete();
  93. }
  94. public function testDeleteForbidden(): void {
  95. $this->expectException(\Sabre\DAV\Exception\Forbidden::class);
  96. $user = $this->getMockBuilder(IUser::class)
  97. ->disableOriginalConstructor()
  98. ->getMock();
  99. $user->expects($this->once())
  100. ->method('getUID')
  101. ->willReturn('mallory');
  102. $this->userSession->expects($this->once())
  103. ->method('getUser')
  104. ->willReturn($user);
  105. $this->comment->expects($this->never())
  106. ->method('getId');
  107. $this->comment->expects($this->any())
  108. ->method('getActorType')
  109. ->willReturn('users');
  110. $this->comment->expects($this->any())
  111. ->method('getActorId')
  112. ->willReturn('alice');
  113. $this->commentsManager->expects($this->never())
  114. ->method('delete');
  115. $this->node->delete();
  116. }
  117. public function testGetName(): void {
  118. $id = '19';
  119. $this->comment->expects($this->once())
  120. ->method('getId')
  121. ->willReturn($id);
  122. $this->assertSame($this->node->getName(), $id);
  123. }
  124. public function testSetName(): void {
  125. $this->expectException(\Sabre\DAV\Exception\MethodNotAllowed::class);
  126. $this->node->setName('666');
  127. }
  128. public function testGetLastModified(): void {
  129. $this->assertSame($this->node->getLastModified(), null);
  130. }
  131. public function testUpdateComment(): void {
  132. $msg = 'Hello Earth';
  133. $user = $this->getMockBuilder(IUser::class)
  134. ->disableOriginalConstructor()
  135. ->getMock();
  136. $user->expects($this->once())
  137. ->method('getUID')
  138. ->willReturn('alice');
  139. $this->userSession->expects($this->once())
  140. ->method('getUser')
  141. ->willReturn($user);
  142. $this->comment->expects($this->once())
  143. ->method('setMessage')
  144. ->with($msg);
  145. $this->comment->expects($this->any())
  146. ->method('getActorType')
  147. ->willReturn('users');
  148. $this->comment->expects($this->any())
  149. ->method('getActorId')
  150. ->willReturn('alice');
  151. $this->commentsManager->expects($this->once())
  152. ->method('save')
  153. ->with($this->comment);
  154. $this->assertTrue($this->node->updateComment($msg));
  155. }
  156. public function testUpdateCommentLogException(): void {
  157. $this->expectException(\Exception::class);
  158. $this->expectExceptionMessage('buh!');
  159. $msg = null;
  160. $user = $this->getMockBuilder(IUser::class)
  161. ->disableOriginalConstructor()
  162. ->getMock();
  163. $user->expects($this->once())
  164. ->method('getUID')
  165. ->willReturn('alice');
  166. $this->userSession->expects($this->once())
  167. ->method('getUser')
  168. ->willReturn($user);
  169. $this->comment->expects($this->once())
  170. ->method('setMessage')
  171. ->with($msg)
  172. ->will($this->throwException(new \Exception('buh!')));
  173. $this->comment->expects($this->any())
  174. ->method('getActorType')
  175. ->willReturn('users');
  176. $this->comment->expects($this->any())
  177. ->method('getActorId')
  178. ->willReturn('alice');
  179. $this->commentsManager->expects($this->never())
  180. ->method('save');
  181. $this->logger->expects($this->once())
  182. ->method('error');
  183. $this->node->updateComment($msg);
  184. }
  185. public function testUpdateCommentMessageTooLongException(): void {
  186. $this->expectException(\Sabre\DAV\Exception\BadRequest::class);
  187. $this->expectExceptionMessage('Message exceeds allowed character limit of');
  188. $user = $this->getMockBuilder(IUser::class)
  189. ->disableOriginalConstructor()
  190. ->getMock();
  191. $user->expects($this->once())
  192. ->method('getUID')
  193. ->willReturn('alice');
  194. $this->userSession->expects($this->once())
  195. ->method('getUser')
  196. ->willReturn($user);
  197. $this->comment->expects($this->once())
  198. ->method('setMessage')
  199. ->will($this->throwException(new MessageTooLongException()));
  200. $this->comment->expects($this->any())
  201. ->method('getActorType')
  202. ->willReturn('users');
  203. $this->comment->expects($this->any())
  204. ->method('getActorId')
  205. ->willReturn('alice');
  206. $this->commentsManager->expects($this->never())
  207. ->method('save');
  208. $this->logger->expects($this->once())
  209. ->method('error');
  210. // imagine 'foo' has >1k characters. comment is mocked anyway.
  211. $this->node->updateComment('foo');
  212. }
  213. public function testUpdateForbiddenByUser(): void {
  214. $this->expectException(\Sabre\DAV\Exception\Forbidden::class);
  215. $msg = 'HaXX0r';
  216. $user = $this->getMockBuilder(IUser::class)
  217. ->disableOriginalConstructor()
  218. ->getMock();
  219. $user->expects($this->once())
  220. ->method('getUID')
  221. ->willReturn('mallory');
  222. $this->userSession->expects($this->once())
  223. ->method('getUser')
  224. ->willReturn($user);
  225. $this->comment->expects($this->never())
  226. ->method('setMessage');
  227. $this->comment->expects($this->any())
  228. ->method('getActorType')
  229. ->willReturn('users');
  230. $this->comment->expects($this->any())
  231. ->method('getActorId')
  232. ->willReturn('alice');
  233. $this->commentsManager->expects($this->never())
  234. ->method('save');
  235. $this->node->updateComment($msg);
  236. }
  237. public function testUpdateForbiddenByType(): void {
  238. $this->expectException(\Sabre\DAV\Exception\Forbidden::class);
  239. $msg = 'HaXX0r';
  240. $user = $this->getMockBuilder(IUser::class)
  241. ->disableOriginalConstructor()
  242. ->getMock();
  243. $user->expects($this->never())
  244. ->method('getUID');
  245. $this->userSession->expects($this->once())
  246. ->method('getUser')
  247. ->willReturn($user);
  248. $this->comment->expects($this->never())
  249. ->method('setMessage');
  250. $this->comment->expects($this->any())
  251. ->method('getActorType')
  252. ->willReturn('bots');
  253. $this->commentsManager->expects($this->never())
  254. ->method('save');
  255. $this->node->updateComment($msg);
  256. }
  257. public function testUpdateForbiddenByNotLoggedIn(): void {
  258. $this->expectException(\Sabre\DAV\Exception\Forbidden::class);
  259. $msg = 'HaXX0r';
  260. $this->userSession->expects($this->once())
  261. ->method('getUser')
  262. ->willReturn(null);
  263. $this->comment->expects($this->never())
  264. ->method('setMessage');
  265. $this->comment->expects($this->any())
  266. ->method('getActorType')
  267. ->willReturn('users');
  268. $this->commentsManager->expects($this->never())
  269. ->method('save');
  270. $this->node->updateComment($msg);
  271. }
  272. public function testPropPatch(): void {
  273. $propPatch = $this->getMockBuilder(PropPatch::class)
  274. ->disableOriginalConstructor()
  275. ->getMock();
  276. $propPatch->expects($this->once())
  277. ->method('handle')
  278. ->with('{http://owncloud.org/ns}message');
  279. $this->node->propPatch($propPatch);
  280. }
  281. public function testGetProperties(): void {
  282. $ns = '{http://owncloud.org/ns}';
  283. $expected = [
  284. $ns . 'id' => '123',
  285. $ns . 'parentId' => '12',
  286. $ns . 'topmostParentId' => '2',
  287. $ns . 'childrenCount' => 3,
  288. $ns . 'message' => 'such a nice file you have…',
  289. $ns . 'mentions' => [
  290. [ $ns . 'mention' => [
  291. $ns . 'mentionType' => 'user',
  292. $ns . 'mentionId' => 'alice',
  293. $ns . 'mentionDisplayName' => 'Alice Al-Isson',
  294. ] ],
  295. [ $ns . 'mention' => [
  296. $ns . 'mentionType' => 'user',
  297. $ns . 'mentionId' => 'bob',
  298. $ns . 'mentionDisplayName' => 'Unknown user',
  299. ] ],
  300. ],
  301. $ns . 'verb' => 'comment',
  302. $ns . 'actorType' => 'users',
  303. $ns . 'actorId' => 'alice',
  304. $ns . 'actorDisplayName' => 'Alice of Wonderland',
  305. $ns . 'creationDateTime' => new \DateTime('2016-01-10 18:48:00'),
  306. $ns . 'latestChildDateTime' => new \DateTime('2016-01-12 18:48:00'),
  307. $ns . 'objectType' => 'files',
  308. $ns . 'objectId' => '1848',
  309. $ns . 'referenceId' => 'ref',
  310. $ns . 'isUnread' => null,
  311. $ns . 'reactions' => [],
  312. $ns . 'expireDate' => new \DateTime('2016-01-12 19:00:00'),
  313. ];
  314. $this->commentsManager->expects($this->exactly(2))
  315. ->method('resolveDisplayName')
  316. ->withConsecutive(
  317. [$this->equalTo('user'), $this->equalTo('alice')],
  318. [$this->equalTo('user'), $this->equalTo('bob')]
  319. )
  320. ->willReturnOnConsecutiveCalls('Alice Al-Isson', 'Unknown user');
  321. $this->comment->expects($this->once())
  322. ->method('getId')
  323. ->willReturn($expected[$ns . 'id']);
  324. $this->comment->expects($this->once())
  325. ->method('getParentId')
  326. ->willReturn($expected[$ns . 'parentId']);
  327. $this->comment->expects($this->once())
  328. ->method('getTopmostParentId')
  329. ->willReturn($expected[$ns . 'topmostParentId']);
  330. $this->comment->expects($this->once())
  331. ->method('getChildrenCount')
  332. ->willReturn($expected[$ns . 'childrenCount']);
  333. $this->comment->expects($this->once())
  334. ->method('getMessage')
  335. ->willReturn($expected[$ns . 'message']);
  336. $this->comment->expects($this->once())
  337. ->method('getMentions')
  338. ->willReturn([
  339. ['type' => 'user', 'id' => 'alice'],
  340. ['type' => 'user', 'id' => 'bob'],
  341. ]);
  342. $this->comment->expects($this->once())
  343. ->method('getVerb')
  344. ->willReturn($expected[$ns . 'verb']);
  345. $this->comment->expects($this->exactly(2))
  346. ->method('getActorType')
  347. ->willReturn($expected[$ns . 'actorType']);
  348. $this->comment->expects($this->exactly(2))
  349. ->method('getActorId')
  350. ->willReturn($expected[$ns . 'actorId']);
  351. $this->comment->expects($this->once())
  352. ->method('getCreationDateTime')
  353. ->willReturn($expected[$ns . 'creationDateTime']);
  354. $this->comment->expects($this->once())
  355. ->method('getLatestChildDateTime')
  356. ->willReturn($expected[$ns . 'latestChildDateTime']);
  357. $this->comment->expects($this->once())
  358. ->method('getObjectType')
  359. ->willReturn($expected[$ns . 'objectType']);
  360. $this->comment->expects($this->once())
  361. ->method('getObjectId')
  362. ->willReturn($expected[$ns . 'objectId']);
  363. $this->comment->expects($this->once())
  364. ->method('getReferenceId')
  365. ->willReturn($expected[$ns . 'referenceId']);
  366. $this->comment->expects($this->once())
  367. ->method('getExpireDate')
  368. ->willReturn($expected[$ns . 'expireDate']);
  369. $user = $this->getMockBuilder(IUser::class)
  370. ->disableOriginalConstructor()
  371. ->getMock();
  372. $user->expects($this->once())
  373. ->method('getDisplayName')
  374. ->willReturn($expected[$ns . 'actorDisplayName']);
  375. $this->userManager->expects($this->once())
  376. ->method('get')
  377. ->with('alice')
  378. ->willReturn($user);
  379. $properties = $this->node->getProperties(null);
  380. foreach ($properties as $name => $value) {
  381. $this->assertArrayHasKey($name, $expected);
  382. $this->assertSame($expected[$name], $value);
  383. unset($expected[$name]);
  384. }
  385. $this->assertTrue(empty($expected));
  386. }
  387. public function readCommentProvider() {
  388. $creationDT = new \DateTime('2016-01-19 18:48:00');
  389. $diff = new \DateInterval('PT2H');
  390. $readDT1 = clone $creationDT;
  391. $readDT1->sub($diff);
  392. $readDT2 = clone $creationDT;
  393. $readDT2->add($diff);
  394. return [
  395. [$creationDT, $readDT1, 'true'],
  396. [$creationDT, $readDT2, 'false'],
  397. [$creationDT, null, 'true'],
  398. ];
  399. }
  400. /**
  401. * @dataProvider readCommentProvider
  402. * @param $expected
  403. */
  404. public function testGetPropertiesUnreadProperty($creationDT, $readDT, $expected): void {
  405. $this->comment->expects($this->any())
  406. ->method('getCreationDateTime')
  407. ->willReturn($creationDT);
  408. $this->comment->expects($this->any())
  409. ->method('getMentions')
  410. ->willReturn([]);
  411. $this->commentsManager->expects($this->once())
  412. ->method('getReadMark')
  413. ->willReturn($readDT);
  414. $this->userSession->expects($this->once())
  415. ->method('getUser')
  416. ->willReturn(
  417. $this->getMockBuilder(IUser::class)
  418. ->disableOriginalConstructor()
  419. ->getMock()
  420. );
  421. $properties = $this->node->getProperties(null);
  422. $this->assertTrue(array_key_exists(CommentNode::PROPERTY_NAME_UNREAD, $properties));
  423. $this->assertSame($properties[CommentNode::PROPERTY_NAME_UNREAD], $expected);
  424. }
  425. }