CleanUpTest.php 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156
  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 OCA\User_LDAP\Helper;
  28. use OCP\IConfig;
  29. use OCP\IDBConnection;
  30. class CleanUpTest extends \Test\TestCase {
  31. public function getMocks() {
  32. $mocks = [];
  33. $mocks['userBackend'] =
  34. $this->getMockBuilder('\OCA\User_LDAP\User_Proxy')
  35. ->disableOriginalConstructor()
  36. ->getMock();
  37. $mocks['deletedUsersIndex'] =
  38. $this->getMockBuilder('\OCA\User_LDAP\User\DeletedUsersIndex')
  39. ->disableOriginalConstructor()
  40. ->getMock();
  41. $mocks['ocConfig'] = $this->createMock(IConfig::class);
  42. $mocks['db'] = $this->createMock(IDBConnection::class);
  43. $mocks['helper'] = $this->createMock(Helper::class);
  44. return $mocks;
  45. }
  46. /**
  47. * clean up job must not run when there are disabled configurations
  48. */
  49. public function test_runNotAllowedByDisabledConfigurations() {
  50. $args = $this->getMocks();
  51. $args['helper']->expects($this->once())
  52. ->method('haveDisabledConfigurations')
  53. ->willReturn(true );
  54. $args['ocConfig']->expects($this->never())
  55. ->method('getSystemValue');
  56. $bgJob = new \OCA\User_LDAP\Jobs\CleanUp();
  57. $bgJob->setArguments($args);
  58. $result = $bgJob->isCleanUpAllowed();
  59. $this->assertSame(false, $result);
  60. }
  61. /**
  62. * clean up job must not run when LDAP Helper is broken i.e.
  63. * returning unexpected results
  64. */
  65. public function test_runNotAllowedByBrokenHelper() {
  66. $args = $this->getMocks();
  67. $args['helper']->expects($this->once())
  68. ->method('haveDisabledConfigurations')
  69. ->will($this->throwException(new \Exception()));
  70. $args['ocConfig']->expects($this->never())
  71. ->method('getSystemValue');
  72. $bgJob = new \OCA\User_LDAP\Jobs\CleanUp();
  73. $bgJob->setArguments($args);
  74. $result = $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. $args = $this->getMocks();
  82. $args['helper']->expects($this->once())
  83. ->method('haveDisabledConfigurations')
  84. ->willReturn(false);
  85. $args['ocConfig']->expects($this->once())
  86. ->method('getSystemValue')
  87. ->willReturn(false);
  88. $bgJob = new \OCA\User_LDAP\Jobs\CleanUp();
  89. $bgJob->setArguments($args);
  90. $result = $bgJob->isCleanUpAllowed();
  91. $this->assertSame(false, $result);
  92. }
  93. /**
  94. * clean up job is allowed to run
  95. */
  96. public function test_runIsAllowed() {
  97. $args = $this->getMocks();
  98. $args['helper']->expects($this->once())
  99. ->method('haveDisabledConfigurations')
  100. ->willReturn(false);
  101. $args['ocConfig']->expects($this->once())
  102. ->method('getSystemValue')
  103. ->willReturn(true);
  104. $bgJob = new \OCA\User_LDAP\Jobs\CleanUp();
  105. $bgJob->setArguments($args);
  106. $result = $bgJob->isCleanUpAllowed();
  107. $this->assertSame(true, $result);
  108. }
  109. /**
  110. * check whether offset will be reset when it needs to
  111. */
  112. public function test_OffsetResetIsNecessary() {
  113. $args = $this->getMocks();
  114. $bgJob = new \OCA\User_LDAP\Jobs\CleanUp();
  115. $bgJob->setArguments($args);
  116. $result = $bgJob->isOffsetResetNecessary($bgJob->getChunkSize() - 1);
  117. $this->assertSame(true, $result);
  118. }
  119. /**
  120. * make sure offset is not reset when it is not due
  121. */
  122. public function test_OffsetResetIsNotNecessary() {
  123. $args = $this->getMocks();
  124. $bgJob = new \OCA\User_LDAP\Jobs\CleanUp();
  125. $bgJob->setArguments($args);
  126. $result = $bgJob->isOffsetResetNecessary($bgJob->getChunkSize());
  127. $this->assertSame(false, $result);
  128. }
  129. }