CodingSqlTest.php 8.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282
  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()
  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()
  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()
  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()
  140. {
  141. print __METHOD__."\n";
  142. }
  143. /**
  144. * testSql
  145. *
  146. * @return string
  147. */
  148. public function testSql()
  149. {
  150. global $conf,$user,$langs,$db;
  151. $conf=$this->savconf;
  152. $user=$this->savuser;
  153. $langs=$this->savlangs;
  154. $db=$this->savdb;
  155. $listofsqldir = array(DOL_DOCUMENT_ROOT.'/install/mysql/data', DOL_DOCUMENT_ROOT.'/install/mysql/tables', DOL_DOCUMENT_ROOT.'/install/mysql/migration');
  156. foreach ($listofsqldir as $dir) {
  157. print 'Process dir '.$dir."\n";
  158. $filesarray = scandir($dir);
  159. foreach ($filesarray as $key => $file) {
  160. if (! preg_match('/\.sql$/', $file)) {
  161. continue;
  162. }
  163. print 'Check sql file '.$file."\n";
  164. $filecontent = file_get_contents($dir.'/'.$file);
  165. // Allow ` for 'rank' column name
  166. $filecontent = str_replace('`rank`', '_rank_', $filecontent);
  167. $result=strpos($filecontent, '`');
  168. //print __METHOD__." Result for checking we don't have back quote = ".$result."\n";
  169. $this->assertTrue($result===false, 'Found back quote into '.$file.'. Bad.');
  170. $result=strpos($filecontent, '"');
  171. if ($result) {
  172. $result=(! strpos($filecontent, '["') && ! strpos($filecontent, '{"'));
  173. }
  174. //print __METHOD__." Result for checking we don't have double quote = ".$result."\n";
  175. $this->assertTrue($result===false, 'Found double quote that is not [" neither {" (used for json content) into '.$file.'. Bad.');
  176. $result=strpos($filecontent, 'int(');
  177. //print __METHOD__." Result for checking we don't have 'int(' instead of 'integer' = ".$result."\n";
  178. $this->assertTrue($result===false, 'Found int(x) or tinyint(x) instead of integer or tinyint into '.$file.'. Bad.');
  179. $result=strpos($filecontent, 'ON DELETE CASCADE');
  180. //print __METHOD__." Result for checking we don't have 'ON DELETE CASCADE' = ".$result."\n";
  181. $this->assertTrue($result===false, 'Found ON DELETE CASCADE into '.$file.'. Bad.');
  182. $result=strpos($filecontent, 'NUMERIC(');
  183. //print __METHOD__." Result for checking we don't have 'NUMERIC(' = ".$result."\n";
  184. $this->assertTrue($result===false, 'Found NUMERIC( into '.$file.'. Bad.');
  185. $result=strpos($filecontent, 'NUMERIC(');
  186. //print __METHOD__." Result for checking we don't have 'curdate(' = ".$result."\n";
  187. $this->assertTrue($result===false, 'Found curdate( into '.$file.'. Bad. Current date must be generated with PHP.');
  188. $result=strpos($filecontent, 'integer(');
  189. //print __METHOD__." Result for checking we don't have 'integer(' = ".$result."\n";
  190. $this->assertTrue($result===false, 'Found value in parenthesis after the integer. It must be integer not integer(x) into '.$file.'. Bad.');
  191. $result=strpos($filecontent, 'timestamp,');
  192. //print __METHOD__." Result for checking we don't have 'NUMERIC(' = ".$result."\n";
  193. $this->assertTrue($result===false, 'Found type timestamp with option DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP after into '.$file.'. Bad.');
  194. if ($dir == DOL_DOCUMENT_ROOT.'/install/mysql/migration') {
  195. // Test for migration files only
  196. } elseif ($dir == DOL_DOCUMENT_ROOT.'/install/mysql/data') {
  197. // Test for data files only
  198. } else {
  199. if (preg_match('/\.key\.sql$/', $file)) {
  200. // Test for key files only
  201. } else {
  202. // Test for non key files only
  203. $result=(strpos($filecontent, 'KEY ') && strpos($filecontent, 'PRIMARY KEY') == 0);
  204. //print __METHOD__." Result for checking we don't have ' KEY ' instead of a sql file to create index = ".$result."\n";
  205. $this->assertTrue($result===false, 'Found KEY into '.$file.'. Bad.');
  206. $result=stripos($filecontent, 'ENGINE=innodb');
  207. //print __METHOD__." Result for checking we have the ENGINE=innodb string = ".$result."\n";
  208. $this->assertGreaterThan(0, $result, 'The ENGINE=innodb was not found into '.$file.'. Add it or just fix syntax to match case.');
  209. }
  210. }
  211. }
  212. }
  213. return;
  214. }
  215. /**
  216. * testInitData
  217. *
  218. * @return string
  219. */
  220. public function testInitData()
  221. {
  222. global $conf,$user,$langs,$db;
  223. $conf=$this->savconf;
  224. $user=$this->savuser;
  225. $langs=$this->savlangs;
  226. $db=$this->savdb;
  227. $filesarray = scandir(DOL_DOCUMENT_ROOT.'/../dev/initdemo');
  228. foreach ($filesarray as $key => $file) {
  229. if (! preg_match('/\.sql$/', $file)) {
  230. continue;
  231. }
  232. print 'Check sql file '.$file."\n";
  233. $filecontent=file_get_contents(DOL_DOCUMENT_ROOT.'/../dev/initdemo/'.$file);
  234. $result=strpos($filecontent, '@gmail.com');
  235. print __METHOD__." Result for checking we don't have personal data = ".$result."\n";
  236. $this->assertTrue($result===false, 'Found a bad key @gmail into file '.$file);
  237. $result=strpos($filecontent, 'eldy@');
  238. print __METHOD__." Result for checking we don't have personal data = ".$result."\n";
  239. $this->assertTrue($result===false, 'Found a bad key eldy@ into file '.$file);
  240. }
  241. return;
  242. }
  243. }