|
| 1 | +<?php |
| 2 | +/** |
| 3 | + * SPDX-FileCopyrightText: 2017-2024 Nextcloud GmbH and Nextcloud contributors |
| 4 | + * SPDX-License-Identifier: AGPL-3.0-only |
| 5 | + */ |
| 6 | +namespace OCA\Files_Sharing\Tests\Command; |
| 7 | + |
| 8 | +use OCA\Files_Sharing\Command\FixShareOwners; |
| 9 | +use OCA\Files_Sharing\OrphanHelper; |
| 10 | +use Symfony\Component\Console\Input\InputInterface; |
| 11 | +use Symfony\Component\Console\Output\OutputInterface; |
| 12 | +use Test\TestCase; |
| 13 | + |
| 14 | +/** |
| 15 | + * Class FixShareOwnersTest |
| 16 | + * |
| 17 | + * @package OCA\Files_Sharing\Tests\Command |
| 18 | + */ |
| 19 | +class FixShareOwnersTest extends TestCase { |
| 20 | + /** |
| 21 | + * @var FixShareOwners |
| 22 | + */ |
| 23 | + private $command; |
| 24 | + |
| 25 | + /** |
| 26 | + * @var OrphanHelper|\PHPUnit\Framework\MockObject\MockObject |
| 27 | + */ |
| 28 | + private $orphanHelper; |
| 29 | + |
| 30 | + protected function setUp(): void { |
| 31 | + parent::setUp(); |
| 32 | + |
| 33 | + $this->orphanHelper = $this->createMock(OrphanHelper::class); |
| 34 | + $this->command = new FixShareOwners($this->orphanHelper); |
| 35 | + } |
| 36 | + |
| 37 | + public function testExecuteNoSharesDetected() { |
| 38 | + $this->orphanHelper->expects($this->once()) |
| 39 | + ->method('getAllShares') |
| 40 | + ->willReturn([ |
| 41 | + ['id' => 1, 'owner' => 'user1', 'fileid' => 1, 'target' => 'target1'], |
| 42 | + ['id' => 2, 'owner' => 'user2', 'fileid' => 2, 'target' => 'target2'], |
| 43 | + ]); |
| 44 | + $this->orphanHelper->expects($this->exactly(2)) |
| 45 | + ->method('isShareValid') |
| 46 | + ->willReturn(true); |
| 47 | + |
| 48 | + $input = $this->createMock(InputInterface::class); |
| 49 | + $output = $this->createMock(OutputInterface::class); |
| 50 | + |
| 51 | + $output->expects($this->once()) |
| 52 | + ->method('writeln') |
| 53 | + ->with('No broken shares detected'); |
| 54 | + $this->command->execute($input, $output); |
| 55 | + } |
| 56 | + |
| 57 | + public function testExecuteSharesDetected() { |
| 58 | + $this->orphanHelper->expects($this->once()) |
| 59 | + ->method('getAllShares') |
| 60 | + ->willReturn([ |
| 61 | + ['id' => 1, 'owner' => 'user1', 'fileid' => 1, 'target' => 'target1'], |
| 62 | + ['id' => 2, 'owner' => 'user2', 'fileid' => 2, 'target' => 'target2'], |
| 63 | + ]); |
| 64 | + $this->orphanHelper->expects($this->exactly(2)) |
| 65 | + ->method('isShareValid') |
| 66 | + ->willReturnOnConsecutiveCalls(true, false); |
| 67 | + $this->orphanHelper->expects($this->once()) |
| 68 | + ->method('fileExists') |
| 69 | + ->willReturn(true); |
| 70 | + $this->orphanHelper->expects($this->once()) |
| 71 | + ->method('findOwner') |
| 72 | + ->willReturn('newOwner'); |
| 73 | + $this->orphanHelper->expects($this->once()) |
| 74 | + ->method('updateShareOwner'); |
| 75 | + |
| 76 | + $input = $this->createMock(InputInterface::class); |
| 77 | + $output = $this->createMock(OutputInterface::class); |
| 78 | + |
| 79 | + $output->expects($this->once()) |
| 80 | + ->method('writeln') |
| 81 | + ->with('Share with id <info>2</info> (target: <info>target2</info>) updated to owner <info>newOwner</info>'); |
| 82 | + $this->command->execute($input, $output); |
| 83 | + } |
| 84 | + |
| 85 | + public function testExecuteSharesDetectedDryRun() { |
| 86 | + $this->orphanHelper->expects($this->once()) |
| 87 | + ->method('getAllShares') |
| 88 | + ->willReturn([ |
| 89 | + ['id' => 1, 'owner' => 'user1', 'fileid' => 1, 'target' => 'target1'], |
| 90 | + ['id' => 2, 'owner' => 'user2', 'fileid' => 2, 'target' => 'target2'], |
| 91 | + ]); |
| 92 | + $this->orphanHelper->expects($this->exactly(2)) |
| 93 | + ->method('isShareValid') |
| 94 | + ->willReturnOnConsecutiveCalls(true, false); |
| 95 | + $this->orphanHelper->expects($this->once()) |
| 96 | + ->method('fileExists') |
| 97 | + ->willReturn(true); |
| 98 | + $this->orphanHelper->expects($this->once()) |
| 99 | + ->method('findOwner') |
| 100 | + ->willReturn('newOwner'); |
| 101 | + $this->orphanHelper->expects($this->never()) |
| 102 | + ->method('updateShareOwner'); |
| 103 | + |
| 104 | + $input = $this->createMock(InputInterface::class); |
| 105 | + $output = $this->createMock(OutputInterface::class); |
| 106 | + |
| 107 | + $output->expects($this->once()) |
| 108 | + ->method('writeln') |
| 109 | + ->with('Share with id <info>2</info> (target: <info>target2</info>) can be updated to owner <info>newOwner</info>'); |
| 110 | + $input->expects($this->once()) |
| 111 | + ->method('getOption') |
| 112 | + ->with('dry-run') |
| 113 | + ->willReturn(true); |
| 114 | + $this->command->execute($input, $output); |
| 115 | + } |
| 116 | +} |
0 commit comments