UUIDFixInsertTest.php 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174
  1. <?php
  2. /**
  3. * SPDX-FileCopyrightText: 2017 Nextcloud GmbH and Nextcloud contributors
  4. * SPDX-License-Identifier: AGPL-3.0-or-later
  5. */
  6. namespace OCA\User_LDAP\Tests\Migration;
  7. use OCA\User_LDAP\Mapping\GroupMapping;
  8. use OCA\User_LDAP\Mapping\UserMapping;
  9. use OCA\User_LDAP\Migration\UUIDFixInsert;
  10. use OCP\BackgroundJob\IJobList;
  11. use OCP\IConfig;
  12. use OCP\Migration\IOutput;
  13. use Test\TestCase;
  14. class UUIDFixInsertTest extends TestCase {
  15. /** @var IConfig|\PHPUnit\Framework\MockObject\MockObject */
  16. protected $config;
  17. /** @var UserMapping|\PHPUnit\Framework\MockObject\MockObject */
  18. protected $userMapper;
  19. /** @var GroupMapping|\PHPUnit\Framework\MockObject\MockObject */
  20. protected $groupMapper;
  21. /** @var IJobList|\PHPUnit\Framework\MockObject\MockObject */
  22. protected $jobList;
  23. /** @var UUIDFixInsert */
  24. protected $job;
  25. protected function setUp(): void {
  26. parent::setUp();
  27. $this->jobList = $this->createMock(IJobList::class);
  28. $this->config = $this->createMock(IConfig::class);
  29. $this->userMapper = $this->createMock(UserMapping::class);
  30. $this->groupMapper = $this->createMock(GroupMapping::class);
  31. $this->job = new UUIDFixInsert(
  32. $this->config,
  33. $this->userMapper,
  34. $this->groupMapper,
  35. $this->jobList
  36. );
  37. }
  38. public function testGetName(): void {
  39. $this->assertSame('Insert UUIDFix background job for user and group in batches', $this->job->getName());
  40. }
  41. public function recordProvider() {
  42. $record = [
  43. 'dn' => 'cn=somerecord,dc=somewhere',
  44. 'name' => 'Something',
  45. 'uuid' => 'AB12-3456-CDEF7-8GH9'
  46. ];
  47. array_fill(0, 50, $record);
  48. $userBatches = [
  49. 0 => array_fill(0, 50, $record),
  50. 1 => array_fill(0, 50, $record),
  51. 2 => array_fill(0, 13, $record),
  52. ];
  53. $groupBatches = [
  54. 0 => array_fill(0, 7, $record),
  55. ];
  56. return [
  57. ['userBatches' => $userBatches, 'groupBatches' => $groupBatches]
  58. ];
  59. }
  60. public function recordProviderTooLongAndNone() {
  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, 40, $record),
  70. 2 => array_fill(0, 32, $record),
  71. 3 => array_fill(0, 32, $record),
  72. 4 => array_fill(0, 23, $record),
  73. ];
  74. $groupBatches = [0 => []];
  75. return [
  76. ['userBatches' => $userBatches, 'groupBatches' => $groupBatches]
  77. ];
  78. }
  79. /**
  80. * @dataProvider recordProvider
  81. */
  82. public function testRun($userBatches, $groupBatches): void {
  83. $this->config->expects($this->once())
  84. ->method('getAppValue')
  85. ->with('user_ldap', 'installed_version', '1.2.1')
  86. ->willReturn('1.2.0');
  87. $this->userMapper->expects($this->exactly(3))
  88. ->method('getList')
  89. ->withConsecutive([0, 50], [50, 50], [100, 50])
  90. ->willReturnOnConsecutiveCalls($userBatches[0], $userBatches[1], $userBatches[2]);
  91. $this->groupMapper->expects($this->exactly(1))
  92. ->method('getList')
  93. ->with(0, 50)
  94. ->willReturn($groupBatches[0]);
  95. $this->jobList->expects($this->exactly(4))
  96. ->method('add');
  97. /** @var IOutput $out */
  98. $out = $this->createMock(IOutput::class);
  99. $this->job->run($out);
  100. }
  101. /**
  102. * @dataProvider recordProviderTooLongAndNone
  103. */
  104. public function testRunWithManyAndNone($userBatches, $groupBatches): void {
  105. $this->config->expects($this->once())
  106. ->method('getAppValue')
  107. ->with('user_ldap', 'installed_version', '1.2.1')
  108. ->willReturn('1.2.0');
  109. $this->userMapper->expects($this->exactly(5))
  110. ->method('getList')
  111. ->withConsecutive([0, 50], [0, 40], [0, 32], [32, 32], [64, 32])
  112. ->willReturnOnConsecutiveCalls($userBatches[0], $userBatches[1], $userBatches[2], $userBatches[3], $userBatches[4]);
  113. $this->groupMapper->expects($this->once())
  114. ->method('getList')
  115. ->with(0, 50)
  116. ->willReturn($groupBatches[0]);
  117. $this->jobList->expects($this->exactly(5))
  118. ->method('add')
  119. ->willReturnOnConsecutiveCalls(
  120. $this->throwException(new \InvalidArgumentException('Background job arguments can\'t exceed 4000 etc')),
  121. $this->throwException(new \InvalidArgumentException('Background job arguments can\'t exceed 4000 etc')),
  122. null,
  123. null,
  124. null,
  125. );
  126. /** @var IOutput $out */
  127. $out = $this->createMock(IOutput::class);
  128. $this->job->run($out);
  129. }
  130. public function testDonNotRun(): void {
  131. $this->config->expects($this->once())
  132. ->method('getAppValue')
  133. ->with('user_ldap', 'installed_version', '1.2.1')
  134. ->willReturn('1.2.1');
  135. $this->userMapper->expects($this->never())
  136. ->method('getList');
  137. $this->groupMapper->expects($this->never())
  138. ->method('getList');
  139. $this->jobList->expects($this->never())
  140. ->method('add');
  141. /** @var IOutput $out */
  142. $out = $this->createMock(IOutput::class);
  143. $this->job->run($out);
  144. }
  145. }