ActionTest.php 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387
  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. /** PHPUnit Test Case */
  13. require_once 'PHPUnit/Framework/TestCase.php';
  14. /**
  15. * @package Zend_Pdf
  16. * @subpackage UnitTests
  17. */
  18. class Zend_Pdf_ActionTest extends PHPUnit_Framework_TestCase
  19. {
  20. public function setUp()
  21. {
  22. date_default_timezone_set('GMT');
  23. }
  24. public function testLoad()
  25. {
  26. $dictionary = new Zend_Pdf_Element_Dictionary();
  27. $dictionary->Type = new Zend_Pdf_Element_Name('Action');
  28. $dictionary->S = new Zend_Pdf_Element_Name('GoTo');
  29. $dictionary->D = new Zend_Pdf_Element_String('SomeNamedDestination');
  30. $action2Dictionary = new Zend_Pdf_Element_Dictionary();
  31. $action2Dictionary->Type = new Zend_Pdf_Element_Name('Action');
  32. $action2Dictionary->S = new Zend_Pdf_Element_Name('Thread');
  33. $action2Dictionary->D = new Zend_Pdf_Element_String('NamedDestination 2');
  34. $action2Dictionary->Next = new Zend_Pdf_Element_Array();
  35. $dictionary->Next = $action2Dictionary;
  36. $leafAction = new Zend_Pdf_Element_Dictionary();
  37. $leafAction->Type = new Zend_Pdf_Element_Name('Action');
  38. $leafAction->S = new Zend_Pdf_Element_Name('GoTo');
  39. $leafAction->D = new Zend_Pdf_Element_String('NamedDestination 3');
  40. $action2Dictionary->Next->items[] = $leafAction;
  41. $leafAction = new Zend_Pdf_Element_Dictionary();
  42. $leafAction->Type = new Zend_Pdf_Element_Name('Action');
  43. $leafAction->S = new Zend_Pdf_Element_Name('GoToR');
  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('GoToE');
  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('Launch');
  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('Thread');
  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('URI');
  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('Sound');
  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('Movie');
  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('Hide');
  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('Named');
  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('SubmitForm');
  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('ResetForm');
  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('ImportData');
  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('JavaScript');
  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('SetOCGState');
  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('Rendition');
  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('Trans');
  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('GoTo3DView');
  108. $action2Dictionary->Next->items[] = $leafAction;
  109. $action = Zend_Pdf_Action::load($dictionary);
  110. $this->assertEquals(20, count($action->getAllActions()));
  111. $action->clean();
  112. }
  113. public function testExtract()
  114. {
  115. $dictionary = new Zend_Pdf_Element_Dictionary();
  116. $dictionary->Type = new Zend_Pdf_Element_Name('Action');
  117. $dictionary->S = new Zend_Pdf_Element_Name('GoTo');
  118. $dictionary->D = new Zend_Pdf_Element_String('SomeNamedDestination');
  119. $action2Dictionary = new Zend_Pdf_Element_Dictionary();
  120. $action2Dictionary->Type = new Zend_Pdf_Element_Name('Action');
  121. $action2Dictionary->S = new Zend_Pdf_Element_Name('Thread');
  122. $action2Dictionary->D = new Zend_Pdf_Element_String('NamedDestination 2');
  123. $action2Dictionary->Next = new Zend_Pdf_Element_Array();
  124. $dictionary->Next = $action2Dictionary;
  125. $leafAction = new Zend_Pdf_Element_Dictionary();
  126. $leafAction->Type = new Zend_Pdf_Element_Name('Action');
  127. $leafAction->S = new Zend_Pdf_Element_Name('GoTo');
  128. $leafAction->D = new Zend_Pdf_Element_String('NamedDestination 3');
  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('GoToR');
  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('GoToE');
  137. $action2Dictionary->Next->items[] = $leafAction;
  138. $leafAction = new Zend_Pdf_Element_Dictionary();
  139. $leafAction->Type = new Zend_Pdf_Element_Name('Action');
  140. $leafAction->S = new Zend_Pdf_Element_Name('Launch');
  141. $action2Dictionary->Next->items[] = $leafAction;
  142. $leafAction = new Zend_Pdf_Element_Dictionary();
  143. $leafAction->Type = new Zend_Pdf_Element_Name('Action');
  144. $leafAction->S = new Zend_Pdf_Element_Name('Thread');
  145. $action2Dictionary->Next->items[] = $leafAction;
  146. $leafAction = new Zend_Pdf_Element_Dictionary();
  147. $leafAction->Type = new Zend_Pdf_Element_Name('Action');
  148. $leafAction->S = new Zend_Pdf_Element_Name('URI');
  149. $action2Dictionary->Next->items[] = $leafAction;
  150. $leafAction = new Zend_Pdf_Element_Dictionary();
  151. $leafAction->Type = new Zend_Pdf_Element_Name('Action');
  152. $leafAction->S = new Zend_Pdf_Element_Name('Sound');
  153. $action2Dictionary->Next->items[] = $leafAction;
  154. $leafAction = new Zend_Pdf_Element_Dictionary();
  155. $leafAction->Type = new Zend_Pdf_Element_Name('Action');
  156. $leafAction->S = new Zend_Pdf_Element_Name('Movie');
  157. $action2Dictionary->Next->items[] = $leafAction;
  158. $leafAction = new Zend_Pdf_Element_Dictionary();
  159. $leafAction->Type = new Zend_Pdf_Element_Name('Action');
  160. $leafAction->S = new Zend_Pdf_Element_Name('Hide');
  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('Named');
  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('SubmitForm');
  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('ResetForm');
  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('ImportData');
  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('JavaScript');
  181. $action2Dictionary->Next->items[] = $leafAction;
  182. $leafAction = new Zend_Pdf_Element_Dictionary();
  183. $leafAction->Type = new Zend_Pdf_Element_Name('Action');
  184. $leafAction->S = new Zend_Pdf_Element_Name('SetOCGState');
  185. $action2Dictionary->Next->items[] = $leafAction;
  186. $leafAction = new Zend_Pdf_Element_Dictionary();
  187. $leafAction->Type = new Zend_Pdf_Element_Name('Action');
  188. $leafAction->S = new Zend_Pdf_Element_Name('Rendition');
  189. $action2Dictionary->Next->items[] = $leafAction;
  190. $leafAction = new Zend_Pdf_Element_Dictionary();
  191. $leafAction->Type = new Zend_Pdf_Element_Name('Action');
  192. $leafAction->S = new Zend_Pdf_Element_Name('Trans');
  193. $action2Dictionary->Next->items[] = $leafAction;
  194. $leafAction = new Zend_Pdf_Element_Dictionary();
  195. $leafAction->Type = new Zend_Pdf_Element_Name('Action');
  196. $leafAction->S = new Zend_Pdf_Element_Name('GoTo3DView');
  197. $action2Dictionary->Next->items[] = $leafAction;
  198. $action = Zend_Pdf_Action::load($dictionary);
  199. foreach ($action->getAllActions() as $action) {
  200. if ($action instanceof Zend_Pdf_Action_Thread) {
  201. $root = $action->extract();
  202. }
  203. }
  204. $this->assertTrue($root instanceof Zend_Pdf_Action_GoTo);
  205. $this->assertEquals(18, count($root->getAllActions()));
  206. foreach ($root->getAllActions() as $action) {
  207. if ($action instanceof Zend_Pdf_Action_Goto) {
  208. $root = $action->extract();
  209. }
  210. }
  211. $this->assertTrue($root instanceof Zend_Pdf_Action_GoToR);
  212. $this->assertEquals(16, count($root->getAllActions()));
  213. $root->rebuildSubtree();
  214. $this->assertEquals(
  215. $root->getResource()->toString(),
  216. '<</Type /Action /S /GoToR '
  217. . "/Next [<</Type /Action /S /GoToE >> <</Type /Action /S /Launch >> <</Type /Action /S /URI >> <</Type /Action /S /Sound >> <</Type /Action /S /Movie >> \n"
  218. . "<</Type /Action /S /Hide >> <</Type /Action /S /Named >> <</Type /Action /S /SubmitForm >> <</Type /Action /S /ResetForm >> <</Type /Action /S /ImportData >> \n"
  219. . "<</Type /Action /S /JavaScript >> <</Type /Action /S /SetOCGState >> <</Type /Action /S /Rendition >> <</Type /Action /S /Trans >> \n"
  220. . '<</Type /Action /S /GoTo3DView >> ] >>');
  221. $root->clean();
  222. }
  223. public function testAttach()
  224. {
  225. $action1Dictionary = new Zend_Pdf_Element_Dictionary();
  226. $action1Dictionary->Type = new Zend_Pdf_Element_Name('Action');
  227. $action1Dictionary->S = new Zend_Pdf_Element_Name('GoTo');
  228. $action1Dictionary->D = new Zend_Pdf_Element_String('Destination 1');
  229. $action1 = Zend_Pdf_Action::load($action1Dictionary);
  230. $action2Dictionary = new Zend_Pdf_Element_Dictionary();
  231. $action2Dictionary->Type = new Zend_Pdf_Element_Name('Action');
  232. $action2Dictionary->S = new Zend_Pdf_Element_Name('Thread');
  233. $action2Dictionary->D = new Zend_Pdf_Element_String('Destination 2');
  234. $action2Dictionary->Next = new Zend_Pdf_Element_Array();
  235. $leafAction = new Zend_Pdf_Element_Dictionary();
  236. $leafAction->Type = new Zend_Pdf_Element_Name('Action');
  237. $leafAction->S = new Zend_Pdf_Element_Name('GoTo');
  238. $leafAction->D = new Zend_Pdf_Element_String('Destination 3');
  239. $action2Dictionary->Next->items[] = $leafAction;
  240. $leafAction = new Zend_Pdf_Element_Dictionary();
  241. $leafAction->Type = new Zend_Pdf_Element_Name('Action');
  242. $leafAction->S = new Zend_Pdf_Element_Name('GoToR');
  243. $action2Dictionary->Next->items[] = $leafAction;
  244. $action2 = Zend_Pdf_Action::load($action2Dictionary);
  245. $action1->attach($action2);
  246. $action1->rebuildSubtree();
  247. $this->assertEquals(
  248. $action1->getResource()->toString(),
  249. '<</Type /Action /S /GoTo /D (Destination 1) '
  250. . '/Next <</Type /Action /S /Thread /D (Destination 2) '
  251. . '/Next [<</Type /Action /S /GoTo /D (Destination 3) >> <</Type /Action /S /GoToR >> ] >> >>');
  252. $action1->clean();
  253. }
  254. public function testCreate()
  255. {
  256. $action1 = Zend_Pdf_Action_GoTo::create('SomeNamedDestination');
  257. $action1->attach(Zend_Pdf_Action_GoTo::create('AnotherNamedDestination'));
  258. $action1->rebuildSubtree();
  259. $this->assertEquals($action1->getResource()->toString(),
  260. '<</Type /Action /S /GoTo /D (SomeNamedDestination) /Next <</Type /Action /S /GoTo /D (AnotherNamedDestination) >> >>');
  261. $action1->clean();
  262. }
  263. public function testCreate1()
  264. {
  265. $pdf = new Zend_Pdf();
  266. $page1 = $pdf->newPage(Zend_Pdf_Page::SIZE_A4);
  267. $page2 = $pdf->newPage(Zend_Pdf_Page::SIZE_A4);
  268. require_once 'Zend/Pdf/Destination/Fit.php';
  269. $destination = Zend_Pdf_Destination_Fit::create($page2);
  270. $action = Zend_Pdf_Action_GoTo::create($destination);
  271. $action->rebuildSubtree();
  272. $this->assertEquals($action->getResource()->toString(),
  273. '<</Type /Action /S /GoTo /D [4 0 R /Fit ] >>');
  274. $action->clean();
  275. }
  276. public function testGetDestination()
  277. {
  278. $dictionary = new Zend_Pdf_Element_Dictionary();
  279. $dictionary->Type = new Zend_Pdf_Element_Name('Action');
  280. $dictionary->S = new Zend_Pdf_Element_Name('GoTo');
  281. $dictionary->D = new Zend_Pdf_Element_String('SomeNamedDestination');
  282. $action = Zend_Pdf_Action::load($dictionary);
  283. $this->assertEquals($action->getDestination(), 'SomeNamedDestination');
  284. $action->clean();
  285. }
  286. public function testGetDestination2()
  287. {
  288. $pdf = new Zend_Pdf();
  289. $page1 = $pdf->newPage(Zend_Pdf_Page::SIZE_A4);
  290. $page2 = $pdf->newPage(Zend_Pdf_Page::SIZE_A4);
  291. require_once 'Zend/Pdf/Destination/Fit.php';
  292. $destination = Zend_Pdf_Destination_Fit::create($page2);
  293. $action = Zend_Pdf_Action_GoTo::create($destination);
  294. $this->assertTrue($action->getDestination() == $destination);
  295. $action->clean();
  296. }
  297. }