CleanupTest.php 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176
  1. <?php
  2. /**
  3. * SPDX-FileCopyrightText: 2024 Nextcloud GmbH and Nextcloud contributors
  4. * SPDX-License-Identifier: AGPL-3.0-or-later
  5. */
  6. namespace Core\Command\Preview;
  7. use OC\Core\Command\Preview\Cleanup;
  8. use OCP\Files\Folder;
  9. use OCP\Files\IRootFolder;
  10. use OCP\Files\NotFoundException;
  11. use OCP\Files\NotPermittedException;
  12. use PHPUnit\Framework\MockObject\MockObject;
  13. use Psr\Log\LoggerInterface;
  14. use Symfony\Component\Console\Input\InputInterface;
  15. use Symfony\Component\Console\Output\OutputInterface;
  16. use Test\TestCase;
  17. class CleanupTest extends TestCase {
  18. private IRootFolder&MockObject $rootFolder;
  19. private LoggerInterface&MockObject $logger;
  20. private InputInterface&MockObject $input;
  21. private OutputInterface&MockObject $output;
  22. private Cleanup $repair;
  23. protected function setUp(): void {
  24. parent::setUp();
  25. $this->rootFolder = $this->createMock(IRootFolder::class);
  26. $this->logger = $this->createMock(LoggerInterface::class);
  27. $this->repair = new Cleanup(
  28. $this->rootFolder,
  29. $this->logger,
  30. );
  31. $this->input = $this->createMock(InputInterface::class);
  32. $this->output = $this->createMock(OutputInterface::class);
  33. }
  34. public function testCleanup(): void {
  35. $previewFolder = $this->createMock(Folder::class);
  36. $previewFolder->expects($this->once())
  37. ->method('isDeletable')
  38. ->willReturn(true);
  39. $previewFolder->expects($this->once())
  40. ->method('delete');
  41. $appDataFolder = $this->createMock(Folder::class);
  42. $appDataFolder->expects($this->once())->method('get')->with('preview')->willReturn($previewFolder);
  43. $appDataFolder->expects($this->once())->method('newFolder')->with('preview');
  44. $this->rootFolder->expects($this->once())
  45. ->method('getAppDataDirectoryName')
  46. ->willReturn('appdata_some_id');
  47. $this->rootFolder->expects($this->once())
  48. ->method('get')
  49. ->with('appdata_some_id')
  50. ->willReturn($appDataFolder);
  51. $this->output->expects($this->exactly(3))->method('writeln')
  52. ->with(self::callback(function (string $message): bool {
  53. static $i = 0;
  54. return match (++$i) {
  55. 1 => $message === 'Preview folder deleted',
  56. 2 => $message === 'Preview folder recreated',
  57. 3 => $message === 'Previews removed'
  58. };
  59. }));
  60. $this->assertEquals(0, $this->repair->run($this->input, $this->output));
  61. }
  62. public function testCleanupWhenNotDeletable(): void {
  63. $previewFolder = $this->createMock(Folder::class);
  64. $previewFolder->expects($this->once())
  65. ->method('isDeletable')
  66. ->willReturn(false);
  67. $previewFolder->expects($this->never())
  68. ->method('delete');
  69. $appDataFolder = $this->createMock(Folder::class);
  70. $appDataFolder->expects($this->once())->method('get')->with('preview')->willReturn($previewFolder);
  71. $appDataFolder->expects($this->never())->method('newFolder')->with('preview');
  72. $this->rootFolder->expects($this->once())
  73. ->method('getAppDataDirectoryName')
  74. ->willReturn('appdata_some_id');
  75. $this->rootFolder->expects($this->once())
  76. ->method('get')
  77. ->with('appdata_some_id')
  78. ->willReturn($appDataFolder);
  79. $this->logger->expects($this->once())->method('error')->with("Previews can't be removed: preview folder isn't deletable");
  80. $this->output->expects($this->once())->method('writeln')->with("Previews can't be removed: preview folder isn't deletable");
  81. $this->assertEquals(1, $this->repair->run($this->input, $this->output));
  82. }
  83. /**
  84. * @dataProvider dataForTestCleanupWithDeleteException
  85. */
  86. public function testCleanupWithDeleteException(string $exceptionClass, string $errorMessage): void {
  87. $previewFolder = $this->createMock(Folder::class);
  88. $previewFolder->expects($this->once())
  89. ->method('isDeletable')
  90. ->willReturn(true);
  91. $previewFolder->expects($this->once())
  92. ->method('delete')
  93. ->willThrowException(new $exceptionClass());
  94. $appDataFolder = $this->createMock(Folder::class);
  95. $appDataFolder->expects($this->once())->method('get')->with('preview')->willReturn($previewFolder);
  96. $appDataFolder->expects($this->never())->method('newFolder')->with('preview');
  97. $this->rootFolder->expects($this->once())
  98. ->method('getAppDataDirectoryName')
  99. ->willReturn('appdata_some_id');
  100. $this->rootFolder->expects($this->once())
  101. ->method('get')
  102. ->with('appdata_some_id')
  103. ->willReturn($appDataFolder);
  104. $this->logger->expects($this->once())->method('error')->with($errorMessage);
  105. $this->output->expects($this->once())->method('writeln')->with($errorMessage);
  106. $this->assertEquals(1, $this->repair->run($this->input, $this->output));
  107. }
  108. public static function dataForTestCleanupWithDeleteException(): array {
  109. return [
  110. [NotFoundException::class, "Previews weren't deleted: preview folder was not found while deleting it"],
  111. [NotPermittedException::class, "Previews weren't deleted: you don't have the permission to delete preview folder"],
  112. ];
  113. }
  114. public function testCleanupWithCreateException(): void {
  115. $previewFolder = $this->createMock(Folder::class);
  116. $previewFolder->expects($this->once())
  117. ->method('isDeletable')
  118. ->willReturn(true);
  119. $previewFolder->expects($this->once())
  120. ->method('delete');
  121. $appDataFolder = $this->createMock(Folder::class);
  122. $appDataFolder->expects($this->once())->method('get')->with('preview')->willReturn($previewFolder);
  123. $appDataFolder->expects($this->once())->method('newFolder')->with('preview')->willThrowException(new NotPermittedException());
  124. $this->rootFolder->expects($this->once())
  125. ->method('getAppDataDirectoryName')
  126. ->willReturn('appdata_some_id');
  127. $this->rootFolder->expects($this->once())
  128. ->method('get')
  129. ->with('appdata_some_id')
  130. ->willReturn($appDataFolder);
  131. $this->output->expects($this->exactly(2))->method('writeln')
  132. ->with(self::callback(function (string $message): bool {
  133. static $i = 0;
  134. return match (++$i) {
  135. 1 => $message === 'Preview folder deleted',
  136. 2 => $message === "Preview folder was deleted, but you don't have the permission to create preview folder",
  137. };
  138. }));
  139. $this->logger->expects($this->once())->method('error')->with("Preview folder was deleted, but you don't have the permission to create preview folder");
  140. $this->assertEquals(1, $this->repair->run($this->input, $this->output));
  141. }
  142. }