ExportTest.php 12 KB

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