You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

CleanUpTest.php 4.0KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138
  1. <?php
  2. /**
  3. * @copyright Copyright (c) 2016, ownCloud, Inc.
  4. *
  5. * @author Arthur Schiwon <blizzz@arthur-schiwon.de>
  6. * @author Christoph Wurst <christoph@winzerhof-wurst.at>
  7. * @author Joas Schilling <coding@schilljs.com>
  8. * @author Morris Jobke <hey@morrisjobke.de>
  9. * @author Roeland Jago Douma <roeland@famdouma.nl>
  10. *
  11. * @license AGPL-3.0
  12. *
  13. * This code is free software: you can redistribute it and/or modify
  14. * it under the terms of the GNU Affero General Public License, version 3,
  15. * as published by the Free Software Foundation.
  16. *
  17. * This program is distributed in the hope that it will be useful,
  18. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  19. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  20. * GNU Affero General Public License for more details.
  21. *
  22. * You should have received a copy of the GNU Affero General Public License, version 3,
  23. * along with this program. If not, see <http://www.gnu.org/licenses/>
  24. *
  25. */
  26. namespace OCA\User_LDAP\Tests\Jobs;
  27. use Exception;
  28. use OCA\User_LDAP\Helper;
  29. use OCA\User_LDAP\Jobs\CleanUp;
  30. use OCA\User_LDAP\User\DeletedUsersIndex;
  31. use OCA\User_LDAP\User_Proxy;
  32. use OCP\IConfig;
  33. use OCP\IDBConnection;
  34. use Test\TestCase;
  35. class CleanUpTest extends TestCase {
  36. /** @var CleanUp */
  37. protected $bgJob;
  38. /** @var array */
  39. protected $mocks;
  40. public function setUp(): void {
  41. $this->createMocks();
  42. $this->bgJob = new CleanUp($this->mocks['userBackend']);
  43. $this->bgJob->setArguments($this->mocks);
  44. }
  45. protected function createMocks(): void {
  46. $this->mocks = [];
  47. $this->mocks['userBackend'] = $this->createMock(User_Proxy::class);
  48. $this->mocks['deletedUsersIndex'] = $this->createMock(DeletedUsersIndex::class);
  49. $this->mocks['ocConfig'] = $this->createMock(IConfig::class);
  50. $this->mocks['db'] = $this->createMock(IDBConnection::class);
  51. $this->mocks['helper'] = $this->createMock(Helper::class);
  52. }
  53. /**
  54. * clean up job must not run when there are disabled configurations
  55. */
  56. public function test_runNotAllowedByDisabledConfigurations() {
  57. $this->mocks['helper']->expects($this->once())
  58. ->method('haveDisabledConfigurations')
  59. ->willReturn(true);
  60. $this->mocks['ocConfig']->expects($this->never())
  61. ->method('getSystemValue');
  62. $result = $this->bgJob->isCleanUpAllowed();
  63. $this->assertSame(false, $result);
  64. }
  65. /**
  66. * clean up job must not run when LDAP Helper is broken i.e.
  67. * returning unexpected results
  68. */
  69. public function test_runNotAllowedByBrokenHelper() {
  70. $this->mocks['helper']->expects($this->once())
  71. ->method('haveDisabledConfigurations')
  72. ->will($this->throwException(new Exception()));
  73. $this->mocks['ocConfig']->expects($this->never())
  74. ->method('getSystemValue');
  75. $result = $this->bgJob->isCleanUpAllowed();
  76. $this->assertSame(false, $result);
  77. }
  78. /**
  79. * clean up job must not run when it is not enabled
  80. */
  81. public function test_runNotAllowedBySysConfig() {
  82. $this->mocks['helper']->expects($this->once())
  83. ->method('haveDisabledConfigurations')
  84. ->willReturn(false);
  85. $this->mocks['ocConfig']->expects($this->once())
  86. ->method('getSystemValue')
  87. ->willReturn(false);
  88. $result = $this->bgJob->isCleanUpAllowed();
  89. $this->assertSame(false, $result);
  90. }
  91. /**
  92. * clean up job is allowed to run
  93. */
  94. public function test_runIsAllowed() {
  95. $this->mocks['helper']->expects($this->once())
  96. ->method('haveDisabledConfigurations')
  97. ->willReturn(false);
  98. $this->mocks['ocConfig']->expects($this->once())
  99. ->method('getSystemValue')
  100. ->willReturn(true);
  101. $result = $this->bgJob->isCleanUpAllowed();
  102. $this->assertSame(true, $result);
  103. }
  104. /**
  105. * check whether offset will be reset when it needs to
  106. */
  107. public function test_OffsetResetIsNecessary() {
  108. $result = $this->bgJob->isOffsetResetNecessary($this->bgJob->getChunkSize() - 1);
  109. $this->assertSame(true, $result);
  110. }
  111. /**
  112. * make sure offset is not reset when it is not due
  113. */
  114. public function test_OffsetResetIsNotNecessary() {
  115. $result = $this->bgJob->isOffsetResetNecessary($this->bgJob->getChunkSize());
  116. $this->assertSame(false, $result);
  117. }
  118. }