|
1
|
|
|
<?php |
|
2
|
|
|
/** |
|
3
|
|
|
* @author Arthur Schiwon <[email protected]> |
|
4
|
|
|
* @author Joas Schilling <[email protected]> |
|
5
|
|
|
* @author Morris Jobke <[email protected]> |
|
6
|
|
|
* |
|
7
|
|
|
* @copyright Copyright (c) 2015, ownCloud, Inc. |
|
8
|
|
|
* @license AGPL-3.0 |
|
9
|
|
|
* |
|
10
|
|
|
* This code is free software: you can redistribute it and/or modify |
|
11
|
|
|
* it under the terms of the GNU Affero General Public License, version 3, |
|
12
|
|
|
* as published by the Free Software Foundation. |
|
13
|
|
|
* |
|
14
|
|
|
* This program is distributed in the hope that it will be useful, |
|
15
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of |
|
16
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
|
17
|
|
|
* GNU Affero General Public License for more details. |
|
18
|
|
|
* |
|
19
|
|
|
* You should have received a copy of the GNU Affero General Public License, version 3, |
|
20
|
|
|
* along with this program. If not, see <http://www.gnu.org/licenses/> |
|
21
|
|
|
* |
|
22
|
|
|
*/ |
|
23
|
|
|
|
|
24
|
|
|
namespace OCA\user_ldap\tests; |
|
25
|
|
|
use OCA\user_ldap\lib\Connection; |
|
26
|
|
|
|
|
27
|
|
|
class Test_Connection extends \Test\TestCase { |
|
28
|
1 |
|
/** @var \OCA\user_ldap\lib\ILDAPWrapper */ |
|
29
|
|
|
protected $ldap; |
|
30
|
|
|
|
|
31
|
|
|
/** @var Connection */ |
|
32
|
1 |
|
protected $connection; |
|
33
|
|
|
|
|
34
|
1 |
|
public function setUp() { |
|
35
|
|
|
parent::setUp(); |
|
36
|
1 |
|
|
|
37
|
1 |
|
$this->ldap = $this->getMock('\OCA\user_ldap\lib\ILDAPWrapper'); |
|
38
|
1 |
|
// we use a mock here to replace the cache mechanism, due to missing DI in LDAP backend. |
|
39
|
1 |
|
$this->connection = $this->getMockBuilder('OCA\user_ldap\lib\Connection') |
|
40
|
|
|
->setMethods(['getFromCache', 'writeToCache']) |
|
41
|
1 |
|
->setConstructorArgs([$this->ldap, '', null]) |
|
42
|
|
|
->getMock(); |
|
43
|
1 |
|
|
|
44
|
1 |
|
$this->ldap->expects($this->any()) |
|
45
|
1 |
|
->method('areLDAPFunctionsAvailable') |
|
46
|
1 |
|
->will($this->returnValue(true)); |
|
47
|
|
|
} |
|
48
|
1 |
|
|
|
49
|
1 |
|
public function testOriginalAgentUnchangedOnClone() { |
|
50
|
|
|
//background: upon login a bind is done with the user credentials |
|
51
|
1 |
|
//which is valid for the whole LDAP resource. It needs to be reset |
|
52
|
1 |
|
//to the agent's credentials |
|
53
|
1 |
|
$lw = $this->getMock('\OCA\user_ldap\lib\ILDAPWrapper'); |
|
54
|
|
|
|
|
55
|
|
|
$connection = new Connection($lw, '', null); |
|
56
|
|
|
$agent = array( |
|
57
|
|
|
'ldapAgentName' => 'agent', |
|
58
|
|
|
'ldapAgentPassword' => '123456', |
|
59
|
|
|
); |
|
60
|
|
|
$connection->setConfiguration($agent); |
|
61
|
|
|
|
|
62
|
|
|
$testConnection = clone $connection; |
|
63
|
|
|
$user = array( |
|
64
|
|
|
'ldapAgentName' => 'user', |
|
65
|
|
|
'ldapAgentPassword' => 'password', |
|
66
|
|
|
); |
|
67
|
|
|
$testConnection->setConfiguration($user); |
|
68
|
|
|
|
|
69
|
|
|
$agentName = $connection->ldapAgentName; |
|
|
|
|
|
|
70
|
|
|
$agentPawd = $connection->ldapAgentPassword; |
|
|
|
|
|
|
71
|
|
|
|
|
72
|
|
|
$this->assertSame($agentName, $agent['ldapAgentName']); |
|
73
|
|
|
$this->assertSame($agentPawd, $agent['ldapAgentPassword']); |
|
74
|
|
|
} |
|
75
|
|
|
|
|
76
|
|
|
public function testUseBackupServer() { |
|
77
|
|
|
$mainHost = 'ldap://nixda.ldap'; |
|
78
|
|
|
$backupHost = 'ldap://fallback.ldap'; |
|
79
|
|
|
$config = [ |
|
80
|
|
|
'ldapConfigurationActive' => true, |
|
81
|
|
|
'ldapHost' => $mainHost, |
|
82
|
|
|
'ldapPort' => 389, |
|
83
|
|
|
'ldapBackupHost' => $backupHost, |
|
84
|
|
|
'ldapBackupPort' => 389, |
|
85
|
|
|
'ldapAgentName' => 'uid=agent', |
|
86
|
|
|
'ldapAgentPassword' => 'SuchASecret' |
|
87
|
|
|
]; |
|
88
|
|
|
|
|
89
|
|
|
$this->connection->setIgnoreValidation(true); |
|
90
|
|
|
$this->connection->setConfiguration($config); |
|
91
|
|
|
|
|
92
|
|
|
$this->ldap->expects($this->any()) |
|
93
|
|
|
->method('isResource') |
|
94
|
|
|
->will($this->returnValue(true)); |
|
95
|
|
|
|
|
96
|
|
|
$this->ldap->expects($this->any()) |
|
97
|
|
|
->method('setOption') |
|
98
|
|
|
->will($this->returnValue(true)); |
|
99
|
|
|
|
|
100
|
|
|
$this->ldap->expects($this->exactly(3)) |
|
101
|
|
|
->method('connect') |
|
102
|
|
|
->will($this->returnValue('ldapResource')); |
|
103
|
|
|
|
|
104
|
|
|
// Not called often enough? Then, the fallback to the backup server is broken. |
|
105
|
|
|
$this->connection->expects($this->exactly(4)) |
|
106
|
|
|
->method('getFromCache') |
|
107
|
|
|
->with('overrideMainServer') |
|
108
|
|
|
->will($this->onConsecutiveCalls(false, false, true, true)); |
|
109
|
|
|
|
|
110
|
|
|
$this->connection->expects($this->once()) |
|
111
|
|
|
->method('writeToCache') |
|
112
|
|
|
->with('overrideMainServer', true); |
|
113
|
|
|
|
|
114
|
|
|
$isThrown = false; |
|
115
|
|
|
$this->ldap->expects($this->exactly(3)) |
|
116
|
|
|
->method('bind') |
|
117
|
|
|
->will($this->returnCallback(function () use (&$isThrown) { |
|
118
|
|
|
if(!$isThrown) { |
|
119
|
|
|
$isThrown = true; |
|
120
|
|
|
throw new \OC\ServerNotAvailableException(); |
|
121
|
|
|
} |
|
122
|
|
|
return true; |
|
123
|
|
|
})); |
|
124
|
|
|
|
|
125
|
|
|
$this->connection->init(); |
|
126
|
|
|
$this->connection->resetConnectionResource(); |
|
127
|
|
|
// with the second init() we test whether caching works |
|
128
|
|
|
$this->connection->init(); |
|
129
|
|
|
} |
|
130
|
|
|
|
|
131
|
|
|
} |
Since your code implements the magic setter
_set, this function will be called for any write access on an undefined variable. You can add the@propertyannotation to your class or interface to document the existence of this variable.Since the property has write access only, you can use the @property-write annotation instead.
Of course, you may also just have mistyped another name, in which case you should fix the error.
See also the PhpDoc documentation for @property.