ApplicableTest.php 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155
  1. <?php
  2. /**
  3. * SPDX-FileCopyrightText: 2017-2024 Nextcloud GmbH and Nextcloud contributors
  4. * SPDX-FileCopyrightText: 2016 ownCloud, Inc.
  5. * SPDX-License-Identifier: AGPL-3.0-only
  6. */
  7. namespace OCA\Files_External\Tests\Command;
  8. use OCA\Files_External\Command\Applicable;
  9. use OCP\IGroupManager;
  10. use OCP\IUserManager;
  11. class ApplicableTest extends CommandTest {
  12. private function getInstance($storageService) {
  13. /** @var IUserManager|\PHPUnit\Framework\MockObject\MockObject $userManager */
  14. $userManager = $this->createMock(IUserManager::class);
  15. /** @var IGroupManager|\PHPUnit\Framework\MockObject\MockObject $groupManager */
  16. $groupManager = $this->createMock(IGroupManager::class);
  17. $userManager->expects($this->any())
  18. ->method('userExists')
  19. ->willReturn(true);
  20. $groupManager->expects($this->any())
  21. ->method('groupExists')
  22. ->willReturn(true);
  23. return new Applicable($storageService, $userManager, $groupManager);
  24. }
  25. public function testListEmpty(): void {
  26. $mount = $this->getMount(1, '', '');
  27. $storageService = $this->getGlobalStorageService([$mount]);
  28. $command = $this->getInstance($storageService);
  29. $input = $this->getInput($command, [
  30. 'mount_id' => 1
  31. ], [
  32. 'output' => 'json'
  33. ]);
  34. $result = json_decode($this->executeCommand($command, $input), true);
  35. $this->assertEquals(['users' => [], 'groups' => []], $result);
  36. }
  37. public function testList(): void {
  38. $mount = $this->getMount(1, '', '', '', [], [], ['test', 'asd']);
  39. $storageService = $this->getGlobalStorageService([$mount]);
  40. $command = $this->getInstance($storageService);
  41. $input = $this->getInput($command, [
  42. 'mount_id' => 1
  43. ], [
  44. 'output' => 'json'
  45. ]);
  46. $result = json_decode($this->executeCommand($command, $input), true);
  47. $this->assertEquals(['users' => ['test', 'asd'], 'groups' => []], $result);
  48. }
  49. public function testAddSingle(): void {
  50. $mount = $this->getMount(1, '', '', '', [], [], []);
  51. $storageService = $this->getGlobalStorageService([$mount]);
  52. $command = $this->getInstance($storageService);
  53. $input = $this->getInput($command, [
  54. 'mount_id' => 1
  55. ], [
  56. 'output' => 'json',
  57. 'add-user' => ['foo']
  58. ]);
  59. $this->executeCommand($command, $input);
  60. $this->assertEquals(['foo'], $mount->getApplicableUsers());
  61. }
  62. public function testAddDuplicate(): void {
  63. $mount = $this->getMount(1, '', '', '', [], [], ['foo']);
  64. $storageService = $this->getGlobalStorageService([$mount]);
  65. $command = $this->getInstance($storageService);
  66. $input = $this->getInput($command, [
  67. 'mount_id' => 1
  68. ], [
  69. 'output' => 'json',
  70. 'add-user' => ['foo', 'bar']
  71. ]);
  72. $this->executeCommand($command, $input);
  73. $this->assertEquals(['foo', 'bar'], $mount->getApplicableUsers());
  74. }
  75. public function testRemoveSingle(): void {
  76. $mount = $this->getMount(1, '', '', '', [], [], ['foo', 'bar']);
  77. $storageService = $this->getGlobalStorageService([$mount]);
  78. $command = $this->getInstance($storageService);
  79. $input = $this->getInput($command, [
  80. 'mount_id' => 1
  81. ], [
  82. 'output' => 'json',
  83. 'remove-user' => ['bar']
  84. ]);
  85. $this->executeCommand($command, $input);
  86. $this->assertEquals(['foo'], $mount->getApplicableUsers());
  87. }
  88. public function testRemoveNonExisting(): void {
  89. $mount = $this->getMount(1, '', '', '', [], [], ['foo', 'bar']);
  90. $storageService = $this->getGlobalStorageService([$mount]);
  91. $command = $this->getInstance($storageService);
  92. $input = $this->getInput($command, [
  93. 'mount_id' => 1
  94. ], [
  95. 'output' => 'json',
  96. 'remove-user' => ['bar', 'asd']
  97. ]);
  98. $this->executeCommand($command, $input);
  99. $this->assertEquals(['foo'], $mount->getApplicableUsers());
  100. }
  101. public function testRemoveAddRemove(): void {
  102. $mount = $this->getMount(1, '', '', '', [], [], ['foo', 'bar']);
  103. $storageService = $this->getGlobalStorageService([$mount]);
  104. $command = $this->getInstance($storageService);
  105. $input = $this->getInput($command, [
  106. 'mount_id' => 1
  107. ], [
  108. 'output' => 'json',
  109. 'remove-user' => ['bar', 'asd'],
  110. 'add-user' => ['test']
  111. ]);
  112. $this->executeCommand($command, $input);
  113. $this->assertEquals(['foo', 'test'], $mount->getApplicableUsers());
  114. }
  115. }