ConverterTest.php 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227
  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-2010 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. * Test helper
  24. */
  25. require_once dirname(dirname(dirname(__FILE__))) . DIRECTORY_SEPARATOR . 'TestHelper.php';
  26. /**
  27. * Zend_Ldap_Converter
  28. */
  29. require_once 'Zend/Ldap/Converter.php';
  30. /**
  31. * @category Zend
  32. * @package Zend_Ldap
  33. * @subpackage UnitTests
  34. * @copyright Copyright (c) 2005-2010 Zend Technologies USA Inc. (http://www.zend.com)
  35. * @license http://framework.zend.com/license/new-bsd New BSD License
  36. * @group Zend_Ldap
  37. */
  38. class Zend_Ldap_ConverterTest extends PHPUnit_Framework_TestCase
  39. {
  40. public function testAsc2hex32()
  41. {
  42. $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' .
  43. '\1a\1b\1c\1d\1e\1f !"#$%&\'()*+,-./0123456789:;<=>?@ABCDEFGHIJKLMNOPQRSTUVWXYZ[\]^_`' .
  44. 'abcdefghijklmnopqrstuvwxyz{|}~';
  45. $str='';
  46. for ($i=0; $i<127; $i++) {
  47. $str.=chr($i);
  48. }
  49. $this->assertEquals($expected, Zend_Ldap_Converter::ascToHex32($str));
  50. }
  51. public function testHex2asc()
  52. {
  53. $expected='';
  54. for ($i=0; $i<127; $i++) {
  55. $expected.=chr($i);
  56. }
  57. $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' .
  58. '\1c\1d\1e\1f !"#$%&\'()*+,-./0123456789:;<=>?@ABCDEFGHIJKLMNOPQRSTUVWXYZ[\]^_`abcdefg' .
  59. 'hijklmnopqrstuvwxyz{|}~';
  60. $this->assertEquals($expected, Zend_Ldap_Converter::hex32ToAsc($str));
  61. }
  62. /**
  63. * @dataProvider toLdapDateTimeProvider
  64. */
  65. public function testToLdapDateTime($convert,$expect)
  66. {
  67. $result = Zend_Ldap_Converter::toLdapDatetime($convert['date'], $convert['utc']);
  68. $this->assertEquals( $expect,$result);
  69. }
  70. public function toLdapDateTimeProvider(){
  71. include_once 'Zend/Date.php';
  72. $tz = new DateTimeZone('UTC');
  73. return array(
  74. array(array('date'=> 0, 'utc' => true ),'19700101000000Z'),
  75. array(array('date'=> new DateTime('2010-05-12 13:14:45+0300', $tz), 'utc' => false ),'20100512131445+0300'),
  76. array(array('date'=> new DateTime('2010-05-12 13:14:45+0300', $tz), 'utc' => true ),'20100512101445Z'),
  77. array(array('date'=> '2010-05-12 13:14:45+0300', 'utc' => false ),'20100512131445+0300'),
  78. array(array('date'=> '2010-05-12 13:14:45+0300', 'utc' => true ),'20100512101445Z'),
  79. array(array('date'=> new Zend_Date('2010-05-12T13:14:45+0300', Zend_Date::ISO_8601), 'utc' => true ),'20100512101445Z'),
  80. array(array('date'=> new Zend_Date('2010-05-12T13:14:45+0300', Zend_Date::ISO_8601), 'utc' => false ),'20100512131445+0300'),
  81. array(array('date'=> new Zend_Date('0', Zend_Date::TIMESTAMP), 'utc' => true ),'19700101000000Z'),
  82. );
  83. }
  84. /**
  85. * @dataProvider toLdapBooleanProvider
  86. */
  87. public function testToLdapBoolean($expect, $convert)
  88. {
  89. $this->assertEquals($expect, Zend_Ldap_Converter::toldapBoolean($convert));
  90. }
  91. public function toLdapBooleanProvider(){
  92. return array (
  93. array('TRUE',true),
  94. array('TRUE',1),
  95. array('TRUE','true'),
  96. array('FALSE','false'),
  97. array('FALSE', false),
  98. array('FALSE', array('true')),
  99. array('FALSE', array('false')),
  100. );
  101. }
  102. /**
  103. * @dataProvider toLdapSerializeProvider
  104. */
  105. public function testToLdapSerialize($expect, $convert){
  106. $this->assertEquals($expect, Zend_Ldap_Converter::toLdapSerialize($convert));
  107. }
  108. public function toLdapSerializeProvider(){
  109. return array(
  110. array('N;', null),
  111. array('i:1;', 1),
  112. 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')),
  113. array('a:3:{i:0;s:4:"test";i:1;i:1;s:3:"foo";s:3:"bar";}', array('test',1,'foo'=>'bar')),
  114. );
  115. }
  116. /**
  117. * @dataProvider toLdapProvider
  118. */
  119. public function testToLdap($expect, $convert){
  120. $this->assertEquals($expect, Zend_Ldap_Converter::toLdap($convert['value'], $convert['type']));
  121. }
  122. public function toLdapProvider(){
  123. return array(
  124. array(null, array('value' => null,'type' => 0)),
  125. array('19700101000000Z',array ('value'=> 0, 'type' => 2)),
  126. array('0',array ('value'=> 0, 'type' => 0)),
  127. array('FALSE',array ('value'=> 0, 'type' => 1)),
  128. array('19700101000000Z',array('value'=>new Zend_Date('1970-01-01T00:00:00+0000',Zend_Date::ISO_8601),'type'=>0)),
  129. );
  130. }
  131. /**
  132. * @dataProvider fromLdapUnserializeProvider
  133. */
  134. public function testFromLdapUnserialize ($expect, $convert)
  135. {
  136. $this->assertEquals($expect, Zend_Ldap_Converter::fromLdapUnserialize($convert));
  137. }
  138. public function testFromLdapUnserializeThrowsException ()
  139. {
  140. $this->setExpectedException('UnexpectedValueException');
  141. Zend_Ldap_Converter::fromLdapUnserialize('--');
  142. }
  143. public function fromLdapUnserializeProvider(){
  144. return array (
  145. array(null,'N;'),
  146. array(1,'i:1;'),
  147. array(false,'b:0;'),
  148. );
  149. }
  150. public function testFromLdapBoolean()
  151. {
  152. $this->assertTrue(Zend_Ldap_Converter::fromLdapBoolean('TRUE'));
  153. $this->assertFalse(Zend_Ldap_Converter::fromLdapBoolean('FALSE'));
  154. $this->setExpectedException('InvalidArgumentException');
  155. Zend_Ldap_Converter::fromLdapBoolean('test');
  156. }
  157. /**
  158. * @dataProvider fromLdapDateTimeProvider
  159. */
  160. public function testFromLdapDateTime($expected, $convert, $utc)
  161. {
  162. if ( true === $utc ) {
  163. $expected -> setTimezone(new DateTimeZone('UTC'));
  164. }
  165. $this->assertEquals($expected, Zend_Ldap_Converter::fromLdapDatetime($convert,$utc));
  166. }
  167. public function fromLdapDateTimeProvider ()
  168. {
  169. $tz = new DateTimeZone('UTC');
  170. $tz = null;
  171. return array (
  172. array(new DateTime('2010-12-24 08:00:23+0300'),'20101224080023+0300', false),
  173. array(new DateTime('2010-12-24 08:00:23+0300'),'20101224080023+03\'00\'', false),
  174. array(new DateTime('2010-12-24 08:00:23+0000'),'20101224080023', false),
  175. array(new DateTime('2010-12-24 08:00:00+0000'),'201012240800', false),
  176. array(new DateTime('2010-12-24 08:00:00+0000'),'2010122408', false),
  177. array(new DateTime('2010-12-24 00:00:00+0000'),'20101224', false),
  178. array(new DateTime('2010-12-01 00:00:00+0000'),'201012', false),
  179. array(new DateTime('2010-01-01 00:00:00+0000'),'2010', false),
  180. array(new DateTime('2010-04-03 12:23:34+0000'), '20100403122334', true),
  181. );
  182. }
  183. /**
  184. * @expectedException InvalidArgumentException
  185. * @dataProvider fromLdapDateTimeException
  186. */
  187. public function testFromLdapDateTimeThrowsException ($value)
  188. {
  189. Zend_Ldap_Converter::fromLdapDatetime($value);
  190. }
  191. public static function fromLdapDateTimeException ()
  192. {
  193. return array (
  194. array('foobar'),
  195. array('201'),
  196. array('201013'),
  197. array('20101232'),
  198. array('2010123124'),
  199. array('201012312360'),
  200. array('20101231235960'),
  201. array('20101231235959+13'),
  202. array('20101231235959+1160'),
  203. );
  204. }
  205. }