ConverterTest.php 9.7 KB

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