ConverterTest.php 8.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249
  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. } else {
  113. return array(
  114. array('N;', null),
  115. array('i:1;', 1),
  116. 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')),
  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. }
  120. }
  121. /**
  122. * @dataProvider toLdapProvider
  123. */
  124. public function testToLdap($expect, $convert){
  125. $this->assertEquals($expect, Zend_Ldap_Converter::toLdap($convert['value'], $convert['type']));
  126. }
  127. public function toLdapProvider(){
  128. return array(
  129. array(null, array('value' => null,'type' => 0)),
  130. array('19700101000000Z',array ('value'=> 0, 'type' => 2)),
  131. array('0',array ('value'=> 0, 'type' => 0)),
  132. array('FALSE',array ('value'=> 0, 'type' => 1)),
  133. array('19700101000000Z',array('value'=>new Zend_Date('1970-01-01T00:00:00+0000',Zend_Date::ISO_8601),'type'=>0)),
  134. );
  135. }
  136. /**
  137. * @dataProvider fromLdapUnserializeProvider
  138. */
  139. public function testFromLdapUnserialize ($expect, $convert)
  140. {
  141. $this->assertEquals($expect, Zend_Ldap_Converter::fromLdapUnserialize($convert));
  142. }
  143. public function testFromLdapUnserializeThrowsException ()
  144. {
  145. $this->setExpectedException('UnexpectedValueException');
  146. Zend_Ldap_Converter::fromLdapUnserialize('--');
  147. }
  148. public function fromLdapUnserializeProvider(){
  149. return array (
  150. array(null,'N;'),
  151. array(1,'i:1;'),
  152. array(false,'b:0;'),
  153. );
  154. }
  155. public function testFromLdapBoolean()
  156. {
  157. $this->assertTrue(Zend_Ldap_Converter::fromLdapBoolean('TRUE'));
  158. $this->assertFalse(Zend_Ldap_Converter::fromLdapBoolean('FALSE'));
  159. $this->setExpectedException('InvalidArgumentException');
  160. Zend_Ldap_Converter::fromLdapBoolean('test');
  161. }
  162. /**
  163. * @dataProvider fromLdapDateTimeProvider
  164. */
  165. public function testFromLdapDateTime($expected, $convert, $utc)
  166. {
  167. if ( true === $utc ) {
  168. $expected -> setTimezone(new DateTimeZone('UTC'));
  169. }
  170. $this->assertEquals($expected, Zend_Ldap_Converter::fromLdapDatetime($convert,$utc));
  171. }
  172. public function fromLdapDateTimeProvider ()
  173. {
  174. return array (
  175. array(new DateTime('2010-12-24 08:00:23+0300'),'20101224080023+0300', false),
  176. array(new DateTime('2010-12-24 08:00:23+0300'),'20101224080023+03\'00\'', false),
  177. array(new DateTime('2010-12-24 08:00:23+0000'),'20101224080023', false),
  178. array(new DateTime('2010-12-24 08:00:00+0000'),'201012240800', false),
  179. array(new DateTime('2010-12-24 08:00:00+0000'),'2010122408', false),
  180. array(new DateTime('2010-12-24 00:00:00+0000'),'20101224', false),
  181. array(new DateTime('2010-12-01 00:00:00+0000'),'201012', false),
  182. array(new DateTime('2010-01-01 00:00:00+0000'),'2010', false),
  183. array(new DateTime('2010-04-03 12:23:34+0000'), '20100403122334', true),
  184. );
  185. }
  186. /**
  187. * @expectedException InvalidArgumentException
  188. * @dataProvider fromLdapDateTimeException
  189. */
  190. public function testFromLdapDateTimeThrowsException ($value)
  191. {
  192. Zend_Ldap_Converter::fromLdapDatetime($value);
  193. }
  194. public static function fromLdapDateTimeException ()
  195. {
  196. return array (
  197. array('foobar'),
  198. array('201'),
  199. array('201013'),
  200. array('20101232'),
  201. array('2010123124'),
  202. array('201012312360'),
  203. array('20101231235960'),
  204. array('20101231235959+13'),
  205. array('20101231235959+1160'),
  206. );
  207. }
  208. /**
  209. * @dataProvider fromLdapProvider
  210. */
  211. public function testFromLdap($expect, $value, $type, $dateTimeAsUtc){
  212. $this->assertSame($expect, Zend_Ldap_Converter::fromLdap($value, $type, $dateTimeAsUtc));
  213. }
  214. public function fromLdapProvider(){
  215. return array(
  216. array('1', '1', 0, true),
  217. array('0', '0', 0, true),
  218. array(true, 'TRUE', 0, true),
  219. array(false, 'FALSE', 0, true),
  220. array('123456789', '123456789', 0, true),
  221. // ZF-11639
  222. array('+123456789', '+123456789', 0, true),
  223. );
  224. }
  225. }