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.

SystemTagsObjectMappingCollectionTest.php 9.4KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358
  1. <?php
  2. /**
  3. * @copyright Copyright (c) 2016, ownCloud, Inc.
  4. *
  5. * @author Christoph Wurst <christoph@winzerhof-wurst.at>
  6. * @author Joas Schilling <coding@schilljs.com>
  7. * @author Morris Jobke <hey@morrisjobke.de>
  8. * @author Roeland Jago Douma <roeland@famdouma.nl>
  9. * @author Vincent Petry <vincent@nextcloud.com>
  10. *
  11. * @license AGPL-3.0
  12. *
  13. * This code is free software: you can redistribute it and/or modify
  14. * it under the terms of the GNU Affero General Public License, version 3,
  15. * as published by the Free Software Foundation.
  16. *
  17. * This program is distributed in the hope that it will be useful,
  18. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  19. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  20. * GNU Affero General Public License for more details.
  21. *
  22. * You should have received a copy of the GNU Affero General Public License, version 3,
  23. * along with this program. If not, see <http://www.gnu.org/licenses/>
  24. *
  25. */
  26. namespace OCA\DAV\Tests\unit\SystemTag;
  27. use OC\SystemTag\SystemTag;
  28. use OCP\IUser;
  29. use OCP\SystemTag\ISystemTagManager;
  30. use OCP\SystemTag\ISystemTagObjectMapper;
  31. use OCP\SystemTag\TagNotFoundException;
  32. class SystemTagsObjectMappingCollectionTest extends \Test\TestCase {
  33. /**
  34. * @var \OCP\SystemTag\ISystemTagManager
  35. */
  36. private $tagManager;
  37. /**
  38. * @var \OCP\SystemTag\ISystemTagObjectMapper
  39. */
  40. private $tagMapper;
  41. /**
  42. * @var \OCP\IUser
  43. */
  44. private $user;
  45. protected function setUp(): void {
  46. parent::setUp();
  47. $this->tagManager = $this->getMockBuilder(ISystemTagManager::class)
  48. ->getMock();
  49. $this->tagMapper = $this->getMockBuilder(ISystemTagObjectMapper::class)
  50. ->getMock();
  51. $this->user = $this->getMockBuilder(IUser::class)
  52. ->getMock();
  53. }
  54. public function getNode() {
  55. return new \OCA\DAV\SystemTag\SystemTagsObjectMappingCollection(
  56. 111,
  57. 'files',
  58. $this->user,
  59. $this->tagManager,
  60. $this->tagMapper
  61. );
  62. }
  63. public function testAssignTag() {
  64. $tag = new SystemTag('1', 'Test', true, true);
  65. $this->tagManager->expects($this->once())
  66. ->method('canUserSeeTag')
  67. ->with($tag)
  68. ->willReturn(true);
  69. $this->tagManager->expects($this->once())
  70. ->method('canUserAssignTag')
  71. ->with($tag)
  72. ->willReturn(true);
  73. $this->tagManager->expects($this->once())
  74. ->method('getTagsByIds')
  75. ->with(['555'])
  76. ->willReturn([$tag]);
  77. $this->tagMapper->expects($this->once())
  78. ->method('assignTags')
  79. ->with(111, 'files', '555');
  80. $this->getNode()->createFile('555');
  81. }
  82. public function permissionsProvider() {
  83. return [
  84. // invisible, tag does not exist for user
  85. [false, true, '\Sabre\DAV\Exception\PreconditionFailed'],
  86. // visible but static, cannot assign tag
  87. [true, false, '\Sabre\DAV\Exception\Forbidden'],
  88. ];
  89. }
  90. /**
  91. * @dataProvider permissionsProvider
  92. */
  93. public function testAssignTagNoPermission($userVisible, $userAssignable, $expectedException) {
  94. $tag = new SystemTag('1', 'Test', $userVisible, $userAssignable);
  95. $this->tagManager->expects($this->once())
  96. ->method('canUserSeeTag')
  97. ->with($tag)
  98. ->willReturn($userVisible);
  99. $this->tagManager->expects($this->any())
  100. ->method('canUserAssignTag')
  101. ->with($tag)
  102. ->willReturn($userAssignable);
  103. $this->tagManager->expects($this->once())
  104. ->method('getTagsByIds')
  105. ->with(['555'])
  106. ->willReturn([$tag]);
  107. $this->tagMapper->expects($this->never())
  108. ->method('assignTags');
  109. $thrown = null;
  110. try {
  111. $this->getNode()->createFile('555');
  112. } catch (\Exception $e) {
  113. $thrown = $e;
  114. }
  115. $this->assertInstanceOf($expectedException, $thrown);
  116. }
  117. public function testAssignTagNotFound() {
  118. $this->expectException(\Sabre\DAV\Exception\PreconditionFailed::class);
  119. $this->tagManager->expects($this->once())
  120. ->method('getTagsByIds')
  121. ->with(['555'])
  122. ->will($this->throwException(new TagNotFoundException()));
  123. $this->getNode()->createFile('555');
  124. }
  125. public function testForbiddenCreateDirectory() {
  126. $this->expectException(\Sabre\DAV\Exception\Forbidden::class);
  127. $this->getNode()->createDirectory('789');
  128. }
  129. public function testGetChild() {
  130. $tag = new SystemTag(555, 'TheTag', true, false);
  131. $this->tagManager->expects($this->once())
  132. ->method('canUserSeeTag')
  133. ->with($tag)
  134. ->willReturn(true);
  135. $this->tagMapper->expects($this->once())
  136. ->method('haveTag')
  137. ->with([111], 'files', '555', true)
  138. ->willReturn(true);
  139. $this->tagManager->expects($this->once())
  140. ->method('getTagsByIds')
  141. ->with(['555'])
  142. ->willReturn(['555' => $tag]);
  143. $childNode = $this->getNode()->getChild('555');
  144. $this->assertInstanceOf('\OCA\DAV\SystemTag\SystemTagMappingNode', $childNode);
  145. $this->assertEquals('555', $childNode->getName());
  146. }
  147. public function testGetChildNonVisible() {
  148. $this->expectException(\Sabre\DAV\Exception\NotFound::class);
  149. $tag = new SystemTag(555, 'TheTag', false, false);
  150. $this->tagManager->expects($this->once())
  151. ->method('canUserSeeTag')
  152. ->with($tag)
  153. ->willReturn(false);
  154. $this->tagMapper->expects($this->once())
  155. ->method('haveTag')
  156. ->with([111], 'files', '555', true)
  157. ->willReturn(true);
  158. $this->tagManager->expects($this->once())
  159. ->method('getTagsByIds')
  160. ->with(['555'])
  161. ->willReturn(['555' => $tag]);
  162. $this->getNode()->getChild('555');
  163. }
  164. public function testGetChildRelationNotFound() {
  165. $this->expectException(\Sabre\DAV\Exception\NotFound::class);
  166. $this->tagMapper->expects($this->once())
  167. ->method('haveTag')
  168. ->with([111], 'files', '777')
  169. ->willReturn(false);
  170. $this->getNode()->getChild('777');
  171. }
  172. public function testGetChildInvalidId() {
  173. $this->expectException(\Sabre\DAV\Exception\BadRequest::class);
  174. $this->tagMapper->expects($this->once())
  175. ->method('haveTag')
  176. ->with([111], 'files', 'badid')
  177. ->will($this->throwException(new \InvalidArgumentException()));
  178. $this->getNode()->getChild('badid');
  179. }
  180. public function testGetChildTagDoesNotExist() {
  181. $this->expectException(\Sabre\DAV\Exception\NotFound::class);
  182. $this->tagMapper->expects($this->once())
  183. ->method('haveTag')
  184. ->with([111], 'files', '777')
  185. ->will($this->throwException(new TagNotFoundException()));
  186. $this->getNode()->getChild('777');
  187. }
  188. public function testGetChildren() {
  189. $tag1 = new SystemTag(555, 'TagOne', true, false);
  190. $tag2 = new SystemTag(556, 'TagTwo', true, true);
  191. $tag3 = new SystemTag(557, 'InvisibleTag', false, true);
  192. $this->tagMapper->expects($this->once())
  193. ->method('getTagIdsForObjects')
  194. ->with([111], 'files')
  195. ->willReturn(['111' => ['555', '556', '557']]);
  196. $this->tagManager->expects($this->once())
  197. ->method('getTagsByIds')
  198. ->with(['555', '556', '557'])
  199. ->willReturn(['555' => $tag1, '556' => $tag2, '557' => $tag3]);
  200. $this->tagManager->expects($this->exactly(3))
  201. ->method('canUserSeeTag')
  202. ->willReturnCallback(function ($tag) {
  203. return $tag->isUserVisible();
  204. });
  205. $children = $this->getNode()->getChildren();
  206. $this->assertCount(2, $children);
  207. $this->assertInstanceOf('\OCA\DAV\SystemTag\SystemTagMappingNode', $children[0]);
  208. $this->assertInstanceOf('\OCA\DAV\SystemTag\SystemTagMappingNode', $children[1]);
  209. $this->assertEquals(111, $children[0]->getObjectId());
  210. $this->assertEquals('files', $children[0]->getObjectType());
  211. $this->assertEquals($tag1, $children[0]->getSystemTag());
  212. $this->assertEquals(111, $children[1]->getObjectId());
  213. $this->assertEquals('files', $children[1]->getObjectType());
  214. $this->assertEquals($tag2, $children[1]->getSystemTag());
  215. }
  216. public function testChildExistsWithVisibleTag() {
  217. $tag = new SystemTag(555, 'TagOne', true, false);
  218. $this->tagMapper->expects($this->once())
  219. ->method('haveTag')
  220. ->with([111], 'files', '555')
  221. ->willReturn(true);
  222. $this->tagManager->expects($this->once())
  223. ->method('canUserSeeTag')
  224. ->with($tag)
  225. ->willReturn(true);
  226. $this->tagManager->expects($this->once())
  227. ->method('getTagsByIds')
  228. ->with(['555'])
  229. ->willReturn([$tag]);
  230. $this->assertTrue($this->getNode()->childExists('555'));
  231. }
  232. public function testChildExistsWithInvisibleTag() {
  233. $tag = new SystemTag(555, 'TagOne', false, false);
  234. $this->tagMapper->expects($this->once())
  235. ->method('haveTag')
  236. ->with([111], 'files', '555')
  237. ->willReturn(true);
  238. $this->tagManager->expects($this->once())
  239. ->method('getTagsByIds')
  240. ->with(['555'])
  241. ->willReturn([$tag]);
  242. $this->assertFalse($this->getNode()->childExists('555'));
  243. }
  244. public function testChildExistsNotFound() {
  245. $this->tagMapper->expects($this->once())
  246. ->method('haveTag')
  247. ->with([111], 'files', '555')
  248. ->willReturn(false);
  249. $this->assertFalse($this->getNode()->childExists('555'));
  250. }
  251. public function testChildExistsTagNotFound() {
  252. $this->tagMapper->expects($this->once())
  253. ->method('haveTag')
  254. ->with([111], 'files', '555')
  255. ->will($this->throwException(new TagNotFoundException()));
  256. $this->assertFalse($this->getNode()->childExists('555'));
  257. }
  258. public function testChildExistsInvalidId() {
  259. $this->expectException(\Sabre\DAV\Exception\BadRequest::class);
  260. $this->tagMapper->expects($this->once())
  261. ->method('haveTag')
  262. ->with([111], 'files', '555')
  263. ->will($this->throwException(new \InvalidArgumentException()));
  264. $this->getNode()->childExists('555');
  265. }
  266. public function testDelete() {
  267. $this->expectException(\Sabre\DAV\Exception\Forbidden::class);
  268. $this->getNode()->delete();
  269. }
  270. public function testSetName() {
  271. $this->expectException(\Sabre\DAV\Exception\Forbidden::class);
  272. $this->getNode()->setName('somethingelse');
  273. }
  274. public function testGetName() {
  275. $this->assertEquals('111', $this->getNode()->getName());
  276. }
  277. }