_pdf = Zend_Pdf::load(__DIR__ . '/Pdf/_files/PdfWithFields.pdf'); } public function testGetTextFieldNames() { $fieldNames = $this->_pdf->getTextFieldNames(); //PDF with text fields must return array of text field names $this->assertEquals(array('Field1', 'Field2'), $fieldNames); } public function testGetTextFieldNamesNoFieldsEmptyArray() { $pdf = new Zend_Pdf(); $fieldNames = $pdf->getTextFieldNames(); //PDF with no text fields must return empty array $this->assertEquals(array(), $fieldNames); } public function testSetTextField() { try { $this->_pdf->setTextField('Field1', 'Value1'); $this->assertTrue(TRUE); //in case of --strict } catch (\Exception $e) { $this->fail('Failed to set an existing text field'); } } public function testSetTextFieldNonExistent() { //Setting a non-existent field shouls throw an exception try { $this->_pdf->setTextField('FieldNotExists', 'Value1'); $this->fail('Expected exception when trying to set a non-existent text field'); } catch (Zend_Pdf_Exception $e) { $this->assertEquals("Field 'FieldNotExists' does not exist or is not a textfield", $e->getMessage()); } } public function testSetTextFieldProperties() { try { $this->_pdf->setTextFieldProperties( 'Field1', Zend_Pdf::PDF_FORM_FIELD_READONLY ); $this->_pdf->setTextFieldProperties( 'Field1', Zend_Pdf::PDF_FORM_FIELD_REQUIRED ); $this->_pdf->setTextFieldProperties( 'Field1', Zend_Pdf::PDF_FORM_FIELD_NOEXPORT ); $this->_pdf->setTextFieldProperties( 'Field1', Zend_Pdf::PDF_FORM_FIELD_READONLY | Zend_Pdf::PDF_FORM_FIELD_REQUIRED | Zend_Pdf::PDF_FORM_FIELD_NOEXPORT ); $this->assertTrue(TRUE); //in case of --strict } catch (\Exception $e) { $this->fail('Failed to set property of an existing text field'); } } public function testSetTextFieldPropertiesNonExistent() { //Setting property of a non-existent field shouls throw an exception try { $this->_pdf->setTextFieldProperties('FieldNotExists', Zend_Pdf::PDF_FORM_FIELD_REQUIRED); $this->fail('Expected exception when trying to set property of a non-existent text field'); } catch (Zend_Pdf_Exception $e) { $this->assertEquals("Field 'FieldNotExists' does not exist or is not a textfield", $e->getMessage()); } } public function testMarkTextFieldAsReadOnly() { try { $this->_pdf->markTextFieldAsReadOnly('Field1'); $this->_pdf->markTextFieldAsReadOnly('Field2'); $this->assertTrue(TRUE); //in case of --strict } catch (\Exception $e) { $this->fail('Failed to set an existing text field as read-only'); } } public function testMarkTextFieldAsReadOnlyNonExistent() { //Setting property of a non-existent field shouls throw an exception try { $this->_pdf->markTextFieldAsReadOnly('FieldNotExists'); $this->fail('Expected exception when trying to set a non-existent text field as read-only'); } catch (Zend_Pdf_Exception $e) { $this->assertEquals("Field 'FieldNotExists' does not exist or is not a textfield", $e->getMessage()); } } 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()); } }