1
0

AbstractIntegrationTest.php 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180
  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 Joas Schilling <coding@schilljs.com>
  8. * @author Morris Jobke <hey@morrisjobke.de>
  9. * @author Roeland Jago Douma <roeland@famdouma.nl>
  10. * @author root <root@localhost.localdomain>
  11. * @author Vinicius Cubas Brand <vinicius@eita.org.br>
  12. *
  13. * @license AGPL-3.0
  14. *
  15. * This code is free software: you can redistribute it and/or modify
  16. * it under the terms of the GNU Affero General Public License, version 3,
  17. * as published by the Free Software Foundation.
  18. *
  19. * This program is distributed in the hope that it will be useful,
  20. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  21. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  22. * GNU Affero General Public License for more details.
  23. *
  24. * You should have received a copy of the GNU Affero General Public License, version 3,
  25. * along with this program. If not, see <http://www.gnu.org/licenses/>
  26. *
  27. */
  28. namespace OCA\User_LDAP\Tests\Integration;
  29. use OCA\User_LDAP\Access;
  30. use OCA\User_LDAP\Connection;
  31. use OCA\User_LDAP\FilesystemHelper;
  32. use OCA\User_LDAP\GroupPluginManager;
  33. use OCA\User_LDAP\Helper;
  34. use OCA\User_LDAP\LDAP;
  35. use OCA\User_LDAP\User\Manager;
  36. use OCA\User_LDAP\UserPluginManager;
  37. use OCP\Share\IManager;
  38. use Psr\Log\LoggerInterface;
  39. abstract class AbstractIntegrationTest {
  40. /** @var LDAP */
  41. protected $ldap;
  42. /** @var Connection */
  43. protected $connection;
  44. /** @var Access */
  45. protected $access;
  46. /** @var Manager */
  47. protected $userManager;
  48. /** @var Helper */
  49. protected $helper;
  50. /** @var string */
  51. protected $base;
  52. /** @var string[] */
  53. protected $server;
  54. public function __construct($host, $port, $bind, $pwd, $base) {
  55. $this->base = $base;
  56. $this->server = [
  57. 'host' => $host,
  58. 'port' => $port,
  59. 'dn' => $bind,
  60. 'pwd' => $pwd
  61. ];
  62. }
  63. /**
  64. * prepares the LDAP environment and sets up a test configuration for
  65. * the LDAP backend.
  66. */
  67. public function init() {
  68. \OC::$server->registerService(UserPluginManager::class, function () {
  69. return new \OCA\User_LDAP\UserPluginManager();
  70. });
  71. \OC::$server->registerService(GroupPluginManager::class, function () {
  72. return new \OCA\User_LDAP\GroupPluginManager();
  73. });
  74. $this->initLDAPWrapper();
  75. $this->initConnection();
  76. $this->initUserManager();
  77. $this->initHelper();
  78. $this->initAccess();
  79. }
  80. /**
  81. * initializes the test LDAP wrapper
  82. */
  83. protected function initLDAPWrapper() {
  84. $this->ldap = new LDAP();
  85. }
  86. /**
  87. * sets up the LDAP configuration to be used for the test
  88. */
  89. protected function initConnection() {
  90. $this->connection = new Connection($this->ldap, '', null);
  91. $this->connection->setConfiguration([
  92. 'ldapHost' => $this->server['host'],
  93. 'ldapPort' => $this->server['port'],
  94. 'ldapBase' => $this->base,
  95. 'ldapAgentName' => $this->server['dn'],
  96. 'ldapAgentPassword' => $this->server['pwd'],
  97. 'ldapUserFilter' => 'objectclass=inetOrgPerson',
  98. 'ldapUserDisplayName' => 'cn',
  99. 'ldapGroupDisplayName' => 'cn',
  100. 'ldapLoginFilter' => '(|(uid=%uid)(samaccountname=%uid))',
  101. 'ldapCacheTTL' => 0,
  102. 'ldapConfigurationActive' => 1,
  103. ]);
  104. }
  105. /**
  106. * initializes an LDAP user manager instance
  107. */
  108. protected function initUserManager() {
  109. $this->userManager = new Manager(
  110. \OC::$server->getConfig(),
  111. new FilesystemHelper(),
  112. \OC::$server->get(LoggerInterface::class),
  113. \OC::$server->getAvatarManager(),
  114. new \OCP\Image(),
  115. \OC::$server->getUserManager(),
  116. \OC::$server->getNotificationManager(),
  117. \OC::$server->get(IManager::class)
  118. );
  119. }
  120. /**
  121. * initializes the test Helper
  122. */
  123. protected function initHelper() {
  124. $this->helper = new Helper(\OC::$server->getConfig(), \OC::$server->getDatabaseConnection());
  125. }
  126. /**
  127. * initializes the Access test instance
  128. */
  129. protected function initAccess() {
  130. $this->access = new Access($this->connection, $this->ldap, $this->userManager, $this->helper, \OC::$server->getConfig(), \OC::$server->getLogger());
  131. }
  132. /**
  133. * runs the test cases while outputting progress and result information
  134. *
  135. * If a test failed, the script is exited with return code 1.
  136. */
  137. public function run() {
  138. $methods = get_class_methods($this);
  139. $atLeastOneCaseRan = false;
  140. foreach ($methods as $method) {
  141. if (str_starts_with($method, 'case')) {
  142. print("running $method " . PHP_EOL);
  143. try {
  144. if (!$this->$method()) {
  145. print(PHP_EOL . '>>> !!! Test ' . $method . ' FAILED !!! <<<' . PHP_EOL . PHP_EOL);
  146. exit(1);
  147. }
  148. $atLeastOneCaseRan = true;
  149. } catch (\Exception $e) {
  150. print(PHP_EOL . '>>> !!! Test ' . $method . ' RAISED AN EXCEPTION !!! <<<' . PHP_EOL);
  151. print($e->getMessage() . PHP_EOL . PHP_EOL);
  152. exit(1);
  153. }
  154. }
  155. }
  156. if ($atLeastOneCaseRan) {
  157. print('Tests succeeded' . PHP_EOL);
  158. } else {
  159. print('No Test was available.' . PHP_EOL);
  160. exit(1);
  161. }
  162. }
  163. }