CleanUpTest.php 4.1 KB

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 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\AppFramework\Utility\ITimeFactory;
  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['timeFactory'], $this->mocks['userBackend'], $this->mocks['deletedUsersIndex']);
  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. $this->mocks['timeFactory'] = $this->createMock(ITimeFactory::class);
  53. }
  54. /**
  55. * clean up job must not run when there are disabled configurations
  56. */
  57. public function test_runNotAllowedByDisabledConfigurations() {
  58. $this->mocks['helper']->expects($this->once())
  59. ->method('haveDisabledConfigurations')
  60. ->willReturn(true);
  61. $this->mocks['ocConfig']->expects($this->never())
  62. ->method('getSystemValue');
  63. $result = $this->bgJob->isCleanUpAllowed();
  64. $this->assertSame(false, $result);
  65. }
  66. /**
  67. * clean up job must not run when LDAP Helper is broken i.e.
  68. * returning unexpected results
  69. */
  70. public function test_runNotAllowedByBrokenHelper() {
  71. $this->mocks['helper']->expects($this->once())
  72. ->method('haveDisabledConfigurations')
  73. ->will($this->throwException(new Exception()));
  74. $this->mocks['ocConfig']->expects($this->never())
  75. ->method('getSystemValue');
  76. $result = $this->bgJob->isCleanUpAllowed();
  77. $this->assertSame(false, $result);
  78. }
  79. /**
  80. * clean up job must not run when it is not enabled
  81. */
  82. public function test_runNotAllowedBySysConfig() {
  83. $this->mocks['helper']->expects($this->once())
  84. ->method('haveDisabledConfigurations')
  85. ->willReturn(false);
  86. $this->mocks['ocConfig']->expects($this->once())
  87. ->method('getSystemValue')
  88. ->willReturn(false);
  89. $result = $this->bgJob->isCleanUpAllowed();
  90. $this->assertSame(false, $result);
  91. }
  92. /**
  93. * clean up job is allowed to run
  94. */
  95. public function test_runIsAllowed() {
  96. $this->mocks['helper']->expects($this->once())
  97. ->method('haveDisabledConfigurations')
  98. ->willReturn(false);
  99. $this->mocks['ocConfig']->expects($this->once())
  100. ->method('getSystemValue')
  101. ->willReturn(true);
  102. $result = $this->bgJob->isCleanUpAllowed();
  103. $this->assertSame(true, $result);
  104. }
  105. /**
  106. * check whether offset will be reset when it needs to
  107. */
  108. public function test_OffsetResetIsNecessary() {
  109. $result = $this->bgJob->isOffsetResetNecessary($this->bgJob->getChunkSize() - 1);
  110. $this->assertSame(true, $result);
  111. }
  112. /**
  113. * make sure offset is not reset when it is not due
  114. */
  115. public function test_OffsetResetIsNotNecessary() {
  116. $result = $this->bgJob->isOffsetResetNecessary($this->bgJob->getChunkSize());
  117. $this->assertSame(false, $result);
  118. }
  119. }