AbstractUUIDFixTest.php 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160
  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\Access;
  8. use OCA\User_LDAP\Helper;
  9. use OCA\User_LDAP\LDAP;
  10. use OCA\User_LDAP\Mapping\AbstractMapping;
  11. use OCA\User_LDAP\Migration\UUIDFix;
  12. use OCA\User_LDAP\Proxy;
  13. use OCP\AppFramework\Utility\ITimeFactory;
  14. use OCP\IConfig;
  15. use Test\TestCase;
  16. abstract class AbstractUUIDFixTest extends TestCase {
  17. protected Helper $helper;
  18. protected IConfig $config;
  19. protected LDAP $ldap;
  20. protected AbstractMapping $mapper;
  21. protected UUIDFix $job;
  22. protected Proxy $proxy;
  23. protected Access $access;
  24. protected ITimeFactory $time;
  25. protected bool $isUser = true;
  26. protected function setUp(): void {
  27. parent::setUp();
  28. $this->ldap = $this->createMock(LDAP::class);
  29. $this->config = $this->createMock(IConfig::class);
  30. $this->access = $this->createMock(Access::class);
  31. $this->time = $this->createMock(ITimeFactory::class);
  32. $this->helper = $this->createMock(Helper::class);
  33. $this->helper->expects($this->any())
  34. ->method('getServerConfigurationPrefixes')
  35. ->with(true)
  36. ->willReturn(['s01', 's03']);
  37. }
  38. protected function instantiateJob($className) {
  39. $this->job = new $className($this->time, $this->mapper, $this->proxy);
  40. $this->proxy->expects($this->any())
  41. ->method('getLDAPAccess')
  42. ->willReturn($this->access);
  43. }
  44. public function testRunSingleRecord() {
  45. $args = [
  46. 'records' => [
  47. 0 => [
  48. 'name' => 'Someone',
  49. 'dn' => 'uid=Someone,dc=Somewhere',
  50. 'uuid' => 'kaput'
  51. ]
  52. ]
  53. ];
  54. $correctUUID = '4355-AED3-9D73-03AD';
  55. $this->access->expects($this->once())
  56. ->method('getUUID')
  57. ->with($args['records'][0]['dn'], $this->isUser)
  58. ->willReturn($correctUUID);
  59. $this->mapper->expects($this->once())
  60. ->method('setUUIDbyDN')
  61. ->with($correctUUID, $args['records'][0]['dn']);
  62. $this->job->run($args);
  63. }
  64. public function testRunValidRecord() {
  65. $correctUUID = '4355-AED3-9D73-03AD';
  66. $args = [
  67. 'records' => [
  68. 0 => [
  69. 'name' => 'Someone',
  70. 'dn' => 'uid=Someone,dc=Somewhere',
  71. 'uuid' => $correctUUID
  72. ]
  73. ]
  74. ];
  75. $this->access->expects($this->once())
  76. ->method('getUUID')
  77. ->with($args['records'][0]['dn'], $this->isUser)
  78. ->willReturn($correctUUID);
  79. $this->mapper->expects($this->never())
  80. ->method('setUUIDbyDN');
  81. $this->job->run($args);
  82. }
  83. public function testRunRemovedRecord() {
  84. $args = [
  85. 'records' => [
  86. 0 => [
  87. 'name' => 'Someone',
  88. 'dn' => 'uid=Someone,dc=Somewhere',
  89. 'uuid' => 'kaput'
  90. ]
  91. ]
  92. ];
  93. $this->access->expects($this->once())
  94. ->method('getUUID')
  95. ->with($args['records'][0]['dn'], $this->isUser)
  96. ->willReturn(false);
  97. $this->mapper->expects($this->never())
  98. ->method('setUUIDbyDN');
  99. $this->job->run($args);
  100. }
  101. public function testRunManyRecords() {
  102. $args = [
  103. 'records' => [
  104. 0 => [
  105. 'name' => 'Someone',
  106. 'dn' => 'uid=Someone,dc=Somewhere',
  107. 'uuid' => 'kaput'
  108. ],
  109. 1 => [
  110. 'name' => 'kdslkdsaIdsal',
  111. 'dn' => 'uid=kdslkdsaIdsal,dc=Somewhere',
  112. 'uuid' => 'AED3-4355-03AD-9D73'
  113. ],
  114. 2 => [
  115. 'name' => 'Paperboy',
  116. 'dn' => 'uid=Paperboy,dc=Somewhere',
  117. 'uuid' => 'kaput'
  118. ]
  119. ]
  120. ];
  121. $correctUUIDs = ['4355-AED3-9D73-03AD', 'AED3-4355-03AD-9D73', 'AED3-9D73-4355-03AD'];
  122. $this->access->expects($this->exactly(3))
  123. ->method('getUUID')
  124. ->withConsecutive(
  125. [$args['records'][0]['dn'], $this->isUser],
  126. [$args['records'][1]['dn'], $this->isUser],
  127. [$args['records'][2]['dn'], $this->isUser]
  128. )
  129. ->willReturnOnConsecutiveCalls($correctUUIDs[0], $correctUUIDs[1], $correctUUIDs[2]);
  130. $this->mapper->expects($this->exactly(2))
  131. ->method('setUUIDbyDN')
  132. ->withConsecutive(
  133. [$correctUUIDs[0], $args['records'][0]['dn']],
  134. [$correctUUIDs[2], $args['records'][2]['dn']]
  135. );
  136. $this->job->run($args);
  137. }
  138. }