|
| 1 | +<?php |
| 2 | + |
| 3 | +declare(strict_types=1); |
| 4 | + |
| 5 | +namespace OCA\Files\Tests\Command; |
| 6 | + |
| 7 | +use OCA\Files\Command\TransferOwnership; |
| 8 | +use OCA\Files\Service\OwnershipTransferService; |
| 9 | +use OCP\Files\Mount\IMountManager; |
| 10 | +use OCP\IConfig; |
| 11 | +use OCP\IUser; |
| 12 | +use OCP\IUserManager; |
| 13 | +use PHPUnit\Framework\TestCase; |
| 14 | +use Symfony\Component\Console\Application; |
| 15 | +use Symfony\Component\Console\Tester\CommandTester; |
| 16 | + |
| 17 | +class TransferOwnershipTest extends TestCase { |
| 18 | + |
| 19 | + private IUserManager $userManager; |
| 20 | + private OwnershipTransferService $transferService; |
| 21 | + private IConfig $config; |
| 22 | + private IMountManager $mountManager; |
| 23 | + |
| 24 | + protected function setUp(): void { |
| 25 | + $this->userManager = $this->createMock(IUserManager::class); |
| 26 | + $this->transferService = $this->createMock(OwnershipTransferService::class); |
| 27 | + $this->config = $this->createMock(IConfig::class); |
| 28 | + $this->mountManager = $this->createMock(IMountManager::class); |
| 29 | + } |
| 30 | + |
| 31 | + private function createCommandTester(): CommandTester { |
| 32 | + $app = new Application(); |
| 33 | + $app->setAutoExit(false); |
| 34 | + $app->setCatchExceptions(false); |
| 35 | + |
| 36 | + $cmd = new TransferOwnership( |
| 37 | + $this->userManager, |
| 38 | + $this->transferService, |
| 39 | + $this->config, |
| 40 | + $this->mountManager |
| 41 | + ); |
| 42 | + |
| 43 | + $app->add($cmd); |
| 44 | + $command = $app->find('files:transfer-ownership'); |
| 45 | + |
| 46 | + return new CommandTester($command); |
| 47 | + } |
| 48 | + |
| 49 | + public function testFailsWhenSourceEqualsDestination(): void { |
| 50 | + $tester = $this->createCommandTester(); |
| 51 | + |
| 52 | + $tester->execute([ |
| 53 | + 'source-user' => 'u1', |
| 54 | + 'destination-user' => 'u1' |
| 55 | + ]); |
| 56 | + |
| 57 | + $this->assertEquals(1, $tester->getStatusCode()); |
| 58 | + $this->assertStringContainsString("can't be transferred", $tester->getDisplay()); |
| 59 | + } |
| 60 | + |
| 61 | + public function testFailsWhenSourceUserNotFound(): void { |
| 62 | + $this->userManager->method('get')->willReturn(null); |
| 63 | + |
| 64 | + $tester = $this->createCommandTester(); |
| 65 | + |
| 66 | + $tester->execute([ |
| 67 | + 'source-user' => 'unknown', |
| 68 | + 'destination-user' => 'target' |
| 69 | + ]); |
| 70 | + |
| 71 | + $this->assertEquals(1, $tester->getStatusCode()); |
| 72 | + $this->assertStringContainsString('Unknown source user', $tester->getDisplay()); |
| 73 | + } |
| 74 | + |
| 75 | + public function testFailsWhenDestinationUserNotFound(): void { |
| 76 | + $source = $this->createMock(IUser::class); |
| 77 | + |
| 78 | + $this->userManager->method('get')->willReturnMap([ |
| 79 | + ['user1', $source], |
| 80 | + ['missing', null], |
| 81 | + ]); |
| 82 | + |
| 83 | + $tester = $this->createCommandTester(); |
| 84 | + |
| 85 | + $tester->execute([ |
| 86 | + 'source-user' => 'user1', |
| 87 | + 'destination-user' => 'missing' |
| 88 | + ]); |
| 89 | + |
| 90 | + $this->assertEquals(1, $tester->getStatusCode()); |
| 91 | + $this->assertStringContainsString('Unknown destination user', $tester->getDisplay()); |
| 92 | + } |
| 93 | + |
| 94 | + public function testSuccessfulTransfer(): void { |
| 95 | + $src = $this->createMock(IUser::class); |
| 96 | + $dest = $this->createMock(IUser::class); |
| 97 | + |
| 98 | + $this->userManager->method('get')->willReturnMap([ |
| 99 | + ['u1', $src], |
| 100 | + ['u2', $dest], |
| 101 | + ]); |
| 102 | + |
| 103 | + $this->transferService->expects($this->once()) |
| 104 | + ->method('transfer') |
| 105 | + ->with( |
| 106 | + $src, |
| 107 | + $dest, |
| 108 | + '', |
| 109 | + $this->anything(), |
| 110 | + false, |
| 111 | + false, |
| 112 | + false, |
| 113 | + false |
| 114 | + ); |
| 115 | + |
| 116 | + $tester = $this->createCommandTester(); |
| 117 | + $status = $tester->execute([ |
| 118 | + 'source-user' => 'u1', |
| 119 | + 'destination-user' => 'u2', |
| 120 | + ]); |
| 121 | + |
| 122 | + $this->assertEquals(0, $status); |
| 123 | + } |
| 124 | + |
| 125 | + /** |
| 126 | + * @dataProvider userIdOptionProvider |
| 127 | + */ |
| 128 | + public function testTransferWithUseUserIdOption(bool $useUserId): void { |
| 129 | + $src = $this->createMock(IUser::class); |
| 130 | + $dest = $this->createMock(IUser::class); |
| 131 | + |
| 132 | + $this->userManager->method('get')->willReturnMap([ |
| 133 | + ['u1', $src], |
| 134 | + ['u2', $dest], |
| 135 | + ]); |
| 136 | + |
| 137 | + //expect transfer with last param true/false as user-id is toggled |
| 138 | + $this->transferService->expects($this->once()) |
| 139 | + ->method('transfer') |
| 140 | + ->with( |
| 141 | + $src, |
| 142 | + $dest, |
| 143 | + '', |
| 144 | + $this->anything(), |
| 145 | + false, |
| 146 | + false, |
| 147 | + false, |
| 148 | + $useUserId |
| 149 | + ); |
| 150 | + |
| 151 | + $tester = $this->createCommandTester(); |
| 152 | + $status = $tester->execute([ |
| 153 | + 'source-user' => 'u1', |
| 154 | + 'destination-user' => 'u2', |
| 155 | + '--use-user-id' => $useUserId, |
| 156 | + ]); |
| 157 | + |
| 158 | + $this->assertEquals(0, $status); |
| 159 | + } |
| 160 | + |
| 161 | + public function userIdOptionProvider(): array { |
| 162 | + return [ |
| 163 | + 'use false' => [false], |
| 164 | + 'use true' => [true], |
| 165 | + ]; |
| 166 | + } |
| 167 | + |
| 168 | + |
| 169 | + /** |
| 170 | + * @dataProvider moveOptionProvider |
| 171 | + */ |
| 172 | + public function testTransferOwnershipMoveOptionTrueOrFalse(bool $move): void { |
| 173 | + $src = $this->createMock(IUser::class); |
| 174 | + $dest = $this->createMock(IUser::class); |
| 175 | + |
| 176 | + $this->userManager->method('get')->willReturnMap([ |
| 177 | + ['u1', $src], |
| 178 | + ['u2', $dest], |
| 179 | + ]); |
| 180 | + |
| 181 | + //expect transfer with move param true or false based on input |
| 182 | + $this->transferService->expects($this->once()) |
| 183 | + ->method('transfer') |
| 184 | + ->with( |
| 185 | + $src, |
| 186 | + $dest, |
| 187 | + '', |
| 188 | + $this->anything(), |
| 189 | + $move, |
| 190 | + false, |
| 191 | + false, |
| 192 | + false |
| 193 | + ); |
| 194 | + |
| 195 | + $tester = $this->createCommandTester(); |
| 196 | + $status = $tester->execute([ |
| 197 | + 'source-user' => 'u1', |
| 198 | + 'destination-user' => 'u2', |
| 199 | + '--move' => $move, |
| 200 | + ]); |
| 201 | + |
| 202 | + $this->assertEquals(0, $status); |
| 203 | + } |
| 204 | + |
| 205 | + public function moveOptionProvider(): array { |
| 206 | + return [ |
| 207 | + 'move disabled' => [false], |
| 208 | + 'move enabled' => [true], |
| 209 | + ]; |
| 210 | + } |
| 211 | + |
| 212 | + /** |
| 213 | + * @dataProvider pathOptionProvider |
| 214 | + */ |
| 215 | + public function testTransferOwnershipPathOption(string $path): void { |
| 216 | + $src = $this->createMock(IUser::class); |
| 217 | + $dest = $this->createMock(IUser::class); |
| 218 | + |
| 219 | + $this->userManager->method('get')->willReturnMap([ |
| 220 | + ['u1', $src], |
| 221 | + ['u2', $dest], |
| 222 | + ]); |
| 223 | + |
| 224 | + // expect transfer with provided path |
| 225 | + $this->transferService->expects($this->once()) |
| 226 | + ->method('transfer') |
| 227 | + ->with( |
| 228 | + $src, |
| 229 | + $dest, |
| 230 | + $path, |
| 231 | + $this->anything(), |
| 232 | + false, |
| 233 | + false, |
| 234 | + false, |
| 235 | + false |
| 236 | + ); |
| 237 | + |
| 238 | + $tester = $this->createCommandTester(); |
| 239 | + $status = $tester->execute([ |
| 240 | + 'source-user' => 'u1', |
| 241 | + 'destination-user' => 'u2', |
| 242 | + '--path' => $path, |
| 243 | + ]); |
| 244 | + |
| 245 | + $this->assertEquals(0, $status); |
| 246 | + } |
| 247 | + |
| 248 | + public function pathOptionProvider(): array { |
| 249 | + return [ |
| 250 | + 'root path' => [''], |
| 251 | + 'sub-folder path' => ['sub-folder'], |
| 252 | + 'nested path' => ['sub-folder/nested'], |
| 253 | + ]; |
| 254 | + } |
| 255 | +} |
0 commit comments