* * For the full copyright and license information, please view the LICENSE * file that was distributed with this source code. */ namespace Symfony\Component\Ldap\Tests; use Symfony\Component\Ldap\Adapter\ExtLdap\Adapter; use Symfony\Component\Ldap\Adapter\ExtLdap\Collection; use Symfony\Component\Ldap\Adapter\ExtLdap\Query; use Symfony\Component\Ldap\Entry; use Symfony\Component\Ldap\Exception\NotBoundException; use Symfony\Component\Ldap\LdapInterface; /** * @requires extension ldap */ class AdapterTest extends LdapTestCase { public function testLdapEscape() { $ldap = new Adapter(); $this->assertEquals('\20foo\3dbar\0d(baz)*\20', $ldap->escape(" foo=bar\r(baz)* ", null, LdapInterface::ESCAPE_DN)); } /** * @group functional */ public function testLdapQuery() { $ldap = new Adapter($this->getLdapConfig()); $ldap->getConnection()->bind('cn=admin,dc=symfony,dc=com', 'symfony'); $query = $ldap->createQuery('dc=symfony,dc=com', '(&(objectclass=person)(ou=Maintainers))', array()); $result = $query->execute(); $this->assertInstanceOf(Collection::class, $result); $this->assertCount(1, $result); $entry = $result[0]; $this->assertInstanceOf(Entry::class, $entry); $this->assertEquals(array('Fabien Potencier'), $entry->getAttribute('cn')); $this->assertEquals(array('fabpot@symfony.com', 'fabien@potencier.com'), $entry->getAttribute('mail')); } /** * @group functional */ public function testLdapQueryIterator() { $ldap = new Adapter($this->getLdapConfig()); $ldap->getConnection()->bind('cn=admin,dc=symfony,dc=com', 'symfony'); $query = $ldap->createQuery('dc=symfony,dc=com', '(&(objectclass=person)(ou=Maintainers))', array()); $result = $query->execute(); $iterator = $result->getIterator(); $iterator->rewind(); $entry = $iterator->current(); $this->assertInstanceOf(Entry::class, $entry); $this->assertEquals(array('Fabien Potencier'), $entry->getAttribute('cn')); $this->assertEquals(array('fabpot@symfony.com', 'fabien@potencier.com'), $entry->getAttribute('mail')); } /** * @group functional */ public function testLdapQueryWithoutBind() { $ldap = new Adapter($this->getLdapConfig()); $this->{method_exists($this, $_ = 'expectException') ? $_ : 'setExpectedException'}(NotBoundException::class); $query = $ldap->createQuery('dc=symfony,dc=com', '(&(objectclass=person)(ou=Maintainers))', array()); $query->execute(); } public function testLdapQueryScopeBase() { $ldap = new Adapter($this->getLdapConfig()); $ldap->getConnection()->bind('cn=admin,dc=symfony,dc=com', 'symfony'); $query = $ldap->createQuery('cn=Fabien Potencier,dc=symfony,dc=com', '(objectclass=*)', array( 'scope' => Query::SCOPE_BASE, )); $result = $query->execute(); $entry = $result[0]; $this->assertEquals($result->count(), 1); $this->assertEquals(array('Fabien Potencier'), $entry->getAttribute('cn')); } public function testLdapQueryScopeOneLevel() { $ldap = new Adapter($this->getLdapConfig()); $ldap->getConnection()->bind('cn=admin,dc=symfony,dc=com', 'symfony'); $one_level_result = $ldap->createQuery('ou=Components,dc=symfony,dc=com', '(objectclass=*)', array( 'scope' => Query::SCOPE_ONE, ))->execute(); $subtree_count = $ldap->createQuery('ou=Components,dc=symfony,dc=com', '(objectclass=*)')->execute()->count(); $this->assertNotEquals($one_level_result->count(), $subtree_count); $this->assertEquals($one_level_result->count(), 1); $this->assertEquals($one_level_result[0]->getAttribute('ou'), array('Ldap')); } }