CommentsNodeTest.php 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557
  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 . 'metaData' => [
  313. 'last_edited_at' => 1702553770,
  314. 'last_edited_by_id' => 'charly',
  315. 'last_edited_by_type' => 'user',
  316. ],
  317. $ns . 'expireDate' => new \DateTime('2016-01-12 19:00:00'),
  318. ];
  319. $this->commentsManager->expects($this->exactly(2))
  320. ->method('resolveDisplayName')
  321. ->withConsecutive(
  322. [$this->equalTo('user'), $this->equalTo('alice')],
  323. [$this->equalTo('user'), $this->equalTo('bob')]
  324. )
  325. ->willReturnOnConsecutiveCalls('Alice Al-Isson', 'Unknown user');
  326. $this->comment->expects($this->once())
  327. ->method('getId')
  328. ->willReturn($expected[$ns . 'id']);
  329. $this->comment->expects($this->once())
  330. ->method('getParentId')
  331. ->willReturn($expected[$ns . 'parentId']);
  332. $this->comment->expects($this->once())
  333. ->method('getTopmostParentId')
  334. ->willReturn($expected[$ns . 'topmostParentId']);
  335. $this->comment->expects($this->once())
  336. ->method('getChildrenCount')
  337. ->willReturn($expected[$ns . 'childrenCount']);
  338. $this->comment->expects($this->once())
  339. ->method('getMessage')
  340. ->willReturn($expected[$ns . 'message']);
  341. $this->comment->expects($this->once())
  342. ->method('getMentions')
  343. ->willReturn([
  344. ['type' => 'user', 'id' => 'alice'],
  345. ['type' => 'user', 'id' => 'bob'],
  346. ]);
  347. $this->comment->expects($this->once())
  348. ->method('getVerb')
  349. ->willReturn($expected[$ns . 'verb']);
  350. $this->comment->expects($this->exactly(2))
  351. ->method('getActorType')
  352. ->willReturn($expected[$ns . 'actorType']);
  353. $this->comment->expects($this->exactly(2))
  354. ->method('getActorId')
  355. ->willReturn($expected[$ns . 'actorId']);
  356. $this->comment->expects($this->once())
  357. ->method('getCreationDateTime')
  358. ->willReturn($expected[$ns . 'creationDateTime']);
  359. $this->comment->expects($this->once())
  360. ->method('getLatestChildDateTime')
  361. ->willReturn($expected[$ns . 'latestChildDateTime']);
  362. $this->comment->expects($this->once())
  363. ->method('getObjectType')
  364. ->willReturn($expected[$ns . 'objectType']);
  365. $this->comment->expects($this->once())
  366. ->method('getObjectId')
  367. ->willReturn($expected[$ns . 'objectId']);
  368. $this->comment->expects($this->once())
  369. ->method('getReferenceId')
  370. ->willReturn($expected[$ns . 'referenceId']);
  371. $this->comment->expects($this->once())
  372. ->method('getMetaData')
  373. ->willReturn($expected[$ns . 'metaData']);
  374. $this->comment->expects($this->once())
  375. ->method('getExpireDate')
  376. ->willReturn($expected[$ns . 'expireDate']);
  377. $user = $this->getMockBuilder(IUser::class)
  378. ->disableOriginalConstructor()
  379. ->getMock();
  380. $user->expects($this->once())
  381. ->method('getDisplayName')
  382. ->willReturn($expected[$ns . 'actorDisplayName']);
  383. $this->userManager->expects($this->once())
  384. ->method('get')
  385. ->with('alice')
  386. ->willReturn($user);
  387. $properties = $this->node->getProperties(null);
  388. foreach ($properties as $name => $value) {
  389. $this->assertArrayHasKey($name, $expected, 'Key not found in the list of $expected');
  390. $this->assertSame($expected[$name], $value);
  391. unset($expected[$name]);
  392. }
  393. $this->assertTrue(empty($expected));
  394. }
  395. public function readCommentProvider() {
  396. $creationDT = new \DateTime('2016-01-19 18:48:00');
  397. $diff = new \DateInterval('PT2H');
  398. $readDT1 = clone $creationDT;
  399. $readDT1->sub($diff);
  400. $readDT2 = clone $creationDT;
  401. $readDT2->add($diff);
  402. return [
  403. [$creationDT, $readDT1, 'true'],
  404. [$creationDT, $readDT2, 'false'],
  405. [$creationDT, null, 'true'],
  406. ];
  407. }
  408. /**
  409. * @dataProvider readCommentProvider
  410. * @param $expected
  411. */
  412. public function testGetPropertiesUnreadProperty($creationDT, $readDT, $expected): void {
  413. $this->comment->expects($this->any())
  414. ->method('getCreationDateTime')
  415. ->willReturn($creationDT);
  416. $this->comment->expects($this->any())
  417. ->method('getMentions')
  418. ->willReturn([]);
  419. $this->commentsManager->expects($this->once())
  420. ->method('getReadMark')
  421. ->willReturn($readDT);
  422. $this->userSession->expects($this->once())
  423. ->method('getUser')
  424. ->willReturn(
  425. $this->getMockBuilder(IUser::class)
  426. ->disableOriginalConstructor()
  427. ->getMock()
  428. );
  429. $properties = $this->node->getProperties(null);
  430. $this->assertTrue(array_key_exists(CommentNode::PROPERTY_NAME_UNREAD, $properties));
  431. $this->assertSame($properties[CommentNode::PROPERTY_NAME_UNREAD], $expected);
  432. }
  433. }