WebsiteTest.php 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252
  1. <?php
  2. /* Copyright (C) 2010 Laurent Destailleur <eldy@users.sourceforge.net>
  3. * Copyright (C) 2023 Alexandre Janniaux <alexandre.janniaux@gmail.com>
  4. *
  5. * This program is free software; you can redistribute it and/or modify
  6. * it under the terms of the GNU General Public License as published by
  7. * the Free Software Foundation; either version 3 of the License, or
  8. * (at your option) any later version.
  9. *
  10. * This program is distributed in the hope that it will be useful,
  11. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. * GNU General Public License for more details.
  14. *
  15. * You should have received a copy of the GNU General Public License
  16. * along with this program. If not, see <https://www.gnu.org/licenses/>.
  17. * or see https://www.gnu.org/
  18. */
  19. /**
  20. * \file test/phpunit/WebsiteTest.php
  21. * \ingroup test
  22. * \brief PHPUnit test
  23. * \remarks To run this script as CLI: phpunit filename.php
  24. */
  25. global $conf,$user,$langs,$db;
  26. //define('TEST_DB_FORCE_TYPE','mysql'); // This is to force using mysql driver
  27. //require_once 'PHPUnit/Autoload.php';
  28. if (! defined('NOREQUIRESOC')) {
  29. define('NOREQUIRESOC', '1');
  30. }
  31. if (! defined('NOCSRFCHECK')) {
  32. define('NOCSRFCHECK', '1');
  33. }
  34. if (! defined('NOTOKENRENEWAL')) {
  35. define('NOTOKENRENEWAL', '1');
  36. }
  37. if (! defined('NOREQUIREMENU')) {
  38. define('NOREQUIREMENU', '1'); // If there is no menu to show
  39. }
  40. if (! defined('NOREQUIREHTML')) {
  41. define('NOREQUIREHTML', '1'); // If we don't need to load the html.form.class.php
  42. }
  43. if (! defined('NOREQUIREAJAX')) {
  44. define('NOREQUIREAJAX', '1');
  45. }
  46. if (! defined("NOLOGIN")) {
  47. define("NOLOGIN", '1'); // If this page is public (can be called outside logged session)
  48. }
  49. if (! defined("NOSESSION")) {
  50. define("NOSESSION", '1');
  51. }
  52. require_once dirname(__FILE__).'/../../htdocs/main.inc.php';
  53. require_once dirname(__FILE__).'/../../htdocs/core/lib/website.lib.php';
  54. require_once dirname(__FILE__).'/../../htdocs/core/lib/website2.lib.php';
  55. require_once dirname(__FILE__).'/../../htdocs/website/class/website.class.php';
  56. if (empty($user->id)) {
  57. print "Load permissions for admin user nb 1\n";
  58. $user->fetch(1);
  59. $user->getrights();
  60. if (empty($user->rights->website)) {
  61. $user->rights->website = new stdClass();
  62. }
  63. }
  64. $conf->global->MAIN_DISABLE_ALL_MAILS=1;
  65. /**
  66. * Class for PHPUnit tests
  67. *
  68. * @backupGlobals disabled
  69. * @backupStaticAttributes enabled
  70. * @remarks backupGlobals must be disabled to have db,conf,user and lang not erased.
  71. */
  72. class WebsiteTest extends PHPUnit\Framework\TestCase
  73. {
  74. protected $savconf;
  75. protected $savuser;
  76. protected $savlangs;
  77. protected $savdb;
  78. /**
  79. * Constructor
  80. * We save global variables into local variables
  81. *
  82. * @param string $name Name
  83. * @return WebsiteTest
  84. */
  85. public function __construct($name = '')
  86. {
  87. parent::__construct($name);
  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. * testGetPagesFromSearchCriterias
  145. *
  146. * @return void
  147. */
  148. public function testGetPagesFromSearchCriterias()
  149. {
  150. global $db, $website;
  151. $website = new Website($db); // $website must be defined globally for getPagesFromSearchCriterias()
  152. $s = "123') OR 1=1-- \' xxx";
  153. /*
  154. var_dump($s);
  155. var_dump($db->escapeforlike($s));
  156. var_dump($db->escape($db->escapeforlike($s)));
  157. */
  158. $res = getPagesFromSearchCriterias('page,blogpost', 'meta,content', $s, 2, 'date_creation', 'DESC', 'en');
  159. //var_dump($res);
  160. print __METHOD__." message=".$res['code']."\n";
  161. // We must found no line (so code should be KO). If we found somethiing, it means there is a SQL injection of the 1=1
  162. $this->assertEquals($res['code'], 'KO');
  163. }
  164. /**
  165. * testDolStripPhpCode
  166. *
  167. * @return void
  168. */
  169. public function testDolStripPhpCode()
  170. {
  171. global $db;
  172. $s = "abc\n<?php echo 'def'\n// comment\n ?>ghi";
  173. $result = dolStripPhpCode($s);
  174. $this->assertEquals("abc\n<span phptag></span>ghi", $result);
  175. $s = "abc\n<?PHP echo 'def'\n// comment\n ?>ghi";
  176. $result = dolStripPhpCode($s);
  177. $this->assertEquals("abc\n<span phptag></span>ghi", $result);
  178. }
  179. /**
  180. * testCheckPHPCode
  181. *
  182. * @return void
  183. */
  184. public function testCheckPHPCode()
  185. {
  186. global $user;
  187. // Force permission so this is not the permission that will affect result of checkPHPCode
  188. $user->rights->website->writephp = 1;
  189. $s = '<?php exec("eee"); ?>';
  190. $result = checkPHPCode('', $s);
  191. print __METHOD__." result checkPHPCode=".$result."\n";
  192. $this->assertEquals($result, 1, 'checkPHPCode did not detect the string was dangerous');
  193. $s = '<?php $_="{"; $_=($_^"<").($_^">;").($_^"/"); ?><?=${\'_\'.$_}["_"](${\'_\'.$_}["__"]);?>';
  194. $result = checkPHPCode('', $s);
  195. print __METHOD__." result checkPHPCode=".$result."\n";
  196. $this->assertEquals($result, 1, 'checkPHPCode did not detect the string was dangerous');
  197. }
  198. /**
  199. * testDolKeepOnlyPhpCode
  200. *
  201. * @return void
  202. */
  203. public function testDolKeepOnlyPhpCode()
  204. {
  205. $s = 'HTML content <?php exec("eee"); ?> and more HTML content';
  206. $result = dolKeepOnlyPhpCode($s);
  207. print __METHOD__." result dolKeepOnlyPhpCode=".$result."\n";
  208. $this->assertEquals('<?php exec("eee"); ?>', $result, 'dolKeepOnlyPhpCode did extract the correct string');
  209. $s = 'HTML content <? exec("eee"); ?> and more HTML content';
  210. $result = dolKeepOnlyPhpCode($s);
  211. print __METHOD__." result dolKeepOnlyPhpCode=".$result."\n";
  212. $this->assertEquals('<?php exec("eee"); ?>', $result, 'dolKeepOnlyPhpCode did extract the correct string');
  213. $s = 'HTML content <?php test() <?php test2(); ?> and more HTML content';
  214. $result = dolKeepOnlyPhpCode($s);
  215. print __METHOD__." result dolKeepOnlyPhpCode=".$result."\n";
  216. $this->assertEquals('<?php test() ?><?php test2(); ?>', $result, 'dolKeepOnlyPhpCode did extract the correct string');
  217. }
  218. }