JsonLibTest.php 6.0 KB

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