BackgroundRepairTest.php 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113
  1. <?php
  2. /**
  3. * SPDX-FileCopyrightText: 2016-2024 Nextcloud GmbH and Nextcloud contributors
  4. * SPDX-FileCopyrightText: 2016 ownCloud, Inc.
  5. * SPDX-License-Identifier: AGPL-3.0-only
  6. */
  7. namespace Test\Migration;
  8. use OC\BackgroundJob\JobList;
  9. use OC\Migration\BackgroundRepair;
  10. use OC\NeedsUpdateException;
  11. use OC\Repair;
  12. use OC\Repair\Events\RepairStepEvent;
  13. use OCP\AppFramework\Utility\ITimeFactory;
  14. use OCP\EventDispatcher\IEventDispatcher;
  15. use OCP\Migration\IOutput;
  16. use OCP\Migration\IRepairStep;
  17. use Psr\Log\LoggerInterface;
  18. use Test\TestCase;
  19. class TestRepairStep implements IRepairStep {
  20. /**
  21. * Returns the step's name
  22. *
  23. * @return string
  24. * @since 9.1.0
  25. */
  26. public function getName() {
  27. return 'A test repair step';
  28. }
  29. /**
  30. * Run repair step.
  31. * Must throw exception on error.
  32. *
  33. * @since 9.1.0
  34. * @throws \Exception in case of failure
  35. */
  36. public function run(IOutput $output) {
  37. // TODO: Implement run() method.
  38. }
  39. }
  40. class BackgroundRepairTest extends TestCase {
  41. private JobList $jobList;
  42. private BackgroundRepair $job;
  43. private LoggerInterface $logger;
  44. private IEventDispatcher $dispatcher;
  45. private ITimeFactory $time;
  46. private Repair $repair;
  47. protected function setUp(): void {
  48. parent::setUp();
  49. $this->jobList = $this->getMockBuilder(JobList::class)
  50. ->disableOriginalConstructor()
  51. ->getMock();
  52. $this->logger = $this->getMockBuilder(LoggerInterface::class)
  53. ->disableOriginalConstructor()
  54. ->getMock();
  55. $this->dispatcher = $this->createMock(IEventDispatcher::class);
  56. $this->time = $this->createMock(ITimeFactory::class);
  57. $this->time->method('getTime')
  58. ->willReturn(999999);
  59. $this->repair = new Repair($this->dispatcher, $this->logger);
  60. $this->job = $this->getMockBuilder(BackgroundRepair::class)
  61. ->setConstructorArgs([$this->repair, $this->time, $this->logger, $this->jobList])
  62. ->setMethods(['loadApp'])
  63. ->getMock();
  64. }
  65. public function testNoArguments() {
  66. $this->jobList->expects($this->once())->method('remove');
  67. $this->job->start($this->jobList);
  68. }
  69. public function testAppUpgrading() {
  70. $this->jobList->expects($this->never())->method('remove');
  71. $this->job->expects($this->once())->method('loadApp')->with('test')->willThrowException(new NeedsUpdateException());
  72. $this->job->setArgument([
  73. 'app' => 'test',
  74. 'step' => 'j'
  75. ]);
  76. $this->job->start($this->jobList);
  77. }
  78. public function testUnknownStep() {
  79. $this->dispatcher->expects($this->never())->method('dispatchTyped');
  80. $this->jobList->expects($this->once())->method('remove');
  81. $this->logger->expects($this->once())->method('error');
  82. $this->job->setArgument([
  83. 'app' => 'test',
  84. 'step' => 'j'
  85. ]);
  86. $this->job->start($this->jobList);
  87. }
  88. public function testWorkingStep() {
  89. $this->dispatcher->expects($this->once())->method('dispatchTyped')
  90. ->with($this->equalTo(new RepairStepEvent('A test repair step')));
  91. $this->jobList->expects($this->once())->method('remove');
  92. $this->job->setArgument([
  93. 'app' => 'test',
  94. 'step' => '\Test\Migration\TestRepairStep'
  95. ]);
  96. $this->job->start($this->jobList);
  97. }
  98. }