1<?php
2
3require_once __DIR__ . '/TestBase.php';
4
5/**
6 * Copyright 2010-2017 Horde LLC (http://www.horde.org/)
7 *
8 * @package    Ldap
9 * @subpackage UnitTests
10 * @author     Jan Schneider <jan@horde.org>
11 * @license    http://www.gnu.org/licenses/lgpl-3.0.html LGPL-3.0
12 */
13class Horde_Ldap_SearchTest extends Horde_Ldap_TestBase
14{
15    public static function tearDownAfterClass()
16    {
17        if (!self::$ldapcfg) {
18            return;
19        }
20        try {
21            $ldap = new Horde_Ldap(self::$ldapcfg['server']);
22            try {
23                $ldap->delete('ou=Horde_Ldap_Test_search1,' . self::$ldapcfg['server']['basedn']);
24            } catch (Exception $e) {
25            }
26            try {
27                $ldap->delete('ou=Horde_Ldap_Test_search2,' . self::$ldapcfg['server']['basedn']);
28            } catch (Exception $e) {
29            }
30        } catch (Exception $e) {
31        }
32    }
33
34    /**
35     * Tests SPL iterator.
36     */
37    public function testSPLIterator()
38    {
39        $ldap = new Horde_Ldap(self::$ldapcfg['server']);
40
41        // Some testdata, so we have some entries to search for.
42        $base = self::$ldapcfg['server']['basedn'];
43        $ou1 = Horde_Ldap_Entry::createFresh(
44            'ou=Horde_Ldap_Test_search1,' . $base,
45            array(
46                'objectClass' => array('top', 'organizationalUnit'),
47                'ou' => 'Horde_Ldap_Test_search1'));
48        $ou2 = Horde_Ldap_Entry::createFresh(
49            'ou=Horde_Ldap_Test_search2,' . $base,
50            array(
51                'objectClass' => array('top', 'organizationalUnit'),
52                'ou' => 'Horde_Ldap_Test_search2'));
53
54        $ldap->add($ou1);
55        $this->assertTrue($ldap->exists($ou1->dn()));
56        $ldap->add($ou2);
57        $this->assertTrue($ldap->exists($ou2->dn()));
58
59        /* Search and test each method. */
60        $search = $ldap->search(null, '(ou=Horde_Ldap*)');
61        $this->assertInstanceOf('Horde_Ldap_Search', $search);
62        $this->assertEquals(2, $search->count());
63
64        // current() is supposed to return first valid element.
65        $e1 = $search->current();
66        $this->assertInstanceOf('Horde_Ldap_Entry', $e1);
67        $this->assertEquals($e1->dn(), $search->key());
68        $this->assertTrue($search->valid());
69
70        // Shift to next entry.
71        $search->next();
72        $e2 = $search->current();
73        $this->assertInstanceOf('Horde_Ldap_Entry', $e2);
74        $this->assertEquals($e2->dn(), $search->key());
75        $this->assertTrue($search->valid());
76
77        // Shift to non existent third entry.
78        $search->next();
79        $this->assertFalse($search->current());
80        $this->assertFalse($search->key());
81        $this->assertFalse($search->valid());
82
83        // Rewind and test, which should return the first entry a second time.
84        $search->rewind();
85        $e1_1 = $search->current();
86        $this->assertInstanceOf('Horde_Ldap_Entry', $e1_1);
87        $this->assertEquals($e1_1->dn(), $search->key());
88        $this->assertTrue($search->valid());
89        $this->assertEquals($e1->dn(), $e1_1->dn());
90
91        // Don't rewind but call current, should return first entry again.
92        $e1_2 = $search->current();
93        $this->assertInstanceOf('Horde_Ldap_Entry', $e1_2);
94        $this->assertEquals($e1_2->dn(), $search->key());
95        $this->assertTrue($search->valid());
96        $this->assertEquals($e1->dn(), $e1_2->dn());
97
98        // Rewind again and test, which should return the first entry a third
99        // time.
100        $search->rewind();
101        $e1_3 = $search->current();
102        $this->assertInstanceOf('Horde_Ldap_Entry', $e1_3);
103        $this->assertEquals($e1_3->dn(), $search->key());
104        $this->assertTrue($search->valid());
105        $this->assertEquals($e1->dn(), $e1_3->dn());
106
107        /* Try methods on empty search result. */
108        $search = $ldap->search(null, '(ou=Horde_LdapTest_NotExistentEntry)');
109        $this->assertInstanceOf('Horde_Ldap_Search', $search);
110        $this->assertEquals(0, $search->count());
111        $this->assertFalse($search->current());
112        $this->assertFalse($search->key());
113        $this->assertFalse($search->valid());
114        $search->next();
115        $this->assertFalse($search->current());
116        $this->assertFalse($search->key());
117        $this->assertFalse($search->valid());
118
119        /* Search and simple iterate through the test entries.  Then, rewind
120         * and do it again several times. */
121        $search2 = $ldap->search(null, '(ou=Horde_Ldap*)');
122        $this->assertInstanceOf('Horde_Ldap_Search', $search2);
123        $this->assertEquals(2, $search2->count());
124        for ($i = 0; $i <= 5; $i++) {
125            $counter = 0;
126            foreach ($search2 as $dn => $entry) {
127                $counter++;
128                // Check on type.
129                $this->assertInstanceOf('Horde_Ldap_Entry', $entry);
130                // Check on key.
131                $this->assertThat(strlen($dn), $this->greaterThan(1));
132                $this->assertEquals($dn, $entry->dn());
133            }
134            $this->assertEquals($search2->count(), $counter, "Failed at loop $i");
135
136            // Revert to start.
137            $search2->rewind();
138        }
139    }
140}
141