2
0

ActionTest.php 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374
  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-2009 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 */
  25. require_once 'Zend/Pdf/ElementFactory.php';
  26. /** Zend_Pdf */
  27. require_once 'Zend/Pdf.php';
  28. /** Zend_Pdf_RecursivelyIteratableObjectsContainer */
  29. require_once 'Zend/Pdf/RecursivelyIteratableObjectsContainer.php';
  30. /** Zend_Pdf_ElementFactory */
  31. require_once 'Zend/Pdf/ElementFactory.php';
  32. /** PHPUnit Test Case */
  33. require_once 'PHPUnit/Framework/TestCase.php';
  34. /**
  35. * @category Zend
  36. * @package Zend_Pdf
  37. * @subpackage UnitTests
  38. * @copyright Copyright (c) 2005-2009 Zend Technologies USA Inc. (http://www.zend.com)
  39. * @license http://framework.zend.com/license/new-bsd New BSD License
  40. * @group Zend_Pdf
  41. */
  42. class Zend_Pdf_ActionTest extends PHPUnit_Framework_TestCase
  43. {
  44. public function setUp()
  45. {
  46. date_default_timezone_set('GMT');
  47. }
  48. public function testLoad()
  49. {
  50. $dictionary = new Zend_Pdf_Element_Dictionary();
  51. $dictionary->Type = new Zend_Pdf_Element_Name('Action');
  52. $dictionary->S = new Zend_Pdf_Element_Name('GoTo');
  53. $dictionary->D = new Zend_Pdf_Element_String('SomeNamedDestination');
  54. $action2Dictionary = new Zend_Pdf_Element_Dictionary();
  55. $action2Dictionary->Type = new Zend_Pdf_Element_Name('Action');
  56. $action2Dictionary->S = new Zend_Pdf_Element_Name('Thread');
  57. $action2Dictionary->D = new Zend_Pdf_Element_String('NamedDestination 2');
  58. $action2Dictionary->Next = new Zend_Pdf_Element_Array();
  59. $dictionary->Next = $action2Dictionary;
  60. $leafAction = new Zend_Pdf_Element_Dictionary();
  61. $leafAction->Type = new Zend_Pdf_Element_Name('Action');
  62. $leafAction->S = new Zend_Pdf_Element_Name('GoTo');
  63. $leafAction->D = new Zend_Pdf_Element_String('NamedDestination 3');
  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('GoToR');
  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('GoToE');
  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('Launch');
  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('Thread');
  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('URI');
  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('Sound');
  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('Movie');
  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('Hide');
  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('Named');
  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('SubmitForm');
  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('ResetForm');
  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('ImportData');
  112. $action2Dictionary->Next->items[] = $leafAction;
  113. $leafAction = new Zend_Pdf_Element_Dictionary();
  114. $leafAction->Type = new Zend_Pdf_Element_Name('Action');
  115. $leafAction->S = new Zend_Pdf_Element_Name('JavaScript');
  116. $action2Dictionary->Next->items[] = $leafAction;
  117. $leafAction = new Zend_Pdf_Element_Dictionary();
  118. $leafAction->Type = new Zend_Pdf_Element_Name('Action');
  119. $leafAction->S = new Zend_Pdf_Element_Name('SetOCGState');
  120. $action2Dictionary->Next->items[] = $leafAction;
  121. $leafAction = new Zend_Pdf_Element_Dictionary();
  122. $leafAction->Type = new Zend_Pdf_Element_Name('Action');
  123. $leafAction->S = new Zend_Pdf_Element_Name('Rendition');
  124. $action2Dictionary->Next->items[] = $leafAction;
  125. $leafAction = new Zend_Pdf_Element_Dictionary();
  126. $leafAction->Type = new Zend_Pdf_Element_Name('Action');
  127. $leafAction->S = new Zend_Pdf_Element_Name('Trans');
  128. $action2Dictionary->Next->items[] = $leafAction;
  129. $leafAction = new Zend_Pdf_Element_Dictionary();
  130. $leafAction->Type = new Zend_Pdf_Element_Name('Action');
  131. $leafAction->S = new Zend_Pdf_Element_Name('GoTo3DView');
  132. $action2Dictionary->Next->items[] = $leafAction;
  133. $action = Zend_Pdf_Action::load($dictionary);
  134. $actionsCount = 0;
  135. $iterator = new RecursiveIteratorIterator(new Zend_Pdf_RecursivelyIteratableObjectsContainer(array($action)),
  136. RecursiveIteratorIterator::SELF_FIRST);
  137. foreach ($iterator as $chainedAction) {
  138. $actionsCount++;
  139. }
  140. $this->assertEquals(20, $actionsCount);
  141. }
  142. public function testExtract()
  143. {
  144. $dictionary = new Zend_Pdf_Element_Dictionary();
  145. $dictionary->Type = new Zend_Pdf_Element_Name('Action');
  146. $dictionary->S = new Zend_Pdf_Element_Name('GoToR');
  147. $dictionary->D = new Zend_Pdf_Element_String('SomeNamedDestination');
  148. $action2Dictionary = new Zend_Pdf_Element_Dictionary();
  149. $action2Dictionary->Type = new Zend_Pdf_Element_Name('Action');
  150. $action2Dictionary->S = new Zend_Pdf_Element_Name('Thread');
  151. $action2Dictionary->D = new Zend_Pdf_Element_String('NamedDestination 2');
  152. $action2Dictionary->Next = new Zend_Pdf_Element_Array();
  153. $dictionary->Next = $action2Dictionary;
  154. $leafAction = new Zend_Pdf_Element_Dictionary();
  155. $leafAction->Type = new Zend_Pdf_Element_Name('Action');
  156. $leafAction->S = new Zend_Pdf_Element_Name('GoTo');
  157. $leafAction->D = new Zend_Pdf_Element_String('NamedDestination 3');
  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('GoToR');
  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('GoToE');
  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('Launch');
  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('Thread');
  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('URI');
  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('Sound');
  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('Movie');
  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('Hide');
  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('Named');
  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('SubmitForm');
  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('ResetForm');
  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('ImportData');
  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('JavaScript');
  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('SetOCGState');
  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('Rendition');
  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('Trans');
  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('GoTo3DView');
  226. $action2Dictionary->Next->items[] = $leafAction;
  227. $action = Zend_Pdf_Action::load($dictionary);
  228. $actionsToClean = array();
  229. $deletionCandidateKeys = array();
  230. $iterator = new RecursiveIteratorIterator($action, RecursiveIteratorIterator::SELF_FIRST);
  231. foreach ($iterator as $chainedAction) {
  232. if ($chainedAction instanceof Zend_Pdf_Action_GoTo) {
  233. $actionsToClean[] = $iterator->getSubIterator();
  234. $deletionCandidateKeys[] = $iterator->getSubIterator()->key();
  235. }
  236. }
  237. foreach ($actionsToClean as $id => $action) {
  238. unset($action->next[$deletionCandidateKeys[$id]]);
  239. }
  240. $actionsCount = 0;
  241. $iterator = new RecursiveIteratorIterator(new Zend_Pdf_RecursivelyIteratableObjectsContainer(array($action)),
  242. RecursiveIteratorIterator::SELF_FIRST);
  243. foreach ($iterator as $chainedAction) {
  244. $actionsCount++;
  245. }
  246. $this->assertEquals(18, $actionsCount);
  247. $action->dumpAction(new Zend_Pdf_ElementFactory(1));
  248. $this->assertEquals(
  249. $action->getResource()->toString(),
  250. '<</Type /Action '
  251. . '/S /Thread '
  252. . '/D (NamedDestination 2) '
  253. . '/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 ] >>');
  254. }
  255. public function testCreate()
  256. {
  257. $action1 = Zend_Pdf_Action_GoTo::create('SomeNamedDestination');
  258. $action1->next[] = Zend_Pdf_Action_GoTo::create('AnotherNamedDestination');
  259. $action1->dumpAction(new Zend_Pdf_ElementFactory(1));
  260. $this->assertEquals($action1->getResource()->toString(),
  261. '<</Type /Action /S /GoTo /D (SomeNamedDestination) /Next 1 0 R >>');
  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->dumpAction(new Zend_Pdf_ElementFactory(1));
  272. $this->assertEquals($action->getResource()->toString(),
  273. '<</Type /Action /S /GoTo /D [4 0 R /Fit ] >>');
  274. }
  275. public function testGetDestination()
  276. {
  277. $dictionary = new Zend_Pdf_Element_Dictionary();
  278. $dictionary->Type = new Zend_Pdf_Element_Name('Action');
  279. $dictionary->S = new Zend_Pdf_Element_Name('GoTo');
  280. $dictionary->D = new Zend_Pdf_Element_String('SomeNamedDestination');
  281. $action = Zend_Pdf_Action::load($dictionary);
  282. $this->assertEquals($action->getDestination()->getName(), 'SomeNamedDestination');
  283. }
  284. public function testGetDestination2()
  285. {
  286. $pdf = new Zend_Pdf();
  287. $page1 = $pdf->newPage(Zend_Pdf_Page::SIZE_A4);
  288. $page2 = $pdf->newPage(Zend_Pdf_Page::SIZE_A4);
  289. $page3 = $pdf->newPage(Zend_Pdf_Page::SIZE_A4); // Page created, but not included into pages list
  290. $pdf->pages[] = $page1;
  291. $pdf->pages[] = $page2;
  292. require_once 'Zend/Pdf/Destination/Fit.php';
  293. $action1 = Zend_Pdf_Action_GoTo::create(Zend_Pdf_Destination_Fit::create($page2));
  294. $action2 = Zend_Pdf_Action_GoTo::create(Zend_Pdf_Destination_Fit::create($page3));
  295. $this->assertTrue($pdf->resolveDestination($action1->getDestination()) === $page2);
  296. $this->assertTrue($pdf->resolveDestination($action2->getDestination()) === null);
  297. }
  298. }