CommentsNodeTest.php 13 KB

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