CodingSqlTest.php 9.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332
  1. <?php
  2. /* Copyright (C) 2013 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/CodingSqlTest.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/core/lib/security.lib.php';
  29. require_once dirname(__FILE__).'/../../htdocs/core/lib/security2.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. if (empty($user->id)) {
  61. print "Load permissions for admin user nb 1\n";
  62. $user->fetch(1);
  63. $user->getrights();
  64. }
  65. $conf->global->MAIN_DISABLE_ALL_MAILS=1;
  66. /**
  67. * Class for PHPUnit tests
  68. *
  69. * @backupGlobals disabled
  70. * @backupStaticAttributes enabled
  71. * @remarks backupGlobals must be disabled to have db,conf,user and lang not erased.
  72. */
  73. class CodingSqlTest extends PHPUnit\Framework\TestCase
  74. {
  75. protected $savconf;
  76. protected $savuser;
  77. protected $savlangs;
  78. protected $savdb;
  79. /**
  80. * Constructor
  81. * We save global variables into local variables
  82. *
  83. * @return SecurityTest
  84. */
  85. public function __construct()
  86. {
  87. parent::__construct();
  88. //$this->sharedFixture
  89. global $conf,$user,$langs,$db;
  90. $this->savconf=$conf;
  91. $this->savuser=$user;
  92. $this->savlangs=$langs;
  93. $this->savdb=$db;
  94. print __METHOD__." db->type=".$db->type." user->id=".$user->id;
  95. //print " - db ".$db->db;
  96. print "\n";
  97. }
  98. /**
  99. * setUpBeforeClass
  100. *
  101. * @return void
  102. */
  103. public static function setUpBeforeClass(): void
  104. {
  105. global $conf,$user,$langs,$db;
  106. $db->begin(); // This is to have all actions inside a transaction even if test launched without suite.
  107. print __METHOD__."\n";
  108. }
  109. /**
  110. * tearDownAfterClass
  111. *
  112. * @return void
  113. */
  114. public static function tearDownAfterClass(): void
  115. {
  116. global $conf,$user,$langs,$db;
  117. $db->rollback();
  118. print __METHOD__."\n";
  119. }
  120. /**
  121. * Init phpunit tests
  122. *
  123. * @return void
  124. */
  125. protected function setUp(): void
  126. {
  127. global $conf,$user,$langs,$db;
  128. $conf=$this->savconf;
  129. $user=$this->savuser;
  130. $langs=$this->savlangs;
  131. $db=$this->savdb;
  132. print __METHOD__."\n";
  133. }
  134. /**
  135. * End phpunit tests
  136. *
  137. * @return void
  138. */
  139. protected function tearDown(): void
  140. {
  141. print __METHOD__."\n";
  142. }
  143. /**
  144. * testEscape
  145. *
  146. * @return string
  147. */
  148. public function testEscape()
  149. {
  150. global $conf,$user,$langs,$db;
  151. $conf=$this->savconf;
  152. $user=$this->savuser;
  153. $langs=$this->savlangs;
  154. $db=$this->savdb;
  155. if ($db->type == 'mysqli') {
  156. $a = 'abc"\'def'; // string is abc"'def
  157. print $a;
  158. $result = $db->escape($a); // $result must be abc\"\'def
  159. $this->assertEquals('abc\"\\\'def', $result);
  160. }
  161. if ($db->type == 'pgsql') {
  162. $a = 'abc"\'def'; // string is abc"'def
  163. print $a;
  164. $result = $db->escape($a); // $result must be abc"''def
  165. $this->assertEquals('abc"\'\'def', $result);
  166. }
  167. }
  168. /**
  169. * testEscapeForLike
  170. *
  171. * @return string
  172. */
  173. public function testEscapeForLike()
  174. {
  175. global $conf,$user,$langs,$db;
  176. $conf=$this->savconf;
  177. $user=$this->savuser;
  178. $langs=$this->savlangs;
  179. $db=$this->savdb;
  180. $a = 'abc"\'def_ghi%klm\\nop';
  181. //print $a;
  182. $result = $db->escapeforlike($a); // $result must be abc"'def\_ghi\%klm\\nop with mysql
  183. $this->assertEquals('abc"\'def\_ghi\%klm\\\\nop', $result);
  184. }
  185. /**
  186. * testSql
  187. *
  188. * @return string
  189. */
  190. public function testSql()
  191. {
  192. global $conf,$user,$langs,$db;
  193. $conf=$this->savconf;
  194. $user=$this->savuser;
  195. $langs=$this->savlangs;
  196. $db=$this->savdb;
  197. $listofsqldir = array(DOL_DOCUMENT_ROOT.'/install/mysql/data', DOL_DOCUMENT_ROOT.'/install/mysql/tables', DOL_DOCUMENT_ROOT.'/install/mysql/migration');
  198. foreach ($listofsqldir as $dir) {
  199. print 'Process dir '.$dir."\n";
  200. $filesarray = scandir($dir);
  201. foreach ($filesarray as $key => $file) {
  202. if (! preg_match('/\.sql$/', $file)) {
  203. continue;
  204. }
  205. print 'Check sql file '.$file."\n";
  206. $filecontent = file_get_contents($dir.'/'.$file);
  207. // Allow ` for 'rank' column name
  208. $filecontent = str_replace('`rank`', '_rank_', $filecontent);
  209. $result=strpos($filecontent, '`');
  210. //print __METHOD__." Result for checking we don't have back quote = ".$result."\n";
  211. $this->assertTrue($result===false, 'Found back quote into '.$file.'. Bad.');
  212. $result=strpos($filecontent, '"');
  213. if ($result) {
  214. $result=(! strpos($filecontent, '["') && ! strpos($filecontent, '{"') && ! strpos($filecontent, '("'));
  215. }
  216. //print __METHOD__." Result for checking we don't have double quote = ".$result."\n";
  217. $this->assertTrue($result===false, 'Found double quote that is not [" neither {" (used for json content) neither (" (used for content with string like isModEnabled("")) into '.$file.'. Bad.');
  218. $result=strpos($filecontent, 'int(');
  219. //print __METHOD__." Result for checking we don't have 'int(' instead of 'integer' = ".$result."\n";
  220. $this->assertTrue($result===false, 'Found int(x) or tinyint(x) instead of integer or tinyint into '.$file.'. Bad.');
  221. $result=strpos($filecontent, 'ON DELETE CASCADE');
  222. //print __METHOD__." Result for checking we don't have 'ON DELETE CASCADE' = ".$result."\n";
  223. $this->assertTrue($result===false, 'Found ON DELETE CASCADE into '.$file.'. Bad.');
  224. $result=strpos($filecontent, 'NUMERIC(');
  225. //print __METHOD__." Result for checking we don't have 'NUMERIC(' = ".$result."\n";
  226. $this->assertTrue($result===false, 'Found NUMERIC( into '.$file.'. Bad.');
  227. $result=strpos($filecontent, 'NUMERIC(');
  228. //print __METHOD__." Result for checking we don't have 'curdate(' = ".$result."\n";
  229. $this->assertTrue($result===false, 'Found curdate( into '.$file.'. Bad. Current date must be generated with PHP.');
  230. $result=strpos($filecontent, 'integer(');
  231. //print __METHOD__." Result for checking we don't have 'integer(' = ".$result."\n";
  232. $this->assertTrue($result===false, 'Found value in parenthesis after the integer. It must be integer not integer(x) into '.$file.'. Bad.');
  233. $result=strpos($filecontent, 'timestamp,');
  234. //print __METHOD__." Result for checking we don't have 'NUMERIC(' = ".$result."\n";
  235. $this->assertTrue($result===false, 'Found type timestamp with option DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP after into '.$file.'. Bad.');
  236. if ($dir == DOL_DOCUMENT_ROOT.'/install/mysql/migration') {
  237. // Test for migration files only
  238. } elseif ($dir == DOL_DOCUMENT_ROOT.'/install/mysql/data') {
  239. // Test for data files only
  240. } else {
  241. if (preg_match('/\.key\.sql$/', $file)) {
  242. // Test for key files only
  243. } else {
  244. // Test for non key files only
  245. $result=(strpos($filecontent, 'KEY ') && strpos($filecontent, 'PRIMARY KEY') == 0);
  246. //print __METHOD__." Result for checking we don't have ' KEY ' instead of a sql file to create index = ".$result."\n";
  247. $this->assertTrue($result===false, 'Found KEY into '.$file.'. Bad.');
  248. $result=stripos($filecontent, 'ENGINE=innodb');
  249. //print __METHOD__." Result for checking we have the ENGINE=innodb string = ".$result."\n";
  250. $this->assertGreaterThan(0, $result, 'The ENGINE=innodb was not found into '.$file.'. Add it or just fix syntax to match case.');
  251. }
  252. }
  253. }
  254. }
  255. return;
  256. }
  257. /**
  258. * testInitData
  259. *
  260. * @return string
  261. */
  262. public function testInitData()
  263. {
  264. global $conf,$user,$langs,$db;
  265. $conf=$this->savconf;
  266. $user=$this->savuser;
  267. $langs=$this->savlangs;
  268. $db=$this->savdb;
  269. $filesarray = scandir(DOL_DOCUMENT_ROOT.'/../dev/initdemo');
  270. foreach ($filesarray as $key => $file) {
  271. if (! preg_match('/\.sql$/', $file)) {
  272. continue;
  273. }
  274. print 'Check sql file '.$file."\n";
  275. $filecontent=file_get_contents(DOL_DOCUMENT_ROOT.'/../dev/initdemo/'.$file);
  276. $result=strpos($filecontent, '@gmail.com');
  277. print __METHOD__." Result for checking we don't have personal data = ".$result."\n";
  278. $this->assertTrue($result===false, 'Found a bad key @gmail into file '.$file);
  279. $result=strpos($filecontent, 'eldy@');
  280. print __METHOD__." Result for checking we don't have personal data = ".$result."\n";
  281. $this->assertTrue($result===false, 'Found a bad key eldy@ into file '.$file);
  282. $result=strpos($filecontent, 'INSERT INTO `llx_oauth_token`');
  283. print __METHOD__." Result for checking we don't have data into llx_oauth_token = ".$result."\n";
  284. $this->assertTrue($result===false, 'Found a non expected insert into file '.$file);
  285. }
  286. return;
  287. }
  288. }