Sfoglia il codice sorgente

Merge pull request #375 from adeelnawaz/master

Fixes #374 - Implement Zend_Pdf::getJavascript() and Zend_Pdf::setJavascript()
Frank Brückner 11 anni fa
parent
commit
1a40000c16
3 ha cambiato i file con 238 aggiunte e 4 eliminazioni
  1. 77 4
      library/Zend/Pdf.php
  2. 2 0
      tests/Zend/AllTests.php
  3. 159 0
      tests/Zend/PdfTest.php

+ 77 - 4
library/Zend/Pdf.php

@@ -328,6 +328,7 @@ class Zend_Pdf
 
             $this->_loadNamedDestinations($this->_trailer->Root, $this->_parser->getPDFVersion());
             $this->_loadOutlines($this->_trailer->Root);
+            $this->_loadJavaScript($this->_trailer->Root);
 
             if ($this->_trailer->Info !== null) {
                 $this->properties = $this->_trailer->Info->toPhp();
@@ -575,6 +576,25 @@ class Zend_Pdf
     }
 
     /**
+     * Load JavaScript
+     * Populates the _javaScript string, for later use of getJavaScript method.
+     *
+     * @param Zend_Pdf_Element_Reference $root Document catalog entry
+     */
+    protected function _loadJavaScript(Zend_Pdf_Element_Reference $root)
+    {
+        if ($root->Names === null || $root->Names->JavaScript === null || $root->Names->JavaScript->Names === null) {
+            return;
+        }
+
+        foreach ($root->Names->JavaScript->Names->items as $item) {
+            if ($item instanceof Zend_Pdf_Element_Reference && $item->S->value === 'JavaScript') {
+                $this->_javaScript[] = $item->JS->value;
+            }
+        }
+    }
+
+    /**
      * Orginize pages to tha pages tree structure.
      *
      * @todo atomatically attach page to the document, if it's not done yet.
@@ -1387,17 +1407,70 @@ class Zend_Pdf
         }
     }
 
-
     /**
-     * Set the document-level JavaScript
+     * Sets the document-level JavaScript
      *
-     * @param string $javascript
+     * @param string|array $javascript
      */
     public function setJavaScript($javascript)
     {
-        $this->_javaScript = $javascript;
+        $this->resetJavaScript();
+
+        $this->addJavaScript($javascript);
+    }
+
+    /**
+     * Resets the document-level JavaScript
+     */
+    public function resetJavaScript()
+    {
+        $this->_javaScript = NULL;
+        $root = $this->_trailer->Root;
+
+        if ($root->Names === null || $root->Names->JavaScript === null) {
+            return;
+        }
+        $root->Names->JavaScript = NULL;
     }
 
