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.

SystemTagsObjectTypeCollectionTest.php 4.4KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178
  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 OCP\Files\Folder;
  28. use OCP\IGroupManager;
  29. use OCP\IUser;
  30. use OCP\IUserSession;
  31. use OCP\SystemTag\ISystemTagManager;
  32. use OCP\SystemTag\ISystemTagObjectMapper;
  33. class SystemTagsObjectTypeCollectionTest extends \Test\TestCase {
  34. /**
  35. * @var \OCA\DAV\SystemTag\SystemTagsObjectTypeCollection
  36. */
  37. private $node;
  38. /**
  39. * @var \OCP\SystemTag\ISystemTagManager
  40. */
  41. private $tagManager;
  42. /**
  43. * @var \OCP\SystemTag\ISystemTagObjectMapper
  44. */
  45. private $tagMapper;
  46. /**
  47. * @var \OCP\Files\Folder
  48. */
  49. private $userFolder;
  50. protected function setUp(): void {
  51. parent::setUp();
  52. $this->tagManager = $this->getMockBuilder(ISystemTagManager::class)
  53. ->getMock();
  54. $this->tagMapper = $this->getMockBuilder(ISystemTagObjectMapper::class)
  55. ->getMock();
  56. $user = $this->getMockBuilder(IUser::class)
  57. ->getMock();
  58. $user->expects($this->any())
  59. ->method('getUID')
  60. ->willReturn('testuser');
  61. $userSession = $this->getMockBuilder(IUserSession::class)
  62. ->getMock();
  63. $userSession->expects($this->any())
  64. ->method('getUser')
  65. ->willReturn($user);
  66. $groupManager = $this->getMockBuilder(IGroupManager::class)
  67. ->getMock();
  68. $groupManager->expects($this->any())
  69. ->method('isAdmin')
  70. ->with('testuser')
  71. ->willReturn(true);
  72. $this->userFolder = $this->getMockBuilder(Folder::class)
  73. ->getMock();
  74. $userFolder = $this->userFolder;
  75. $closure = function ($name) use ($userFolder) {
  76. $nodes = $userFolder->getById(intval($name));
  77. return !empty($nodes);
  78. };
  79. $this->node = new \OCA\DAV\SystemTag\SystemTagsObjectTypeCollection(
  80. 'files',
  81. $this->tagManager,
  82. $this->tagMapper,
  83. $userSession,
  84. $groupManager,
  85. $closure
  86. );
  87. }
  88. public function testForbiddenCreateFile() {
  89. $this->expectException(\Sabre\DAV\Exception\Forbidden::class);
  90. $this->node->createFile('555');
  91. }
  92. public function testForbiddenCreateDirectory() {
  93. $this->expectException(\Sabre\DAV\Exception\Forbidden::class);
  94. $this->node->createDirectory('789');
  95. }
  96. public function testGetChild() {
  97. $this->userFolder->expects($this->once())
  98. ->method('getById')
  99. ->with('555')
  100. ->willReturn([true]);
  101. $childNode = $this->node->getChild('555');
  102. $this->assertInstanceOf('\OCA\DAV\SystemTag\SystemTagsObjectMappingCollection', $childNode);
  103. $this->assertEquals('555', $childNode->getName());
  104. }
  105. public function testGetChildWithoutAccess() {
  106. $this->expectException(\Sabre\DAV\Exception\NotFound::class);
  107. $this->userFolder->expects($this->once())
  108. ->method('getById')
  109. ->with('555')
  110. ->willReturn([]);
  111. $this->node->getChild('555');
  112. }
  113. public function testGetChildren() {
  114. $this->expectException(\Sabre\DAV\Exception\MethodNotAllowed::class);
  115. $this->node->getChildren();
  116. }
  117. public function testChildExists() {
  118. $this->userFolder->expects($this->once())
  119. ->method('getById')
  120. ->with('123')
  121. ->willReturn([true]);
  122. $this->assertTrue($this->node->childExists('123'));
  123. }
  124. public function testChildExistsWithoutAccess() {
  125. $this->userFolder->expects($this->once())
  126. ->method('getById')
  127. ->with('555')
  128. ->willReturn([]);
  129. $this->assertFalse($this->node->childExists('555'));
  130. }
  131. public function testDelete() {
  132. $this->expectException(\Sabre\DAV\Exception\Forbidden::class);
  133. $this->node->delete();
  134. }
  135. public function testSetName() {
  136. $this->expectException(\Sabre\DAV\Exception\Forbidden::class);
  137. $this->node->setName('somethingelse');
  138. }
  139. public function testGetName() {
  140. $this->assertEquals('files', $this->node->getName());
  141. }
  142. }