IntegrationTestUserCleanUp.php 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104
  1. <?php
  2. /**
  3. * @copyright Copyright (c) 2016, ownCloud, Inc.
  4. *
  5. * @author Arthur Schiwon <blizzz@arthur-schiwon.de>
  6. * @author Christoph Wurst <christoph@winzerhof-wurst.at>
  7. * @author Morris Jobke <hey@morrisjobke.de>
  8. *
  9. * @license AGPL-3.0
  10. *
  11. * This code is free software: you can redistribute it and/or modify
  12. * it under the terms of the GNU Affero General Public License, version 3,
  13. * as published by the Free Software Foundation.
  14. *
  15. * This program is distributed in the hope that it will be useful,
  16. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  17. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  18. * GNU Affero General Public License for more details.
  19. *
  20. * You should have received a copy of the GNU Affero General Public License, version 3,
  21. * along with this program. If not, see <http://www.gnu.org/licenses/>
  22. *
  23. */
  24. namespace OCA\User_LDAP\Tests\Integration\Lib\User;
  25. use OCA\User_LDAP\Jobs\CleanUp;
  26. use OCA\User_LDAP\Mapping\UserMapping;
  27. use OCA\User_LDAP\Tests\Integration\AbstractIntegrationTest;
  28. use OCA\User_LDAP\User_LDAP;
  29. use OCA\User_LDAP\UserPluginManager;
  30. require_once __DIR__ . '/../../Bootstrap.php';
  31. class IntegrationTestUserCleanUp extends AbstractIntegrationTest {
  32. /** @var UserMapping */
  33. protected $mapping;
  34. /**
  35. * prepares the LDAP environment and sets up a test configuration for
  36. * the LDAP backend.
  37. */
  38. public function init() {
  39. require(__DIR__ . '/../../setup-scripts/createExplicitUsers.php');
  40. parent::init();
  41. $this->mapping = new UserMapping(\OC::$server->getDatabaseConnection());
  42. $this->mapping->clear();
  43. $this->access->setUserMapper($this->mapping);
  44. $userBackend = new User_LDAP($this->access, \OC::$server->getConfig(), \OC::$server->getNotificationManager(), \OC::$server->getUserSession(), \OC::$server->query(UserPluginManager::class));
  45. \OC_User::useBackend($userBackend);
  46. }
  47. /**
  48. * adds a map entry for the user, so we know the username
  49. *
  50. * @param $dn
  51. * @param $username
  52. */
  53. private function prepareUser($dn, $username) {
  54. // assigns our self-picked oc username to the dn
  55. $this->mapping->map($dn, $username, 'fakeUUID-' . $username);
  56. }
  57. private function deleteUserFromLDAP($dn) {
  58. $cr = $this->connection->getConnectionResource();
  59. ldap_delete($cr, $dn);
  60. }
  61. /**
  62. * tests whether a display name consisting of two parts is created correctly
  63. *
  64. * @return bool
  65. */
  66. protected function case1() {
  67. $username = 'alice1337';
  68. $dn = 'uid=alice,ou=Users,' . $this->base;
  69. $this->prepareUser($dn, $username);
  70. $this->deleteUserFromLDAP($dn);
  71. $job = new CleanUp();
  72. $job->run([]);
  73. // user instance must not be requested from global user manager, before
  74. // it is deleted from the LDAP server. The instance will be returned
  75. // from cache and may false-positively confirm the correctness.
  76. $user = \OC::$server->getUserManager()->get($username);
  77. if ($user === null) {
  78. return false;
  79. }
  80. $user->delete();
  81. return null === \OC::$server->getUserManager()->get($username);
  82. }
  83. }
  84. /** @var string $host */
  85. /** @var int $port */
  86. /** @var string $adn */
  87. /** @var string $apwd */
  88. /** @var string $bdn */
  89. $test = new IntegrationTestUserCleanUp($host, $port, $adn, $apwd, $bdn);
  90. $test->init();
  91. $test->run();