1
0

AbstractIntegrationTest.php 4.0 KB

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