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.

SystemTagsByIdCollectionTest.php 6.6KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245
  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\IGroupManager;
  29. use OCP\IUser;
  30. use OCP\IUserSession;
  31. use OCP\SystemTag\ISystemTagManager;
  32. use OCP\SystemTag\TagNotFoundException;
  33. class SystemTagsByIdCollectionTest extends \Test\TestCase {
  34. /**
  35. * @var \OCP\SystemTag\ISystemTagManager
  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. }
  47. public function getNode($isAdmin = true) {
  48. $this->user = $this->getMockBuilder(IUser::class)
  49. ->getMock();
  50. $this->user->expects($this->any())
  51. ->method('getUID')
  52. ->willReturn('testuser');
  53. $userSession = $this->getMockBuilder(IUserSession::class)
  54. ->getMock();
  55. $userSession->expects($this->any())
  56. ->method('getUser')
  57. ->willReturn($this->user);
  58. $groupManager = $this->getMockBuilder(IGroupManager::class)
  59. ->getMock();
  60. $groupManager->expects($this->any())
  61. ->method('isAdmin')
  62. ->with('testuser')
  63. ->willReturn($isAdmin);
  64. return new \OCA\DAV\SystemTag\SystemTagsByIdCollection(
  65. $this->tagManager,
  66. $userSession,
  67. $groupManager
  68. );
  69. }
  70. public function adminFlagProvider() {
  71. return [[true], [false]];
  72. }
  73. public function testForbiddenCreateFile() {
  74. $this->expectException(\Sabre\DAV\Exception\Forbidden::class);
  75. $this->getNode()->createFile('555');
  76. }
  77. public function testForbiddenCreateDirectory() {
  78. $this->expectException(\Sabre\DAV\Exception\Forbidden::class);
  79. $this->getNode()->createDirectory('789');
  80. }
  81. public function testGetChild() {
  82. $tag = new SystemTag(123, 'Test', true, false);
  83. $this->tagManager->expects($this->once())
  84. ->method('canUserSeeTag')
  85. ->with($tag)
  86. ->willReturn(true);
  87. $this->tagManager->expects($this->once())
  88. ->method('getTagsByIds')
  89. ->with(['123'])
  90. ->willReturn([$tag]);
  91. $childNode = $this->getNode()->getChild('123');
  92. $this->assertInstanceOf('\OCA\DAV\SystemTag\SystemTagNode', $childNode);
  93. $this->assertEquals('123', $childNode->getName());
  94. $this->assertEquals($tag, $childNode->getSystemTag());
  95. }
  96. public function testGetChildInvalidName() {
  97. $this->expectException(\Sabre\DAV\Exception\BadRequest::class);
  98. $this->tagManager->expects($this->once())
  99. ->method('getTagsByIds')
  100. ->with(['invalid'])
  101. ->will($this->throwException(new \InvalidArgumentException()));
  102. $this->getNode()->getChild('invalid');
  103. }
  104. public function testGetChildNotFound() {
  105. $this->expectException(\Sabre\DAV\Exception\NotFound::class);
  106. $this->tagManager->expects($this->once())
  107. ->method('getTagsByIds')
  108. ->with(['444'])
  109. ->will($this->throwException(new TagNotFoundException()));
  110. $this->getNode()->getChild('444');
  111. }
  112. public function testGetChildUserNotVisible() {
  113. $this->expectException(\Sabre\DAV\Exception\NotFound::class);
  114. $tag = new SystemTag(123, 'Test', false, false);
  115. $this->tagManager->expects($this->once())
  116. ->method('getTagsByIds')
  117. ->with(['123'])
  118. ->willReturn([$tag]);
  119. $this->getNode(false)->getChild('123');
  120. }
  121. public function testGetChildrenAdmin() {
  122. $tag1 = new SystemTag(123, 'One', true, false);
  123. $tag2 = new SystemTag(456, 'Two', true, true);
  124. $this->tagManager->expects($this->once())
  125. ->method('getAllTags')
  126. ->with(null)
  127. ->willReturn([$tag1, $tag2]);
  128. $children = $this->getNode(true)->getChildren();
  129. $this->assertCount(2, $children);
  130. $this->assertInstanceOf('\OCA\DAV\SystemTag\SystemTagNode', $children[0]);
  131. $this->assertInstanceOf('\OCA\DAV\SystemTag\SystemTagNode', $children[1]);
  132. $this->assertEquals($tag1, $children[0]->getSystemTag());
  133. $this->assertEquals($tag2, $children[1]->getSystemTag());
  134. }
  135. public function testGetChildrenNonAdmin() {
  136. $tag1 = new SystemTag(123, 'One', true, false);
  137. $tag2 = new SystemTag(456, 'Two', true, true);
  138. $this->tagManager->expects($this->once())
  139. ->method('getAllTags')
  140. ->with(true)
  141. ->willReturn([$tag1, $tag2]);
  142. $children = $this->getNode(false)->getChildren();
  143. $this->assertCount(2, $children);
  144. $this->assertInstanceOf('\OCA\DAV\SystemTag\SystemTagNode', $children[0]);
  145. $this->assertInstanceOf('\OCA\DAV\SystemTag\SystemTagNode', $children[1]);
  146. $this->assertEquals($tag1, $children[0]->getSystemTag());
  147. $this->assertEquals($tag2, $children[1]->getSystemTag());
  148. }
  149. public function testGetChildrenEmpty() {
  150. $this->tagManager->expects($this->once())
  151. ->method('getAllTags')
  152. ->with(null)
  153. ->willReturn([]);
  154. $this->assertCount(0, $this->getNode()->getChildren());
  155. }
  156. public function childExistsProvider() {
  157. return [
  158. [true, true],
  159. [false, false],
  160. ];
  161. }
  162. /**
  163. * @dataProvider childExistsProvider
  164. */
  165. public function testChildExists($userVisible, $expectedResult) {
  166. $tag = new SystemTag(123, 'One', $userVisible, false);
  167. $this->tagManager->expects($this->once())
  168. ->method('canUserSeeTag')
  169. ->with($tag)
  170. ->willReturn($userVisible);
  171. $this->tagManager->expects($this->once())
  172. ->method('getTagsByIds')
  173. ->with(['123'])
  174. ->willReturn([$tag]);
  175. $this->assertEquals($expectedResult, $this->getNode()->childExists('123'));
  176. }
  177. public function testChildExistsNotFound() {
  178. $this->tagManager->expects($this->once())
  179. ->method('getTagsByIds')
  180. ->with(['123'])
  181. ->will($this->throwException(new TagNotFoundException()));
  182. $this->assertFalse($this->getNode()->childExists('123'));
  183. }
  184. public function testChildExistsBadRequest() {
  185. $this->expectException(\Sabre\DAV\Exception\BadRequest::class);
  186. $this->tagManager->expects($this->once())
  187. ->method('getTagsByIds')
  188. ->with(['invalid'])
  189. ->will($this->throwException(new \InvalidArgumentException()));
  190. $this->getNode()->childExists('invalid');
  191. }
  192. }