ActionTest.php 19 KB

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