RepairTest.php 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157
  1. <?php
  2. /**
  3. * SPDX-FileCopyrightText: 2020 Nextcloud GmbH and Nextcloud contributors
  4. * SPDX-License-Identifier: AGPL-3.0-or-later
  5. */
  6. namespace Tests\Core\Command\Preview;
  7. use bantu\IniGetWrapper\IniGetWrapper;
  8. use OC\Core\Command\Preview\Repair;
  9. use OCP\Files\Folder;
  10. use OCP\Files\IRootFolder;
  11. use OCP\Files\Node;
  12. use OCP\IConfig;
  13. use OCP\Lock\ILockingProvider;
  14. use PHPUnit\Framework\MockObject\MockObject;
  15. use Psr\Log\LoggerInterface;
  16. use Symfony\Component\Console\Formatter\OutputFormatterInterface;
  17. use Symfony\Component\Console\Input\InputInterface;
  18. use Symfony\Component\Console\Output\OutputInterface;
  19. use Test\TestCase;
  20. class RepairTest extends TestCase {
  21. /** @var IConfig|MockObject */
  22. private $config;
  23. /** @var IRootFolder|MockObject */
  24. private $rootFolder;
  25. /** @var LoggerInterface|MockObject */
  26. private $logger;
  27. /** @var IniGetWrapper|MockObject */
  28. private $iniGetWrapper;
  29. /** @var InputInterface|MockObject */
  30. private $input;
  31. /** @var OutputInterface|MockObject */
  32. private $output;
  33. /** @var string */
  34. private $outputLines = '';
  35. /** @var Repair */
  36. private $repair;
  37. protected function setUp(): void {
  38. parent::setUp();
  39. $this->config = $this->getMockBuilder(IConfig::class)
  40. ->getMock();
  41. $this->rootFolder = $this->getMockBuilder(IRootFolder::class)
  42. ->getMock();
  43. $this->logger = $this->getMockBuilder(LoggerInterface::class)
  44. ->getMock();
  45. $this->iniGetWrapper = $this->getMockBuilder(IniGetWrapper::class)
  46. ->getMock();
  47. $this->repair = new Repair(
  48. $this->config,
  49. $this->rootFolder,
  50. $this->logger,
  51. $this->iniGetWrapper,
  52. $this->createMock(ILockingProvider::class)
  53. );
  54. $this->input = $this->getMockBuilder(InputInterface::class)
  55. ->getMock();
  56. $this->input->expects($this->any())
  57. ->method('getOption')
  58. ->willReturnCallback(function ($parameter) {
  59. if ($parameter === 'batch') {
  60. return true;
  61. }
  62. return null;
  63. });
  64. $this->output = $this->getMockBuilder(OutputInterface::class)
  65. ->setMethods(['section', 'writeln', 'write', 'setVerbosity', 'getVerbosity', 'isQuiet', 'isVerbose', 'isVeryVerbose', 'isDebug', 'setDecorated', 'isDecorated', 'setFormatter', 'getFormatter'])
  66. ->getMock();
  67. $self = $this;
  68. $this->output->expects($this->any())
  69. ->method('section')
  70. ->willReturn($this->output);
  71. /* We need format method to return a string */
  72. $outputFormatter = $this->createMock(OutputFormatterInterface::class);
  73. $outputFormatter->method('isDecorated')->willReturn(false);
  74. $outputFormatter->method('format')->willReturnArgument(0);
  75. $this->output->expects($this->any())
  76. ->method('getFormatter')
  77. ->willReturn($outputFormatter);
  78. $this->output->expects($this->any())
  79. ->method('writeln')
  80. ->willReturnCallback(function ($line) use ($self) {
  81. $self->outputLines .= $line . "\n";
  82. });
  83. }
  84. public function emptyTestDataProvider() {
  85. /** directoryNames, expectedOutput */
  86. return [
  87. [
  88. [],
  89. 'All previews are already migrated.'
  90. ],
  91. [
  92. [['name' => 'a'], ['name' => 'b'], ['name' => 'c']],
  93. 'All previews are already migrated.'
  94. ],
  95. [
  96. [['name' => '0', 'content' => ['folder', 'folder']], ['name' => 'b'], ['name' => 'c']],
  97. 'All previews are already migrated.'
  98. ],
  99. [
  100. [['name' => '0', 'content' => ['file', 'folder', 'folder']], ['name' => 'b'], ['name' => 'c']],
  101. 'A total of 1 preview files need to be migrated.'
  102. ],
  103. [
  104. [['name' => '23'], ['name' => 'b'], ['name' => 'c']],
  105. 'A total of 1 preview files need to be migrated.'
  106. ],
  107. ];
  108. }
  109. /**
  110. * @dataProvider emptyTestDataProvider
  111. */
  112. public function testEmptyExecute($directoryNames, $expectedOutput): void {
  113. $previewFolder = $this->getMockBuilder(Folder::class)
  114. ->getMock();
  115. $directories = array_map(function ($element) {
  116. $dir = $this->getMockBuilder(Folder::class)
  117. ->getMock();
  118. $dir->expects($this->any())
  119. ->method('getName')
  120. ->willReturn($element['name']);
  121. if (isset($element['content'])) {
  122. $list = [];
  123. foreach ($element['content'] as $item) {
  124. if ($item === 'file') {
  125. $list[] = $this->getMockBuilder(Node::class)
  126. ->getMock();
  127. } elseif ($item === 'folder') {
  128. $list[] = $this->getMockBuilder(Folder::class)
  129. ->getMock();
  130. }
  131. }
  132. $dir->expects($this->once())
  133. ->method('getDirectoryListing')
  134. ->willReturn($list);
  135. }
  136. return $dir;
  137. }, $directoryNames);
  138. $previewFolder->expects($this->once())
  139. ->method('getDirectoryListing')
  140. ->willReturn($directories);
  141. $this->rootFolder->expects($this->once())
  142. ->method('get')
  143. ->with('appdata_/preview')
  144. ->willReturn($previewFolder);
  145. $this->repair->run($this->input, $this->output);
  146. $this->assertStringContainsString($expectedOutput, $this->outputLines);
  147. }
  148. }