weekly release 3.0.3+
[moodle.git] / lib / tests / ldaplib_test.php
blob08643331b028237df3fb97659fa032ab2bb36795
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.
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/>.
17 /**
18 * ldap tests.
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
26 defined('MOODLE_INTERNAL') || die();
28 global $CFG;
29 require_once($CFG->libdir . '/ldaplib.php');
31 class core_ldaplib_testcase extends advanced_testcase {
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.
37 $tests = array(
38 array (
39 'test' => 'Simplest',
40 'expected' => 'Simplest',
42 array (
43 'test' => 'Simple case',
44 'expected' => 'Simple\\20case',
46 array (
47 'test' => 'Medium ‒ case',
48 'expected' => 'Medium\\20‒\\20case',
50 array (
51 'test' => '#Harder+case#',
52 'expected' => '\\23Harder\\2bcase\\23',
54 array (
55 'test' => ' Harder (and); harder case ',
56 'expected' => '\\20Harder\\20(and)\\3b\\20harder\\20case\\20',
58 array (
59 'test' => 'Really \\0 (hard) case!\\',
60 'expected' => 'Really\\20\\5c0\\20(hard)\\20case!\\5c',
62 array (
63 'test' => 'James "Jim" = Smith, III',
64 'expected' => 'James\\20\\22Jim\22\\20\\3d\\20Smith\\2c\\20III',
66 array (
67 'test' => ' <jsmith@example.com> ',
68 'expected' => '\\20\\20\\3cjsmith@example.com\\3e\\20',
73 foreach ($tests as $test) {
74 $this->assertSame($test['expected'], ldap_addslashes($test['test']));
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.
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.
90 $tests = array(
91 array (
92 'test' => 'Simplest',
93 'expected' => 'Simplest',
95 array (
96 'test' => 'Simple\\20case',
97 'expected' => 'Simple case',
99 array (
100 'test' => 'Simple\\ case',
101 'expected' => 'Simple case',
103 array (
104 'test' => 'Simple\\ \\63\\61\\73\\65',
105 'expected' => 'Simple case',
107 array (
108 'test' => 'Medium\\ \\ case',
109 'expected' => 'Medium ‒ case',
111 array (
112 'test' => 'Medium\\20‒\\20case',
113 'expected' => 'Medium ‒ case',
115 array (
116 'test' => 'Medium\\20\\E2\\80\\92\\20case',
117 'expected' => 'Medium ‒ case',
119 array (
120 'test' => '\\23Harder\\2bcase\\23',
121 'expected' => '#Harder+case#',
123 array (
124 'test' => '\\#Harder\\+case\\#',
125 'expected' => '#Harder+case#',
127 array (
128 'test' => '\\20Harder\\20(and)\\3b\\20harder\\20case\\20',
129 'expected' => ' Harder (and); harder case ',
131 array (
132 'test' => '\\ Harder\\ (and)\\;\\ harder\\ case\\ ',
133 'expected' => ' Harder (and); harder case ',
135 array (
136 'test' => 'Really\\20\\5c0\\20(hard)\\20case!\\5c',
137 'expected' => 'Really \\0 (hard) case!\\',
139 array (
140 'test' => 'Really\\ \\\\0\\ (hard)\\ case!\\\\',
141 'expected' => 'Really \\0 (hard) case!\\',
143 array (
144 'test' => 'James\\20\\22Jim\\22\\20\\3d\\20Smith\\2c\\20III',
145 'expected' => 'James "Jim" = Smith, III',
147 array (
148 'test' => 'James\\ \\"Jim\\" \\= Smith\\, III',
149 'expected' => 'James "Jim" = Smith, III',
151 array (
152 'test' => '\\20\\20\\3cjsmith@example.com\\3e\\20',
153 'expected' => ' <jsmith@example.com> ',
155 array (
156 'test' => '\\ \\<jsmith@example.com\\>\\ ',
157 'expected' => ' <jsmith@example.com> ',
159 array (
160 'test' => 'Lu\\C4\\8Di\\C4\\87',
161 'expected' => 'Lučić',
165 foreach ($tests as $test) {
166 $this->assertSame($test['expected'], ldap_stripslashes($test['test']));