RepairTest.php 4.4 KB

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