UUIDFixInsertTest.php 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193
  1. <?php
  2. /**
  3. * @copyright Copyright (c) 2017 Arthur Schiwon <blizzz@arthur-schiwon.de>
  4. *
  5. * @author Arthur Schiwon <blizzz@arthur-schiwon.de>
  6. * @author Morris Jobke <hey@morrisjobke.de>
  7. * @author Roeland Jago Douma <roeland@famdouma.nl>
  8. *
  9. * @license GNU AGPL version 3 or any later version
  10. *
  11. * This program is free software: you can redistribute it and/or modify
  12. * it under the terms of the GNU Affero General Public License as
  13. * published by the Free Software Foundation, either version 3 of the
  14. * License, or (at your option) any later version.
  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
  22. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  23. *
  24. */
  25. namespace OCA\User_LDAP\Tests\Migration;
  26. use OCA\User_LDAP\Mapping\GroupMapping;
  27. use OCA\User_LDAP\Mapping\UserMapping;
  28. use OCA\User_LDAP\Migration\UUIDFixInsert;
  29. use OCP\BackgroundJob\IJobList;
  30. use OCP\IConfig;
  31. use OCP\Migration\IOutput;
  32. use Test\TestCase;
  33. class UUIDFixInsertTest extends TestCase {
  34. /** @var IConfig|\PHPUnit\Framework\MockObject\MockObject */
  35. protected $config;
  36. /** @var UserMapping|\PHPUnit\Framework\MockObject\MockObject */
  37. protected $userMapper;
  38. /** @var GroupMapping|\PHPUnit\Framework\MockObject\MockObject */
  39. protected $groupMapper;
  40. /** @var IJobList|\PHPUnit\Framework\MockObject\MockObject */
  41. protected $jobList;
  42. /** @var UUIDFixInsert */
  43. protected $job;
  44. protected function setUp(): void {
  45. parent::setUp();
  46. $this->jobList = $this->createMock(IJobList::class);
  47. $this->config = $this->createMock(IConfig::class);
  48. $this->userMapper = $this->createMock(UserMapping::class);
  49. $this->groupMapper = $this->createMock(GroupMapping::class);
  50. $this->job = new UUIDFixInsert(
  51. $this->config,
  52. $this->userMapper,
  53. $this->groupMapper,
  54. $this->jobList
  55. );
  56. }
  57. public function testGetName() {
  58. $this->assertSame('Insert UUIDFix background job for user and group in batches', $this->job->getName());
  59. }
  60. public function recordProvider() {
  61. $record = [
  62. 'dn' => 'cn=somerecord,dc=somewhere',
  63. 'name' => 'Something',
  64. 'uuid' => 'AB12-3456-CDEF7-8GH9'
  65. ];
  66. array_fill(0, 50, $record);
  67. $userBatches = [
  68. 0 => array_fill(0, 50, $record),
  69. 1 => array_fill(0, 50, $record),
  70. 2 => array_fill(0, 13, $record),
  71. ];
  72. $groupBatches = [
  73. 0 => array_fill(0, 7, $record),
  74. ];
  75. return [
  76. ['userBatches' => $userBatches, 'groupBatches' => $groupBatches]
  77. ];
  78. }
  79. public function recordProviderTooLongAndNone() {
  80. $record = [
  81. 'dn' => 'cn=somerecord,dc=somewhere',
  82. 'name' => 'Something',
  83. 'uuid' => 'AB12-3456-CDEF7-8GH9'
  84. ];
  85. array_fill(0, 50, $record);
  86. $userBatches = [
  87. 0 => array_fill(0, 50, $record),
  88. 1 => array_fill(0, 40, $record),
  89. 2 => array_fill(0, 32, $record),
  90. 3 => array_fill(0, 32, $record),
  91. 4 => array_fill(0, 23, $record),
  92. ];
  93. $groupBatches = [0 => []];
  94. return [
  95. ['userBatches' => $userBatches, 'groupBatches' => $groupBatches]
  96. ];
  97. }
  98. /**
  99. * @dataProvider recordProvider
  100. */
  101. public function testRun($userBatches, $groupBatches) {
  102. $this->config->expects($this->once())
  103. ->method('getAppValue')
  104. ->with('user_ldap', 'installed_version', '1.2.1')
  105. ->willReturn('1.2.0');
  106. $this->userMapper->expects($this->exactly(3))
  107. ->method('getList')
  108. ->withConsecutive([0, 50], [50, 50], [100, 50])
  109. ->willReturnOnConsecutiveCalls($userBatches[0], $userBatches[1], $userBatches[2]);
  110. $this->groupMapper->expects($this->exactly(1))
  111. ->method('getList')
  112. ->with(0, 50)
  113. ->willReturn($groupBatches[0]);
  114. $this->jobList->expects($this->exactly(4))
  115. ->method('add');
  116. /** @var IOutput $out */
  117. $out = $this->createMock(IOutput::class);
  118. $this->job->run($out);
  119. }
  120. /**
  121. * @dataProvider recordProviderTooLongAndNone
  122. */
  123. public function testRunWithManyAndNone($userBatches, $groupBatches) {
  124. $this->config->expects($this->once())
  125. ->method('getAppValue')
  126. ->with('user_ldap', 'installed_version', '1.2.1')
  127. ->willReturn('1.2.0');
  128. $this->userMapper->expects($this->exactly(5))
  129. ->method('getList')
  130. ->withConsecutive([0, 50], [0, 40], [0, 32], [32, 32], [64, 32])
  131. ->willReturnOnConsecutiveCalls($userBatches[0], $userBatches[1], $userBatches[2], $userBatches[3], $userBatches[4]);
  132. $this->groupMapper->expects($this->once())
  133. ->method('getList')
  134. ->with(0, 50)
  135. ->willReturn($groupBatches[0]);
  136. $this->jobList->expects($this->exactly(5))
  137. ->method('add')
  138. ->willReturnOnConsecutiveCalls(
  139. $this->throwException(new \InvalidArgumentException('Background job arguments can\'t exceed 4000 etc')),
  140. $this->throwException(new \InvalidArgumentException('Background job arguments can\'t exceed 4000 etc')),
  141. null,
  142. null,
  143. null,
  144. );
  145. /** @var IOutput $out */
  146. $out = $this->createMock(IOutput::class);
  147. $this->job->run($out);
  148. }
  149. public function testDonNotRun() {
  150. $this->config->expects($this->once())
  151. ->method('getAppValue')
  152. ->with('user_ldap', 'installed_version', '1.2.1')
  153. ->willReturn('1.2.1');
  154. $this->userMapper->expects($this->never())
  155. ->method('getList');
  156. $this->groupMapper->expects($this->never())
  157. ->method('getList');
  158. $this->jobList->expects($this->never())
  159. ->method('add');
  160. /** @var IOutput $out */
  161. $out = $this->createMock(IOutput::class);
  162. $this->job->run($out);
  163. }
  164. }