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.

SystemTagMappingNodeTest.php 4.6KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170
  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\ISystemTagObjectMapper;
  32. use OCP\SystemTag\TagNotFoundException;
  33. class SystemTagMappingNodeTest extends \Test\TestCase {
  34. /**
  35. * @var \OCP\SystemTag\ISystemTagManager
  36. */
  37. private $tagManager;
  38. /**
  39. * @var \OCP\SystemTag\ISystemTagObjectMapper
  40. */
  41. private $tagMapper;
  42. /**
  43. * @var \OCP\IUser
  44. */
  45. private $user;
  46. protected function setUp(): void {
  47. parent::setUp();
  48. $this->tagManager = $this->getMockBuilder(ISystemTagManager::class)
  49. ->getMock();
  50. $this->tagMapper = $this->getMockBuilder(ISystemTagObjectMapper::class)
  51. ->getMock();
  52. $this->user = $this->getMockBuilder(IUser::class)
  53. ->getMock();
  54. }
  55. public function getMappingNode($tag = null) {
  56. if ($tag === null) {
  57. $tag = new SystemTag(1, 'Test', true, true);
  58. }
  59. return new \OCA\DAV\SystemTag\SystemTagMappingNode(
  60. $tag,
  61. 123,
  62. 'files',
  63. $this->user,
  64. $this->tagManager,
  65. $this->tagMapper
  66. );
  67. }
  68. public function testGetters() {
  69. $tag = new SystemTag(1, 'Test', true, false);
  70. $node = $this->getMappingNode($tag);
  71. $this->assertEquals('1', $node->getName());
  72. $this->assertEquals($tag, $node->getSystemTag());
  73. $this->assertEquals(123, $node->getObjectId());
  74. $this->assertEquals('files', $node->getObjectType());
  75. }
  76. public function testDeleteTag() {
  77. $node = $this->getMappingNode();
  78. $this->tagManager->expects($this->once())
  79. ->method('canUserSeeTag')
  80. ->with($node->getSystemTag())
  81. ->willReturn(true);
  82. $this->tagManager->expects($this->once())
  83. ->method('canUserAssignTag')
  84. ->with($node->getSystemTag())
  85. ->willReturn(true);
  86. $this->tagManager->expects($this->never())
  87. ->method('deleteTags');
  88. $this->tagMapper->expects($this->once())
  89. ->method('unassignTags')
  90. ->with(123, 'files', 1);
  91. $node->delete();
  92. }
  93. public function tagNodeDeleteProviderPermissionException() {
  94. return [
  95. [
  96. // cannot unassign invisible tag
  97. new SystemTag(1, 'Original', false, true),
  98. 'Sabre\DAV\Exception\NotFound',
  99. ],
  100. [
  101. // cannot unassign non-assignable tag
  102. new SystemTag(1, 'Original', true, false),
  103. 'Sabre\DAV\Exception\Forbidden',
  104. ],
  105. ];
  106. }
  107. /**
  108. * @dataProvider tagNodeDeleteProviderPermissionException
  109. */
  110. public function testDeleteTagExpectedException(ISystemTag $tag, $expectedException) {
  111. $this->tagManager->expects($this->any())
  112. ->method('canUserSeeTag')
  113. ->with($tag)
  114. ->willReturn($tag->isUserVisible());
  115. $this->tagManager->expects($this->any())
  116. ->method('canUserAssignTag')
  117. ->with($tag)
  118. ->willReturn($tag->isUserAssignable());
  119. $this->tagManager->expects($this->never())
  120. ->method('deleteTags');
  121. $this->tagMapper->expects($this->never())
  122. ->method('unassignTags');
  123. $thrown = null;
  124. try {
  125. $this->getMappingNode($tag)->delete();
  126. } catch (\Exception $e) {
  127. $thrown = $e;
  128. }
  129. $this->assertInstanceOf($expectedException, $thrown);
  130. }
  131. public function testDeleteTagNotFound() {
  132. $this->expectException(\Sabre\DAV\Exception\NotFound::class);
  133. // assuming the tag existed at the time the node was created,
  134. // but got deleted concurrently in the database
  135. $tag = new SystemTag(1, 'Test', true, true);
  136. $this->tagManager->expects($this->once())
  137. ->method('canUserSeeTag')
  138. ->with($tag)
  139. ->willReturn($tag->isUserVisible());
  140. $this->tagManager->expects($this->once())
  141. ->method('canUserAssignTag')
  142. ->with($tag)
  143. ->willReturn($tag->isUserAssignable());
  144. $this->tagMapper->expects($this->once())
  145. ->method('unassignTags')
  146. ->with(123, 'files', 1)
  147. ->will($this->throwException(new TagNotFoundException()));
  148. $this->getMappingNode($tag)->delete();
  149. }
  150. }