2
0

ConverterTest.php 8.5 KB

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