PdfTest.php 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270
  1. <?php
  2. /**
  3. * Zend Framework
  4. *
  5. * LICENSE
  6. *
  7. * This source file is subject to the new BSD license that is bundled
  8. * with this package in the file LICENSE.txt.
  9. * It is also available through the world-wide-web at this URL:
  10. * http://framework.zend.com/license/new-bsd
  11. * If you did not receive a copy of the license and are unable to
  12. * obtain it through the world-wide-web, please send an email
  13. * to license@zend.com so we can send you a copy immediately.
  14. *
  15. * @category Zend
  16. * @package Zend_Pdf
  17. * @subpackage UnitTests
  18. * @copyright Copyright (c) 2005-2015 Zend Technologies USA Inc. (http://www.zend.com)
  19. * @license http://framework.zend.com/license/new-bsd New BSD License
  20. * @version $Id$
  21. */
  22. /**
  23. * @see Zend_Pdf
  24. */
  25. require_once 'Zend/Pdf.php';
  26. /**
  27. * @see Zend_Pdf_Exception
  28. */
  29. require_once 'Zend/Pdf/Exception.php';
  30. /**
  31. * @category Zend
  32. * @package Zend_Pdf
  33. * @subpackage UnitTests
  34. * @copyright Copyright (c) 2005-2015 Zend Technologies USA Inc. (http://www.zend.com)
  35. * @license http://framework.zend.com/license/new-bsd New BSD License
  36. * @group Zend_Pdf
  37. */
  38. class Zend_PdfTest extends PHPUnit_Framework_TestCase
  39. {
  40. /**
  41. * @var Zend_Pdf = null
  42. */
  43. private $_pdf;
  44. protected function setUp()
  45. {
  46. $this->_pdf = Zend_Pdf::load(dirname(__FILE__) . '/Pdf/_files/PdfWithFields.pdf');
  47. }
  48. /**
  49. * PDF with text fields must return array of text field names
  50. */
  51. public function testGetTextFieldNames()
  52. {
  53. $fieldNames = $this->_pdf->getTextFieldNames();
  54. $this->assertEquals(array('Field1', 'Field2'), $fieldNames);
  55. }
  56. /**
  57. * PDF with no text fields must return empty array
  58. */
  59. public function testGetTextFieldNamesNoFieldsEmptyArray()
  60. {
  61. $pdf = new Zend_Pdf();
  62. $fieldNames = $pdf->getTextFieldNames();
  63. $this->assertEquals(array(), $fieldNames);
  64. }
  65. public function testSetTextField()
  66. {
  67. try {
  68. $this->_pdf->setTextField('Field1', 'Value1');
  69. $this->assertTrue(true); // in case of --strict
  70. } catch (\Exception $e) {
  71. $this->fail('Failed to set an existing text field');
  72. }
  73. }
  74. /**
  75. * Asserts: Setting a non-existent field shouls throw an exception
  76. *
  77. * @expectedException Zend_Pdf_Exception
  78. * @expectedExceptionMessage Field 'FieldNotExists' does not exist or is not
  79. * a textfield
  80. */
  81. public function testSetTextFieldNonExistent()
  82. {
  83. $this->_pdf->setTextField('FieldNotExists', 'Value1');
  84. }
  85. public function testSetTextFieldProperties()
  86. {
  87. try {
  88. $this->_pdf->setTextFieldProperties(
  89. 'Field1', Zend_Pdf::PDF_FORM_FIELD_READONLY
  90. );
  91. $this->_pdf->setTextFieldProperties(
  92. 'Field1', Zend_Pdf::PDF_FORM_FIELD_REQUIRED
  93. );
  94. $this->_pdf->setTextFieldProperties(
  95. 'Field1', Zend_Pdf::PDF_FORM_FIELD_NOEXPORT
  96. );
  97. $this->_pdf->setTextFieldProperties(
  98. 'Field1', Zend_Pdf::PDF_FORM_FIELD_READONLY
  99. | Zend_Pdf::PDF_FORM_FIELD_REQUIRED
  100. | Zend_Pdf::PDF_FORM_FIELD_NOEXPORT
  101. );
  102. $this->assertTrue(true); // in case of --strict
  103. } catch (\Exception $e) {
  104. $this->fail('Failed to set property of an existing text field');
  105. }
  106. }
  107. /**
  108. * Asserts setting property of non-existent field shouls throw an exception
  109. *
  110. * @expectedException Zend_Pdf_Exception
  111. * @expectedExceptionMessage Field 'FieldNotExists' does not exist or is not
  112. * a textfield
  113. */
  114. public function testSetTextFieldPropertiesNonExistent()
  115. {
  116. $this->_pdf->setTextFieldProperties(
  117. 'FieldNotExists', Zend_Pdf::PDF_FORM_FIELD_REQUIRED
  118. );
  119. }
  120. public function testMarkTextFieldAsReadOnly()
  121. {
  122. try {
  123. $this->_pdf->markTextFieldAsReadOnly('Field1');
  124. $this->_pdf->markTextFieldAsReadOnly('Field2');
  125. $this->assertTrue(true); // in case of --strict
  126. } catch (\Exception $e) {
  127. $this->fail('Failed to set an existing text field as read-only');
  128. }
  129. }
  130. /**
  131. * Asserts setting property of non-existent field shouls throw an exception
  132. *
  133. * @expectedException Zend_Pdf_Exception
  134. * @expectedExceptionMessage Field 'FieldNotExists' does not exist or is not
  135. * a textfield
  136. */
  137. public function testMarkTextFieldAsReadOnlyNonExistent()
  138. {
  139. $this->_pdf->markTextFieldAsReadOnly('FieldNotExists');
  140. }
  141. public function testGetJavasriptNull()
  142. {
  143. // getting JavaScript without setting it returns NULL
  144. $pdf = new Zend_Pdf();
  145. $this->assertNull($pdf->getJavaScript());
  146. }
  147. public function testSetAndGetJavasriptArray()
  148. {
  149. // getting JavaScript after setting it returns array
  150. $pdf = new Zend_Pdf();
  151. $pdf->setJavaScript('print();');
  152. $this->assertTrue(is_array($pdf->getJavaScript()));
  153. }
  154. public function testSetJavaScriptString()
  155. {
  156. // setting string value is possible
  157. $pdf = new Zend_Pdf();
  158. $javaScriptString = 'print();';
  159. $pdf->setJavaScript($javaScriptString);
  160. $javaScript = $pdf->getJavaScript();
  161. $this->assertEquals($javaScriptString, $javaScript[0]);
  162. }
  163. public function testSetJavaScriptArray()
  164. {
  165. // setting string value is possible
  166. $pdf = new Zend_Pdf();
  167. $javaScriptArray = array('print();', 'alert();');
  168. $pdf->setJavaScript($javaScriptArray);
  169. $this->assertEquals($javaScriptArray, $pdf->getJavaScript());
  170. }
  171. public function testResetJavaScript()
  172. {
  173. // reset removes the added JavaScript
  174. $pdf = new Zend_Pdf();
  175. $pdf->setJavaScript('print();');
  176. $pdf->resetJavaScript();
  177. $this->assertNull($pdf->getJavaScript());
  178. }
  179. public function testAddJavaScript()
  180. {
  181. // adding JavaScript appends previously added JavaScript
  182. $pdf = new Zend_Pdf();
  183. $javaScriptArray = array('print();', 'alert();');
  184. $pdf->addJavaScript($javaScriptArray[0]);
  185. $pdf->addJavaScript($javaScriptArray[1]);
  186. $this->assertEquals($javaScriptArray, $pdf->getJavaScript());
  187. }
  188. /**
  189. * Asserts setting empty JavaScript string throws exception
  190. *
  191. * @expectedException Zend_Pdf_Exception
  192. * @expectedExceptionMessage JavaScript must be a non empty string or array
  193. * of strings
  194. */
  195. public function testSetJavaScriptEmptyString()
  196. {
  197. $pdf = new Zend_Pdf();
  198. $pdf->setJavaScript('');
  199. }
  200. /**
  201. * Asserts setting empty JavaScript array throws exception
  202. *
  203. * @expectedException Zend_Pdf_Exception
  204. * @expectedExceptionMessage JavaScript must be a non empty string or array
  205. * of strings
  206. */
  207. public function testSetJavaScriptEmptyArray()
  208. {
  209. $pdf = new Zend_Pdf();
  210. $pdf->setJavaScript(array());
  211. }
  212. public function testSetAndSaveLoadAndGetJavaScript()
  213. {
  214. $tempFile = tempnam(sys_get_temp_dir(), 'PdfUnitFile');
  215. $javaScript = array('print();', 'alert();');
  216. $pdf = new Zend_Pdf();
  217. $pdf->setJavaScript($javaScript);
  218. $pdf->save($tempFile);
  219. unset($pdf);
  220. $pdf = Zend_Pdf::load($tempFile);
  221. unlink($tempFile);
  222. $this->assertEquals($javaScript, $pdf->getJavaScript());
  223. }
  224. public function testSetAndSaveLoadAndResetAndSaveLoadAndGetJavaScript()
  225. {
  226. $tempFile = tempnam(sys_get_temp_dir(), 'PdfUnitFile');
  227. $javaScript = array('print();', 'alert();');
  228. $pdf = new Zend_Pdf();
  229. $pdf->setJavaScript($javaScript);
  230. $pdf->save($tempFile);
  231. unset($pdf);
  232. $pdf = Zend_Pdf::load($tempFile);
  233. unlink($tempFile);
  234. $pdf->resetJavaScript();
  235. $pdf->save($tempFile);
  236. unset($pdf);
  237. $pdf = Zend_Pdf::load($tempFile);
  238. unlink($tempFile);
  239. $this->assertNull($pdf->getJavaScript());
  240. }
  241. }