ExportTest.php 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391
  1. <?php
  2. /* Copyright (C) 2010 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/ExportTest.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. require_once dirname(__FILE__).'/../../htdocs/exports/class/export.class.php';
  29. require_once dirname(__FILE__).'/../../htdocs/core/lib/files.lib.php';
  30. if (! defined('NOREQUIREUSER')) {
  31. define('NOREQUIREUSER', '1');
  32. }
  33. if (! defined('NOREQUIREDB')) {
  34. define('NOREQUIREDB', '1');
  35. }
  36. if (! defined('NOREQUIRESOC')) {
  37. define('NOREQUIRESOC', '1');
  38. }
  39. if (! defined('NOREQUIRETRAN')) {
  40. define('NOREQUIRETRAN', '1');
  41. }
  42. if (! defined('NOCSRFCHECK')) {
  43. define('NOCSRFCHECK', '1');
  44. }
  45. if (! defined('NOTOKENRENEWAL')) {
  46. define('NOTOKENRENEWAL', '1');
  47. }
  48. if (! defined('NOREQUIREMENU')) {
  49. define('NOREQUIREMENU', '1'); // If there is no menu to show
  50. }
  51. if (! defined('NOREQUIREHTML')) {
  52. define('NOREQUIREHTML', '1'); // If we don't need to load the html.form.class.php
  53. }
  54. if (! defined('NOREQUIREAJAX')) {
  55. define('NOREQUIREAJAX', '1');
  56. }
  57. if (! defined("NOLOGIN")) {
  58. define("NOLOGIN", '1'); // If this page is public (can be called outside logged session)
  59. }
  60. /**
  61. * Class for PHPUnit tests
  62. *
  63. * @backupGlobals disabled
  64. * @backupStaticAttributes enabled
  65. * @remarks backupGlobals must be disabled to have db,conf,user and lang not erased.
  66. */
  67. class ExportTest extends PHPUnit\Framework\TestCase
  68. {
  69. protected $savconf;
  70. protected $savuser;
  71. protected $savlangs;
  72. protected $savdb;
  73. /**
  74. * Constructor
  75. * We save global variables into local variables
  76. *
  77. * @return ExportTest
  78. */
  79. public function __construct()
  80. {
  81. parent::__construct();
  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()
  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()
  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()
  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()
  134. {
  135. print __METHOD__."\n";
  136. }
  137. /**
  138. * Other tests
  139. *
  140. * @return void
  141. */
  142. public function testExportOther()
  143. {
  144. global $conf,$user,$langs,$db;
  145. $model='csv';
  146. $conf->global->EXPORT_CSV_SEPARATOR_TO_USE = ',';
  147. print 'EXPORT_CSV_SEPARATOR_TO_USE = '.$conf->global->EXPORT_CSV_SEPARATOR_TO_USE;
  148. // Creation of class to export using model ExportXXX
  149. $dir = DOL_DOCUMENT_ROOT . "/core/modules/export/";
  150. $file = "export_".$model.".modules.php";
  151. $classname = "Export".$model;
  152. require_once $dir.$file;
  153. $objmodel = new $classname($db);
  154. // First test without option USE_STRICT_CSV_RULES
  155. unset($conf->global->USE_STRICT_CSV_RULES);
  156. $valtotest='A simple string';
  157. print __METHOD__." valtotest=".$valtotest."\n";
  158. $result = $objmodel->csvClean($valtotest, $langs->charset_output);
  159. print __METHOD__." result=".$result."\n";
  160. $this->assertEquals($result, 'A simple string');
  161. $valtotest='A string with , and ; inside';
  162. print __METHOD__." valtotest=".$valtotest."\n";
  163. $result = $objmodel->csvClean($valtotest, $langs->charset_output);
  164. print __METHOD__." result=".$result."\n";
  165. $this->assertEquals($result, '"A string with , and ; inside"', 'Error in csvClean for '.$file);
  166. $valtotest='A string with " inside';
  167. print __METHOD__." valtotest=".$valtotest."\n";
  168. $result = $objmodel->csvClean($valtotest, $langs->charset_output);
  169. print __METHOD__." result=".$result."\n";
  170. $this->assertEquals($result, '"A string with "" inside"');
  171. $valtotest='A string with " inside and '."\r\n".' carriage returns';
  172. print __METHOD__." valtotest=".$valtotest."\n";
  173. $result = $objmodel->csvClean($valtotest, $langs->charset_output);
  174. print __METHOD__." result=".$result."\n";
  175. $this->assertEquals($result, '"A string with "" inside and \n carriage returns"');
  176. $valtotest='A string with <a href="aaa"><strong>html<br>content</strong></a> inside<br>'."\n";
  177. print __METHOD__." valtotest=".$valtotest."\n";
  178. $result = $objmodel->csvClean($valtotest, $langs->charset_output);
  179. print __METHOD__." result=".$result."\n";
  180. $this->assertEquals($result, '"A string with <a href=""aaa""><strong>html<br>content</strong></a> inside"');
  181. // Same tests with strict mode
  182. $conf->global->USE_STRICT_CSV_RULES = 1;
  183. $valtotest='A simple string';
  184. print __METHOD__." valtotest=".$valtotest."\n";
  185. $result = $objmodel->csvClean($valtotest, $langs->charset_output);
  186. print __METHOD__." result=".$result."\n";
  187. $this->assertEquals($result, 'A simple string');
  188. $valtotest='A string with , and ; inside';
  189. print __METHOD__." valtotest=".$valtotest."\n";
  190. $result = $objmodel->csvClean($valtotest, $langs->charset_output);
  191. print __METHOD__." result=".$result."\n";
  192. $this->assertEquals($result, '"A string with , and ; inside"');
  193. $valtotest='A string with " inside';
  194. print __METHOD__." valtotest=".$valtotest."\n";
  195. $result = $objmodel->csvClean($valtotest, $langs->charset_output);
  196. print __METHOD__." result=".$result."\n";
  197. $this->assertEquals($result, '"A string with "" inside"');
  198. $valtotest='A string with " inside and '."\r\n".' carriage returns';
  199. print __METHOD__." valtotest=".$valtotest."\n";
  200. $result = $objmodel->csvClean($valtotest, $langs->charset_output);
  201. print __METHOD__." result=".$result."\n";
  202. $this->assertEquals($result, "\"A string with \"\" inside and \r\n carriage returns\"");
  203. $valtotest='A string with <a href="aaa"><strong>html<br>content</strong></a> inside<br>'."\n";
  204. print __METHOD__." valtotest=".$valtotest."\n";
  205. $result = $objmodel->csvClean($valtotest, $langs->charset_output);
  206. print __METHOD__." result=".$result."\n";
  207. $this->assertEquals($result, '"A string with <a href=""aaa""><strong>html<br>content</strong></a> inside"');
  208. }
  209. /**
  210. * Test export function for a personalized dataset
  211. *
  212. * @depends testExportOther
  213. * @return void
  214. */
  215. public function testExportPersonalizedExport()
  216. {
  217. global $conf,$user,$langs,$db;
  218. $sql = "SELECT f.ref as f_ref, f.total_ht as f_total, f.total_tva as f_tva FROM ".MAIN_DB_PREFIX."facture f";
  219. $objexport=new Export($db);
  220. //$objexport->load_arrays($user,$datatoexport);
  221. // Define properties
  222. $datatoexport='test';
  223. $array_selected = array("f.ref"=>1, "f.total"=>2, "f.tva"=>3);
  224. $array_export_fields = array("f.ref"=>"FacNumber", "f.total"=>"FacTotal", "f.tva"=>"FacVat");
  225. $array_alias = array("f_ref"=>"ref", "f_total"=>"total", "f_tva"=>"tva");
  226. $objexport->array_export_fields[0]=$array_export_fields;
  227. $objexport->array_export_alias[0]=$array_alias;
  228. dol_mkdir($conf->export->dir_temp);
  229. $model='csv';
  230. // Build export file
  231. print "Process build_file for model = ".$model."\n";
  232. $result=$objexport->build_file($user, $model, $datatoexport, $array_selected, array(), $sql);
  233. $expectedresult = 1;
  234. $this->assertEquals($expectedresult, $result, 'Error in CSV export');
  235. $model='tsv';
  236. // Build export file
  237. print "Process build_file for model = ".$model."\n";
  238. $result=$objexport->build_file($user, $model, $datatoexport, $array_selected, array(), $sql);
  239. $expectedresult=1;
  240. $this->assertEquals($expectedresult, $result, 'Error in TSV export');
  241. $model='excel2007';
  242. // Build export file
  243. /* ko on php 7.4 on travis (zip not available) */
  244. print "Process build_file for model = ".$model."\n";
  245. $result=$objexport->build_file($user, $model, $datatoexport, $array_selected, array(), $sql);
  246. $expectedresult=1;
  247. $this->assertEquals($expectedresult, $result, 'Error in Excel2007 export');
  248. return true;
  249. }
  250. /**
  251. * Test export function for a personalized dataset with filters
  252. *
  253. * @depends testExportPersonalizedExport
  254. * @return void
  255. */
  256. public function testExportPersonalizedWithFilter()
  257. {
  258. global $conf,$user,$langs,$db;
  259. /*
  260. $sql = "SELECT f.ref as f_ref, f.total_ht as f_total_ht, f.total_tva as f_total_tva FROM ".MAIN_DB_PREFIX."facture f";
  261. $objexport=new Export($db);
  262. //$objexport->load_arrays($user,$datatoexport);
  263. // Define properties
  264. $datatoexport='test_filtered';
  265. $array_selected = array("f.ref"=>1, "f.total_ht"=>2, "f.total_tva"=>3);
  266. $array_export_fields = array("f.ref"=>"FacNumber", "f.total_ht"=>"FacTotal", "f.total_tva"=>"FacVat");
  267. $array_filtervalue = array("f.total_ht" => ">100");
  268. $array_filtered = array("f.total_ht" => 1);
  269. $array_alias = array("f_ref"=>"ref", "f_total_ht"=>"total_ht", "f_total_tva"=>"total_tva");
  270. $objexport->array_export_fields[0]=$array_export_fields;
  271. $objexport->array_export_alias[0]=$array_alias;
  272. dol_mkdir($conf->export->dir_temp);
  273. $model='csv';
  274. // Build export file
  275. $result=$objexport->build_file($user, $model, $datatoexport, $array_selected, $array_filtervalue, $sql);
  276. $expectedresult=1;
  277. $this->assertEquals($expectedresult,$result);
  278. $model='tsv';
  279. // Build export file
  280. $result=$objexport->build_file($user, $model, $datatoexport, $array_selected, $array_filtervalue, $sql);
  281. $expectedresult=1;
  282. $this->assertEquals($expectedresult,$result);
  283. $model='excel';
  284. // Build export file
  285. $result=$objexport->build_file($user, $model, $datatoexport, $array_selected, $array_filtervalue, $sql);
  286. $expectedresult=1;
  287. $this->assertEquals($expectedresult,$result);
  288. */
  289. $this->assertEquals(true, true);
  290. return true;
  291. }
  292. /**
  293. * Test export function for all dataset predefined into modules
  294. *
  295. * @depends testExportPersonalizedWithFilter
  296. * @return void
  297. */
  298. public function testExportModulesDatasets()
  299. {
  300. global $conf,$user,$langs,$db;
  301. $model='csv';
  302. $filterdatatoexport='';
  303. //$filterdatatoexport='';
  304. //$array_selected = array("s.rowid"=>1, "s.nom"=>2); // Mut be fields found into declaration of dataset
  305. // Load properties of arrays to make export
  306. $objexport=new Export($db);
  307. $result=$objexport->load_arrays($user, $filterdatatoexport); // This load ->array_export_xxx properties for datatoexport
  308. // Loop on each dataset
  309. foreach ($objexport->array_export_code as $key => $datatoexport) {
  310. $exportfile=$conf->export->dir_temp.'/'.$user->id.'/export_'.$datatoexport.'.csv';
  311. print "Process export for dataset ".$datatoexport." into ".$exportfile."\n";
  312. dol_delete_file($exportfile);
  313. // Generate $array_selected
  314. $i=0;
  315. $array_selected=array();
  316. foreach ($objexport->array_export_fields[$key] as $key => $val) {
  317. $array_selected[$key]=$i++;
  318. }
  319. //var_dump($array_selected);
  320. // Build export file
  321. $sql = "";
  322. $result=$objexport->build_file($user, $model, $datatoexport, $array_selected, array(), $sql);
  323. $expectedresult = 1;
  324. $this->assertEquals($expectedresult, $result, "Call build_file() to export ".$exportfile.' failed: '.$objexport->error);
  325. $result=dol_is_file($exportfile);
  326. $this->assertTrue($result, 'File '.$exportfile.' not found');
  327. }
  328. return true;
  329. }
  330. }