CleanUpTest.php 4.0 KB

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