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.

CommentsNodeTest.php 14KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541
  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 <pvince81@owncloud.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\ILogger;
  33. use OCP\IUser;
  34. use OCP\IUserManager;
  35. use OCP\IUserSession;
  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(ILogger::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() {
  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() {
  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() {
  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() {
  125. $this->expectException(\Sabre\DAV\Exception\MethodNotAllowed::class);
  126. $this->node->setName('666');
  127. }
  128. public function testGetLastModified() {
  129. $this->assertSame($this->node->getLastModified(), null);
  130. }
  131. public function testUpdateComment() {
  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() {
  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('logException');
  183. $this->node->updateComment($msg);
  184. }
  185. public function testUpdateCommentMessageTooLongException() {
  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('logException');
  210. // imagine 'foo' has >1k characters. comment is mocked anyway.
  211. $this->node->updateComment('foo');
  212. }
  213. public function testUpdateForbiddenByUser() {
  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() {
  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() {
  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() {
  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() {
  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. ];
  312. $this->commentsManager->expects($this->exactly(2))
  313. ->method('resolveDisplayName')
  314. ->withConsecutive(
  315. [$this->equalTo('user'), $this->equalTo('alice')],
  316. [$this->equalTo('user'), $this->equalTo('bob')]
  317. )
  318. ->willReturnOnConsecutiveCalls('Alice Al-Isson', 'Unknown user');
  319. $this->comment->expects($this->once())
  320. ->method('getId')
  321. ->willReturn($expected[$ns . 'id']);
  322. $this->comment->expects($this->once())
  323. ->method('getParentId')
  324. ->willReturn($expected[$ns . 'parentId']);
  325. $this->comment->expects($this->once())
  326. ->method('getTopmostParentId')
  327. ->willReturn($expected[$ns . 'topmostParentId']);
  328. $this->comment->expects($this->once())
  329. ->method('getChildrenCount')
  330. ->willReturn($expected[$ns . 'childrenCount']);
  331. $this->comment->expects($this->once())
  332. ->method('getMessage')
  333. ->willReturn($expected[$ns . 'message']);
  334. $this->comment->expects($this->once())
  335. ->method('getMentions')
  336. ->willReturn([
  337. ['type' => 'user', 'id' => 'alice'],
  338. ['type' => 'user', 'id' => 'bob'],
  339. ]);
  340. $this->comment->expects($this->once())
  341. ->method('getVerb')
  342. ->willReturn($expected[$ns . 'verb']);
  343. $this->comment->expects($this->exactly(2))
  344. ->method('getActorType')
  345. ->willReturn($expected[$ns . 'actorType']);
  346. $this->comment->expects($this->exactly(2))
  347. ->method('getActorId')
  348. ->willReturn($expected[$ns . 'actorId']);
  349. $this->comment->expects($this->once())
  350. ->method('getCreationDateTime')
  351. ->willReturn($expected[$ns . 'creationDateTime']);
  352. $this->comment->expects($this->once())
  353. ->method('getLatestChildDateTime')
  354. ->willReturn($expected[$ns . 'latestChildDateTime']);
  355. $this->comment->expects($this->once())
  356. ->method('getObjectType')
  357. ->willReturn($expected[$ns . 'objectType']);
  358. $this->comment->expects($this->once())
  359. ->method('getObjectId')
  360. ->willReturn($expected[$ns . 'objectId']);
  361. $this->comment->expects($this->once())
  362. ->method('getReferenceId')
  363. ->willReturn($expected[$ns . 'referenceId']);
  364. $user = $this->getMockBuilder(IUser::class)
  365. ->disableOriginalConstructor()
  366. ->getMock();
  367. $user->expects($this->once())
  368. ->method('getDisplayName')
  369. ->willReturn($expected[$ns . 'actorDisplayName']);
  370. $this->userManager->expects($this->once())
  371. ->method('get')
  372. ->with('alice')
  373. ->willReturn($user);
  374. $properties = $this->node->getProperties(null);
  375. foreach($properties as $name => $value) {
  376. $this->assertArrayHasKey($name, $expected);
  377. $this->assertSame($expected[$name], $value);
  378. unset($expected[$name]);
  379. }
  380. $this->assertTrue(empty($expected));
  381. }
  382. public function readCommentProvider() {
  383. $creationDT = new \DateTime('2016-01-19 18:48:00');
  384. $diff = new \DateInterval('PT2H');
  385. $readDT1 = clone $creationDT; $readDT1->sub($diff);
  386. $readDT2 = clone $creationDT; $readDT2->add($diff);
  387. return [
  388. [$creationDT, $readDT1, 'true'],
  389. [$creationDT, $readDT2, 'false'],
  390. [$creationDT, null, 'true'],
  391. ];
  392. }
  393. /**
  394. * @dataProvider readCommentProvider
  395. * @param $expected
  396. */
  397. public function testGetPropertiesUnreadProperty($creationDT, $readDT, $expected) {
  398. $this->comment->expects($this->any())
  399. ->method('getCreationDateTime')
  400. ->willReturn($creationDT);
  401. $this->comment->expects($this->any())
  402. ->method('getMentions')
  403. ->willReturn([]);
  404. $this->commentsManager->expects($this->once())
  405. ->method('getReadMark')
  406. ->willReturn($readDT);
  407. $this->userSession->expects($this->once())
  408. ->method('getUser')
  409. ->willReturn(
  410. $this->getMockBuilder(IUser::class)
  411. ->disableOriginalConstructor()
  412. ->getMock()
  413. );
  414. $properties = $this->node->getProperties(null);
  415. $this->assertTrue(array_key_exists(CommentNode::PROPERTY_NAME_UNREAD, $properties));
  416. $this->assertSame($properties[CommentNode::PROPERTY_NAME_UNREAD], $expected);
  417. }
  418. }