1
0

ApplicableTest.php 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173
  1. <?php
  2. /**
  3. * @copyright Copyright (c) 2016, ownCloud, Inc.
  4. *
  5. * @author Christoph Wurst <christoph@winzerhof-wurst.at>
  6. * @author Morris Jobke <hey@morrisjobke.de>
  7. * @author Robin Appelman <robin@icewind.nl>
  8. * @author Roeland Jago Douma <roeland@famdouma.nl>
  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\Files_External\Tests\Command;
  26. use OCA\Files_External\Command\Applicable;
  27. use OCP\IGroupManager;
  28. use OCP\IUserManager;
  29. class ApplicableTest extends CommandTest {
  30. private function getInstance($storageService) {
  31. /** @var \OCP\IUserManager|\PHPUnit\Framework\MockObject\MockObject $userManager */
  32. $userManager = $this->createMock(IUserManager::class);
  33. /** @var \OCP\IGroupManager|\PHPUnit\Framework\MockObject\MockObject $groupManager */
  34. $groupManager = $this->createMock(IGroupManager::class);
  35. $userManager->expects($this->any())
  36. ->method('userExists')
  37. ->willReturn(true);
  38. $groupManager->expects($this->any())
  39. ->method('groupExists')
  40. ->willReturn(true);
  41. return new Applicable($storageService, $userManager, $groupManager);
  42. }
  43. public function testListEmpty() {
  44. $mount = $this->getMount(1, '', '');
  45. $storageService = $this->getGlobalStorageService([$mount]);
  46. $command = $this->getInstance($storageService);
  47. $input = $this->getInput($command, [
  48. 'mount_id' => 1
  49. ], [
  50. 'output' => 'json'
  51. ]);
  52. $result = json_decode($this->executeCommand($command, $input), true);
  53. $this->assertEquals(['users' => [], 'groups' => []], $result);
  54. }
  55. public function testList() {
  56. $mount = $this->getMount(1, '', '', '', [], [], ['test', 'asd']);
  57. $storageService = $this->getGlobalStorageService([$mount]);
  58. $command = $this->getInstance($storageService);
  59. $input = $this->getInput($command, [
  60. 'mount_id' => 1
  61. ], [
  62. 'output' => 'json'
  63. ]);
  64. $result = json_decode($this->executeCommand($command, $input), true);
  65. $this->assertEquals(['users' => ['test', 'asd'], 'groups' => []], $result);
  66. }
  67. public function testAddSingle() {
  68. $mount = $this->getMount(1, '', '', '', [], [], []);
  69. $storageService = $this->getGlobalStorageService([$mount]);
  70. $command = $this->getInstance($storageService);
  71. $input = $this->getInput($command, [
  72. 'mount_id' => 1
  73. ], [
  74. 'output' => 'json',
  75. 'add-user' => ['foo']
  76. ]);
  77. $this->executeCommand($command, $input);
  78. $this->assertEquals(['foo'], $mount->getApplicableUsers());
  79. }
  80. public function testAddDuplicate() {
  81. $mount = $this->getMount(1, '', '', '', [], [], ['foo']);
  82. $storageService = $this->getGlobalStorageService([$mount]);
  83. $command = $this->getInstance($storageService);
  84. $input = $this->getInput($command, [
  85. 'mount_id' => 1
  86. ], [
  87. 'output' => 'json',
  88. 'add-user' => ['foo', 'bar']
  89. ]);
  90. $this->executeCommand($command, $input);
  91. $this->assertEquals(['foo', 'bar'], $mount->getApplicableUsers());
  92. }
  93. public function testRemoveSingle() {
  94. $mount = $this->getMount(1, '', '', '', [], [], ['foo', 'bar']);
  95. $storageService = $this->getGlobalStorageService([$mount]);
  96. $command = $this->getInstance($storageService);
  97. $input = $this->getInput($command, [
  98. 'mount_id' => 1
  99. ], [
  100. 'output' => 'json',
  101. 'remove-user' => ['bar']
  102. ]);
  103. $this->executeCommand($command, $input);
  104. $this->assertEquals(['foo'], $mount->getApplicableUsers());
  105. }
  106. public function testRemoveNonExisting() {
  107. $mount = $this->getMount(1, '', '', '', [], [], ['foo', 'bar']);
  108. $storageService = $this->getGlobalStorageService([$mount]);
  109. $command = $this->getInstance($storageService);
  110. $input = $this->getInput($command, [
  111. 'mount_id' => 1
  112. ], [
  113. 'output' => 'json',
  114. 'remove-user' => ['bar', 'asd']
  115. ]);
  116. $this->executeCommand($command, $input);
  117. $this->assertEquals(['foo'], $mount->getApplicableUsers());
  118. }
  119. public function testRemoveAddRemove() {
  120. $mount = $this->getMount(1, '', '', '', [], [], ['foo', 'bar']);
  121. $storageService = $this->getGlobalStorageService([$mount]);
  122. $command = $this->getInstance($storageService);
  123. $input = $this->getInput($command, [
  124. 'mount_id' => 1
  125. ], [
  126. 'output' => 'json',
  127. 'remove-user' => ['bar', 'asd'],
  128. 'add-user' => ['test']
  129. ]);
  130. $this->executeCommand($command, $input);
  131. $this->assertEquals(['foo', 'test'], $mount->getApplicableUsers());
  132. }
  133. }