1
0

CleanPreviewsTest.php 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109
  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 PHPUnit\Framework\MockObject\MockObject;
  15. use Test\TestCase;
  16. class CleanPreviewsTest extends TestCase {
  17. private IJobList&MockObject $jobList;
  18. private IUserManager&MockObject $userManager;
  19. private IConfig&MockObject $config;
  20. /** @var CleanPreviews */
  21. private $repair;
  22. public function setUp(): void {
  23. parent::setUp();
  24. $this->jobList = $this->createMock(IJobList::class);
  25. $this->userManager = $this->createMock(IUserManager::class);
  26. $this->config = $this->createMock(IConfig::class);
  27. $this->repair = new CleanPreviews(
  28. $this->jobList,
  29. $this->userManager,
  30. $this->config
  31. );
  32. }
  33. public function testGetName(): void {
  34. $this->assertSame('Add preview cleanup background jobs', $this->repair->getName());
  35. }
  36. public function testRun(): void {
  37. $user1 = $this->createMock(IUser::class);
  38. $user1->method('getUID')
  39. ->willReturn('user1');
  40. $user2 = $this->createMock(IUser::class);
  41. $user2->method('getUID')
  42. ->willReturn('user2');
  43. $this->userManager->expects($this->once())
  44. ->method('callForSeenUsers')
  45. ->will($this->returnCallback(function (\Closure $function) use (&$user1, $user2) {
  46. $function($user1);
  47. $function($user2);
  48. }));
  49. $jobListCalls = [];
  50. $this->jobList->expects($this->exactly(2))
  51. ->method('add')
  52. ->willReturnCallback(function () use (&$jobListCalls) {
  53. $jobListCalls[] = func_get_args();
  54. });
  55. $this->config->expects($this->once())
  56. ->method('getAppValue')
  57. ->with(
  58. $this->equalTo('core'),
  59. $this->equalTo('previewsCleanedUp'),
  60. $this->equalTo(false)
  61. )->willReturn(false);
  62. $this->config->expects($this->once())
  63. ->method('setAppValue')
  64. ->with(
  65. $this->equalTo('core'),
  66. $this->equalTo('previewsCleanedUp'),
  67. $this->equalTo(1)
  68. );
  69. $this->repair->run($this->createMock(IOutput::class));
  70. $this->assertEqualsCanonicalizing([
  71. [CleanPreviewsBackgroundJob::class, ['uid' => 'user1']],
  72. [CleanPreviewsBackgroundJob::class, ['uid' => 'user2']],
  73. ], $jobListCalls);
  74. }
  75. public function testRunAlreadyDone(): void {
  76. $this->userManager->expects($this->never())
  77. ->method($this->anything());
  78. $this->jobList->expects($this->never())
  79. ->method($this->anything());
  80. $this->config->expects($this->once())
  81. ->method('getAppValue')
  82. ->with(
  83. $this->equalTo('core'),
  84. $this->equalTo('previewsCleanedUp'),
  85. $this->equalTo(false)
  86. )->willReturn('1');
  87. $this->config->expects($this->never())
  88. ->method('setAppValue');
  89. $this->repair->run($this->createMock(IOutput::class));
  90. }
  91. }