CleanUpTest.php 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121
  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 OCA\User_LDAP\Tests\Jobs;
  8. use Exception;
  9. use OCA\User_LDAP\Helper;
  10. use OCA\User_LDAP\Jobs\CleanUp;
  11. use OCA\User_LDAP\User\DeletedUsersIndex;
  12. use OCA\User_LDAP\User_Proxy;
  13. use OCP\AppFramework\Utility\ITimeFactory;
  14. use OCP\IConfig;
  15. use OCP\IDBConnection;
  16. use Test\TestCase;
  17. class CleanUpTest extends TestCase {
  18. /** @var CleanUp */
  19. protected $bgJob;
  20. /** @var array */
  21. protected $mocks;
  22. public function setUp(): void {
  23. $this->createMocks();
  24. $this->bgJob = new CleanUp($this->mocks['timeFactory'], $this->mocks['userBackend'], $this->mocks['deletedUsersIndex']);
  25. $this->bgJob->setArguments($this->mocks);
  26. }
  27. protected function createMocks(): void {
  28. $this->mocks = [];
  29. $this->mocks['userBackend'] = $this->createMock(User_Proxy::class);
  30. $this->mocks['deletedUsersIndex'] = $this->createMock(DeletedUsersIndex::class);
  31. $this->mocks['ocConfig'] = $this->createMock(IConfig::class);
  32. $this->mocks['db'] = $this->createMock(IDBConnection::class);
  33. $this->mocks['helper'] = $this->createMock(Helper::class);
  34. $this->mocks['timeFactory'] = $this->createMock(ITimeFactory::class);
  35. }
  36. /**
  37. * clean up job must not run when there are disabled configurations
  38. */
  39. public function test_runNotAllowedByDisabledConfigurations(): void {
  40. $this->mocks['helper']->expects($this->once())
  41. ->method('haveDisabledConfigurations')
  42. ->willReturn(true);
  43. $this->mocks['ocConfig']->expects($this->never())
  44. ->method('getSystemValue');
  45. $result = $this->bgJob->isCleanUpAllowed();
  46. $this->assertSame(false, $result);
  47. }
  48. /**
  49. * clean up job must not run when LDAP Helper is broken i.e.
  50. * returning unexpected results
  51. */
  52. public function test_runNotAllowedByBrokenHelper(): void {
  53. $this->mocks['helper']->expects($this->once())
  54. ->method('haveDisabledConfigurations')
  55. ->will($this->throwException(new Exception()));
  56. $this->mocks['ocConfig']->expects($this->never())
  57. ->method('getSystemValue');
  58. $result = $this->bgJob->isCleanUpAllowed();
  59. $this->assertSame(false, $result);
  60. }
  61. /**
  62. * clean up job must not run when it is not enabled
  63. */
  64. public function test_runNotAllowedBySysConfig(): void {
  65. $this->mocks['helper']->expects($this->once())
  66. ->method('haveDisabledConfigurations')
  67. ->willReturn(false);
  68. $this->mocks['ocConfig']->expects($this->once())
  69. ->method('getSystemValue')
  70. ->willReturn(false);
  71. $result = $this->bgJob->isCleanUpAllowed();
  72. $this->assertSame(false, $result);
  73. }
  74. /**
  75. * clean up job is allowed to run
  76. */
  77. public function test_runIsAllowed(): void {
  78. $this->mocks['helper']->expects($this->once())
  79. ->method('haveDisabledConfigurations')
  80. ->willReturn(false);
  81. $this->mocks['ocConfig']->expects($this->once())
  82. ->method('getSystemValue')
  83. ->willReturn(true);
  84. $result = $this->bgJob->isCleanUpAllowed();
  85. $this->assertSame(true, $result);
  86. }
  87. /**
  88. * check whether offset will be reset when it needs to
  89. */
  90. public function test_OffsetResetIsNecessary(): void {
  91. $result = $this->bgJob->isOffsetResetNecessary($this->bgJob->getChunkSize() - 1);
  92. $this->assertSame(true, $result);
  93. }
  94. /**
  95. * make sure offset is not reset when it is not due
  96. */
  97. public function test_OffsetResetIsNotNecessary(): void {
  98. $result = $this->bgJob->isOffsetResetNecessary($this->bgJob->getChunkSize());
  99. $this->assertSame(false, $result);
  100. }
  101. }