JsonLibTest.php 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210
  1. <?php
  2. /* Copyright (C) 2010-2012 Laurent Destailleur <eldy@users.sourceforge.net>
  3. *
  4. * This program is free software; you can redistribute it and/or modify
  5. * it under the terms of the GNU General Public License as published by
  6. * the Free Software Foundation; either version 3 of the License, or
  7. * (at your option) any later version.
  8. *
  9. * This program is distributed in the hope that it will be useful,
  10. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. * GNU General Public License for more details.
  13. *
  14. * You should have received a copy of the GNU General Public License
  15. * along with this program. If not, see <https://www.gnu.org/licenses/>.
  16. * or see https://www.gnu.org/
  17. */
  18. /**
  19. * \file test/phpunit/JsonLibTest.php
  20. * \ingroup test
  21. * \brief PHPUnit test
  22. * \remarks To run this script as CLI: phpunit filename.php
  23. */
  24. global $conf,$user,$langs,$db;
  25. //define('TEST_DB_FORCE_TYPE','mysql'); // This is to force using mysql driver
  26. //require_once 'PHPUnit/Autoload.php';
  27. require_once dirname(__FILE__).'/../../htdocs/master.inc.php';
  28. if (! defined('NOREQUIREUSER')) {
  29. define('NOREQUIREUSER', '1');
  30. }
  31. if (! defined('NOREQUIREDB')) {
  32. define('NOREQUIREDB', '1');
  33. }
  34. if (! defined('NOREQUIRESOC')) {
  35. define('NOREQUIRESOC', '1');
  36. }
  37. if (! defined('NOREQUIRETRAN')) {
  38. define('NOREQUIRETRAN', '1');
  39. }
  40. if (! defined('NOCSRFCHECK')) {
  41. define('NOCSRFCHECK', '1');
  42. }
  43. if (! defined('NOTOKENRENEWAL')) {
  44. define('NOTOKENRENEWAL', '1');
  45. }
  46. if (! defined('NOREQUIREMENU')) {
  47. define('NOREQUIREMENU', '1'); // If there is no menu to show
  48. }
  49. if (! defined('NOREQUIREHTML')) {
  50. define('NOREQUIREHTML', '1'); // If we don't need to load the html.form.class.php
  51. }
  52. if (! defined('NOREQUIREAJAX')) {
  53. define('NOREQUIREAJAX', '1');
  54. }
  55. if (! defined("NOLOGIN")) {
  56. define("NOLOGIN", '1'); // If this page is public (can be called outside logged session)
  57. }
  58. /**
  59. * Class for PHPUnit tests
  60. *
  61. * @backupGlobals disabled
  62. * @backupStaticAttributes enabled
  63. * @remarks backupGlobals must be disabled to have db,conf,user and lang not erased.
  64. */
  65. class JsonLibTest extends PHPUnit\Framework\TestCase
  66. {
  67. protected $savconf;
  68. protected $savuser;
  69. protected $savlangs;
  70. protected $savdb;
  71. /**
  72. * Constructor
  73. * We save global variables into local variables
  74. *
  75. * @return CoreTest
  76. */
  77. public function __construct()
  78. {
  79. parent::__construct();
  80. //$this->sharedFixture
  81. global $conf,$user,$langs,$db;
  82. $this->savconf=$conf;
  83. $this->savuser=$user;
  84. $this->savlangs=$langs;
  85. $this->savdb=$db;
  86. print __METHOD__." db->type=".$db->type." user->id=".$user->id;
  87. //print " - db ".$db->db;
  88. print "\n";
  89. }
  90. /**
  91. * setUpBeforeClass
  92. *
  93. * @return void
  94. */
  95. public static function setUpBeforeClass()
  96. {
  97. global $conf,$user,$langs,$db;
  98. //$db->begin(); // This is to have all actions inside a transaction even if test launched without suite.
  99. print __METHOD__."\n";
  100. }
  101. /**
  102. * tearDownAfterClass
  103. *
  104. * @return void
  105. */
  106. public static function tearDownAfterClass()
  107. {
  108. global $conf,$user,$langs,$db;
  109. //$db->rollback();
  110. print __METHOD__."\n";
  111. }
  112. /**
  113. * Init phpunit tests
  114. *
  115. * @return void
  116. */
  117. protected function setUp()
  118. {
  119. global $conf,$user,$langs,$db;
  120. $conf=$this->savconf;
  121. $user=$this->savuser;
  122. $langs=$this->savlangs;
  123. $db=$this->savdb;
  124. print __METHOD__."\n";
  125. }
  126. /**
  127. * End phpunit tests
  128. *
  129. * @return void
  130. */
  131. protected function tearDown()
  132. {
  133. print __METHOD__."\n";
  134. }
  135. /**
  136. * testJsonEncode
  137. *
  138. * @return void
  139. */
  140. public function testJsonEncode()
  141. {
  142. //$this->sharedFixture
  143. global $conf,$user,$langs,$db;
  144. $this->savconf=$conf;
  145. $this->savuser=$user;
  146. $this->savlangs=$langs;
  147. $this->savdb=$db;
  148. // Try to decode a string encoded with serialize
  149. $encoded = 'a:1:{s:7:"options";a:3:{s:3:"app";s:11:"Application";s:6:"system";s:6:"System";s:6:"option";s:6:"Option";}}';
  150. $decoded=json_decode($encoded, true);
  151. $this->assertEquals(null, $decoded, 'test to json_decode() a string that was encoded with serialize()');
  152. $encoded = 'rubishstring!aa{bcd';
  153. $decoded=json_decode($encoded, true);
  154. $this->assertEquals(null, $decoded, 'test to json_decode() a string that was encoded with serialize()');
  155. // Do a test with an array starting with 0
  156. $arraytotest=array(0=>array('key'=>1,'value'=>'PRODREF','label'=>'Product ref with é and special chars \\ \' "'));
  157. $arrayencodedexpected='[{"key":1,"value":"PRODREF","label":"Product ref with \u00e9 and special chars \\\\ \' \""}]';
  158. $encoded=json_encode($arraytotest);
  159. $this->assertEquals($arrayencodedexpected, $encoded);
  160. $decoded=json_decode($encoded, true);
  161. $this->assertEquals($arraytotest, $decoded, 'test for json_xxx');
  162. $encoded=dol_json_encode($arraytotest);
  163. $this->assertEquals($arrayencodedexpected, $encoded);
  164. $decoded=dol_json_decode($encoded, true);
  165. $this->assertEquals($arraytotest, $decoded, 'test for dol_json_xxx');
  166. // Same test but array start with 2 instead of 0
  167. $arraytotest=array(2=>array('key'=>1,'value'=>'PRODREF','label'=>'Product ref with é and special chars \\ \' "'));
  168. $arrayencodedexpected='{"2":{"key":1,"value":"PRODREF","label":"Product ref with \u00e9 and special chars \\\\ \' \""}}';
  169. $encoded=json_encode($arraytotest);
  170. $this->assertEquals($arrayencodedexpected, $encoded);
  171. $decoded=json_decode($encoded, true);
  172. $this->assertEquals($arraytotest, $decoded, 'test for json_xxx');
  173. $encoded=dol_json_encode($arraytotest);
  174. $this->assertEquals($arrayencodedexpected, $encoded);
  175. $decoded=dol_json_decode($encoded, true);
  176. $this->assertEquals($arraytotest, $decoded, 'test for dol_json_xxx');
  177. // Test with object
  178. $now=gmmktime(12, 0, 0, 1, 1, 1970);
  179. $objecttotest=new stdClass();
  180. $objecttotest->property1='abc';
  181. $objecttotest->property2=1234;
  182. $objecttotest->property3=$now;
  183. $encoded=dol_json_encode($objecttotest);
  184. $this->assertEquals('{"property1":"abc","property2":1234,"property3":43200}', $encoded);
  185. }
  186. }