AbstractIntegrationTest.php 4.6 KB

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