ExportTest.php 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359
  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 <http://www.gnu.org/licenses/>.
  16. * or see http://www.gnu.org/
  17. */
  18. /**
  19. * \file test/phpunit/ImportTest.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')) define('NOREQUIREUSER','1');
  31. if (! defined('NOREQUIREDB')) define('NOREQUIREDB','1');
  32. if (! defined('NOREQUIRESOC')) define('NOREQUIRESOC','1');
  33. if (! defined('NOREQUIRETRAN')) define('NOREQUIRETRAN','1');
  34. if (! defined('NOCSRFCHECK')) define('NOCSRFCHECK','1');
  35. if (! defined('NOTOKENRENEWAL')) define('NOTOKENRENEWAL','1');
  36. if (! defined('NOREQUIREMENU')) define('NOREQUIREMENU','1'); // If there is no menu to show
  37. if (! defined('NOREQUIREHTML')) define('NOREQUIREHTML','1'); // If we don't need to load the html.form.class.php
  38. if (! defined('NOREQUIREAJAX')) define('NOREQUIREAJAX','1');
  39. if (! defined("NOLOGIN")) define("NOLOGIN",'1'); // If this page is public (can be called outside logged session)
  40. /**
  41. * Class for PHPUnit tests
  42. *
  43. * @backupGlobals disabled
  44. * @backupStaticAttributes enabled
  45. * @remarks backupGlobals must be disabled to have db,conf,user and lang not erased.
  46. */
  47. class ExportTest extends PHPUnit_Framework_TestCase
  48. {
  49. protected $savconf;
  50. protected $savuser;
  51. protected $savlangs;
  52. protected $savdb;
  53. /**
  54. * Constructor
  55. * We save global variables into local variables
  56. *
  57. * @return ExportTest
  58. */
  59. function __construct()
  60. {
  61. parent::__construct();
  62. //$this->sharedFixture
  63. global $conf,$user,$langs,$db;
  64. $this->savconf=$conf;
  65. $this->savuser=$user;
  66. $this->savlangs=$langs;
  67. $this->savdb=$db;
  68. print __METHOD__." db->type=".$db->type." user->id=".$user->id;
  69. //print " - db ".$db->db;
  70. print "\n";
  71. }
  72. // Static methods
  73. public static function setUpBeforeClass()
  74. {
  75. global $conf,$user,$langs,$db;
  76. //$db->begin(); // This is to have all actions inside a transaction even if test launched without suite.
  77. print __METHOD__."\n";
  78. }
  79. // tear down after class
  80. public static function tearDownAfterClass()
  81. {
  82. global $conf,$user,$langs,$db;
  83. //$db->rollback();
  84. print __METHOD__."\n";
  85. }
  86. /**
  87. * Init phpunit tests
  88. *
  89. * @return void
  90. */
  91. protected function setUp()
  92. {
  93. global $conf,$user,$langs,$db;
  94. $conf=$this->savconf;
  95. $user=$this->savuser;
  96. $langs=$this->savlangs;
  97. $db=$this->savdb;
  98. print __METHOD__."\n";
  99. }
  100. /**
  101. * End phpunit tests
  102. *
  103. * @return void
  104. */
  105. protected function tearDown()
  106. {
  107. print __METHOD__."\n";
  108. }
  109. /**
  110. * Other tests
  111. *
  112. * @return void
  113. */
  114. public function testExportOther()
  115. {
  116. global $conf,$user,$langs,$db;
  117. $model='csv';
  118. // Creation of class to export using model ExportXXX
  119. $dir = DOL_DOCUMENT_ROOT . "/core/modules/export/";
  120. $file = "export_".$model.".modules.php";
  121. $classname = "Export".$model;
  122. require_once $dir.$file;
  123. $objmodel = new $classname($this->db);
  124. // First test without option USE_STRICT_CSV_RULES
  125. unset($conf->global->USE_STRICT_CSV_RULES);
  126. $valtotest='A simple string';
  127. print __METHOD__." valtotest=".$valtotest."\n";
  128. $result = $objmodel->csvClean($valtotest, $langs->charset_output);
  129. print __METHOD__." result=".$result."\n";
  130. $this->assertEquals($result, 'A simple string');
  131. $valtotest='A string with , and ; inside';
  132. print __METHOD__." valtotest=".$valtotest."\n";
  133. $result = $objmodel->csvClean($valtotest, $langs->charset_output);
  134. print __METHOD__." result=".$result."\n";
  135. $this->assertEquals($result, '"A string with , and ; inside"');
  136. $valtotest='A string with " inside';
  137. print __METHOD__." valtotest=".$valtotest."\n";
  138. $result = $objmodel->csvClean($valtotest, $langs->charset_output);
  139. print __METHOD__." result=".$result."\n";
  140. $this->assertEquals($result, '"A string with "" inside"');
  141. $valtotest='A string with " inside and '."\r\n".' carriage returns';
  142. print __METHOD__." valtotest=".$valtotest."\n";
  143. $result = $objmodel->csvClean($valtotest, $langs->charset_output);
  144. print __METHOD__." result=".$result."\n";
  145. $this->assertEquals($result, '"A string with "" inside and \n carriage returns"');
  146. $valtotest='A string with <a href="aaa"><strong>html<br>content</strong></a> inside<br>'."\n";
  147. print __METHOD__." valtotest=".$valtotest."\n";
  148. $result = $objmodel->csvClean($valtotest, $langs->charset_output);
  149. print __METHOD__." result=".$result."\n";
  150. $this->assertEquals($result, '"A string with <a href=""aaa""><strong>html<br>content</strong></a> inside"');
  151. // Same tests with strict mode
  152. $conf->global->USE_STRICT_CSV_RULES=1;
  153. $valtotest='A simple string';
  154. print __METHOD__." valtotest=".$valtotest."\n";
  155. $result = $objmodel->csvClean($valtotest, $langs->charset_output);
  156. print __METHOD__." result=".$result."\n";
  157. $this->assertEquals($result, 'A simple string');
  158. $valtotest='A string with , and ; inside';
  159. print __METHOD__." valtotest=".$valtotest."\n";
  160. $result = $objmodel->csvClean($valtotest, $langs->charset_output);
  161. print __METHOD__." result=".$result."\n";
  162. $this->assertEquals($result, '"A string with , and ; inside"');
  163. $valtotest='A string with " inside';
  164. print __METHOD__." valtotest=".$valtotest."\n";
  165. $result = $objmodel->csvClean($valtotest, $langs->charset_output);
  166. print __METHOD__." result=".$result."\n";
  167. $this->assertEquals($result, '"A string with "" inside"');
  168. $valtotest='A string with " inside and '."\r\n".' carriage returns';
  169. print __METHOD__." valtotest=".$valtotest."\n";
  170. $result = $objmodel->csvClean($valtotest, $langs->charset_output);
  171. print __METHOD__." result=".$result."\n";
  172. $this->assertEquals($result, "\"A string with \"\" inside and \r\n carriage returns\"");
  173. $valtotest='A string with <a href="aaa"><strong>html<br>content</strong></a> inside<br>'."\n";
  174. print __METHOD__." valtotest=".$valtotest."\n";
  175. $result = $objmodel->csvClean($valtotest, $langs->charset_output);
  176. print __METHOD__." result=".$result."\n";
  177. $this->assertEquals($result, '"A string with <a href=""aaa""><strong>html<br>content</strong></a> inside"');
  178. }
  179. /**
  180. * Test export function for a personalized dataset
  181. *
  182. * @depends testExportOther
  183. * @return void
  184. */
  185. public function testExportPersonalizedExport()
  186. {
  187. global $conf,$user,$langs,$db;
  188. $sql = "SELECT f.facnumber as f_facnumber, f.total as f_total, f.tva as f_tva FROM ".MAIN_DB_PREFIX."facture f";
  189. $objexport=new Export($db);
  190. //$objexport->load_arrays($user,$datatoexport);
  191. // Define properties
  192. $datatoexport='test';
  193. $array_selected = array("f.facnumber"=>1, "f.total"=>2, "f.tva"=>3);
  194. $array_export_fields = array("f.facnumber"=>"FacNumber", "f.total"=>"FacTotal", "f.tva"=>"FacVat");
  195. $array_alias = array("f_facnumber"=>"facnumber", "f_total"=>"total", "f_tva"=>"tva");
  196. $objexport->array_export_fields[0]=$array_export_fields;
  197. $objexport->array_export_alias[0]=$array_alias;
  198. dol_mkdir($conf->export->dir_temp);
  199. $model='csv';
  200. // Build export file
  201. $result=$objexport->build_file($user, $model, $datatoexport, $array_selected, array(), $sql);
  202. $expectedresult=1;
  203. $this->assertEquals($expectedresult,$result);
  204. $model='tsv';
  205. // Build export file
  206. $result=$objexport->build_file($user, $model, $datatoexport, $array_selected, array(), $sql);
  207. $expectedresult=1;
  208. $this->assertEquals($expectedresult,$result);
  209. $model='excel';
  210. // Build export file
  211. $result=$objexport->build_file($user, $model, $datatoexport, $array_selected, array(), $sql);
  212. $expectedresult=1;
  213. $this->assertEquals($expectedresult,$result);
  214. return true;
  215. }
  216. /**
  217. * Test export function for a personalized dataset with filters
  218. *
  219. * @depends testExportPersonalizedExport
  220. * @return void
  221. */
  222. public function testExportPersonalizedWithFilter()
  223. {
  224. global $conf,$user,$langs,$db;
  225. /*
  226. $sql = "SELECT f.facnumber as f_facnumber, f.total as f_total, f.tva as f_tva FROM ".MAIN_DB_PREFIX."facture f";
  227. $objexport=new Export($db);
  228. //$objexport->load_arrays($user,$datatoexport);
  229. // Define properties
  230. $datatoexport='test_filtered';
  231. $array_selected = array("f.facnumber"=>1, "f.total"=>2, "f.tva"=>3);
  232. $array_export_fields = array("f.facnumber"=>"FacNumber", "f.total"=>"FacTotal", "f.tva"=>"FacVat");
  233. $array_filtervalue = array("f.total" => ">100");
  234. $array_filtered = array("f.total" => 1);
  235. $array_alias = array("f_facnumber"=>"facnumber", "f_total"=>"total", "f_tva"=>"tva");
  236. $objexport->array_export_fields[0]=$array_export_fields;
  237. $objexport->array_export_alias[0]=$array_alias;
  238. dol_mkdir($conf->export->dir_temp);
  239. $model='csv';
  240. // Build export file
  241. $result=$objexport->build_file($user, $model, $datatoexport, $array_selected, $array_filtervalue, $sql);
  242. $expectedresult=1;
  243. $this->assertEquals($expectedresult,$result);
  244. $model='tsv';
  245. // Build export file
  246. $result=$objexport->build_file($user, $model, $datatoexport, $array_selected, $array_filtervalue, $sql);
  247. $expectedresult=1;
  248. $this->assertEquals($expectedresult,$result);
  249. $model='excel';
  250. // Build export file
  251. $result=$objexport->build_file($user, $model, $datatoexport, $array_selected, $array_filtervalue, $sql);
  252. $expectedresult=1;
  253. $this->assertEquals($expectedresult,$result);
  254. */
  255. return true;
  256. }
  257. /**
  258. * Test export function for all dataset predefined into modules
  259. *
  260. * @depends testExportPersonalizedWithFilter
  261. * @return void
  262. */
  263. public function testExportModulesDatasets()
  264. {
  265. global $conf,$user,$langs,$db;
  266. $model='csv';
  267. $filterdatatoexport='';
  268. //$filterdatatoexport='';
  269. //$array_selected = array("s.rowid"=>1, "s.nom"=>2); // Mut be fields found into declaration of dataset
  270. // Load properties of arrays to make export
  271. $objexport=new Export($db);
  272. $result=$objexport->load_arrays($user,$filterdatatoexport); // This load ->array_export_xxx properties for datatoexport
  273. // Loop on each dataset
  274. foreach($objexport->array_export_code as $key => $datatoexport)
  275. {
  276. $exportfile=$conf->export->dir_temp.'/'.$user->id.'/export_'.$datatoexport.'.csv';
  277. print "Process export for dataset ".$datatoexport." into ".$exportfile."\n";
  278. dol_delete_file($exportfile);
  279. // Generate $array_selected
  280. $i=0;
  281. $array_selected=array();
  282. foreach($objexport->array_export_fields[$key] as $key => $val)
  283. {
  284. $array_selected[$key]=$i++;
  285. }
  286. //var_dump($array_selected);
  287. // Build export file
  288. $sql = "";
  289. $result=$objexport->build_file($user, $model, $datatoexport, $array_selected, array(), $sql);
  290. $expectedresult=1;
  291. $this->assertEquals($expectedresult, $result, "Call build_file() to export ".$exportfile.' failed');
  292. $result=dol_is_file($exportfile);
  293. $this->assertTrue($result, 'File '.$exportfile.' not found');
  294. }
  295. return true;
  296. }
  297. }