Skip to content
This repository has been archived by the owner on May 16, 2018. It is now read-only.

Fixes #374 - Implement Zend_Pdf::getJavascript() and Zend_Pdf::setJavascript() #375

Merged
merged 3 commits into from
Jun 26, 2014
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
81 changes: 77 additions & 4 deletions library/Zend/Pdf.php
Original file line number Diff line number Diff line change
Expand Up @@ -328,6 +328,7 @@ public function __construct($source = null, $revision = null, $load = false)

$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();
Expand Down Expand Up @@ -574,6 +575,25 @@ protected function _loadOutlines(Zend_Pdf_Element_Reference $root)
}
}

/**
* 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.
*
Expand Down Expand Up @@ -1387,17 +1407,70 @@ public function render($newSegmentOnly = false, $outputStream = null)
}
}


/**
* 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
Expand Down
2 changes: 2 additions & 0 deletions tests/Zend/AllTests.php
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,7 @@
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';
Expand Down Expand Up @@ -215,6 +216,7 @@ public static function suite()
$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');
Expand Down
159 changes: 159 additions & 0 deletions tests/Zend/PdfTest.php
Original file line number Diff line number Diff line change
@@ -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 [email protected] 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()
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Typo: should be testGetJavaScript.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I had an illusion that Zend_Pdf has methods like Javascript and not JavaScript. I must have mixed it up with another library. I'll fix that asap.

{
// getting JavaScript without setting it returns NULL
$pdf = new Zend_Pdf();
$this->assertNull($pdf->getJavaScript());
}

public function testSetAndGetJavasriptArray()
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

And here.

{
// 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());
}

}