PdfTest.php 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265
  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-2014 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-2014 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. *
  42. * @var NULL|Zend_Pdf
  43. */
  44. private $_pdf = NULL;
  45. protected function setUp()
  46. {
  47. $this->_pdf = Zend_Pdf::load(dirname(__FILE__) . '/Pdf/_files/PdfWithFields.pdf');
  48. }
  49. public function testGetTextFieldNames()
  50. {
  51. $fieldNames = $this->_pdf->getTextFieldNames();
  52. //PDF with text fields must return array of text field names
  53. $this->assertEquals(array('Field1', 'Field2'), $fieldNames);
  54. }
  55. public function testGetTextFieldNamesNoFieldsEmptyArray()
  56. {
  57. $pdf = new Zend_Pdf();
  58. $fieldNames = $pdf->getTextFieldNames();
  59. //PDF with no text fields must return empty array
  60. $this->assertEquals(array(), $fieldNames);
  61. }
  62. public function testSetTextField()
  63. {
  64. try {
  65. $this->_pdf->setTextField('Field1', 'Value1');
  66. $this->assertTrue(TRUE); //in case of --strict
  67. } catch (\Exception $e) {
  68. $this->fail('Failed to set an existing text field');
  69. }
  70. }
  71. /**
  72. * Asserts: Setting a non-existent field shouls throw an exception
  73. *
  74. * @expectedException Zend_Pdf_Exception
  75. * @expectedExceptionMessage Field 'FieldNotExists' does not exist or is not
  76. * a textfield
  77. */
  78. public function testSetTextFieldNonExistent()
  79. {
  80. $this->_pdf->setTextField('FieldNotExists', 'Value1');
  81. }
  82. public function testSetTextFieldProperties()
  83. {
  84. try {
  85. $this->_pdf->setTextFieldProperties(
  86. 'Field1', Zend_Pdf::PDF_FORM_FIELD_READONLY
  87. );
  88. $this->_pdf->setTextFieldProperties(
  89. 'Field1', Zend_Pdf::PDF_FORM_FIELD_REQUIRED
  90. );
  91. $this->_pdf->setTextFieldProperties(
  92. 'Field1', Zend_Pdf::PDF_FORM_FIELD_NOEXPORT
  93. );
  94. $this->_pdf->setTextFieldProperties(
  95. 'Field1', Zend_Pdf::PDF_FORM_FIELD_READONLY
  96. | Zend_Pdf::PDF_FORM_FIELD_REQUIRED
  97. | Zend_Pdf::PDF_FORM_FIELD_NOEXPORT
  98. );
  99. $this->assertTrue(TRUE); //in case of --strict
  100. } catch (\Exception $e) {
  101. $this->fail('Failed to set property of an existing text field');
  102. }
  103. }
  104. /**
  105. * Asserts setting property of non-existent field shouls throw an exception
  106. *
  107. * @expectedException Zend_Pdf_Exception
  108. * @expectedExceptionMessage Field 'FieldNotExists' does not exist or is not
  109. * a textfield
  110. */
  111. public function testSetTextFieldPropertiesNonExistent()
  112. {
  113. $this->_pdf->setTextFieldProperties('FieldNotExists', Zend_Pdf::PDF_FORM_FIELD_REQUIRED);
  114. }
  115. public function testMarkTextFieldAsReadOnly()
  116. {
  117. try {
  118. $this->_pdf->markTextFieldAsReadOnly('Field1');
  119. $this->_pdf->markTextFieldAsReadOnly('Field2');
  120. $this->assertTrue(TRUE); //in case of --strict
  121. } catch (\Exception $e) {
  122. $this->fail('Failed to set an existing text field as read-only');
  123. }
  124. }
  125. /**
  126. * Asserts setting property of non-existent field shouls throw an exception
  127. *
  128. * @expectedException Zend_Pdf_Exception
  129. * @expectedExceptionMessage Field 'FieldNotExists' does not exist or is not
  130. * a textfield
  131. */
  132. public function testMarkTextFieldAsReadOnlyNonExistent()
  133. {
  134. $this->_pdf->markTextFieldAsReadOnly('FieldNotExists');
  135. }
  136. public function testGetJavasriptNull()
  137. {
  138. // getting JavaScript without setting it returns NULL
  139. $pdf = new Zend_Pdf();
  140. $this->assertNull($pdf->getJavaScript());
  141. }
  142. public function testSetAndGetJavasriptArray()
  143. {
  144. // getting JavaScript after setting it returns array
  145. $pdf = new Zend_Pdf();
  146. $pdf->setJavaScript('print();');
  147. $this->assertTrue(is_array($pdf->getJavaScript()));
  148. }
  149. public function testSetJavaScriptString()
  150. {
  151. // setting string value is possible
  152. $pdf = new Zend_Pdf();
  153. $javaScriptString = 'print();';
  154. $pdf->setJavaScript($javaScriptString);
  155. $javaScript = $pdf->getJavaScript();
  156. $this->assertEquals($javaScriptString, $javaScript[0]);
  157. }
  158. public function testSetJavaScriptArray()
  159. {
  160. // setting string value is possible
  161. $pdf = new Zend_Pdf();
  162. $javaScriptArray = array('print();', 'alert();');
  163. $pdf->setJavaScript($javaScriptArray);
  164. $this->assertEquals($javaScriptArray, $pdf->getJavaScript());
  165. }
  166. public function testResetJavaScript()
  167. {
  168. // reset removes the added JavaScript
  169. $pdf = new Zend_Pdf();
  170. $pdf->setJavaScript('print();');
  171. $pdf->resetJavaScript();
  172. $this->assertNull($pdf->getJavaScript());
  173. }
  174. public function testAddJavaScript()
  175. {
  176. // adding JavaScript appends previously added JavaScript
  177. $pdf = new Zend_Pdf();
  178. $javaScriptArray = array('print();', 'alert();');
  179. $pdf->addJavaScript($javaScriptArray[0]);
  180. $pdf->addJavaScript($javaScriptArray[1]);
  181. $this->assertEquals($javaScriptArray, $pdf->getJavaScript());
  182. }
  183. /**
  184. * Asserts setting empty JavaScript string throws exception
  185. *
  186. * @expectedException Zend_Pdf_Exception
  187. * @expectedExceptionMessage JavaScript must be a non empty string or array
  188. * of strings
  189. */
  190. public function testSetJavaScriptEmptyString()
  191. {
  192. $pdf = new Zend_Pdf();
  193. $pdf->setJavaScript('');
  194. }
  195. /**
  196. * Asserts setting empty JavaScript array throws exception
  197. *
  198. * @expectedException Zend_Pdf_Exception
  199. * @expectedExceptionMessage JavaScript must be a non empty string or array
  200. * of strings
  201. */
  202. public function testSetJavaScriptEmptyArray()
  203. {
  204. $pdf = new Zend_Pdf();
  205. $pdf->setJavaScript(array());
  206. }
  207. public function testSetAndSaveLoadAndGetJavaScript()
  208. {
  209. $tempFile = tempnam(sys_get_temp_dir(), 'PdfUnitFile');
  210. $javaScript = array('print();', 'alert();');
  211. $pdf = new Zend_Pdf();
  212. $pdf->setJavaScript($javaScript);
  213. $pdf->save($tempFile);
  214. unset($pdf);
  215. $pdf = Zend_Pdf::load($tempFile);
  216. unlink($tempFile);
  217. $this->assertEquals($javaScript, $pdf->getJavaScript());
  218. }
  219. public function testSetAndSaveLoadAndResetAndSaveLoadAndGetJavaScript()
  220. {
  221. $tempFile = tempnam(sys_get_temp_dir(), 'PdfUnitFile');
  222. $javaScript = array('print();', 'alert();');
  223. $pdf = new Zend_Pdf();
  224. $pdf->setJavaScript($javaScript);
  225. $pdf->save($tempFile);
  226. unset($pdf);
  227. $pdf = Zend_Pdf::load($tempFile);
  228. unlink($tempFile);
  229. $pdf->resetJavaScript();
  230. $pdf->save($tempFile);
  231. unset($pdf);
  232. $pdf = Zend_Pdf::load($tempFile);
  233. unlink($tempFile);
  234. $this->assertNull($pdf->getJavaScript());
  235. }
  236. }