CleanPreviewsTest.php 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114
  1. <?php
  2. /**
  3. * SPDX-FileCopyrightText: 2016 Nextcloud GmbH and Nextcloud contributors
  4. * SPDX-License-Identifier: AGPL-3.0-or-later
  5. */
  6. namespace Test\Repair\Owncloud;
  7. use OC\Repair\Owncloud\CleanPreviews;
  8. use OC\Repair\Owncloud\CleanPreviewsBackgroundJob;
  9. use OCP\BackgroundJob\IJobList;
  10. use OCP\IConfig;
  11. use OCP\IUser;
  12. use OCP\IUserManager;
  13. use OCP\Migration\IOutput;
  14. use Test\TestCase;
  15. class CleanPreviewsTest extends TestCase {
  16. /** @var IJobList|\PHPUnit_Framework_MockObject_MockObject */
  17. private $jobList;
  18. /** @var IUserManager|\PHPUnit_Framework_MockObject_MockObject */
  19. private $userManager;
  20. /** @var IConfig|\PHPUnit_Framework_MockObject_MockObject */
  21. private $config;
  22. /** @var CleanPreviews */
  23. private $repair;
  24. public function setUp(): void {
  25. parent::setUp();
  26. $this->jobList = $this->createMock(IJobList::class);
  27. $this->userManager = $this->createMock(IUserManager::class);
  28. $this->config = $this->createMock(IConfig::class);
  29. $this->repair = new CleanPreviews(
  30. $this->jobList,
  31. $this->userManager,
  32. $this->config
  33. );
  34. }
  35. public function testGetName(): void {
  36. $this->assertSame('Add preview cleanup background jobs', $this->repair->getName());
  37. }
  38. public function testRun(): void {
  39. $user1 = $this->createMock(IUser::class);
  40. $user1->method('getUID')
  41. ->willReturn('user1');
  42. $user2 = $this->createMock(IUser::class);
  43. $user2->method('getUID')
  44. ->willReturn('user2');
  45. $this->userManager->expects($this->once())
  46. ->method('callForSeenUsers')
  47. ->will($this->returnCallback(function (\Closure $function) use ($user1, $user2) {
  48. $function($user1);
  49. $function($user2);
  50. }));
  51. $this->jobList->expects($this->exactly(2))
  52. ->method('add')
  53. ->withConsecutive(
  54. [
  55. $this->equalTo(CleanPreviewsBackgroundJob::class),
  56. $this->equalTo(['uid' => 'user1'])
  57. ],
  58. [
  59. $this->equalTo(CleanPreviewsBackgroundJob::class),
  60. $this->equalTo(['uid' => 'user2'])
  61. ],
  62. );
  63. $this->config->expects($this->once())
  64. ->method('getAppValue')
  65. ->with(
  66. $this->equalTo('core'),
  67. $this->equalTo('previewsCleanedUp'),
  68. $this->equalTo(false)
  69. )->willReturn(false);
  70. $this->config->expects($this->once())
  71. ->method('setAppValue')
  72. ->with(
  73. $this->equalTo('core'),
  74. $this->equalTo('previewsCleanedUp'),
  75. $this->equalTo(1)
  76. );
  77. $this->repair->run($this->createMock(IOutput::class));
  78. }
  79. public function testRunAlreadyDoone(): void {
  80. $this->userManager->expects($this->never())
  81. ->method($this->anything());
  82. $this->jobList->expects($this->never())
  83. ->method($this->anything());
  84. $this->config->expects($this->once())
  85. ->method('getAppValue')
  86. ->with(
  87. $this->equalTo('core'),
  88. $this->equalTo('previewsCleanedUp'),
  89. $this->equalTo(false)
  90. )->willReturn('1');
  91. $this->config->expects($this->never())
  92. ->method('setAppValue');
  93. $this->repair->run($this->createMock(IOutput::class));
  94. }
  95. }