CommentsNodeTest.php 12 KB

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