8000 Added functional tests for Ldap component · symfony/symfony@33a6723 · GitHub
[go: up one dir, main page]

Skip to content

Commit 33a6723

Browse files
committed
Added functional tests for Ldap component
1 parent cd0e97c commit 33a6723

File tree

7 files changed

+161
-2
lines changed

7 files changed

+161
-2
lines changed

.travis.yml

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ addons:
99
apt_packages:
1010
- parallel
1111
- language-pack-fr-base
12+
- ldap-utils
1213

1314
env:
1415
global:
@@ -36,6 +37,7 @@ services: mongodb
3637

3738
before_install:
3839
- PHP=$TRAVIS_PHP_VERSION
40+
- slapd -f src/Symfony/Component/Ldap/Tests/Fixtures/conf/slapd.conf -h ldap://localhost:3389 &
3941
# Matrix lines for intermediate PHP versions are skipped for pull requests
4042
- if [[ ! $deps && ! $PHP = ${MIN_PHP%.*} && $PHP != hhvm && $TRAVIS_PULL_REQUEST != false ]]; then deps=skip; skip=1; fi
4143
# A sigchild-enabled-PHP is used to test the Process component on the lowest PHP matrix line
@@ -56,6 +58,8 @@ before_install:
5658
- if [[ ! $skip ]]; then ./phpunit install; fi
5759
- if [[ ! $skip && $deps ]]; then composer global remove hirak/prestissimo; fi
5860
- if [[ ! $skip ]]; then export PHPUNIT=$(readlink -f ./phpunit); fi
61+
- if [[ ! $skip ]]; then ldapadd -h localhost:3389 -D cn=admin,dc=symfony,dc=com -w symfony -f src/Symfony/Component/Ldap/Tests/Fixtures/data/base.ldif; fi
62+
- if [[ ! $skip ]]; then ldapadd -h localhost:3389 -D cn=admin,dc=symfony,dc=com -w symfony -f src/Symfony/Component/Ldap/Tests/Fixtures/data/fixtures.ldif; fi
5963

6064
install:
6165
- if [[ ! $skip ]]; then COMPONENTS=$(find src/Symfony -mindepth 3 -type f -name phpunit.xml.dist -printf '%h\n'); fi

src/Symfony/Component/Ldap/LdapClient.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
* @author Grégoire Pineau <lyrixx@lyrixx.info>
1919
* @author Francis Besset <francis.besset@gmail.com>
2020
* @author Charles Sarrazin <charles@sarraz.in>
21+
* @author Hidde Wieringa <hidde@hiddewieringa.nl>
2122
*
2223
* @internal
2324
*/
@@ -83,7 +84,7 @@ public function find($dn, $query, $filter = '*')
8384
$filter = array($filter);
8485
}
8586

86-
$search = ldap_search($this->connection, $dn, $query, $filter);
87+
$search = @ldap_search($this->connection, $dn, $query, $filter);
8788

