IntegrationTestUserCleanUp.php 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990
  1. <?php
  2. /**
  3. * SPDX-FileCopyrightText: 2017-2024 Nextcloud GmbH and Nextcloud contributors
  4. * SPDX-FileCopyrightText: 2016 ownCloud, Inc.
  5. * SPDX-License-Identifier: AGPL-3.0-only
  6. */
  7. namespace OCA\User_LDAP\Tests\Integration\Lib\User;
  8. use OCA\User_LDAP\Jobs\CleanUp;
  9. use OCA\User_LDAP\Mapping\UserMapping;
  10. use OCA\User_LDAP\Tests\Integration\AbstractIntegrationTest;
  11. use OCA\User_LDAP\User\DeletedUsersIndex;
  12. use OCA\User_LDAP\User_LDAP;
  13. use OCA\User_LDAP\UserPluginManager;
  14. use Psr\Log\LoggerInterface;
  15. require_once __DIR__ . '/../../Bootstrap.php';
  16. class IntegrationTestUserCleanUp extends AbstractIntegrationTest {
  17. /** @var UserMapping */
  18. protected $mapping;
  19. /**
  20. * prepares the LDAP environment and sets up a test configuration for
  21. * the LDAP backend.
  22. */
  23. public function init() {
  24. require(__DIR__ . '/../../setup-scripts/createExplicitUsers.php');
  25. parent::init();
  26. $this->mapping = new UserMapping(\OC::$server->getDatabaseConnection());
  27. $this->mapping->clear();
  28. $this->access->setUserMapper($this->mapping);
  29. $userBackend = new User_LDAP($this->access, \OC::$server->getNotificationManager(), \OC::$server->get(UserPluginManager::class), \OC::$server->get(LoggerInterface::class), \OC::$server->get(DeletedUsersIndex::class));
  30. \OC_User::useBackend($userBackend);
  31. }
  32. /**
  33. * adds a map entry for the user, so we know the username
  34. *
  35. * @param $dn
  36. * @param $username
  37. */
  38. private function prepareUser($dn, $username) {
  39. // assigns our self-picked oc username to the dn
  40. $this->mapping->map($dn, $username, 'fakeUUID-' . $username);
  41. }
  42. private function deleteUserFromLDAP($dn) {
  43. $cr = $this->connection->getConnectionResource();
  44. ldap_delete($cr, $dn);
  45. }
  46. /**
  47. * tests whether a display name consisting of two parts is created correctly
  48. *
  49. * @return bool
  50. */
  51. protected function case1() {
  52. $username = 'alice1337';
  53. $dn = 'uid=alice,ou=Users,' . $this->base;
  54. $this->prepareUser($dn, $username);
  55. $this->deleteUserFromLDAP($dn);
  56. $job = new CleanUp();
  57. $job->run([]);
  58. // user instance must not be requested from global user manager, before
  59. // it is deleted from the LDAP server. The instance will be returned
  60. // from cache and may false-positively confirm the correctness.
  61. $user = \OC::$server->getUserManager()->get($username);
  62. if ($user === null) {
  63. return false;
  64. }
  65. $user->delete();
  66. return \OC::$server->getUserManager()->get($username) === null;
  67. }
  68. }
  69. /** @var string $host */
  70. /** @var int $port */
  71. /** @var string $adn */
  72. /** @var string $apwd */
  73. /** @var string $bdn */
  74. $test = new IntegrationTestUserCleanUp($host, $port, $adn, $apwd, $bdn);
  75. $test->init();
  76. $test->run();