/lib/tests/ldaplib_test.php

https://github.com/vadimonus/moodle · PHP · 169 lines · 124 code · 12 blank · 33 comment · 1 complexity · e86437de513b04d5f9f06affedf4ffa6 MD5 · raw file

  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. * ldap tests.
  18. *
  19. * @package core
  20. * @category phpunit
  21. * @copyright Damyon Wiese, Iñaki Arenaza 2014
  22. * @license http://www.gnu.org/copyleft/gpl.html GNU Public License
  23. */
  24. defined('MOODLE_INTERNAL') || die();
  25. global $CFG;
  26. require_once($CFG->libdir . '/ldaplib.php');
  27. class core_ldaplib_testcase extends advanced_testcase {
  28. public function test_ldap_addslashes() {
  29. // See http://tools.ietf.org/html/rfc4514#section-5.2 if you want
  30. // to add additional tests.
  31. $tests = array(
  32. array (
  33. 'test' => 'Simplest',
  34. 'expected' => 'Simplest',
  35. ),
  36. array (
  37. 'test' => 'Simple case',
  38. 'expected' => 'Simple\\20case',
  39. ),
  40. array (
  41. 'test' => 'Medium ‒ case',
  42. 'expected' => 'Medium\\20‒\\20case',
  43. ),
  44. array (
  45. 'test' => '#Harder+case#',
  46. 'expected' => '\\23Harder\\2bcase\\23',
  47. ),
  48. array (
  49. 'test' => ' Harder (and); harder case ',
  50. 'expected' => '\\20Harder\\20(and)\\3b\\20harder\\20case\\20',
  51. ),
  52. array (
  53. 'test' => 'Really \\0 (hard) case!\\',
  54. 'expected' => 'Really\\20\\5c0\\20(hard)\\20case!\\5c',
  55. ),
  56. array (
  57. 'test' => 'James "Jim" = Smith, III',
  58. 'expected' => 'James\\20\\22Jim\22\\20\\3d\\20Smith\\2c\\20III',
  59. ),
  60. array (
  61. 'test' => ' <jsmith@example.com> ',
  62. 'expected' => '\\20\\20\\3cjsmith@example.com\\3e\\20',
  63. ),
  64. );
  65. foreach ($tests as $test) {
  66. $this->assertSame($test['expected'], ldap_addslashes($test['test']));
  67. }
  68. }
  69. public function test_ldap_stripslashes() {
  70. // See http://tools.ietf.org/html/rfc4514#section-5.2 if you want
  71. // to add additional tests.
  72. // IMPORTANT NOTICE: While ldap_addslashes() only produces one
  73. // of the two defined ways of escaping/quoting (the ESC HEX
  74. // HEX way defined in the grammar in Section 3 of RFC-4514)
  75. // ldap_stripslashes() has to deal with both of them. So in
  76. // addition to testing the same strings we test in
  77. // test_ldap_stripslashes(), we need to also test strings
  78. // using the second method.
  79. $tests = array(
  80. array (
  81. 'test' => 'Simplest',
  82. 'expected' => 'Simplest',
  83. ),
  84. array (
  85. 'test' => 'Simple\\20case',
  86. 'expected' => 'Simple case',
  87. ),
  88. array (
  89. 'test' => 'Simple\\ case',
  90. 'expected' => 'Simple case',
  91. ),
  92. array (
  93. 'test' => 'Simple\\ \\63\\61\\73\\65',
  94. 'expected' => 'Simple case',
  95. ),
  96. array (
  97. 'test' => 'Medium\\ ‒\\ case',
  98. 'expected' => 'Medium ‒ case',
  99. ),
  100. array (
  101. 'test' => 'Medium\\20‒\\20case',
  102. 'expected' => 'Medium ‒ case',
  103. ),
  104. array (
  105. 'test' => 'Medium\\20\\E2\\80\\92\\20case',
  106. 'expected' => 'Medium ‒ case',
  107. ),
  108. array (
  109. 'test' => '\\23Harder\\2bcase\\23',
  110. 'expected' => '#Harder+case#',
  111. ),
  112. array (
  113. 'test' => '\\#Harder\\+case\\#',
  114. 'expected' => '#Harder+case#',
  115. ),
  116. array (
  117. 'test' => '\\20Harder\\20(and)\\3b\\20harder\\20case\\20',
  118. 'expected' => ' Harder (and); harder case ',
  119. ),
  120. array (
  121. 'test' => '\\ Harder\\ (and)\\;\\ harder\\ case\\ ',
  122. 'expected' => ' Harder (and); harder case ',
  123. ),
  124. array (
  125. 'test' => 'Really\\20\\5c0\\20(hard)\\20case!\\5c',
  126. 'expected' => 'Really \\0 (hard) case!\\',
  127. ),
  128. array (
  129. 'test' => 'Really\\ \\\\0\\ (hard)\\ case!\\\\',
  130. 'expected' => 'Really \\0 (hard) case!\\',
  131. ),
  132. array (
  133. 'test' => 'James\\20\\22Jim\\22\\20\\3d\\20Smith\\2c\\20III',
  134. 'expected' => 'James "Jim" = Smith, III',
  135. ),
  136. array (
  137. 'test' => 'James\\ \\"Jim\\" \\= Smith\\, III',
  138. 'expected' => 'James "Jim" = Smith, III',
  139. ),
  140. array (
  141. 'test' => '\\20\\20\\3cjsmith@example.com\\3e\\20',
  142. 'expected' => ' <jsmith@example.com> ',
  143. ),
  144. array (
  145. 'test' => '\\ \\<jsmith@example.com\\>\\ ',
  146. 'expected' => ' <jsmith@example.com> ',
  147. ),
  148. array (
  149. 'test' => 'Lu\\C4\\8Di\\C4\\87',
  150. 'expected' => 'Lučić',
  151. ),
  152. );
  153. foreach ($tests as $test) {
  154. $this->assertSame($test['expected'], ldap_stripslashes($test['test']));
  155. }
  156. }
  157. }