Completed
Push — stable8.2 ( 458a90...47a1fb )
by Morris
115:41
created

IntegrationConnect::__construct()   A

Complexity

Conditions 3
Paths 4

Size

Total Lines 12
Code Lines 8

Duplication

Lines 6
Ratio 50 %

Code Coverage

Tests 0
CRAP Score 12

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 6
loc 12
ccs 0
cts 11
cp 0
rs 9.4286
cc 3
eloc 8
nc 4
nop 5
crap 12
1
<?php
2
/**
3
 * @author Arthur Schiwon <[email protected]>
4
 *
5
 * @copyright Copyright (c) 2015, ownCloud, Inc.
6
 * @license AGPL-3.0
7
 *
8
 * This code is free software: you can redistribute it and/or modify
9
 * it under the terms of the GNU Affero General Public License, version 3,
10
 * as published by the Free Software Foundation.
11
 *
12
 * This program is distributed in the hope that it will be useful,
13
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15
 * GNU Affero General Public License for more details.
16
 *
17
 * You should have received a copy of the GNU Affero General Public License, version 3,
18
 * along with this program.  If not, see <http://www.gnu.org/licenses/>
19
 *
20
 */
21
22
namespace OCA\user_ldap\tests\integration\lib;
23
24
use OCA\user_ldap\lib\user\Manager as LDAPUserManager;
25
use OCA\user_ldap\tests\integration\AbstractIntegrationTest;
26
use OCA\User_LDAP\Mapping\UserMapping;
27
use OCA\user_ldap\USER_LDAP;
28
29
require_once __DIR__  . '/../../../../../lib/base.php';
30
31
class IntegrationConnect extends AbstractIntegrationTest {
32
	/** @var  UserMapping */
33
	protected $mapping;
34
35
	/** @var USER_LDAP */
36
	protected $backend;
37
38
	/** @var  string */
39
	protected $host;
40
41
	/** @var  int */
42
	protected $port;
43
44
	public function __construct($host, $port, $bind, $pwd, $base) {
45
		// make sure host is a simple host name
46 View Code Duplication
		if(strpos($host, '://') !== false) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
47
			$host = substr_replace($host, '', 0, strpos($host, '://') + 3);
48
		}
49 View Code Duplication
		if(strpos($host, ':') !== false) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
50
			$host = substr_replace($host, '', strpos($host, ':'));
51
		}
52
		$this->host = $host;
53
		$this->port = $port;
54
		parent::__construct($host, $port, $bind, $pwd, $base);
55
	}
56
57
	/**
58
	 * test that a faulty host will does not connect successfully
59
	 *
60
	 * @return bool
61
	 */
62 View Code Duplication
	protected function case1() {
63
		// reset possible LDAP connection
64
		$this->initConnection();
65
		$this->connection->setConfiguration([
66
			'ldapHost' => 'qwertz.uiop',
67
		]);
68
		try {
69
			$this->connection->getConnectionResource();
70
		} catch (\OC\ServerNotAvailableException $e) {
71
			return true;
72
		}
73
		return false;
74
	}
75
76
	/**
77
	 * tests that a connect succeeds when only a hostname is provided
78
	 *
79
	 * @return bool
80
	 */
81
	protected function case2() {
82
		// reset possible LDAP connection
83
		$this->initConnection();
84
		$this->connection->setConfiguration([
85
				'ldapHost' => $this->host,
86
		]);
87
		try {
88
			$this->connection->getConnectionResource();
89
		} catch (\OC\ServerNotAvailableException $e) {
90
			return false;
91
		}
92
		return true;
93
	}
94
95
	/**
96
	 * tests that a connect succeeds when an LDAP URL is provided
97
	 *
98
	 * @return bool
99
	 */
100 View Code Duplication
	protected function case3() {
101
		// reset possible LDAP connection
102
		$this->initConnection();
103
		$this->connection->setConfiguration([
104
				'ldapHost' => 'ldap://' . $this->host,
105
		]);
106
		try {
107
			$this->connection->getConnectionResource();
108
		} catch (\OC\ServerNotAvailableException $e) {
109
			return false;
110
		}
111
		return true;
112
	}
113
114
	/**
115
	 * tests that a connect succeeds when an LDAP URL with port is provided
116
	 *
117
	 * @return bool
118
	 */
119 View Code Duplication
	protected function case4() {
120
		// reset possible LDAP connection
121
		$this->initConnection();
122
		$this->connection->setConfiguration([
123
				'ldapHost' => 'ldap://' . $this->host  . ':' . $this->port,
124
		]);
125
		try {
126
			$this->connection->getConnectionResource();
127
		} catch (\OC\ServerNotAvailableException $e) {
128
			return false;
129
		}
130
		return true;
131
	}
132
133
	/**
134
	 * tests that a connect succeeds when a hostname with port is provided
135
	 *
136
	 * @return bool
137
	 */
138 View Code Duplication
	protected function case5() {
139
		// reset possible LDAP connection
140
		$this->initConnection();
141
		$this->connection->setConfiguration([
142
				'ldapHost' => $this->host  . ':' . $this->port,
143
		]);
144
		try {
145
			$this->connection->getConnectionResource();
146
		} catch (\OC\ServerNotAvailableException $e) {
147
			return false;
148
		}
149
		return true;
150
	}
151
152
	/**
153
	 * repeat case1, only to make sure that not a connection was reused by
154
	 * accident.
155
	 *
156
	 * @return bool
157
	 */
158
	protected function case6() {
159
		return $this->case1();
160
	}
161
}
162
163
require_once(__DIR__ . '/../setup-scripts/config.php');
164
$test = new IntegrationConnect($host, $port, $adn, $apwd, $bdn);
165
$test->init();
166
$test->run();
167