ActionTest.php 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455
  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_View
  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. /** Zend_Pdf_Action */
  23. require_once 'Zend/Pdf/Action.php';
  24. /** Zend_Pdf_Action_GoTo */
  25. require_once 'Zend/Pdf/Action/GoTo.php';
  26. /** Zend_Pdf_Action_URI */
  27. require_once 'Zend/Pdf/Action/URI.php';
  28. /** Zend_Pdf_ElementFactory */
  29. require_once 'Zend/Pdf/ElementFactory.php';
  30. /** Zend_Pdf */
  31. require_once 'Zend/Pdf.php';
  32. /** Zend_Pdf_RecursivelyIteratableObjectsContainer */
  33. require_once 'Zend/Pdf/RecursivelyIteratableObjectsContainer.php';
  34. /** Zend_Pdf_ElementFactory */
  35. require_once 'Zend/Pdf/ElementFactory.php';
  36. /**
  37. * @category Zend
  38. * @package Zend_Pdf
  39. * @subpackage UnitTests
  40. * @copyright Copyright (c) 2005-2015 Zend Technologies USA Inc. (http://www.zend.com)
  41. * @license http://framework.zend.com/license/new-bsd New BSD License
  42. * @group Zend_Pdf
  43. */
  44. class Zend_Pdf_ActionTest extends PHPUnit_Framework_TestCase
  45. {
  46. public function setUp()
  47. {
  48. date_default_timezone_set('GMT');
  49. }
  50. public function testLoad()
  51. {
  52. $dictionary = new Zend_Pdf_Element_Dictionary();
  53. $dictionary->Type = new Zend_Pdf_Element_Name('Action');
  54. $dictionary->S = new Zend_Pdf_Element_Name('GoTo');
  55. $dictionary->D = new Zend_Pdf_Element_String('SomeNamedDestination');
  56. $action2Dictionary = new Zend_Pdf_Element_Dictionary();
  57. $action2Dictionary->Type = new Zend_Pdf_Element_Name('Action');
  58. $action2Dictionary->S = new Zend_Pdf_Element_Name('Thread');
  59. $action2Dictionary->D = new Zend_Pdf_Element_String('NamedDestination 2');
  60. $action2Dictionary->Next = new Zend_Pdf_Element_Array();
  61. $dictionary->Next = $action2Dictionary;
  62. $leafAction = new Zend_Pdf_Element_Dictionary();
  63. $leafAction->Type = new Zend_Pdf_Element_Name('Action');
  64. $leafAction->S = new Zend_Pdf_Element_Name('GoTo');
  65. $leafAction->D = new Zend_Pdf_Element_String('NamedDestination 3');
  66. $action2Dictionary->Next->items[] = $leafAction;
  67. $leafAction = new Zend_Pdf_Element_Dictionary();
  68. $leafAction->Type = new Zend_Pdf_Element_Name('Action');
  69. $leafAction->S = new Zend_Pdf_Element_Name('GoToR');
  70. $action2Dictionary->Next->items[] = $leafAction;
  71. $leafAction = new Zend_Pdf_Element_Dictionary();
  72. $leafAction->Type = new Zend_Pdf_Element_Name('Action');
  73. $leafAction->S = new Zend_Pdf_Element_Name('GoToE');
  74. $action2Dictionary->Next->items[] = $leafAction;
  75. $leafAction = new Zend_Pdf_Element_Dictionary();
  76. $leafAction->Type = new Zend_Pdf_Element_Name('Action');
  77. $leafAction->S = new Zend_Pdf_Element_Name('Launch');
  78. $action2Dictionary->Next->items[] = $leafAction;
  79. $leafAction = new Zend_Pdf_Element_Dictionary();
  80. $leafAction->Type = new Zend_Pdf_Element_Name('Action');
  81. $leafAction->S = new Zend_Pdf_Element_Name('Thread');
  82. $action2Dictionary->Next->items[] = $leafAction;
  83. $leafAction = new Zend_Pdf_Element_Dictionary();
  84. $leafAction->Type = new Zend_Pdf_Element_Name('Action');
  85. $leafAction->S = new Zend_Pdf_Element_Name('URI');
  86. $leafAction->URI = new Zend_Pdf_Element_Name('http://some_host/');
  87. $action2Dictionary->Next->items[] = $leafAction;
  88. $leafAction = new Zend_Pdf_Element_Dictionary();
  89. $leafAction->Type = new Zend_Pdf_Element_Name('Action');
  90. $leafAction->S = new Zend_Pdf_Element_Name('Sound');
  91. $action2Dictionary->Next->items[] = $leafAction;
  92. $leafAction = new Zend_Pdf_Element_Dictionary();
  93. $leafAction->Type = new Zend_Pdf_Element_Name('Action');
  94. $leafAction->S = new Zend_Pdf_Element_Name('Movie');
  95. $action2Dictionary->Next->items[] = $leafAction;
  96. $leafAction = new Zend_Pdf_Element_Dictionary();
  97. $leafAction->Type = new Zend_Pdf_Element_Name('Action');
  98. $leafAction->S = new Zend_Pdf_Element_Name('Hide');
  99. $action2Dictionary->Next->items[] = $leafAction;
  100. $leafAction = new Zend_Pdf_Element_Dictionary();
  101. $leafAction->Type = new Zend_Pdf_Element_Name('Action');
  102. $leafAction->S = new Zend_Pdf_Element_Name('Named');
  103. $action2Dictionary->Next->items[] = $leafAction;
  104. $leafAction = new Zend_Pdf_Element_Dictionary();
  105. $leafAction->Type = new Zend_Pdf_Element_Name('Action');
  106. $leafAction->S = new Zend_Pdf_Element_Name('SubmitForm');
  107. $action2Dictionary->Next->items[] = $leafAction;
  108. $leafAction = new Zend_Pdf_Element_Dictionary();
  109. $leafAction->Type = new Zend_Pdf_Element_Name('Action');
  110. $leafAction->S = new Zend_Pdf_Element_Name('ResetForm');
  111. $action2Dictionary->Next->items[] = $leafAction;
  112. $leafAction = new Zend_Pdf_Element_Dictionary();
  113. $leafAction->Type = new Zend_Pdf_Element_Name('Action');
  114. $leafAction->S = new Zend_Pdf_Element_Name('ImportData');
  115. $action2Dictionary->Next->items[] = $leafAction;
  116. $leafAction = new Zend_Pdf_Element_Dictionary();
  117. $leafAction->Type = new Zend_Pdf_Element_Name('Action');
  118. $leafAction->S = new Zend_Pdf_Element_Name('JavaScript');
  119. $action2Dictionary->Next->items[] = $leafAction;
  120. $leafAction = new Zend_Pdf_Element_Dictionary();
  121. $leafAction->Type = new Zend_Pdf_Element_Name('Action');
  122. $leafAction->S = new Zend_Pdf_Element_Name('SetOCGState');
  123. $action2Dictionary->Next->items[] = $leafAction;
  124. $leafAction = new Zend_Pdf_Element_Dictionary();
  125. $leafAction->Type = new Zend_Pdf_Element_Name('Action');
  126. $leafAction->S = new Zend_Pdf_Element_Name('Rendition');
  127. $action2Dictionary->Next->items[] = $leafAction;
  128. $leafAction = new Zend_Pdf_Element_Dictionary();
  129. $leafAction->Type = new Zend_Pdf_Element_Name('Action');
  130. $leafAction->S = new Zend_Pdf_Element_Name('Trans');
  131. $action2Dictionary->Next->items[] = $leafAction;
  132. $leafAction = new Zend_Pdf_Element_Dictionary();
  133. $leafAction->Type = new Zend_Pdf_Element_Name('Action');
  134. $leafAction->S = new Zend_Pdf_Element_Name('GoTo3DView');
  135. $action2Dictionary->Next->items[] = $leafAction;
  136. $action = Zend_Pdf_Action::load($dictionary);
  137. $actionsCount = 0;
  138. $iterator = new RecursiveIteratorIterator(new Zend_Pdf_RecursivelyIteratableObjectsContainer(array($action)),
  139. RecursiveIteratorIterator::SELF_FIRST);
  140. foreach ($iterator as $chainedAction) {
  141. $actionsCount++;
  142. }
  143. $this->assertEquals(20, $actionsCount);
  144. }
  145. public function testExtract()
  146. {
  147. $dictionary = new Zend_Pdf_Element_Dictionary();
  148. $dictionary->Type = new Zend_Pdf_Element_Name('Action');
  149. $dictionary->S = new Zend_Pdf_Element_Name('GoToR');
  150. $dictionary->D = new Zend_Pdf_Element_String('SomeNamedDestination');
  151. $action2Dictionary = new Zend_Pdf_Element_Dictionary();
  152. $action2Dictionary->Type = new Zend_Pdf_Element_Name('Action');
  153. $action2Dictionary->S = new Zend_Pdf_Element_Name('Thread');
  154. $action2Dictionary->D = new Zend_Pdf_Element_String('NamedDestination 2');
  155. $action2Dictionary->Next = new Zend_Pdf_Element_Array();
  156. $dictionary->Next = $action2Dictionary;
  157. $leafAction = new Zend_Pdf_Element_Dictionary();
  158. $leafAction->Type = new Zend_Pdf_Element_Name('Action');
  159. $leafAction->S = new Zend_Pdf_Element_Name('GoTo');
  160. $leafAction->D = new Zend_Pdf_Element_String('NamedDestination 3');
  161. $action2Dictionary->Next->items[] = $leafAction;
  162. $leafAction = new Zend_Pdf_Element_Dictionary();
  163. $leafAction->Type = new Zend_Pdf_Element_Name('Action');
  164. $leafAction->S = new Zend_Pdf_Element_Name('GoToR');
  165. $action2Dictionary->Next->items[] = $leafAction;
  166. $leafAction = new Zend_Pdf_Element_Dictionary();
  167. $leafAction->Type = new Zend_Pdf_Element_Name('Action');
  168. $leafAction->S = new Zend_Pdf_Element_Name('GoToE');
  169. $action2Dictionary->Next->items[] = $leafAction;
  170. $leafAction = new Zend_Pdf_Element_Dictionary();
  171. $leafAction->Type = new Zend_Pdf_Element_Name('Action');
  172. $leafAction->S = new Zend_Pdf_Element_Name('Launch');
  173. $action2Dictionary->Next->items[] = $leafAction;
  174. $leafAction = new Zend_Pdf_Element_Dictionary();
  175. $leafAction->Type = new Zend_Pdf_Element_Name('Action');
  176. $leafAction->S = new Zend_Pdf_Element_Name('Thread');
  177. $action2Dictionary->Next->items[] = $leafAction;
  178. $leafAction = new Zend_Pdf_Element_Dictionary();
  179. $leafAction->Type = new Zend_Pdf_Element_Name('Action');
  180. $leafAction->S = new Zend_Pdf_Element_Name('URI');
  181. $leafAction->URI = new Zend_Pdf_Element_Name('http://some_host/');
  182. $action2Dictionary->Next->items[] = $leafAction;
  183. $leafAction = new Zend_Pdf_Element_Dictionary();
  184. $leafAction->Type = new Zend_Pdf_Element_Name('Action');
  185. $leafAction->S = new Zend_Pdf_Element_Name('Sound');
  186. $action2Dictionary->Next->items[] = $leafAction;
  187. $leafAction = new Zend_Pdf_Element_Dictionary();
  188. $leafAction->Type = new Zend_Pdf_Element_Name('Action');
  189. $leafAction->S = new Zend_Pdf_Element_Name('Movie');
  190. $action2Dictionary->Next->items[] = $leafAction;
  191. $leafAction = new Zend_Pdf_Element_Dictionary();
  192. $leafAction->Type = new Zend_Pdf_Element_Name('Action');
  193. $leafAction->S = new Zend_Pdf_Element_Name('Hide');
  194. $action2Dictionary->Next->items[] = $leafAction;
  195. $leafAction = new Zend_Pdf_Element_Dictionary();
  196. $leafAction->Type = new Zend_Pdf_Element_Name('Action');
  197. $leafAction->S = new Zend_Pdf_Element_Name('Named');
  198. $action2Dictionary->Next->items[] = $leafAction;
  199. $leafAction = new Zend_Pdf_Element_Dictionary();
  200. $leafAction->Type = new Zend_Pdf_Element_Name('Action');
  201. $leafAction->S = new Zend_Pdf_Element_Name('SubmitForm');
  202. $action2Dictionary->Next->items[] = $leafAction;
  203. $leafAction = new Zend_Pdf_Element_Dictionary();
  204. $leafAction->Type = new Zend_Pdf_Element_Name('Action');
  205. $leafAction->S = new Zend_Pdf_Element_Name('ResetForm');
  206. $action2Dictionary->Next->items[] = $leafAction;
  207. $leafAction = new Zend_Pdf_Element_Dictionary();
  208. $leafAction->Type = new Zend_Pdf_Element_Name('Action');
  209. $leafAction->S = new Zend_Pdf_Element_Name('ImportData');
  210. $action2Dictionary->Next->items[] = $leafAction;
  211. $leafAction = new Zend_Pdf_Element_Dictionary();
  212. $leafAction->Type = new Zend_Pdf_Element_Name('Action');
  213. $leafAction->S = new Zend_Pdf_Element_Name('JavaScript');
  214. $action2Dictionary->Next->items[] = $leafAction;
  215. $leafAction = new Zend_Pdf_Element_Dictionary();
  216. $leafAction->Type = new Zend_Pdf_Element_Name('Action');
  217. $leafAction->S = new Zend_Pdf_Element_Name('SetOCGState');
  218. $action2Dictionary->Next->items[] = $leafAction;
  219. $leafAction = new Zend_Pdf_Element_Dictionary();
  220. $leafAction->Type = new Zend_Pdf_Element_Name('Action');
  221. $leafAction->S = new Zend_Pdf_Element_Name('Rendition');
  222. $action2Dictionary->Next->items[] = $leafAction;
  223. $leafAction = new Zend_Pdf_Element_Dictionary();
  224. $leafAction->Type = new Zend_Pdf_Element_Name('Action');
  225. $leafAction->S = new Zend_Pdf_Element_Name('Trans');
  226. $action2Dictionary->Next->items[] = $leafAction;
  227. $leafAction = new Zend_Pdf_Element_Dictionary();
  228. $leafAction->Type = new Zend_Pdf_Element_Name('Action');
  229. $leafAction->S = new Zend_Pdf_Element_Name('GoTo3DView');
  230. $action2Dictionary->Next->items[] = $leafAction;
  231. $action = Zend_Pdf_Action::load($dictionary);
  232. $actionsToClean = array();
  233. $deletionCandidateKeys = array();
  234. $iterator = new RecursiveIteratorIterator($action, RecursiveIteratorIterator::SELF_FIRST);
  235. foreach ($iterator as $chainedAction) {
  236. if ($chainedAction instanceof Zend_Pdf_Action_GoTo) {
  237. $actionsToClean[] = $iterator->getSubIterator();
  238. $deletionCandidateKeys[] = $iterator->getSubIterator()->key();
  239. }
  240. }
  241. foreach ($actionsToClean as $id => $action) {
  242. unset($action->next[$deletionCandidateKeys[$id]]);
  243. }
  244. $actionsCount = 0;
  245. $iterator = new RecursiveIteratorIterator(new Zend_Pdf_RecursivelyIteratableObjectsContainer(array($action)),
  246. RecursiveIteratorIterator::SELF_FIRST);
  247. foreach ($iterator as $chainedAction) {
  248. $actionsCount++;
  249. }
  250. $this->assertEquals(18, $actionsCount);
  251. $action->dumpAction(new Zend_Pdf_ElementFactory(1));
  252. $this->assertEquals(
  253. $action->getResource()->toString(),
  254. '<</Type /Action '
  255. . '/S /Thread '
  256. . '/D (NamedDestination 2) '
  257. . '/Next [1 0 R 2 0 R 3 0 R 4 0 R 5 0 R 6 0 R 7 0 R 8 0 R 9 0 R 10 0 R 11 0 R 12 0 R 13 0 R 14 0 R 15 0 R 16 0 R 17 0 R ] >>');
  258. }
  259. public function testCreate()
  260. {
  261. $action1 = Zend_Pdf_Action_GoTo::create('SomeNamedDestination');
  262. $action1->next[] = Zend_Pdf_Action_GoTo::create('AnotherNamedDestination');
  263. $action1->dumpAction(new Zend_Pdf_ElementFactory(1));
  264. $this->assertEquals($action1->getResource()->toString(),
  265. '<</Type /Action /S /GoTo /D (SomeNamedDestination) /Next 1 0 R >>');
  266. }
  267. public function testCreate1()
  268. {
  269. $pdf = new Zend_Pdf();
  270. $page1 = $pdf->newPage(Zend_Pdf_Page::SIZE_A4);
  271. $page2 = $pdf->newPage(Zend_Pdf_Page::SIZE_A4);
  272. require_once 'Zend/Pdf/Destination/Fit.php';
  273. $destination = Zend_Pdf_Destination_Fit::create($page2);
  274. $action = Zend_Pdf_Action_GoTo::create($destination);
  275. $action->dumpAction(new Zend_Pdf_ElementFactory(1));
  276. $this->assertEquals($action->getResource()->toString(),
  277. '<</Type /Action /S /GoTo /D [4 0 R /Fit ] >>');
  278. }
  279. public function testGetDestination()
  280. {
  281. $dictionary = new Zend_Pdf_Element_Dictionary();
  282. $dictionary->Type = new Zend_Pdf_Element_Name('Action');
  283. $dictionary->S = new Zend_Pdf_Element_Name('GoTo');
  284. $dictionary->D = new Zend_Pdf_Element_String('SomeNamedDestination');
  285. $action = Zend_Pdf_Action::load($dictionary);
  286. $this->assertEquals($action->getDestination()->getName(), 'SomeNamedDestination');
  287. }
  288. public function testGetDestination2()
  289. {
  290. $pdf = new Zend_Pdf();
  291. $page1 = $pdf->newPage(Zend_Pdf_Page::SIZE_A4);
  292. $page2 = $pdf->newPage(Zend_Pdf_Page::SIZE_A4);
  293. $page3 = $pdf->newPage(Zend_Pdf_Page::SIZE_A4); // Page created, but not included into pages list
  294. $pdf->pages[] = $page1;
  295. $pdf->pages[] = $page2;
  296. require_once 'Zend/Pdf/Destination/Fit.php';
  297. $action1 = Zend_Pdf_Action_GoTo::create(Zend_Pdf_Destination_Fit::create($page2));
  298. $action2 = Zend_Pdf_Action_GoTo::create(Zend_Pdf_Destination_Fit::create($page3));
  299. $this->assertTrue($pdf->resolveDestination($action1->getDestination()) === $page2);
  300. $this->assertTrue($pdf->resolveDestination($action2->getDestination()) === null);
  301. }
  302. public function testActionURILoad1()
  303. {
  304. $dictionary = new Zend_Pdf_Element_Dictionary();
  305. $dictionary->Type = new Zend_Pdf_Element_Name('Action');
  306. $dictionary->S = new Zend_Pdf_Element_Name('URI');
  307. $dictionary->URI = new Zend_Pdf_Element_String('http://somehost/');
  308. $action = Zend_Pdf_Action::load($dictionary);
  309. $this->assertTrue($action instanceof Zend_Pdf_Action_URI);
  310. }
  311. public function testActionURILoad2()
  312. {
  313. $dictionary = new Zend_Pdf_Element_Dictionary();
  314. $dictionary->Type = new Zend_Pdf_Element_Name('Action');
  315. $dictionary->S = new Zend_Pdf_Element_Name('URI');
  316. try {
  317. $action = Zend_Pdf_Action::load($dictionary);
  318. $this->fail("exception expected");
  319. } catch (Zend_Pdf_Exception $e) {
  320. $this->assertContains('URI action dictionary entry is required', $e->getMessage());
  321. }
  322. }
  323. public function testActionURICreate()
  324. {
  325. $action = Zend_Pdf_Action_URI::create('http://somehost/');
  326. $this->assertTrue($action instanceof Zend_Pdf_Action_URI);
  327. $this->assertEquals($action->getResource()->toString(),
  328. '<</Type /Action /S /URI /URI (http://somehost/) >>');
  329. }
  330. public function testActionURIGettersSetters()
  331. {
  332. $action = Zend_Pdf_Action_URI::create('http://somehost/');
  333. $this->assertEquals($action->getUri(), 'http://somehost/');
  334. $action->setUri('http://another_host/');
  335. $this->assertEquals($action->getUri(), 'http://another_host/');
  336. $this->assertEquals($action->getIsMap(), false);
  337. $action->setIsMap(true);
  338. $this->assertEquals($action->getIsMap(), true);
  339. $this->assertEquals($action->getResource()->toString(),
  340. '<</Type /Action /S /URI /URI (http://another_host/) /IsMap true >>');
  341. $action->setIsMap(false);
  342. $this->assertEquals($action->getIsMap(), false);
  343. $this->assertEquals($action->getResource()->toString(),
  344. '<</Type /Action /S /URI /URI (http://another_host/) >>');
  345. }
  346. /**
  347. * @group ZF-8462
  348. */
  349. public function testPhpVersionBug()
  350. {
  351. try {
  352. $file = dirname(__FILE__) . '/_files/ZF-8462.pdf';
  353. $pdf = Zend_Pdf::load($file);
  354. } catch (Zend_Pdf_Exception $e) {
  355. if (strpos($e->getMessage(), 'Cross-reference streams are not supported yet.') !== false) {
  356. // Skip expected exception
  357. return;
  358. }
  359. throw $e;
  360. }
  361. }
  362. }