AbstractIntegrationTest.php 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161
  1. <?php
  2. /**
  3. * SPDX-FileCopyrightText: 2016-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;
  8. use OCA\User_LDAP\Access;
  9. use OCA\User_LDAP\Connection;
  10. use OCA\User_LDAP\FilesystemHelper;
  11. use OCA\User_LDAP\GroupPluginManager;
  12. use OCA\User_LDAP\Helper;
  13. use OCA\User_LDAP\LDAP;
  14. use OCA\User_LDAP\User\Manager;
  15. use OCA\User_LDAP\UserPluginManager;
  16. use OCP\IAvatarManager;
  17. use OCP\Share\IManager;
  18. use Psr\Log\LoggerInterface;
  19. abstract class AbstractIntegrationTest {
  20. /** @var LDAP */
  21. protected $ldap;
  22. /** @var Connection */
  23. protected $connection;
  24. /** @var Access */
  25. protected $access;
  26. /** @var Manager */
  27. protected $userManager;
  28. /** @var Helper */
  29. protected $helper;
  30. /** @var string */
  31. protected $base;
  32. /** @var string[] */
  33. protected $server;
  34. public function __construct($host, $port, $bind, $pwd, $base) {
  35. $this->base = $base;
  36. $this->server = [
  37. 'host' => $host,
  38. 'port' => $port,
  39. 'dn' => $bind,
  40. 'pwd' => $pwd
  41. ];
  42. }
  43. /**
  44. * prepares the LDAP environment and sets up a test configuration for
  45. * the LDAP backend.
  46. */
  47. public function init() {
  48. \OC::$server->registerService(UserPluginManager::class, function () {
  49. return new \OCA\User_LDAP\UserPluginManager();
  50. });
  51. \OC::$server->registerService(GroupPluginManager::class, function () {
  52. return new \OCA\User_LDAP\GroupPluginManager();
  53. });
  54. $this->initLDAPWrapper();
  55. $this->initConnection();
  56. $this->initUserManager();
  57. $this->initHelper();
  58. $this->initAccess();
  59. }
  60. /**
  61. * initializes the test LDAP wrapper
  62. */
  63. protected function initLDAPWrapper() {
  64. $this->ldap = new LDAP();
  65. }
  66. /**
  67. * sets up the LDAP configuration to be used for the test
  68. */
  69. protected function initConnection() {
  70. $this->connection = new Connection($this->ldap, '', null);
  71. $this->connection->setConfiguration([
  72. 'ldapHost' => $this->server['host'],
  73. 'ldapPort' => $this->server['port'],
  74. 'ldapBase' => $this->base,
  75. 'ldapAgentName' => $this->server['dn'],
  76. 'ldapAgentPassword' => $this->server['pwd'],
  77. 'ldapUserFilter' => 'objectclass=inetOrgPerson',
  78. 'ldapUserDisplayName' => 'cn',
  79. 'ldapGroupDisplayName' => 'cn',
  80. 'ldapLoginFilter' => '(|(uid=%uid)(samaccountname=%uid))',
  81. 'ldapCacheTTL' => 0,
  82. 'ldapConfigurationActive' => 1,
  83. ]);
  84. }
  85. /**
  86. * initializes an LDAP user manager instance
  87. */
  88. protected function initUserManager() {
  89. $this->userManager = new Manager(
  90. \OC::$server->getConfig(),
  91. new FilesystemHelper(),
  92. \OC::$server->get(LoggerInterface::class),
  93. \OC::$server->get(IAvatarManager::class),
  94. new \OCP\Image(),
  95. \OC::$server->getUserManager(),
  96. \OC::$server->getNotificationManager(),
  97. \OC::$server->get(IManager::class)
  98. );
  99. }
  100. /**
  101. * initializes the test Helper
  102. */
  103. protected function initHelper() {
  104. $this->helper = new Helper(\OC::$server->getConfig(), \OC::$server->getDatabaseConnection());
  105. }
  106. /**
  107. * initializes the Access test instance
  108. */
  109. protected function initAccess() {
  110. $this->access = new Access($this->connection, $this->ldap, $this->userManager, $this->helper, \OC::$server->getConfig(), \OCP\Server::get(LoggerInterface::class));
  111. }
  112. /**
  113. * runs the test cases while outputting progress and result information
  114. *
  115. * If a test failed, the script is exited with return code 1.
  116. */
  117. public function run() {
  118. $methods = get_class_methods($this);
  119. $atLeastOneCaseRan = false;
  120. foreach ($methods as $method) {
  121. if (str_starts_with($method, 'case')) {
  122. print("running $method " . PHP_EOL);
  123. try {
  124. if (!$this->$method()) {
  125. print(PHP_EOL . '>>> !!! Test ' . $method . ' FAILED !!! <<<' . PHP_EOL . PHP_EOL);
  126. exit(1);
  127. }
  128. $atLeastOneCaseRan = true;
  129. } catch (\Exception $e) {
  130. print(PHP_EOL . '>>> !!! Test ' . $method . ' RAISED AN EXCEPTION !!! <<<' . PHP_EOL);
  131. print($e->getMessage() . PHP_EOL . PHP_EOL);
  132. exit(1);
  133. }
  134. }
  135. }
  136. if ($atLeastOneCaseRan) {
  137. print('Tests succeeded' . PHP_EOL);
  138. } else {
  139. print('No Test was available.' . PHP_EOL);
  140. exit(1);
  141. }
  142. }
  143. }