PdfDocTest.php 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188
  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/PdfDocTest.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/compta/facture/class/facture.class.php';
  30. require_once dirname(__FILE__).'/../../htdocs/product/class/product.class.php';
  31. require_once dirname(__FILE__).'/../../htdocs/core/lib/pdf.lib.php';
  32. require_once dirname(__FILE__).'/../../htdocs/core/lib/doc.lib.php';
  33. if (empty($user->id)) {
  34. print "Load permissions for admin user nb 1\n";
  35. $user->fetch(1);
  36. $user->getrights();
  37. }
  38. $conf->global->MAIN_DISABLE_ALL_MAILS=1;
  39. /**
  40. * Class for PHPUnit tests
  41. *
  42. * @backupGlobals disabled
  43. * @backupStaticAttributes enabled
  44. * @remarks backupGlobals must be disabled to have db,conf,user and lang not erased.
  45. */
  46. class PdfDocTest extends PHPUnit\Framework\TestCase
  47. {
  48. protected $savconf;
  49. protected $savuser;
  50. protected $savlangs;
  51. protected $savdb;
  52. /**
  53. * Constructor
  54. * We save global variables into local variables
  55. *
  56. * @param string $name Name
  57. * @return PdfDocTest
  58. */
  59. public function __construct($name = '')
  60. {
  61. parent::__construct($name);
  62. //$this->sharedFixture
  63. global $conf,$user,$langs,$db;
  64. $this->savconf=$conf;
  65. $this->savuser=$user;
  66. $this->savlangs=$langs;
  67. $this->savdb=$db;
  68. print __METHOD__." db->type=".$db->type." user->id=".$user->id;
  69. //print " - db ".$db->db;
  70. print "\n";
  71. }
  72. /**
  73. * setUpBeforeClass
  74. *
  75. * @return void
  76. */
  77. public static function setUpBeforeClass(): void
  78. {
  79. global $conf,$user,$langs,$db;
  80. $db->begin(); // This is to have all actions inside a transaction even if test launched without suite.
  81. print __METHOD__."\n";
  82. }
  83. /**
  84. * tearDownAfterClass
  85. *
  86. * @return void
  87. */
  88. public static function tearDownAfterClass(): void
  89. {
  90. global $conf,$user,$langs,$db;
  91. $db->rollback();
  92. print __METHOD__."\n";
  93. }
  94. /**
  95. * Init phpunit tests
  96. *
  97. * @return void
  98. */
  99. protected function setUp(): void
  100. {
  101. global $conf,$user,$langs,$db;
  102. $conf=$this->savconf;
  103. $user=$this->savuser;
  104. $langs=$this->savlangs;
  105. $db=$this->savdb;
  106. print __METHOD__."\n";
  107. }
  108. /**
  109. * End phpunit tests
  110. *
  111. * @return void
  112. */
  113. protected function tearDown(): void
  114. {
  115. print __METHOD__."\n";
  116. }
  117. /**
  118. * testPdfDocGetLineDesc
  119. *
  120. * @return void
  121. */
  122. public function testPdfDocGetLineDesc()
  123. {
  124. global $conf,$user,$langs,$db;
  125. $conf=$this->savconf;
  126. $user=$this->savuser;
  127. $langs=$this->savlangs;
  128. $db=$this->savdb;
  129. $localproduct=new Product($db);
  130. $result = $localproduct->fetch(0, 'PINKDRESS');
  131. if ($result < 0) {
  132. print "\n".__METHOD__." Failed to make the fetch of product PINKDRESS. ".$localproduct->error; die(1);
  133. }
  134. $product_id = $localproduct->id;
  135. if ($product_id <= 0) {
  136. print "\n".__METHOD__." A product with ref PINKDRESS must exists into database. Create it manually before running the test"; die(1);
  137. }
  138. $localobject=new Facture($db);
  139. $localobject->initAsSpecimen();
  140. $localobject->lines=array();
  141. $localobject->lines[0]=new FactureLigne($db);
  142. $localobject->lines[0]->fk_product=$product_id;
  143. $localobject->lines[0]->label='Label 1';
  144. $localobject->lines[0]->desc="This is a description with a é accent\n(Country of origin: France)";
  145. $result=pdf_getlinedesc($localobject, 0, $langs);
  146. print __METHOD__." result=".$result."\n";
  147. $this->assertEquals("PINKDRESS - Label 1<br>This is a description with a &eacute; accent<br>(Country of origin: France)", $result);
  148. $result=doc_getlinedesc($localobject->lines[0], $langs);
  149. print __METHOD__." result=".$result."\n";
  150. $this->assertEquals("PINKDRESS - Label 1\nThis is a description with a é accent\n(Country of origin: France)", $result);
  151. }
  152. /**
  153. * testPdfGetHeightForLogo
  154. *
  155. * @return void
  156. */
  157. public function testPdfGetHeightForLogo()
  158. {
  159. $file=dirname(__FILE__).'/img250x50.jpg';
  160. $result=pdf_getHeightForLogo($file);
  161. print __METHOD__." result=".$result."\n";
  162. $this->assertEquals($result, 20);
  163. $file=dirname(__FILE__).'/img250x20.png';
  164. $result=pdf_getHeightForLogo($file);
  165. print __METHOD__." result=".$result."\n";
  166. $this->assertEquals($result, 10.4);
  167. }
  168. }