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.

SystemTagNodeTest.php 8.2KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304
  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\ISystemTag;
  30. use OCP\SystemTag\ISystemTagManager;
  31. use OCP\SystemTag\TagAlreadyExistsException;
  32. use OCP\SystemTag\TagNotFoundException;
  33. use Sabre\DAV\Exception\Forbidden;
  34. class SystemTagNodeTest extends \Test\TestCase {
  35. /**
  36. * @var \OCP\SystemTag\ISystemTagManager|\PHPUnit\Framework\MockObject\MockObject
  37. */
  38. private $tagManager;
  39. /**
  40. * @var \OCP\IUser
  41. */
  42. private $user;
  43. protected function setUp(): void {
  44. parent::setUp();
  45. $this->tagManager = $this->getMockBuilder(ISystemTagManager::class)
  46. ->getMock();
  47. $this->user = $this->getMockBuilder(IUser::class)
  48. ->getMock();
  49. }
  50. protected function getTagNode($isAdmin = true, $tag = null) {
  51. if ($tag === null) {
  52. $tag = new SystemTag(1, 'Test', true, true);
  53. }
  54. return new \OCA\DAV\SystemTag\SystemTagNode(
  55. $tag,
  56. $this->user,
  57. $isAdmin,
  58. $this->tagManager
  59. );
  60. }
  61. public function adminFlagProvider() {
  62. return [[true], [false]];
  63. }
  64. /**
  65. * @dataProvider adminFlagProvider
  66. */
  67. public function testGetters($isAdmin) {
  68. $tag = new SystemTag('1', 'Test', true, true);
  69. $node = $this->getTagNode($isAdmin, $tag);
  70. $this->assertEquals('1', $node->getName());
  71. $this->assertEquals($tag, $node->getSystemTag());
  72. }
  73. public function testSetName() {
  74. $this->expectException(\Sabre\DAV\Exception\MethodNotAllowed::class);
  75. $this->getTagNode()->setName('2');
  76. }
  77. public function tagNodeProvider() {
  78. return [
  79. // admin
  80. [
  81. true,
  82. new SystemTag(1, 'Original', true, true),
  83. ['Renamed', true, true]
  84. ],
  85. [
  86. true,
  87. new SystemTag(1, 'Original', true, true),
  88. ['Original', false, false]
  89. ],
  90. // non-admin
  91. [
  92. // renaming allowed
  93. false,
  94. new SystemTag(1, 'Original', true, true),
  95. ['Rename', true, true]
  96. ],
  97. ];
  98. }
  99. /**
  100. * @dataProvider tagNodeProvider
  101. */
  102. public function testUpdateTag($isAdmin, ISystemTag $originalTag, $changedArgs) {
  103. $this->tagManager->expects($this->once())
  104. ->method('canUserSeeTag')
  105. ->with($originalTag)
  106. ->willReturn($originalTag->isUserVisible() || $isAdmin);
  107. $this->tagManager->expects($this->once())
  108. ->method('canUserAssignTag')
  109. ->with($originalTag)
  110. ->willReturn($originalTag->isUserAssignable() || $isAdmin);
  111. $this->tagManager->expects($this->once())
  112. ->method('updateTag')
  113. ->with(1, $changedArgs[0], $changedArgs[1], $changedArgs[2]);
  114. $this->getTagNode($isAdmin, $originalTag)
  115. ->update($changedArgs[0], $changedArgs[1], $changedArgs[2]);
  116. }
  117. public function tagNodeProviderPermissionException() {
  118. return [
  119. [
  120. // changing permissions not allowed
  121. new SystemTag(1, 'Original', true, true),
  122. ['Original', false, true],
  123. 'Sabre\DAV\Exception\Forbidden',
  124. ],
  125. [
  126. // changing permissions not allowed
  127. new SystemTag(1, 'Original', true, true),
  128. ['Original', true, false],
  129. 'Sabre\DAV\Exception\Forbidden',
  130. ],
  131. [
  132. // changing permissions not allowed
  133. new SystemTag(1, 'Original', true, true),
  134. ['Original', false, false],
  135. 'Sabre\DAV\Exception\Forbidden',
  136. ],
  137. [
  138. // changing non-assignable not allowed
  139. new SystemTag(1, 'Original', true, false),
  140. ['Rename', true, false],
  141. 'Sabre\DAV\Exception\Forbidden',
  142. ],
  143. [
  144. // changing non-assignable not allowed
  145. new SystemTag(1, 'Original', true, false),
  146. ['Original', true, true],
  147. 'Sabre\DAV\Exception\Forbidden',
  148. ],
  149. [
  150. // invisible tag does not exist
  151. new SystemTag(1, 'Original', false, false),
  152. ['Rename', false, false],
  153. 'Sabre\DAV\Exception\NotFound',
  154. ],
  155. ];
  156. }
  157. /**
  158. * @dataProvider tagNodeProviderPermissionException
  159. */
  160. public function testUpdateTagPermissionException(ISystemTag $originalTag, $changedArgs, $expectedException = null) {
  161. $this->tagManager->expects($this->any())
  162. ->method('canUserSeeTag')
  163. ->with($originalTag)
  164. ->willReturn($originalTag->isUserVisible());
  165. $this->tagManager->expects($this->any())
  166. ->method('canUserAssignTag')
  167. ->with($originalTag)
  168. ->willReturn($originalTag->isUserAssignable());
  169. $this->tagManager->expects($this->never())
  170. ->method('updateTag');
  171. $thrown = null;
  172. try {
  173. $this->getTagNode(false, $originalTag)
  174. ->update($changedArgs[0], $changedArgs[1], $changedArgs[2]);
  175. } catch (\Exception $e) {
  176. $thrown = $e;
  177. }
  178. $this->assertInstanceOf($expectedException, $thrown);
  179. }
  180. public function testUpdateTagAlreadyExists() {
  181. $this->expectException(\Sabre\DAV\Exception\Conflict::class);
  182. $tag = new SystemTag(1, 'tag1', true, true);
  183. $this->tagManager->expects($this->any())
  184. ->method('canUserSeeTag')
  185. ->with($tag)
  186. ->willReturn(true);
  187. $this->tagManager->expects($this->any())
  188. ->method('canUserAssignTag')
  189. ->with($tag)
  190. ->willReturn(true);
  191. $this->tagManager->expects($this->once())
  192. ->method('updateTag')
  193. ->with(1, 'Renamed', true, true)
  194. ->will($this->throwException(new TagAlreadyExistsException()));
  195. $this->getTagNode(false, $tag)->update('Renamed', true, true);
  196. }
  197. public function testUpdateTagNotFound() {
  198. $this->expectException(\Sabre\DAV\Exception\NotFound::class);
  199. $tag = new SystemTag(1, 'tag1', true, true);
  200. $this->tagManager->expects($this->any())
  201. ->method('canUserSeeTag')
  202. ->with($tag)
  203. ->willReturn(true);
  204. $this->tagManager->expects($this->any())
  205. ->method('canUserAssignTag')
  206. ->with($tag)
  207. ->willReturn(true);
  208. $this->tagManager->expects($this->once())
  209. ->method('updateTag')
  210. ->with(1, 'Renamed', true, true)
  211. ->will($this->throwException(new TagNotFoundException()));
  212. $this->getTagNode(false, $tag)->update('Renamed', true, true);
  213. }
  214. /**
  215. * @dataProvider adminFlagProvider
  216. */
  217. public function testDeleteTag($isAdmin) {
  218. $tag = new SystemTag(1, 'tag1', true, true);
  219. $this->tagManager->expects($isAdmin ? $this->once() : $this->never())
  220. ->method('canUserSeeTag')
  221. ->with($tag)
  222. ->willReturn(true);
  223. $this->tagManager->expects($isAdmin ? $this->once() : $this->never())
  224. ->method('deleteTags')
  225. ->with('1');
  226. if (!$isAdmin) {
  227. $this->expectException(Forbidden::class);
  228. }
  229. $this->getTagNode($isAdmin, $tag)->delete();
  230. }
  231. public function tagNodeDeleteProviderPermissionException() {
  232. return [
  233. [
  234. // cannot delete invisible tag
  235. new SystemTag(1, 'Original', false, true),
  236. 'Sabre\DAV\Exception\Forbidden',
  237. ],
  238. [
  239. // cannot delete non-assignable tag
  240. new SystemTag(1, 'Original', true, false),
  241. 'Sabre\DAV\Exception\Forbidden',
  242. ],
  243. ];
  244. }
  245. /**
  246. * @dataProvider tagNodeDeleteProviderPermissionException
  247. */
  248. public function testDeleteTagPermissionException(ISystemTag $tag, $expectedException) {
  249. $this->tagManager->expects($this->any())
  250. ->method('canUserSeeTag')
  251. ->with($tag)
  252. ->willReturn($tag->isUserVisible());
  253. $this->tagManager->expects($this->never())
  254. ->method('deleteTags');
  255. $this->expectException($expectedException);
  256. $this->getTagNode(false, $tag)->delete();
  257. }
  258. public function testDeleteTagNotFound() {
  259. $this->expectException(\Sabre\DAV\Exception\NotFound::class);
  260. $tag = new SystemTag(1, 'tag1', true, true);
  261. $this->tagManager->expects($this->any())
  262. ->method('canUserSeeTag')
  263. ->with($tag)
  264. ->willReturn($tag->isUserVisible());
  265. $this->tagManager->expects($this->once())
  266. ->method('deleteTags')
  267. ->with('1')
  268. ->will($this->throwException(new TagNotFoundException()));
  269. $this->getTagNode(true, $tag)->delete();
  270. }
  271. }