RepositoryTest.php 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216
  1. <?php
  2. /* Copyright (C) 2022 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/RepositoryTest.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. require_once dirname(__FILE__).'/../../htdocs/master.inc.php';
  29. require_once dirname(__FILE__).'/../../htdocs/core/lib/files.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 RepositoryTest 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. * @param string $name Name
  84. * @return RepositoryTest
  85. */
  86. public function __construct($name = '')
  87. {
  88. parent::__construct($name);
  89. //$this->sharedFixture
  90. global $conf,$user,$langs,$db;
  91. $this->savconf=$conf;
  92. $this->savuser=$user;
  93. $this->savlangs=$langs;
  94. $this->savdb=$db;
  95. print __METHOD__." db->type=".$db->type." user->id=".$user->id;
  96. //print " - db ".$db->db;
  97. print "\n";
  98. }
  99. /**
  100. * setUpBeforeClass
  101. *
  102. * @return void
  103. */
  104. public static function setUpBeforeClass(): void
  105. {
  106. global $conf,$user,$langs,$db;
  107. $db->begin(); // This is to have all actions inside a transaction even if test launched without suite.
  108. print __METHOD__."\n";
  109. }
  110. /**
  111. * tearDownAfterClass
  112. *
  113. * @return void
  114. */
  115. public static function tearDownAfterClass(): void
  116. {
  117. global $conf,$user,$langs,$db;
  118. $db->rollback();
  119. print __METHOD__."\n";
  120. }
  121. /**
  122. * Init phpunit tests
  123. *
  124. * @return void
  125. */
  126. protected function setUp(): void
  127. {
  128. global $conf,$user,$langs,$db;
  129. $conf=$this->savconf;
  130. $user=$this->savuser;
  131. $langs=$this->savlangs;
  132. $db=$this->savdb;
  133. print __METHOD__."\n";
  134. }
  135. /**
  136. * End phpunit tests
  137. *
  138. * @return void
  139. */
  140. protected function tearDown(): void
  141. {
  142. print __METHOD__."\n";
  143. }
  144. /**
  145. * testJqueryOnce
  146. *
  147. * @return void
  148. *
  149. * The depends says test is run only if previous is ok
  150. */
  151. public function testJqueryOnce()
  152. {
  153. global $conf,$user,$langs,$db;
  154. // Scan dir to guarante we don't have library jquery twice
  155. $foundfiles=dol_dir_list(DOL_DOCUMENT_ROOT.'/includes/', 'files', 1, '^jquery\.js', array('ckeditor'));
  156. print __METHOD__." count(founddirs)=".count($foundfiles)."\n";
  157. $this->assertEquals(1, count($foundfiles), 'We found jquery lib (jquery.js) twice');
  158. // Scan dir to guarante we don't have library jquery twice
  159. $foundfiles=dol_dir_list(DOL_DOCUMENT_ROOT.'/includes/', 'files', 1, '^jquery\.min\.js', array('ckeditor'));
  160. print __METHOD__." count(founddirs)=".count($foundfiles)."\n";
  161. $this->assertEquals(1, count($foundfiles), 'We found jquery lib (jquery.min.js) twice '.(empty($foundfiles[0]) ? '' : $foundfiles[0]['fullname']).' '.(empty($foundfiles[1]) ? '' : $foundfiles[1]['fullname']));
  162. }
  163. /**
  164. * testRepository
  165. *
  166. * @return string
  167. */
  168. public function testRepository()
  169. {
  170. global $conf,$user,$langs,$db;
  171. $conf=$this->savconf;
  172. $user=$this->savuser;
  173. $langs=$this->savlangs;
  174. $db=$this->savdb;
  175. include_once DOL_DOCUMENT_ROOT.'/core/lib/files.lib.php';
  176. $filesarray = dol_dir_list(DOL_DOCUMENT_ROOT, 'directories', 1, '', array('\/custom\/'), 'fullname', SORT_ASC, 0, 1, '', 1);
  177. //$filesarray = dol_dir_list(DOL_DOCUMENT_ROOT, 'files', 1, '\.php', null, 'fullname');
  178. $ok = true;
  179. foreach ($filesarray as $key => $file) {
  180. if (preg_match('/\/vendor\//', $file['fullname'])) {
  181. if (!preg_match('/php-imap/', $file['fullname'])) { // we accept 'vendor' dir for php-imap (not find how to do it easily without)
  182. print "Found a vendor dir into ".$file['fullname']."\n";
  183. $ok = false;
  184. }
  185. }
  186. }
  187. $this->assertTrue($ok, 'Pb in list of files of repository');
  188. return '';
  189. }
  190. }