SystemTagNodeTest.php 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304
  1. <?php
  2. /**
  3. * @copyright Copyright (c) 2016, ownCloud, Inc.
  4. *
  5. * @author Joas Schilling <coding@schilljs.com>
  6. * @author Morris Jobke <hey@morrisjobke.de>
  7. * @author Roeland Jago Douma <roeland@famdouma.nl>
  8. * @author Vincent Petry <pvince81@owncloud.com>
  9. *
  10. * @license AGPL-3.0
  11. *
  12. * This code is free software: you can redistribute it and/or modify
  13. * it under the terms of the GNU Affero General Public License, version 3,
  14. * as published by the Free Software Foundation.
  15. *
  16. * This program is distributed in the hope that it will be useful,
  17. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  18. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  19. * GNU Affero General Public License for more details.
  20. *
  21. * You should have received a copy of the GNU Affero General Public License, version 3,
  22. * along with this program. If not, see <http://www.gnu.org/licenses/>
  23. *
  24. */
  25. namespace OCA\DAV\Tests\unit\SystemTag;
  26. use OC\SystemTag\SystemTag;
  27. use OCP\IUser;
  28. use OCP\SystemTag\ISystemTag;
  29. use OCP\SystemTag\ISystemTagManager;
  30. use OCP\SystemTag\TagAlreadyExistsException;
  31. use OCP\SystemTag\TagNotFoundException;
  32. use Sabre\DAV\Exception\Forbidden;
  33. class SystemTagNodeTest extends \Test\TestCase {
  34. /**
  35. * @var \OCP\SystemTag\ISystemTagManager|\PHPUnit_Framework_MockObject_MockObject
  36. */
  37. private $tagManager;
  38. /**
  39. * @var \OCP\IUser
  40. */
  41. private $user;
  42. protected function setUp(): void {
  43. parent::setUp();
  44. $this->tagManager = $this->getMockBuilder(ISystemTagManager::class)
  45. ->getMock();
  46. $this->user = $this->getMockBuilder(IUser::class)
  47. ->getMock();
  48. }
  49. protected function getTagNode($isAdmin = true, $tag = null) {
  50. if ($tag === null) {
  51. $tag = new SystemTag(1, 'Test', true, true);
  52. }
  53. return new \OCA\DAV\SystemTag\SystemTagNode(
  54. $tag,
  55. $this->user,
  56. $isAdmin,
  57. $this->tagManager
  58. );
  59. }
  60. public function adminFlagProvider() {
  61. return [[true], [false]];
  62. }
  63. /**
  64. * @dataProvider adminFlagProvider
  65. */
  66. public function testGetters($isAdmin) {
  67. $tag = new SystemTag('1', 'Test', true, true);
  68. $node = $this->getTagNode($isAdmin, $tag);
  69. $this->assertEquals('1', $node->getName());
  70. $this->assertEquals($tag, $node->getSystemTag());
  71. }
  72. public function testSetName() {
  73. $this->expectException(\Sabre\DAV\Exception\MethodNotAllowed::class);
  74. $this->getTagNode()->setName('2');
  75. }
  76. public function tagNodeProvider() {
  77. return [
  78. // admin
  79. [
  80. true,
  81. new SystemTag(1, 'Original', true, true),
  82. ['Renamed', true, true]
  83. ],
  84. [
  85. true,
  86. new SystemTag(1, 'Original', true, true),
  87. ['Original', false, false]
  88. ],
  89. // non-admin
  90. [
  91. // renaming allowed
  92. false,
  93. new SystemTag(1, 'Original', true, true),
  94. ['Rename', true, true]
  95. ],
  96. ];
  97. }
  98. /**
  99. * @dataProvider tagNodeProvider
  100. */
  101. public function testUpdateTag($isAdmin, ISystemTag $originalTag, $changedArgs) {
  102. $this->tagManager->expects($this->once())
  103. ->method('canUserSeeTag')
  104. ->with($originalTag)
  105. ->will($this->returnValue($originalTag->isUserVisible() || $isAdmin));
  106. $this->tagManager->expects($this->once())
  107. ->method('canUserAssignTag')
  108. ->with($originalTag)
  109. ->will($this->returnValue($originalTag->isUserAssignable() || $isAdmin));
  110. $this->tagManager->expects($this->once())
  111. ->method('updateTag')
  112. ->with(1, $changedArgs[0], $changedArgs[1], $changedArgs[2]);
  113. $this->getTagNode($isAdmin, $originalTag)
  114. ->update($changedArgs[0], $changedArgs[1], $changedArgs[2]);
  115. }
  116. public function tagNodeProviderPermissionException() {
  117. return [
  118. [
  119. // changing permissions not allowed
  120. new SystemTag(1, 'Original', true, true),
  121. ['Original', false, true],
  122. 'Sabre\DAV\Exception\Forbidden',
  123. ],
  124. [
  125. // changing permissions not allowed
  126. new SystemTag(1, 'Original', true, true),
  127. ['Original', true, false],
  128. 'Sabre\DAV\Exception\Forbidden',
  129. ],
  130. [
  131. // changing permissions not allowed
  132. new SystemTag(1, 'Original', true, true),
  133. ['Original', false, false],
  134. 'Sabre\DAV\Exception\Forbidden',
  135. ],
  136. [
  137. // changing non-assignable not allowed
  138. new SystemTag(1, 'Original', true, false),
  139. ['Rename', true, false],
  140. 'Sabre\DAV\Exception\Forbidden',
  141. ],
  142. [
  143. // changing non-assignable not allowed
  144. new SystemTag(1, 'Original', true, false),
  145. ['Original', true, true],
  146. 'Sabre\DAV\Exception\Forbidden',
  147. ],
  148. [
  149. // invisible tag does not exist
  150. new SystemTag(1, 'Original', false, false),
  151. ['Rename', false, false],
  152. 'Sabre\DAV\Exception\NotFound',
  153. ],
  154. ];
  155. }
  156. /**
  157. * @dataProvider tagNodeProviderPermissionException
  158. */
  159. public function testUpdateTagPermissionException(ISystemTag $originalTag, $changedArgs, $expectedException = null) {
  160. $this->tagManager->expects($this->any())
  161. ->method('canUserSeeTag')
  162. ->with($originalTag)
  163. ->will($this->returnValue($originalTag->isUserVisible()));
  164. $this->tagManager->expects($this->any())
  165. ->method('canUserAssignTag')
  166. ->with($originalTag)
  167. ->will($this->returnValue($originalTag->isUserAssignable()));
  168. $this->tagManager->expects($this->never())
  169. ->method('updateTag');
  170. $thrown = null;
  171. try {
  172. $this->getTagNode(false, $originalTag)
  173. ->update($changedArgs[0], $changedArgs[1], $changedArgs[2]);
  174. } catch (\Exception $e) {
  175. $thrown = $e;
  176. }
  177. $this->assertInstanceOf($expectedException, $thrown);
  178. }
  179. public function testUpdateTagAlreadyExists() {
  180. $this->expectException(\Sabre\DAV\Exception\Conflict::class);
  181. $tag = new SystemTag(1, 'tag1', true, true);
  182. $this->tagManager->expects($this->any())
  183. ->method('canUserSeeTag')
  184. ->with($tag)
  185. ->will($this->returnValue(true));
  186. $this->tagManager->expects($this->any())
  187. ->method('canUserAssignTag')
  188. ->with($tag)
  189. ->will($this->returnValue(true));
  190. $this->tagManager->expects($this->once())
  191. ->method('updateTag')
  192. ->with(1, 'Renamed', true, true)
  193. ->will($this->throwException(new TagAlreadyExistsException()));
  194. $this->getTagNode(false, $tag)->update('Renamed', true, true);
  195. }
  196. public function testUpdateTagNotFound() {
  197. $this->expectException(\Sabre\DAV\Exception\NotFound::class);
  198. $tag = new SystemTag(1, 'tag1', true, true);
  199. $this->tagManager->expects($this->any())
  200. ->method('canUserSeeTag')
  201. ->with($tag)
  202. ->will($this->returnValue(true));
  203. $this->tagManager->expects($this->any())
  204. ->method('canUserAssignTag')
  205. ->with($tag)
  206. ->will($this->returnValue(true));
  207. $this->tagManager->expects($this->once())
  208. ->method('updateTag')
  209. ->with(1, 'Renamed', true, true)
  210. ->will($this->throwException(new TagNotFoundException()));
  211. $this->getTagNode(false, $tag)->update('Renamed', true, true);
  212. }
  213. /**
  214. * @dataProvider adminFlagProvider
  215. */
  216. public function testDeleteTag($isAdmin) {
  217. $tag = new SystemTag(1, 'tag1', true, true);
  218. $this->tagManager->expects($isAdmin ? $this->once() : $this->never())
  219. ->method('canUserSeeTag')
  220. ->with($tag)
  221. ->will($this->returnValue(true));
  222. $this->tagManager->expects($isAdmin ? $this->once() : $this->never())
  223. ->method('deleteTags')
  224. ->with('1');
  225. if (!$isAdmin) {
  226. $this->expectException(Forbidden::class);
  227. }
  228. $this->getTagNode($isAdmin, $tag)->delete();
  229. }
  230. public function tagNodeDeleteProviderPermissionException() {
  231. return [
  232. [
  233. // cannot delete invisible tag
  234. new SystemTag(1, 'Original', false, true),
  235. 'Sabre\DAV\Exception\Forbidden',
  236. ],
  237. [
  238. // cannot delete non-assignable tag
  239. new SystemTag(1, 'Original', true, false),
  240. 'Sabre\DAV\Exception\Forbidden',
  241. ],
  242. ];
  243. }
  244. /**
  245. * @dataProvider tagNodeDeleteProviderPermissionException
  246. */
  247. public function testDeleteTagPermissionException(ISystemTag $tag, $expectedException) {
  248. $this->tagManager->expects($this->any())
  249. ->method('canUserSeeTag')
  250. ->with($tag)
  251. ->will($this->returnValue($tag->isUserVisible()));
  252. $this->tagManager->expects($this->never())
  253. ->method('deleteTags');
  254. $this->expectException($expectedException);
  255. $this->getTagNode(false, $tag)->delete();
  256. }
  257. public function testDeleteTagNotFound() {
  258. $this->expectException(\Sabre\DAV\Exception\NotFound::class);
  259. $tag = new SystemTag(1, 'tag1', true, true);
  260. $this->tagManager->expects($this->any())
  261. ->method('canUserSeeTag')
  262. ->with($tag)
  263. ->will($this->returnValue($tag->isUserVisible()));
  264. $this->tagManager->expects($this->once())
  265. ->method('deleteTags')
  266. ->with('1')
  267. ->will($this->throwException(new TagNotFoundException()));
  268. $this->getTagNode(true, $tag)->delete();
  269. }
  270. }