2
0

PdfTest.php 8.4 KB

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