ActionTest.php 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464
  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_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-2009 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. $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('Sound');
  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('Movie');
  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('Hide');
  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('Named');
  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('SubmitForm');
  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('ResetForm');
  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('ImportData');
  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('JavaScript');
  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('SetOCGState');
  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('Rendition');
  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('Trans');
  132. $action2Dictionary->Next->items[] = $leafAction;
  133. $leafAction = new Zend_Pdf_Element_Dictionary();
  134. $leafAction->Type = new Zend_Pdf_Element_Name('Action');
  135. $leafAction->S = new Zend_Pdf_Element_Name('GoTo3DView');
  136. $action2Dictionary->Next->items[] = $leafAction;
  137. $action = Zend_Pdf_Action::load($dictionary);
  138. $actionsCount = 0;
  139. $iterator = new RecursiveIteratorIterator(new Zend_Pdf_RecursivelyIteratableObjectsContainer(array($action)),
  140. RecursiveIteratorIterator::SELF_FIRST);
  141. foreach ($iterator as $chainedAction) {
  142. $actionsCount++;
  143. }
  144. $this->assertEquals(20, $actionsCount);
  145. }
  146. public function testExtract()
  147. {
  148. $dictionary = new Zend_Pdf_Element_Dictionary();
  149. $dictionary->Type = new Zend_Pdf_Element_Name('Action');
  150. $dictionary->S = new Zend_Pdf_Element_Name('GoToR');
  151. $dictionary->D = new Zend_Pdf_Element_String('SomeNamedDestination');
  152. $action2Dictionary = new Zend_Pdf_Element_Dictionary();
  153. $action2Dictionary->Type = new Zend_Pdf_Element_Name('Action');
  154. $action2Dictionary->S = new Zend_Pdf_Element_Name('Thread');
  155. $action2Dictionary->D = new Zend_Pdf_Element_String('NamedDestination 2');
  156. $action2Dictionary->Next = new Zend_Pdf_Element_Array();
  157. $dictionary->Next = $action2Dictionary;
  158. $leafAction = new Zend_Pdf_Element_Dictionary();
  159. $leafAction->Type = new Zend_Pdf_Element_Name('Action');
  160. $leafAction->S = new Zend_Pdf_Element_Name('GoTo');
  161. $leafAction->D = new Zend_Pdf_Element_String('NamedDestination 3');
  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('GoToR');
  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('GoToE');
  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('Launch');
  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('Thread');
  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('URI');
  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. if (!version_compare(phpversion(), '5.3.0', '>=')) {
  352. $this->markTestSkipped('PHP Version must be 5.3.0 or higher');
  353. }
  354. try {
  355. $file = '_files/ZF-8462.pdf';
  356. $pdf = Zend_Pdf::load($file);
  357. } catch (Zend_Pdf_Exception $e) {
  358. // skip this Exception because that should happen
  359. $error = error_get_last();
  360. if ($error !== null && $error['type'] == E_WARNING) {
  361. $this->fail('The expected bug exists. Please verify.');
  362. }
  363. // nothing happen no bug?
  364. return;
  365. }
  366. $this->fail('An expected Exception has never been raised.');
  367. }
  368. }