ConverterTest.php 9.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256
  1. <?php
  2. /**
  3. * Zend Framework
  4. *
  5. * LICENSE
  6. *
  7. * This source file is subject to the new BSD license that is bundled
  8. * with this package in the file LICENSE.txt.
  9. * It is also available through the world-wide-web at this URL:
  10. * http://framework.zend.com/license/new-bsd
  11. * If you did not receive a copy of the license and are unable to
  12. * obtain it through the world-wide-web, please send an email
  13. * to license@zend.com so we can send you a copy immediately.
  14. *
  15. * @category Zend
  16. * @package Zend_Ldap
  17. * @subpackage UnitTests
  18. * @copyright Copyright (c) 2005-2014 Zend Technologies USA Inc. (http://www.zend.com)
  19. * @license http://framework.zend.com/license/new-bsd New BSD License
  20. * @version $Id$
  21. */
  22. /**
  23. * Zend_Ldap_Converter
  24. */
  25. require_once 'Zend/Ldap/Converter.php';
  26. /**
  27. * @category Zend
  28. * @package Zend_Ldap
  29. * @subpackage UnitTests
  30. * @copyright Copyright (c) 2005-2014 Zend Technologies USA Inc. (http://www.zend.com)
  31. * @license http://framework.zend.com/license/new-bsd New BSD License
  32. * @group Zend_Ldap
  33. */
  34. class Zend_Ldap_ConverterTest extends PHPUnit_Framework_TestCase
  35. {
  36. public function testAsc2hex32()
  37. {
  38. $expected='\00\01\02\03\04\05\06\07\08\09\0a\0b\0c\0d\0e\0f\10\11\12\13\14\15\16\17\18\19' .
  39. '\1a\1b\1c\1d\1e\1f !"#$%&\'()*+,-./0123456789:;<=>?@ABCDEFGHIJKLMNOPQRSTUVWXYZ[\]^_`' .
  40. 'abcdefghijklmnopqrstuvwxyz{|}~';
  41. $str='';
  42. for ($i=0; $i<127; $i++) {
  43. $str.=chr($i);
  44. }
  45. $this->assertEquals($expected, Zend_Ldap_Converter::ascToHex32($str));
  46. }
  47. public function testHex2asc()
  48. {
  49. $expected='';
  50. for ($i=0; $i<127; $i++) {
  51. $expected.=chr($i);
  52. }
  53. $str='\00\01\02\03\04\05\06\07\08\09\0a\0b\0c\0d\0e\0f\10\11\12\13\14\15\16\17\18\19\1a\1b' .
  54. '\1c\1d\1e\1f !"#$%&\'()*+,-./0123456789:;<=>?@ABCDEFGHIJKLMNOPQRSTUVWXYZ[\]^_`abcdefg' .
  55. 'hijklmnopqrstuvwxyz{|}~';
  56. $this->assertEquals($expected, Zend_Ldap_Converter::hex32ToAsc($str));
  57. }
  58. /**
  59. * @dataProvider toLdapDateTimeProvider
  60. */
  61. public function testToLdapDateTime($convert,$expect)
  62. {
  63. $result = Zend_Ldap_Converter::toLdapDatetime($convert['date'], $convert['utc']);
  64. $this->assertEquals( $expect,$result);
  65. }
  66. public function toLdapDateTimeProvider(){
  67. include_once 'Zend/Date.php';
  68. $tz = new DateTimeZone('UTC');
  69. return array(
  70. array(array('date'=> 0, 'utc' => true ),'19700101000000Z'),
  71. array(array('date'=> new DateTime('2010-05-12 13:14:45+0300', $tz), 'utc' => false ),'20100512131445+0300'),
  72. array(array('date'=> new DateTime('2010-05-12 13:14:45+0300', $tz), 'utc' => true ),'20100512101445Z'),
  73. array(array('date'=> '2010-05-12 13:14:45+0300', 'utc' => false ),'20100512131445+0300'),
  74. array(array('date'=> '2010-05-12 13:14:45+0300', 'utc' => true ),'20100512101445Z'),
  75. array(array('date'=> new Zend_Date('2010-05-12T13:14:45+0300', Zend_Date::ISO_8601), 'utc' => true ),'20100512101445Z'),
  76. array(array('date'=> new Zend_Date('2010-05-12T13:14:45+0300', Zend_Date::ISO_8601), 'utc' => false ),'20100512131445+0300'),
  77. array(array('date'=> new Zend_Date('0', Zend_Date::TIMESTAMP), 'utc' => true ),'19700101000000Z'),
  78. );
  79. }
  80. /**
  81. * @dataProvider toLdapBooleanProvider
  82. */
  83. public function testToLdapBoolean($expect, $convert)
  84. {
  85. $this->assertEquals($expect, Zend_Ldap_Converter::toldapBoolean($convert));
  86. }
  87. public function toLdapBooleanProvider(){
  88. return array (
  89. array('TRUE',true),
  90. array('TRUE',1),
  91. array('TRUE','true'),
  92. array('FALSE','false'),
  93. array('FALSE', false),
  94. array('FALSE', array('true')),
  95. array('FALSE', array('false')),
  96. );
  97. }
  98. /**
  99. * @dataProvider toLdapSerializeProvider
  100. */
  101. public function testToLdapSerialize($expect, $convert){
  102. $this->assertEquals($expect, Zend_Ldap_Converter::toLdapSerialize($convert));
  103. }
  104. public function toLdapSerializeProvider(){
  105. if (version_compare(PHP_VERSION, '5.3.0', '<')) {
  106. return array(
  107. array('N;', null),
  108. array('i:1;', 1),
  109. array('O:8:"DateTime":0:{}', new DateTime('@0')),
  110. array('a:3:{i:0;s:4:"test";i:1;i:1;s:3:"foo";s:3:"bar";}', array('test',1,'foo'=>'bar')),
  111. );
  112. } elseif (version_compare(PHP_VERSION, '5.6.0beta4', '>=')) {
  113. return array(
  114. array('N;', null),
  115. array('i:1;', 1),
  116. array('O:8:"DateTime":3:{s:4:"date";s:26:"1970-01-01 00:00:00.000000";s:13:"timezone_type";i:1;s:8:"timezone";s:6:"+00:00";}', new DateTime('@0')),
  117. array('a:3:{i:0;s:4:"test";i:1;i:1;s:3:"foo";s:3:"bar";}', array('test',1,'foo'=>'bar')),
  118. );
  119. } else {
  120. return array(
  121. array('N;', null),
  122. array('i:1;', 1),
  123. array('O:8:"DateTime":3:{s:4:"date";s:19:"1970-01-01 00:00:00";s:13:"timezone_type";i:1;s:8:"timezone";s:6:"+00:00";}', new DateTime('@0')),
  124. array('a:3:{i:0;s:4:"test";i:1;i:1;s:3:"foo";s:3:"bar";}', array('test',1,'foo'=>'bar')),
  125. );
  126. }
  127. }
  128. /**
  129. * @dataProvider toLdapProvider
  130. */
  131. public function testToLdap($expect, $convert){
  132. $this->assertEquals($expect, Zend_Ldap_Converter::toLdap($convert['value'], $convert['type']));
  133. }
  134. public function toLdapProvider(){
  135. return array(
  136. array(null, array('value' => null,'type' => 0)),
  137. array('19700101000000Z',array ('value'=> 0, 'type' => 2)),
  138. array('0',array ('value'=> 0, 'type' => 0)),
  139. array('FALSE',array ('value'=> 0, 'type' => 1)),
  140. array('19700101000000Z',array('value'=>new Zend_Date('1970-01-01T00:00:00+0000',Zend_Date::ISO_8601),'type'=>0)),
  141. );
  142. }
  143. /**
  144. * @dataProvider fromLdapUnserializeProvider
  145. */
  146. public function testFromLdapUnserialize ($expect, $convert)
  147. {
  148. $this->assertEquals($expect, Zend_Ldap_Converter::fromLdapUnserialize($convert));
  149. }
  150. public function testFromLdapUnserializeThrowsException ()
  151. {
  152. $this->setExpectedException('UnexpectedValueException');
  153. Zend_Ldap_Converter::fromLdapUnserialize('--');
  154. }
  155. public function fromLdapUnserializeProvider(){
  156. return array (
  157. array(null,'N;'),
  158. array(1,'i:1;'),
  159. array(false,'b:0;'),
  160. );
  161. }
  162. public function testFromLdapBoolean()
  163. {
  164. $this->assertTrue(Zend_Ldap_Converter::fromLdapBoolean('TRUE'));
  165. $this->assertFalse(Zend_Ldap_Converter::fromLdapBoolean('FALSE'));
  166. $this->setExpectedException('InvalidArgumentException');
  167. Zend_Ldap_Converter::fromLdapBoolean('test');
  168. }
  169. /**
  170. * @dataProvider fromLdapDateTimeProvider
  171. */
  172. public function testFromLdapDateTime($expected, $convert, $utc)
  173. {
  174. if ( true === $utc ) {
  175. $expected -> setTimezone(new DateTimeZone('UTC'));
  176. }
  177. $this->assertEquals($expected, Zend_Ldap_Converter::fromLdapDatetime($convert,$utc));
  178. }
  179. public function fromLdapDateTimeProvider ()
  180. {
  181. return array (
  182. array(new DateTime('2010-12-24 08:00:23+0300'),'20101224080023+0300', false),
  183. array(new DateTime('2010-12-24 08:00:23+0300'),'20101224080023+03\'00\'', false),
  184. array(new DateTime('2010-12-24 08:00:23+0000'),'20101224080023', false),
  185. array(new DateTime('2010-12-24 08:00:00+0000'),'201012240800', false),
  186. array(new DateTime('2010-12-24 08:00:00+0000'),'2010122408', false),
  187. array(new DateTime('2010-12-24 00:00:00+0000'),'20101224', false),
  188. array(new DateTime('2010-12-01 00:00:00+0000'),'201012', false),
  189. array(new DateTime('2010-01-01 00:00:00+0000'),'2010', false),
  190. array(new DateTime('2010-04-03 12:23:34+0000'), '20100403122334', true),
  191. );
  192. }
  193. /**
  194. * @expectedException InvalidArgumentException
  195. * @dataProvider fromLdapDateTimeException
  196. */
  197. public function testFromLdapDateTimeThrowsException ($value)
  198. {
  199. Zend_Ldap_Converter::fromLdapDatetime($value);
  200. }
  201. public static function fromLdapDateTimeException ()
  202. {
  203. return array (
  204. array('foobar'),
  205. array('201'),
  206. array('201013'),
  207. array('20101232'),
  208. array('2010123124'),
  209. array('201012312360'),
  210. array('20101231235960'),
  211. array('20101231235959+13'),
  212. array('20101231235959+1160'),
  213. );
  214. }
  215. /**
  216. * @dataProvider fromLdapProvider
  217. */
  218. public function testFromLdap($expect, $value, $type, $dateTimeAsUtc){
  219. $this->assertSame($expect, Zend_Ldap_Converter::fromLdap($value, $type, $dateTimeAsUtc));
  220. }
  221. public function fromLdapProvider(){
  222. return array(
  223. array('1', '1', 0, true),
  224. array('0', '0', 0, true),
  225. array(true, 'TRUE', 0, true),
  226. array(false, 'FALSE', 0, true),
  227. array('123456789', '123456789', 0, true),
  228. // ZF-11639
  229. array('+123456789', '+123456789', 0, true),
  230. );
  231. }
  232. }