You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

AbstractUUIDFixTest.php 4.9KB

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\Access;
  27. use OCA\User_LDAP\Helper;
  28. use OCA\User_LDAP\LDAP;
  29. use OCA\User_LDAP\Mapping\GroupMapping;
  30. use OCA\User_LDAP\Mapping\UserMapping;
  31. use OCA\User_LDAP\Migration\UUIDFixUser;
  32. use OCA\User_LDAP\User_Proxy;
  33. use OCP\IConfig;
  34. use Test\TestCase;
  35. abstract class AbstractUUIDFixTest extends TestCase {
  36. /** @var Helper|\PHPUnit\Framework\MockObject\MockObject */
  37. protected $helper;
  38. /** @var IConfig|\PHPUnit\Framework\MockObject\MockObject */
  39. protected $config;
  40. /** @var LDAP|\PHPUnit\Framework\MockObject\MockObject */
  41. protected $ldap;
  42. /** @var UserMapping|GroupMapping|\PHPUnit\Framework\MockObject\MockObject */
  43. protected $mapper;
  44. /** @var UUIDFixUser */
  45. protected $job;
  46. /** @var User_Proxy|\PHPUnit\Framework\MockObject\MockObject */
  47. protected $proxy;
  48. /** @var Access|\PHPUnit\Framework\MockObject\MockObject */
  49. protected $access;
  50. /** @var bool */
  51. protected $isUser = true;
  52. protected function setUp(): void {
  53. parent::setUp();
  54. $this->ldap = $this->createMock(LDAP::class);
  55. $this->config = $this->createMock(IConfig::class);
  56. $this->access = $this->createMock(Access::class);
  57. $this->helper = $this->createMock(Helper::class);
  58. $this->helper->expects($this->any())
  59. ->method('getServerConfigurationPrefixes')
  60. ->with(true)
  61. ->willReturn(['s01', 's03']);
  62. }
  63. protected function instantiateJob($className) {
  64. $this->job = new $className($this->mapper, $this->proxy);
  65. $this->proxy->expects($this->any())
  66. ->method('getLDAPAccess')
  67. ->willReturn($this->access);
  68. }
  69. public function testRunSingleRecord() {
  70. $args = [
  71. 'records' => [
  72. 0 => [
  73. 'name' => 'Someone',
  74. 'dn' => 'uid=Someone,dc=Somewhere',
  75. 'uuid' => 'kaput'
  76. ]
  77. ]
  78. ];
  79. $correctUUID = '4355-AED3-9D73-03AD';
  80. $this->access->expects($this->once())
  81. ->method('getUUID')
  82. ->with($args['records'][0]['dn'], $this->isUser)
  83. ->willReturn($correctUUID);
  84. $this->mapper->expects($this->once())
  85. ->method('setUUIDbyDN')
  86. ->with($correctUUID, $args['records'][0]['dn']);
  87. $this->job->run($args);
  88. }
  89. public function testRunValidRecord() {
  90. $correctUUID = '4355-AED3-9D73-03AD';
  91. $args = [
  92. 'records' => [
  93. 0 => [
  94. 'name' => 'Someone',
  95. 'dn' => 'uid=Someone,dc=Somewhere',
  96. 'uuid' => $correctUUID
  97. ]
  98. ]
  99. ];
  100. $this->access->expects($this->once())
  101. ->method('getUUID')
  102. ->with($args['records'][0]['dn'], $this->isUser)
  103. ->willReturn($correctUUID);
  104. $this->mapper->expects($this->never())
  105. ->method('setUUIDbyDN');
  106. $this->job->run($args);
  107. }
  108. public function testRunRemovedRecord() {
  109. $args = [
  110. 'records' => [
  111. 0 => [
  112. 'name' => 'Someone',
  113. 'dn' => 'uid=Someone,dc=Somewhere',
  114. 'uuid' => 'kaput'
  115. ]
  116. ]
  117. ];
  118. $this->access->expects($this->once())
  119. ->method('getUUID')
  120. ->with($args['records'][0]['dn'], $this->isUser)
  121. ->willReturn(false);
  122. $this->mapper->expects($this->never())
  123. ->method('setUUIDbyDN');
  124. $this->job->run($args);
  125. }
  126. public function testRunManyRecords() {
  127. $args = [
  128. 'records' => [
  129. 0 => [
  130. 'name' => 'Someone',
  131. 'dn' => 'uid=Someone,dc=Somewhere',
  132. 'uuid' => 'kaput'
  133. ],
  134. 1 => [
  135. 'name' => 'kdslkdsaIdsal',
  136. 'dn' => 'uid=kdslkdsaIdsal,dc=Somewhere',
  137. 'uuid' => 'AED3-4355-03AD-9D73'
  138. ],
  139. 2 => [
  140. 'name' => 'Paperboy',
  141. 'dn' => 'uid=Paperboy,dc=Somewhere',
  142. 'uuid' => 'kaput'
  143. ]
  144. ]
  145. ];
  146. $correctUUIDs = ['4355-AED3-9D73-03AD', 'AED3-4355-03AD-9D73', 'AED3-9D73-4355-03AD'];
  147. $this->access->expects($this->exactly(3))
  148. ->method('getUUID')
  149. ->withConsecutive(
  150. [$args['records'][0]['dn'], $this->isUser],
  151. [$args['records'][1]['dn'], $this->isUser],
  152. [$args['records'][2]['dn'], $this->isUser]
  153. )
  154. ->willReturnOnConsecutiveCalls($correctUUIDs[0], $correctUUIDs[1], $correctUUIDs[2]);
  155. $this->mapper->expects($this->exactly(2))
  156. ->method('setUUIDbyDN')
  157. ->withConsecutive(
  158. [$correctUUIDs[0], $args['records'][0]['dn']],
  159. [$correctUUIDs[2], $args['records'][2]['dn']]
  160. );
  161. $this->job->run($args);
  162. }
  163. }