8889
if (false === $search) {
8990
throw new LdapException(ldap_error($this->connection));
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
# See slapd.conf(5) for details on configuration options.
2+
include /etc/ldap/schema/core.schema
3+
include /etc/ldap/schema/cosine.schema
4+
include /etc/ldap/schema/inetorgperson.schema
5+
include /etc/ldap/schema/nis.schema
6+
7+
pidfile /tmp/slapd/slapd.pid
8+
argsfile /tmp/slapd/slapd.args
9+
10+
modulepath /usr/lib/openldap
11+
12+
database ldif
13+
directory /tmp/slapd
14+
15+
suffix "dc=symfony,dc=com"
16+
rootdn "cn=admin,dc=symfony,dc=com"
17+
rootpw {SSHA}btWUi971ytYpVMbZLkaQ2A6ETh3VA0lL
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
dn: dc=symfony,dc=com
2+
objectClass: dcObject
3+
objectClass: organizationalUnit
4+
ou: Organization
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
dn: cn=Fabien Potencier,dc=symfony,dc=com
2+
objectClass: inetOrgPerson
3+
objectClass: organizationalPerson
4+
objectClass: person
5+
objectClass: top
6+
cn: Fabien Potencier
7+
sn: fabpot
8+
mail: fabpot@symfony.com
9+
mail: fabien@potencier.com
10+
ou: People
11+
ou: Maintainers
12+
ou: Founder
13+
givenName: Fabien Potencier
14+
description: Founder and project lead @Symfony

src/Symfony/Component/Ldap/Tests/LdapClientTest.php

Lines changed: 104 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,17 +12,120 @@
1212
namespace Symfony\Component\Ldap\Tests;
1313

1414
use Symfony\Component\Ldap\LdapClient;
15+
use Symfony\Component\Ldap\Exception\ConnectionException;
16+
use Symfony\Component\Ldap\Exception\LdapException;
1517
use Symfony\Polyfill\Php56\Php56 as p;
1618

1719
/**
1820
* @requires extension ldap
1921
*/
20-
class LdapClientTest extends \PHPUnit_Framework_TestCase
22+
class LdapClientTest extends LdapTestCase
2123
{
24+
/**
25+
* Ldap client, only used for the functional tests
26+
* @var [type]
27+
*/
28+
private $ldap;
29+
30+
protected function setUp()
31+
{
32+
$this->ldap = new LdapClient($this->getLdapHost(), $this->getLdapPort());
33+
}
34+
2235
public function testLdapEscape()
2336
{
2437
$ldap = new LdapClient();
2538

2639
$this->assertEquals('\20foo\3dbar\0d(baz)*\20', $ldap->escape(" foo=bar\r(baz)* ", null, p::LDAP_ESCAPE_DN));
2740
}
41+
42+
/**
43+
* @group functional
44+
*/
45+
public function testLdapQuery()
46+
{
47+
$this->ldap->bind('cn=admin,dc=symfony,dc=com', 'symfony');
48+
$result = $this->ldap->find('dc=symfony,dc=com', '(&(objectclass=person)(ou=Maintainers))', array());
49+
50+
$expected = array(
51+
'count' => 1,
52+
0 => array(
53+
'objectclass' => array(
54+
'count' => 4,
55+
0 => 'inetOrgPerson',
56+
1 => 'organizationalPerson',
57+
2 => 'person',
58+
3 => 'top',
59+
),
60+
0 => 'objectclass',
61+
'cn' => array(
62+
'count' => 1,
63+
0 => 'Fabien Potencier',
64+
),
65+
1 => 'cn',
66+
'sn' => array(
67+
'count' => 1,
68+
0 => 'fabpot',
69+
),
70+
2 => 'sn',
71+
'mail' => array(
72+
'count' => 2,
73+
0 => 'fabpot@symfony.com',
74+
1 => 'fabien@potencier.com',
75+
),
76+
3 => 'mail',
77+
'ou' => array(
78+
'count' => 3,
79+
0 => 'People',
80+
1 => 'Maintainers',
81+
2 => 'Founder',
82+
),
83+
4 => 'ou',
84+
'givenname' => array(
85+
'count' => 1,
86+
0 => 'Fabien Potencier',
87+
),
88+
5 => 'givenname',
89+
'description' => array(
90+
'count' => 1,
91+
0 => 'Founder and project lead @Symfony',
92+
),
93+
6 => 'description',
94+
'count' => 7,
95+
'dn' => 'cn=Fabien Potencier,dc=symfony,dc=com',
96+
),
97+
);
98+
99+
$this->assertEquals($expected, $result);
100+
}
101+
102+
/**
103+
* @group functional
104+
* @expectedException Symfony\Component\Ldap\Exception\ConnectionException
105+
*/
106+
public function testLdapWrongPassword()
107+
{
108+
$this->ldap->bind('cn=admin,dc=symfony,dc=com', 'wrongpass');
109+
}
110+
111+
/**
112+
* @group functional
113+
*/
114+
public function testLdapNotFound()
115+
{
116+
$this->ldap->bind('cn=admin,dc=symfony,dc=com', 'symfony');
117+
$result = $this->ldap->find('dc=symfony,dc=com', 'uid=nonexistent');
118+
119+
$this->assertNull($result);
120+
}
121+
122+
/**
123+
* @group functional
124+
* @expectedException Symfony\Component\Ldap\Exception\LdapException
125+
*/
126+
public function testLdapInvalidDn()
127+
{
128+
$this->ldap->bind('cn=admin,dc=symfony,dc=com', 'symfony');
129+
$result = $this->ldap->find('dc=invalid,dc=com', 'uid=nonexistent');
130+
}
28131
}
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
<?php
2+
3+
namespace Symfony\Component\Ldap\Tests;
4+
5+
class LdapTestCase extends \PHPUnit_Framework_TestCase
6+
{
7+
protected function getLdapHost()
8+
{
9+
return getenv('LDAP_HOST');
10+
}
11+
12+
protected function getLdapPort()
13+
{
14+
return getenv('LDAP_PORT');
15+
}
16+
}

0 commit comments

Comments
 (0)
0