ImagesLibTest.php 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182
  1. <?php
  2. /* Copyright (C) 2010-2012 Laurent Destailleur <eldy@users.sourceforge.net>
  3. * Copyright (C) 2012 Regis Houssin <regis.houssin@inodbox.com>
  4. * Copyright (C) 2023 Alexandre Janniaux <alexandre.janniaux@gmail.com>
  5. *
  6. * This program is free software; you can redistribute it and/or modify
  7. * it under the terms of the GNU General Public License as published by
  8. * the Free Software Foundation; either version 3 of the License, or
  9. * (at your option) any later version.
  10. *
  11. * This program is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. * GNU General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU General Public License
  17. * along with this program. If not, see <https://www.gnu.org/licenses/>.
  18. * or see https://www.gnu.org/
  19. */
  20. /**
  21. * \file test/phpunit/ImagesLibTest.php
  22. * \ingroup test
  23. * \brief PHPUnit test
  24. * \remarks To run this script as CLI: phpunit filename.php
  25. */
  26. global $conf,$user,$langs,$db;
  27. //define('TEST_DB_FORCE_TYPE','mysql'); // This is to force using mysql driver
  28. //require_once 'PHPUnit/Autoload.php';
  29. require_once dirname(__FILE__).'/../../htdocs/master.inc.php';
  30. require_once dirname(__FILE__).'/../../htdocs/core/lib/images.lib.php';
  31. require_once dirname(__FILE__).'/../../htdocs/core/lib/files.lib.php';
  32. if (empty($user->id)) {
  33. print "Load permissions for admin user nb 1\n";
  34. $user->fetch(1);
  35. $user->getrights();
  36. }
  37. $conf->global->MAIN_DISABLE_ALL_MAILS=1;
  38. /**
  39. * Class for PHPUnit tests
  40. *
  41. * @backupGlobals disabled
  42. * @backupStaticAttributes enabled
  43. * @remarks backupGlobals must be disabled to have db,conf,user and lang not erased.
  44. */
  45. class ImagesLibTest extends PHPUnit\Framework\TestCase
  46. {
  47. protected $savconf;
  48. protected $savuser;
  49. protected $savlangs;
  50. protected $savdb;
  51. /**
  52. * Constructor
  53. * We save global variables into local variables
  54. *
  55. * @param string $name Name
  56. * @return ImagesLibTest
  57. */
  58. public function __construct($name = '')
  59. {
  60. parent::__construct($name);
  61. //$this->sharedFixture
  62. global $conf,$user,$langs,$db;
  63. $this->savconf=$conf;
  64. $this->savuser=$user;
  65. $this->savlangs=$langs;
  66. $this->savdb=$db;
  67. print __METHOD__." db->type=".$db->type." user->id=".$user->id;
  68. //print " - db ".$db->db;
  69. print "\n";
  70. }
  71. /**
  72. * setUpBeforeClass
  73. *
  74. * @return void
  75. */
  76. public static function setUpBeforeClass(): void
  77. {
  78. global $conf,$user,$langs,$db;
  79. $db->begin(); // This is to have all actions inside a transaction even if test launched without suite.
  80. print __METHOD__."\n";
  81. }
  82. /**
  83. * tearDownAfterClass
  84. *
  85. * @return void
  86. */
  87. public static function tearDownAfterClass(): void
  88. {
  89. global $conf,$user,$langs,$db;
  90. $db->rollback();
  91. print __METHOD__."\n";
  92. }
  93. /**
  94. * Init phpunit tests
  95. *
  96. * @return void
  97. */
  98. protected function setUp(): void
  99. {
  100. global $conf,$user,$langs,$db;
  101. $conf=$this->savconf;
  102. $user=$this->savuser;
  103. $langs=$this->savlangs;
  104. $db=$this->savdb;
  105. print __METHOD__."\n";
  106. }
  107. /**
  108. * End phpunit tests
  109. *
  110. * @return void
  111. */
  112. protected function tearDown(): void
  113. {
  114. print __METHOD__."\n";
  115. }
  116. /**
  117. * testDolCountNbOfLine
  118. *
  119. * @return int
  120. */
  121. public function testgetImageSize()
  122. {
  123. $file=dirname(__FILE__).'/img250x50.jpg';
  124. $tmp=dol_getImageSize($file);
  125. print __METHOD__." result=".$tmp['width'].'/'.$tmp['height']."\n";
  126. $this->assertEquals($tmp['width'], 250);
  127. $this->assertEquals($tmp['height'], 50);
  128. $file=dirname(__FILE__).'/img250x20.png';
  129. $tmp=dol_getImageSize($file);
  130. print __METHOD__." result=".$tmp['width'].'/'.$tmp['height']."\n";
  131. $this->assertEquals($tmp['width'], 250);
  132. $this->assertEquals($tmp['height'], 20);
  133. /*$file=dirname(__FILE__).'/filenotfound.png';
  134. $tmp=dol_getImageSize($file);
  135. print __METHOD__." result=".$tmp['width'].'/'.$tmp['height']."\n";
  136. $this->assertEquals($tmp['width'],250);
  137. $this->assertEquals($tmp['height'],20);*/
  138. return 1;
  139. }
  140. /**
  141. * testDolImageResizeOrCrop
  142. *
  143. * @return int
  144. */
  145. public function testDolImageResizeOrCrop()
  146. {
  147. global $conf;
  148. $file=dirname(__FILE__).'/img250x20.png';
  149. $filetarget=$conf->admin->dir_temp.'/img250x20.jpg';
  150. dol_delete_file($filetarget);
  151. $result = dol_imageResizeOrCrop($file, 0, 0, 0, 0, 0, $filetarget);
  152. print __METHOD__." result=".$result."\n";
  153. $this->assertEquals($filetarget, $result, 'Failed to convert PNG '.$file.' into '.$filetarget);
  154. /*$file=dirname(__FILE__).'/img250x20.png';
  155. $filetarget=$conf->admin->dir_temp.'/img250x20.webp';
  156. dol_delete_file($filetarget);
  157. $result = dol_imageResizeOrCrop($file, 0, 0, 0, 0, 0, $filetarget);
  158. print __METHOD__." result=".$result."\n";
  159. $this->assertEquals($filetarget, $result, 'Failed to convert PNG '.$file.' into WEBP '.$filetarget);*/
  160. }
  161. }