MDL-51723 ldap: Normalise the user objectclass
[moodle.git] / lib / tests / ldaplib_test.php
CommitLineData
cc220325
IA
1<?php
2// This file is part of Moodle - http://moodle.org/
3//
4// Moodle is free software: you can redistribute it and/or modify
5// it under the terms of the GNU General Public License as published by
6// the Free Software Foundation, either version 3 of the License, or
7// (at your option) any later version.
8//
9// Moodle is distributed in the hope that it will be useful,
10// but WITHOUT ANY WARRANTY; without even the implied warranty of
11// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12// GNU General Public License for more details.
13//
14// You should have received a copy of the GNU General Public License
15// along with Moodle. If not, see <http://www.gnu.org/licenses/>.
16
17/**
18 * ldap tests.
19 *
20 * @package core
21 * @category phpunit
22 * @copyright Damyon Wiese, Iñaki Arenaza 2014
23 * @license http://www.gnu.org/copyleft/gpl.html GNU Public License
24 */
25
26defined('MOODLE_INTERNAL') || die();
27
28global $CFG;
29require_once($CFG->libdir . '/ldaplib.php');
30
31class core_ldaplib_testcase extends advanced_testcase {
32
33 public function test_ldap_addslashes() {
34 // See http://tools.ietf.org/html/rfc4514#section-5.2 if you want
35 // to add additional tests.
36
37 $tests = array(
38 array (
39 'test' => 'Simplest',
40 'expected' => 'Simplest',
41 ),
42 array (
43 'test' => 'Simple case',
44 'expected' => 'Simple\\20case',
45 ),
46 array (
47 'test' => 'Medium ‒ case',
48 'expected' => 'Medium\\20‒\\20case',
49 ),
50 array (
51 'test' => '#Harder+case#',
52 'expected' => '\\23Harder\\2bcase\\23',
53 ),
54 array (
55 'test' => ' Harder (and); harder case ',
56 'expected' => '\\20Harder\\20(and)\\3b\\20harder\\20case\\20',
57 ),
58 array (
59 'test' => 'Really \\0 (hard) case!\\',
60 'expected' => 'Really\\20\\5c0\\20(hard)\\20case!\\5c',
61 ),
62 array (
63 'test' => 'James "Jim" = Smith, III',
64 'expected' => 'James\\20\\22Jim\22\\20\\3d\\20Smith\\2c\\20III',
65 ),
66 array (
0fe86bbd
RT
67 'test' => ' <jsmith@example.com> ',
68 'expected' => '\\20\\20\\3cjsmith@example.com\\3e\\20',
cc220325
IA
69 ),
70 );
71
72
73 foreach ($tests as $test) {
74 $this->assertSame($test['expected'], ldap_addslashes($test['test']));
75 }
76 }
77
78 public function test_ldap_stripslashes() {
79 // See http://tools.ietf.org/html/rfc4514#section-5.2 if you want
80 // to add additional tests.
81
82 // IMPORTANT NOTICE: While ldap_addslashes() only produces one
83 // of the two defined ways of escaping/quoting (the ESC HEX
84 // HEX way defined in the grammar in Section 3 of RFC-4514)
85 // ldap_stripslashes() has to deal with both of them. So in
86 // addition to testing the same strings we test in
87 // test_ldap_stripslashes(), we need to also test strings
88 // using the second method.
89
90 $tests = array(
91 array (
92 'test' => 'Simplest',
93 'expected' => 'Simplest',
94 ),
95 array (
96 'test' => 'Simple\\20case',
97 'expected' => 'Simple case',
98 ),
99 array (
100 'test' => 'Simple\\ case',
101 'expected' => 'Simple case',
102 ),
103 array (
104 'test' => 'Simple\\ \\63\\61\\73\\65',
105 'expected' => 'Simple case',
106 ),
107 array (
108 'test' => 'Medium\\ ‒\\ case',
109 'expected' => 'Medium ‒ case',
110 ),
111 array (
112 'test' => 'Medium\\20‒\\20case',
113 'expected' => 'Medium ‒ case',
114 ),
115 array (
116 'test' => 'Medium\\20\\E2\\80\\92\\20case',
117 'expected' => 'Medium ‒ case',
118 ),
119 array (
120 'test' => '\\23Harder\\2bcase\\23',
121 'expected' => '#Harder+case#',
122 ),
123 array (
124 'test' => '\\#Harder\\+case\\#',
125 'expected' => '#Harder+case#',
126 ),
127 array (
128 'test' => '\\20Harder\\20(and)\\3b\\20harder\\20case\\20',
129 'expected' => ' Harder (and); harder case ',
130 ),
131 array (
132 'test' => '\\ Harder\\ (and)\\;\\ harder\\ case\\ ',
133 'expected' => ' Harder (and); harder case ',
134 ),
135 array (
136 'test' => 'Really\\20\\5c0\\20(hard)\\20case!\\5c',
137 'expected' => 'Really \\0 (hard) case!\\',
138 ),
139 array (
140 'test' => 'Really\\ \\\\0\\ (hard)\\ case!\\\\',
141 'expected' => 'Really \\0 (hard) case!\\',
142 ),
143 array (
144 'test' => 'James\\20\\22Jim\\22\\20\\3d\\20Smith\\2c\\20III',
145 'expected' => 'James "Jim" = Smith, III',
146 ),
147 array (
148 'test' => 'James\\ \\"Jim\\" \\= Smith\\, III',
149 'expected' => 'James "Jim" = Smith, III',
150 ),
151 array (
0fe86bbd
RT
152 'test' => '\\20\\20\\3cjsmith@example.com\\3e\\20',
153 'expected' => ' <jsmith@example.com> ',
cc220325
IA
154 ),
155 array (
0fe86bbd
RT
156 'test' => '\\ \\<jsmith@example.com\\>\\ ',
157 'expected' => ' <jsmith@example.com> ',
cc220325
IA
158 ),
159 array (
160 'test' => 'Lu\\C4\\8Di\\C4\\87',
161 'expected' => 'Lučić',
162 ),
163 );
164
165 foreach ($tests as $test) {
166 $this->assertSame($test['expected'], ldap_stripslashes($test['test']));
167 }
168 }
abedeb8c
AN
169
170 /**
171 * Tests for ldap_normalise_objectclass.
172 *
173 * @dataProvider ldap_normalise_objectclass_provider
174 * @param array $args Arguments passed to ldap_normalise_objectclass
175 * @param string $expected The expected objectclass filter
176 */
177 public function test_ldap_normalise_objectclass($args, $expected) {
178 $this->assertEquals($expected, call_user_func_array('ldap_normalise_objectclass', $args));
179 }
180
181 /**
182 * Data provider for the test_ldap_normalise_objectclass testcase.
183 *
184 * @return array of testcases.
185 */
186 public function ldap_normalise_objectclass_provider() {
187 return array(
188 'Empty value' => array(
189 array(null),
190 '(objectClass=*)',
191 ),
192 'Empty value with different default' => array(
193 array(null, 'lion'),
194 '(objectClass=lion)',
195 ),
196 'Supplied unwrapped objectClass' => array(
197 array('objectClass=tiger'),
198 '(objectClass=tiger)',
199 ),
200 'Supplied string value' => array(
201 array('leopard'),
202 '(objectClass=leopard)',
203 ),
204 'Supplied complex' => array(
205 array('(&(objectClass=cheetah)(enabledMoodleUser=1))'),
206 '(&(objectClass=cheetah)(enabledMoodleUser=1))',
207 ),
208 );
209 }
cc220325 210}