ActionTest.php 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355
  1. <?php
  2. /**
  3. * @package Zend_Pdf
  4. * @subpackage UnitTests
  5. */
  6. /** Zend_Pdf_Action */
  7. require_once 'Zend/Pdf/Action.php';
  8. /** Zend_Pdf_Action */
  9. require_once 'Zend/Pdf/ElementFactory.php';
  10. /** Zend_Pdf */
  11. require_once 'Zend/Pdf.php';
  12. /** Zend_Pdf_RecursivelyIteratableObjectsContainer */
  13. require_once 'Zend/Pdf/RecursivelyIteratableObjectsContainer.php';
  14. /** Zend_Pdf_ElementFactory */
  15. require_once 'Zend/Pdf/ElementFactory.php';
  16. /** PHPUnit Test Case */
  17. require_once 'PHPUnit/Framework/TestCase.php';
  18. /**
  19. * @package Zend_Pdf
  20. * @subpackage UnitTests
  21. */
  22. class Zend_Pdf_ActionTest extends PHPUnit_Framework_TestCase
  23. {
  24. public function setUp()
  25. {
  26. date_default_timezone_set('GMT');
  27. }
  28. public function testLoad()
  29. {
  30. $dictionary = new Zend_Pdf_Element_Dictionary();
  31. $dictionary->Type = new Zend_Pdf_Element_Name('Action');
  32. $dictionary->S = new Zend_Pdf_Element_Name('GoTo');
  33. $dictionary->D = new Zend_Pdf_Element_String('SomeNamedDestination');
  34. $action2Dictionary = new Zend_Pdf_Element_Dictionary();
  35. $action2Dictionary->Type = new Zend_Pdf_Element_Name('Action');
  36. $action2Dictionary->S = new Zend_Pdf_Element_Name('Thread');
  37. $action2Dictionary->D = new Zend_Pdf_Element_String('NamedDestination 2');
  38. $action2Dictionary->Next = new Zend_Pdf_Element_Array();
  39. $dictionary->Next = $action2Dictionary;
  40. $leafAction = new Zend_Pdf_Element_Dictionary();
  41. $leafAction->Type = new Zend_Pdf_Element_Name('Action');
  42. $leafAction->S = new Zend_Pdf_Element_Name('GoTo');
  43. $leafAction->D = new Zend_Pdf_Element_String('NamedDestination 3');
  44. $action2Dictionary->Next->items[] = $leafAction;
  45. $leafAction = new Zend_Pdf_Element_Dictionary();
  46. $leafAction->Type = new Zend_Pdf_Element_Name('Action');
  47. $leafAction->S = new Zend_Pdf_Element_Name('GoToR');
  48. $action2Dictionary->Next->items[] = $leafAction;
  49. $leafAction = new Zend_Pdf_Element_Dictionary();
  50. $leafAction->Type = new Zend_Pdf_Element_Name('Action');
  51. $leafAction->S = new Zend_Pdf_Element_Name('GoToE');
  52. $action2Dictionary->Next->items[] = $leafAction;
  53. $leafAction = new Zend_Pdf_Element_Dictionary();
  54. $leafAction->Type = new Zend_Pdf_Element_Name('Action');
  55. $leafAction->S = new Zend_Pdf_Element_Name('Launch');
  56. $action2Dictionary->Next->items[] = $leafAction;
  57. $leafAction = new Zend_Pdf_Element_Dictionary();
  58. $leafAction->Type = new Zend_Pdf_Element_Name('Action');
  59. $leafAction->S = new Zend_Pdf_Element_Name('Thread');
  60. $action2Dictionary->Next->items[] = $leafAction;
  61. $leafAction = new Zend_Pdf_Element_Dictionary();
  62. $leafAction->Type = new Zend_Pdf_Element_Name('Action');
  63. $leafAction->S = new Zend_Pdf_Element_Name('URI');
  64. $action2Dictionary->Next->items[] = $leafAction;
  65. $leafAction = new Zend_Pdf_Element_Dictionary();
  66. $leafAction->Type = new Zend_Pdf_Element_Name('Action');
  67. $leafAction->S = new Zend_Pdf_Element_Name('Sound');
  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('Movie');
  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('Hide');
  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('Named');
  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('SubmitForm');
  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('ResetForm');
  88. $action2Dictionary->Next->items[] = $leafAction;
  89. $leafAction = new Zend_Pdf_Element_Dictionary();
  90. $leafAction->Type = new Zend_Pdf_Element_Name('Action');
  91. $leafAction->S = new Zend_Pdf_Element_Name('ImportData');
  92. $action2Dictionary->Next->items[] = $leafAction;
  93. $leafAction = new Zend_Pdf_Element_Dictionary();
  94. $leafAction->Type = new Zend_Pdf_Element_Name('Action');
  95. $leafAction->S = new Zend_Pdf_Element_Name('JavaScript');
  96. $action2Dictionary->Next->items[] = $leafAction;
  97. $leafAction = new Zend_Pdf_Element_Dictionary();
  98. $leafAction->Type = new Zend_Pdf_Element_Name('Action');
  99. $leafAction->S = new Zend_Pdf_Element_Name('SetOCGState');
  100. $action2Dictionary->Next->items[] = $leafAction;
  101. $leafAction = new Zend_Pdf_Element_Dictionary();
  102. $leafAction->Type = new Zend_Pdf_Element_Name('Action');
  103. $leafAction->S = new Zend_Pdf_Element_Name('Rendition');
  104. $action2Dictionary->Next->items[] = $leafAction;
  105. $leafAction = new Zend_Pdf_Element_Dictionary();
  106. $leafAction->Type = new Zend_Pdf_Element_Name('Action');
  107. $leafAction->S = new Zend_Pdf_Element_Name('Trans');
  108. $action2Dictionary->Next->items[] = $leafAction;
  109. $leafAction = new Zend_Pdf_Element_Dictionary();
  110. $leafAction->Type = new Zend_Pdf_Element_Name('Action');
  111. $leafAction->S = new Zend_Pdf_Element_Name('GoTo3DView');
  112. $action2Dictionary->Next->items[] = $leafAction;
  113. $action = Zend_Pdf_Action::load($dictionary);
  114. $actionsCount = 0;
  115. $iterator = new RecursiveIteratorIterator(new Zend_Pdf_RecursivelyIteratableObjectsContainer(array($action)),
  116. RecursiveIteratorIterator::SELF_FIRST);
  117. foreach ($iterator as $chainedAction) {
  118. $actionsCount++;
  119. }
  120. $this->assertEquals(20, $actionsCount);
  121. }
  122. public function testExtract()
  123. {
  124. $dictionary = new Zend_Pdf_Element_Dictionary();
  125. $dictionary->Type = new Zend_Pdf_Element_Name('Action');
  126. $dictionary->S = new Zend_Pdf_Element_Name('GoToR');
  127. $dictionary->D = new Zend_Pdf_Element_String('SomeNamedDestination');
  128. $action2Dictionary = new Zend_Pdf_Element_Dictionary();
  129. $action2Dictionary->Type = new Zend_Pdf_Element_Name('Action');
  130. $action2Dictionary->S = new Zend_Pdf_Element_Name('Thread');
  131. $action2Dictionary->D = new Zend_Pdf_Element_String('NamedDestination 2');
  132. $action2Dictionary->Next = new Zend_Pdf_Element_Array();
  133. $dictionary->Next = $action2Dictionary;
  134. $leafAction = new Zend_Pdf_Element_Dictionary();
  135. $leafAction->Type = new Zend_Pdf_Element_Name('Action');
  136. $leafAction->S = new Zend_Pdf_Element_Name('GoTo');
  137. $leafAction->D = new Zend_Pdf_Element_String('NamedDestination 3');
  138. $action2Dictionary->Next->items[] = $leafAction;
  139. $leafAction = new Zend_Pdf_Element_Dictionary();
  140. $leafAction->Type = new Zend_Pdf_Element_Name('Action');
  141. $leafAction->S = new Zend_Pdf_Element_Name('GoToR');
  142. $action2Dictionary->Next->items[] = $leafAction;
  143. $leafAction = new Zend_Pdf_Element_Dictionary();
  144. $leafAction->Type = new Zend_Pdf_Element_Name('Action');
  145. $leafAction->S = new Zend_Pdf_Element_Name('GoToE');
  146. $action2Dictionary->Next->items[] = $leafAction;
  147. $leafAction = new Zend_Pdf_Element_Dictionary();
  148. $leafAction->Type = new Zend_Pdf_Element_Name('Action');
  149. $leafAction->S = new Zend_Pdf_Element_Name('Launch');
  150. $action2Dictionary->Next->items[] = $leafAction;
  151. $leafAction = new Zend_Pdf_Element_Dictionary();
  152. $leafAction->Type = new Zend_Pdf_Element_Name('Action');
  153. $leafAction->S = new Zend_Pdf_Element_Name('Thread');
  154. $action2Dictionary->Next->items[] = $leafAction;
  155. $leafAction = new Zend_Pdf_Element_Dictionary();
  156. $leafAction->Type = new Zend_Pdf_Element_Name('Action');
  157. $leafAction->S = new Zend_Pdf_Element_Name('URI');
  158. $action2Dictionary->Next->items[] = $leafAction;
  159. $leafAction = new Zend_Pdf_Element_Dictionary();
  160. $leafAction->Type = new Zend_Pdf_Element_Name('Action');
  161. $leafAction->S = new Zend_Pdf_Element_Name('Sound');
  162. $action2Dictionary->Next->items[] = $leafAction;
  163. $leafAction = new Zend_Pdf_Element_Dictionary();
  164. $leafAction->Type = new Zend_Pdf_Element_Name('Action');
  165. $leafAction->S = new Zend_Pdf_Element_Name('Movie');
  166. $action2Dictionary->Next->items[] = $leafAction;
  167. $leafAction = new Zend_Pdf_Element_Dictionary();
  168. $leafAction->Type = new Zend_Pdf_Element_Name('Action');
  169. $leafAction->S = new Zend_Pdf_Element_Name('Hide');
  170. $action2Dictionary->Next->items[] = $leafAction;
  171. $leafAction = new Zend_Pdf_Element_Dictionary();
  172. $leafAction->Type = new Zend_Pdf_Element_Name('Action');
  173. $leafAction->S = new Zend_Pdf_Element_Name('Named');
  174. $action2Dictionary->Next->items[] = $leafAction;
  175. $leafAction = new Zend_Pdf_Element_Dictionary();
  176. $leafAction->Type = new Zend_Pdf_Element_Name('Action');
  177. $leafAction->S = new Zend_Pdf_Element_Name('SubmitForm');
  178. $action2Dictionary->Next->items[] = $leafAction;
  179. $leafAction = new Zend_Pdf_Element_Dictionary();
  180. $leafAction->Type = new Zend_Pdf_Element_Name('Action');
  181. $leafAction->S = new Zend_Pdf_Element_Name('ResetForm');
  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('ImportData');
  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('JavaScript');
  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('SetOCGState');
  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('Rendition');
  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('Trans');
  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('GoTo3DView');
  206. $action2Dictionary->Next->items[] = $leafAction;
  207. $action = Zend_Pdf_Action::load($dictionary);
  208. $actionsToClean = array();
  209. $deletionCandidateKeys = array();
  210. $iterator = new RecursiveIteratorIterator($action, RecursiveIteratorIterator::SELF_FIRST);
  211. foreach ($iterator as $chainedAction) {
  212. if ($chainedAction instanceof Zend_Pdf_Action_GoTo) {
  213. $actionsToClean[] = $iterator->getSubIterator();
  214. $deletionCandidateKeys[] = $iterator->getSubIterator()->key();
  215. }
  216. }
  217. foreach ($actionsToClean as $id => $action) {
  218. unset($action->next[$deletionCandidateKeys[$id]]);
  219. }
  220. $actionsCount = 0;
  221. $iterator = new RecursiveIteratorIterator(new Zend_Pdf_RecursivelyIteratableObjectsContainer(array($action)),
  222. RecursiveIteratorIterator::SELF_FIRST);
  223. foreach ($iterator as $chainedAction) {
  224. $actionsCount++;
  225. }
  226. $this->assertEquals(18, $actionsCount);
  227. $action->dumpAction(new Zend_Pdf_ElementFactory(1));
  228. $this->assertEquals(
  229. $action->getResource()->toString(),
  230. '<</Type /Action '
  231. . '/S /Thread '
  232. . '/D (NamedDestination 2) '
  233. . '/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 ] >>');
  234. }
  235. public function testCreate()
  236. {
  237. $action1 = Zend_Pdf_Action_GoTo::create('SomeNamedDestination');
  238. $action1->next[] = Zend_Pdf_Action_GoTo::create('AnotherNamedDestination');
  239. $action1->dumpAction(new Zend_Pdf_ElementFactory(1));
  240. $this->assertEquals($action1->getResource()->toString(),
  241. '<</Type /Action /S /GoTo /D (SomeNamedDestination) /Next 1 0 R >>');
  242. }
  243. public function testCreate1()
  244. {
  245. $pdf = new Zend_Pdf();
  246. $page1 = $pdf->newPage(Zend_Pdf_Page::SIZE_A4);
  247. $page2 = $pdf->newPage(Zend_Pdf_Page::SIZE_A4);
  248. require_once 'Zend/Pdf/Destination/Fit.php';
  249. $destination = Zend_Pdf_Destination_Fit::create($page2);
  250. $action = Zend_Pdf_Action_GoTo::create($destination);
  251. $action->dumpAction(new Zend_Pdf_ElementFactory(1));
  252. $this->assertEquals($action->getResource()->toString(),
  253. '<</Type /Action /S /GoTo /D [4 0 R /Fit ] >>');
  254. }
  255. public function testGetDestination()
  256. {
  257. $dictionary = new Zend_Pdf_Element_Dictionary();
  258. $dictionary->Type = new Zend_Pdf_Element_Name('Action');
  259. $dictionary->S = new Zend_Pdf_Element_Name('GoTo');
  260. $dictionary->D = new Zend_Pdf_Element_String('SomeNamedDestination');
  261. $action = Zend_Pdf_Action::load($dictionary);
  262. $this->assertEquals($action->getDestination()->getName(), 'SomeNamedDestination');
  263. }
  264. public function testGetDestination2()
  265. {
  266. $pdf = new Zend_Pdf();
  267. $page1 = $pdf->newPage(Zend_Pdf_Page::SIZE_A4);
  268. $page2 = $pdf->newPage(Zend_Pdf_Page::SIZE_A4);
  269. $page3 = $pdf->newPage(Zend_Pdf_Page::SIZE_A4); // Page created, but not included into pages list
  270. $pdf->pages[] = $page1;
  271. $pdf->pages[] = $page2;
  272. require_once 'Zend/Pdf/Destination/Fit.php';
  273. $action1 = Zend_Pdf_Action_GoTo::create(Zend_Pdf_Destination_Fit::create($page2));
  274. $action2 = Zend_Pdf_Action_GoTo::create(Zend_Pdf_Destination_Fit::create($page3));
  275. $this->assertTrue($pdf->resolveDestination($action1->getDestination()) === $page2);
  276. $this->assertTrue($pdf->resolveDestination($action2->getDestination()) === null);
  277. }
  278. }