RepairTest.php 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154
  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 Psr\Log\LoggerInterface;
  12. use Symfony\Component\Console\Formatter\OutputFormatterInterface;
  13. use Symfony\Component\Console\Input\InputInterface;
  14. use Symfony\Component\Console\Output\OutputInterface;
  15. use Test\TestCase;
  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('isDecorated')->willReturn(false);
  70. $outputFormatter->method('format')->willReturnArgument(0);
  71. $this->output->expects($this->any())
  72. ->method('getFormatter')
  73. ->willReturn($outputFormatter);
  74. $this->output->expects($this->any())
  75. ->method('writeln')
  76. ->willReturnCallback(function ($line) use ($self) {
  77. $self->outputLines .= $line . "\n";
  78. });
  79. }
  80. public function emptyTestDataProvider() {
  81. /** directoryNames, expectedOutput */
  82. return [
  83. [
  84. [],
  85. 'All previews are already migrated.'
  86. ],
  87. [
  88. [['name' => 'a'], ['name' => 'b'], ['name' => 'c']],
  89. 'All previews are already migrated.'
  90. ],
  91. [
  92. [['name' => '0', 'content' => ['folder', 'folder']], ['name' => 'b'], ['name' => 'c']],
  93. 'All previews are already migrated.'
  94. ],
  95. [
  96. [['name' => '0', 'content' => ['file', 'folder', 'folder']], ['name' => 'b'], ['name' => 'c']],
  97. 'A total of 1 preview files need to be migrated.'
  98. ],
  99. [
  100. [['name' => '23'], ['name' => 'b'], ['name' => 'c']],
  101. 'A total of 1 preview files need to be migrated.'
  102. ],
  103. ];
  104. }
  105. /**
  106. * @dataProvider emptyTestDataProvider
  107. */
  108. public function testEmptyExecute($directoryNames, $expectedOutput) {
  109. $previewFolder = $this->getMockBuilder(Folder::class)
  110. ->getMock();
  111. $directories = array_map(function ($element) {
  112. $dir = $this->getMockBuilder(Folder::class)
  113. ->getMock();
  114. $dir->expects($this->any())
  115. ->method('getName')
  116. ->willReturn($element['name']);
  117. if (isset($element['content'])) {
  118. $list = [];
  119. foreach ($element['content'] as $item) {
  120. if ($item === 'file') {
  121. $list[] = $this->getMockBuilder(Node::class)
  122. ->getMock();
  123. } elseif ($item === 'folder') {
  124. $list[] = $this->getMockBuilder(Folder::class)
  125. ->getMock();
  126. }
  127. }
  128. $dir->expects($this->once())
  129. ->method('getDirectoryListing')
  130. ->willReturn($list);
  131. }
  132. return $dir;
  133. }, $directoryNames);
  134. $previewFolder->expects($this->once())
  135. ->method('getDirectoryListing')
  136. ->willReturn($directories);
  137. $this->rootFolder->expects($this->once())
  138. ->method('get')
  139. ->with("appdata_/preview")
  140. ->willReturn($previewFolder);
  141. $this->repair->run($this->input, $this->output);
  142. $this->assertStringContainsString($expectedOutput, $this->outputLines);
  143. }
  144. }