Website.class.php 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178
  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 <https://www.gnu.org/licenses/>.
  16. * or see https://www.gnu.org/
  17. */
  18. /**
  19. * \file test/phpunit/WebsiteTest.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. if (! defined('NOREQUIRESOC')) {
  28. define('NOREQUIRESOC', '1');
  29. }
  30. if (! defined('NOCSRFCHECK')) {
  31. define('NOCSRFCHECK', '1');
  32. }
  33. if (! defined('NOTOKENRENEWAL')) {
  34. define('NOTOKENRENEWAL', '1');
  35. }
  36. if (! defined('NOREQUIREMENU')) {
  37. define('NOREQUIREMENU', '1'); // If there is no menu to show
  38. }
  39. if (! defined('NOREQUIREHTML')) {
  40. define('NOREQUIREHTML', '1'); // If we don't need to load the html.form.class.php
  41. }
  42. if (! defined('NOREQUIREAJAX')) {
  43. define('NOREQUIREAJAX', '1');
  44. }
  45. if (! defined("NOLOGIN")) {
  46. define("NOLOGIN", '1'); // If this page is public (can be called outside logged session)
  47. }
  48. if (! defined("NOSESSION")) {
  49. define("NOSESSION", '1');
  50. }
  51. require_once dirname(__FILE__).'/../../htdocs/main.inc.php';
  52. require_once dirname(__FILE__).'/../../htdocs/core/lib/website.lib.php';
  53. if (empty($user->id)) {
  54. print "Load permissions for admin user nb 1\n";
  55. $user->fetch(1);
  56. $user->getrights();
  57. }
  58. $conf->global->MAIN_DISABLE_ALL_MAILS=1;
  59. /**
  60. * Class for PHPUnit tests
  61. *
  62. * @backupGlobals disabled
  63. * @backupStaticAttributes enabled
  64. * @remarks backupGlobals must be disabled to have db,conf,user and lang not erased.
  65. */
  66. class WebsiteTest extends PHPUnit\Framework\TestCase
  67. {
  68. protected $savconf;
  69. protected $savuser;
  70. protected $savlangs;
  71. protected $savdb;
  72. /**
  73. * Constructor
  74. * We save global variables into local variables
  75. *
  76. * @return SecurityTest
  77. */
  78. public function __construct()
  79. {
  80. parent::__construct();
  81. //$this->sharedFixture
  82. global $conf,$user,$langs,$db;
  83. $this->savconf=$conf;
  84. $this->savuser=$user;
  85. $this->savlangs=$langs;
  86. $this->savdb=$db;
  87. print __METHOD__." db->type=".$db->type." user->id=".$user->id;
  88. //print " - db ".$db->db;
  89. print "\n";
  90. }
  91. /**
  92. * setUpBeforeClass
  93. *
  94. * @return void
  95. */
  96. public static function setUpBeforeClass()
  97. {
  98. global $conf,$user,$langs,$db;
  99. $db->begin(); // This is to have all actions inside a transaction even if test launched without suite.
  100. print __METHOD__."\n";
  101. }
  102. /**
  103. * tearDownAfterClass
  104. *
  105. * @return void
  106. */
  107. public static function tearDownAfterClass()
  108. {
  109. global $conf,$user,$langs,$db;
  110. $db->rollback();
  111. print __METHOD__."\n";
  112. }
  113. /**
  114. * Init phpunit tests
  115. *
  116. * @return void
  117. */
  118. protected function setUp()
  119. {
  120. global $conf,$user,$langs,$db;
  121. $conf=$this->savconf;
  122. $user=$this->savuser;
  123. $langs=$this->savlangs;
  124. $db=$this->savdb;
  125. print __METHOD__."\n";
  126. }
  127. /**
  128. * End phpunit tests
  129. *
  130. * @return void
  131. */
  132. protected function tearDown()
  133. {
  134. print __METHOD__."\n";
  135. }
  136. /**
  137. * testGetPagesFromSearchCriterias
  138. *
  139. * @return void
  140. */
  141. public function testGetPagesFromSearchCriterias()
  142. {
  143. global $db;
  144. $s = "123') OR 1=1-- \' xxx";
  145. /*
  146. var_dump($s);
  147. var_dump($db->escapeforlike($s));
  148. var_dump($db->escape($db->escapeforlike($s)));
  149. */
  150. $res = getPagesFromSearchCriterias('page,blogpost', 'meta,content', $s, 2, 'date_creation', 'DESC', 'en');
  151. //var_dump($res);
  152. print __METHOD__." message=".$res['code']."\n";
  153. // 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
  154. $this->assertEquals($res['code'], 'KO');
  155. }
  156. }