+    /**
+     * Appends JavaScript to the document-level JavaScript
+     *
+     * @param string|array $javascript
+     */
+    public function addJavaScript($javascript)
+    {
+        if (empty($javascript)) {
+            throw new Zend_Pdf_Exception('JavaScript must be a non empty string or array of strings');
+        } else if (!is_array($javascript)) {
+            $javascript = array($javascript);
+        }
+
+        if (NULL === $this->_javaScript) {
+            $this->_javaScript = $javascript;
+        } else {
+            $this->_javaScript = array_merge($this->_javaScript, $javascript);
+        }
+
+        if (!empty($this->_javaScript)) {
+            $items = array();
+
+            foreach ($this->_javaScript as $javascript) {
+                $jsCode = array(
+                    'S' => new Zend_Pdf_Element_Name('JavaScript'),
+                    'JS' => new Zend_Pdf_Element_String($javascript)
+                );
+                $items[] = new Zend_Pdf_Element_String('EmbeddedJS');
+                $items[] = $this->_objFactory->newObject(new Zend_Pdf_Element_Dictionary($jsCode));
+            }
+
+            $jsRef = $this->_objFactory->newObject(new Zend_Pdf_Element_Dictionary(array('Names' => new Zend_Pdf_Element_Array($items))));
+            if (NULL === $this->_trailer->Root->Names) {
+                $this->_trailer->Root->Names = new Zend_Pdf_Element_Dictionary();
+            }
+            $this->_trailer->Root->Names->JavaScript = $jsRef;
+        }
+    }
 
     /**
      * Convert date to PDF format (it's close to ASN.1 (Abstract Syntax Notation

+ 2 - 0
tests/Zend/AllTests.php

@@ -76,6 +76,7 @@ require_once 'Zend/Oauth/AllTests.php';
 require_once 'Zend/OpenIdTest.php';
 require_once 'Zend/OpenId/AllTests.php';
 require_once 'Zend/Paginator/AllTests.php';
+require_once 'Zend/PdfTest.php';
 require_once 'Zend/Pdf/AllTests.php';
 require_once 'Zend/ProgressBar/AllTests.php';
 require_once 'Zend/Reflection/AllTests.php';
@@ -215,6 +216,7 @@ class Zend_AllTests
         $suite->addTest(Zend_Navigation_AllTests::suite());
         $suite->addTest(Zend_Oauth_AllTests::suite());
         $suite->addTest(Zend_Paginator_AllTests::suite());
+        $suite->addTestSuite('Zend_PdfTest');
         $suite->addTest(Zend_Pdf_AllTests::suite());
         $suite->addTest(Zend_ProgressBar_AllTests::suite());
         $suite->addTestSuite('Zend_RegistryTest');

+ 159 - 0
tests/Zend/PdfTest.php

@@ -0,0 +1,159 @@
+<?php
+
+/**
+ * Zend Framework
+ *
+ * LICENSE
+ *
+ * This source file is subject to the new BSD license that is bundled
+ * with this package in the file LICENSE.txt.
+ * It is also available through the world-wide-web at this URL:
+ * http://framework.zend.com/license/new-bsd
+ * If you did not receive a copy of the license and are unable to
+ * obtain it through the world-wide-web, please send an email
+ * to license@zend.com so we can send you a copy immediately.
+ *
+ * @category   Zend
+ * @package    Zend_Pdf
+ * @subpackage UnitTests
+ * @copyright  Copyright (c) 2005-2014 Zend Technologies USA Inc. (http://www.zend.com)
+ * @license    http://framework.zend.com/license/new-bsd     New BSD License
+ * @version    $Id$
+ */
+/**
+ * @see Zend_Pdf
+ */
+require_once 'Zend/Pdf.php';
+/**
+ * @see Zend_Pdf_Exception
+ */
+require_once 'Zend/Pdf/Exception.php';
+
+/**
+ * @category   Zend
+ * @package    Zend_Pdf
+ * @subpackage UnitTests
+ * @copyright  Copyright (c) 2005-2014 Zend Technologies USA Inc. (http://www.zend.com)
+ * @license    http://framework.zend.com/license/new-bsd     New BSD License
+ * @group      Zend_Pdf
+ */
+class Zend_PdfTest extends PHPUnit_Framework_TestCase
+{
+
+    public function testGetJavasriptNull()
+    {
+        // getting JavaScript without setting it returns NULL
+        $pdf = new Zend_Pdf();
+        $this->assertNull($pdf->getJavaScript());
+    }
+
+    public function testSetAndGetJavasriptArray()
+    {
+        // getting JavaScript after setting it returns array
+        $pdf = new Zend_Pdf();
+        $pdf->setJavaScript('print();');
+        $this->assertTrue(is_array($pdf->getJavaScript()));
+    }
+
+    public function testSetJavaScriptString()
+    {
+        // setting string value is possible
+        $pdf = new Zend_Pdf();
+        $javaScriptString = 'print();';
+        $pdf->setJavaScript($javaScriptString);
+        $javaScript = $pdf->getJavaScript();
+        $this->assertEquals($javaScriptString, $javaScript[0]);
+    }
+
+    public function testSetJavaScriptArray()
+    {
+        // setting string value is possible
+        $pdf = new Zend_Pdf();
+        $javaScriptArray = array('print();', 'alert();');
+        $pdf->setJavaScript($javaScriptArray);
+        $this->assertEquals($javaScriptArray, $pdf->getJavaScript());
+    }
+
+    public function testResetJavaScript()
+    {
+        // reset removes the added JavaScript
+        $pdf = new Zend_Pdf();
+        $pdf->setJavaScript('print();');
+        $pdf->resetJavaScript();
+        $this->assertNull($pdf->getJavaScript());
+    }
+
+    public function testAddJavaScript()
+    {
+        // adding JavaScript appends previously added JavaScript
+        $pdf = new Zend_Pdf();
+        $javaScriptArray = array('print();', 'alert();');
+        $pdf->addJavaScript($javaScriptArray[0]);
+        $pdf->addJavaScript($javaScriptArray[1]);
+        $this->assertEquals($javaScriptArray, $pdf->getJavaScript());
+    }
+
+    public function testSetJavaScriptEmptyString()
+    {
+        // setting empty JavaScript string throws exception
+        $pdf = new Zend_Pdf();
+        try {
+            $pdf->setJavaScript('');
+            $this->fail('Expected exception when trying to set empty string.');
+        } catch (Zend_Pdf_Exception $e) {
+            $this->assertContains('JavaScript must be a non empty string or array of strings', $e->getMessage());
+        }
+    }
+
+    public function testSetJavaScriptEmptyArray()
+    {
+        // setting empty JavaScript string throws exception
+        $pdf = new Zend_Pdf();
+        try {
+            $pdf->setJavaScript(array());
+            $this->fail('Expected exception when trying to set empty array.');
+        } catch (Zend_Pdf_Exception $e) {
+            $this->assertContains('JavaScript must be a non empty string or array of strings', $e->getMessage());
+        }
+    }
+
+    public function testSetAndSaveLoadAndGetJavaScript()
+    {
+        $tempFile = tempnam(sys_get_temp_dir(), 'PdfUnitFile');
+        $javaScript = array('print();', 'alert();');
+
+        $pdf = new Zend_Pdf();
+        $pdf->setJavaScript($javaScript);
+        $pdf->save($tempFile);
+        unset($pdf);
+
+        $pdf = Zend_Pdf::load($tempFile);
+        unlink($tempFile);
+
+        $this->assertEquals($javaScript, $pdf->getJavaScript());
+    }
+
+    public function testSetAndSaveLoadAndResetAndSaveLoadAndGetJavaScript()
+    {
+        $tempFile = tempnam(sys_get_temp_dir(), 'PdfUnitFile');
+        $javaScript = array('print();', 'alert();');
+
+        $pdf = new Zend_Pdf();
+        $pdf->setJavaScript($javaScript);
+        $pdf->save($tempFile);
+        unset($pdf);
+
+        $pdf = Zend_Pdf::load($tempFile);
+        unlink($tempFile);
+
+        $pdf->resetJavaScript();
+        $pdf->save($tempFile);
+        unset($pdf);
+
+        $pdf = Zend_Pdf::load($tempFile);
+        unlink($tempFile);
+
+        $this->assertNull($pdf->getJavaScript());
+    }
+
+}