Completed
Push — stable8.2 ( 36c43b...aa408d )
by
unknown
14:32
created

Test_Connection::setUp()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 14
Code Lines 10

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 10
CRAP Score 1

Importance

Changes 1
Bugs 1 Features 0
Metric Value
c 1
b 1
f 0
dl 0
loc 14
ccs 10
cts 10
cp 1
rs 9.4285
cc 1
eloc 10
nc 1
nop 0
crap 1
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;
0 ignored issues
show
Documentation introduced by
The property ldapAgentName does not exist on object<OCA\user_ldap\lib\Connection>. Since you implemented __set, maybe consider adding a @property annotation.

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 @property annotation to your class or interface to document the existence of this variable.

<?php

/**
 * @property int $x
 * @property int $y
 * @property string $text
 */
class MyLabel
{
    private $properties;

    private $allowedProperties = array('x', 'y', 'text');

    public function __get($name)
    {
        if (isset($properties[$name]) && in_array($name, $this->allowedProperties)) {
            return $properties[$name];
        } else {
            return null;
        }
    }

    public function __set($name, $value)
    {
        if (in_array($name, $this->allowedProperties)) {
            $properties[$name] = $value;
        } else {
            throw new \LogicException("Property $name is not defined.");
        }
    }

}

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.

Loading history...
70
		$agentPawd = $connection->ldapAgentPassword;
0 ignored issues
show
Documentation introduced by
The property ldapAgentPassword does not exist on object<OCA\user_ldap\lib\Connection>. Since you implemented __set, maybe consider adding a @property annotation.

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 @property annotation to your class or interface to document the existence of this variable.

<?php

/**
 * @property int $x
 * @property int $y
 * @property string $text
 */
class MyLabel
{
    private $properties;

    private $allowedProperties = array('x', 'y', 'text');

    public function __get($name)
    {
        if (isset($properties[$name]) && in_array($name, $this->allowedProperties)) {
            return $properties[$name];
        } else {
            return null;
        }
    }

    public function __set($name, $value)
    {
        if (in_array($name, $this->allowedProperties)) {
            $properties[$name] = $value;
        } else {
            throw new \LogicException("Property $name is not defined.");
        }
    }

}

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.

Loading history...
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
}