1
0

FixShareOwnersTest.php 3.6 KB

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