LDAPAccountManager/lam/tests/lib/pdfstructTest.php

245 lines
7.2 KiB
PHP
Raw Normal View History

2017-06-25 07:47:32 +00:00
<?php
2020-05-07 19:10:47 +00:00
use LAM\PDF\PDFSectionEntry;
2017-06-25 12:23:28 +00:00
use LAM\PDF\PDFTextSection;
use LAM\PDF\PDFEntrySection;
use LAM\PDF\PDFStructureReader;
2017-06-25 12:47:33 +00:00
use LAM\PDF\PDFStructure;
2017-07-02 07:00:14 +00:00
use LAM\PDF\PDFStructureWriter;
2019-11-28 20:18:25 +00:00
use PHPUnit\Framework\TestCase;
2017-06-25 12:23:28 +00:00
2017-06-25 07:47:32 +00:00
/*
This code is part of LDAP Account Manager (http://www.ldap-account-manager.org/)
Copyright (C) 2017 Roland Gruber
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
*/
2020-05-07 19:10:47 +00:00
include_once __DIR__ . '/../../lib/pdfstruct.inc';
2017-06-25 07:47:32 +00:00
/**
2020-05-07 19:31:21 +00:00
* Tests classes in pdfstruct.inc.
2017-06-25 07:47:32 +00:00
*
* @author Roland Gruber
*
*/
2020-05-07 19:31:21 +00:00
class PdfStructTest extends TestCase {
2017-06-25 07:47:32 +00:00
/**
* Reads the sample structure.
*/
public function testRead() {
2017-06-25 12:23:28 +00:00
$reader = $this->getMockBuilder('\LAM\PDF\PDFStructureReader')
2020-05-07 18:22:41 +00:00
->setConstructorArgs(array('test'))
2017-06-25 12:23:28 +00:00
->setMethods(array('getFileName'))
->getMock();
$reader->method('getFileName')->willReturn($this->getTestFileName('test.xml'));
$structure = $reader->read('type', 'name');
2017-06-25 07:47:32 +00:00
$this->assertEquals('printLogo.jpg', $structure->getLogo());
$this->assertEquals('User information', $structure->getTitle());
2017-06-25 12:47:33 +00:00
$this->assertEquals(PDFStructure::FOLDING_STANDARD, $structure->getFoldingMarks());
2017-06-25 12:23:28 +00:00
$sections = $structure->getSections();
2017-07-02 07:00:14 +00:00
$this->assertEquals(4, sizeof($sections));
2017-06-25 12:23:28 +00:00
// check first section
$this->assertInstanceOf(PDFEntrySection::class, $sections[0]);
$this->assertFalse($sections[0]->isAttributeTitle());
$this->assertEquals('Personal user information', $sections[0]->getTitle());
$entries = $sections[0]->getEntries();
$this->assertEquals(3, sizeof($entries));
$this->assertEquals('inetOrgPerson_givenName', $entries[0]->getKey());
$this->assertEquals('inetOrgPerson_sn', $entries[1]->getKey());
$this->assertEquals('inetOrgPerson_street', $entries[2]->getKey());
// check text section
$this->assertInstanceOf(PDFTextSection::class, $sections[1]);
$this->assertEquals('test text', $sections[1]->getText());
// check third section
$this->assertInstanceOf(PDFEntrySection::class, $sections[2]);
$this->assertTrue($sections[2]->isAttributeTitle());
$this->assertEquals('posixAccount_uid', $sections[2]->getPdfKey());
$entries = $sections[2]->getEntries();
$this->assertEquals(2, sizeof($entries));
$this->assertEquals('posixAccount_homeDirectory', $entries[0]->getKey());
$this->assertEquals('posixAccount_loginShell', $entries[1]->getKey());
2017-07-02 07:00:14 +00:00
// check fourth section
$this->assertInstanceOf(PDFEntrySection::class, $sections[3]);
$this->assertFalse($sections[3]->isAttributeTitle());
$this->assertEquals('No entries', $sections[3]->getTitle());
$entries = $sections[3]->getEntries();
$this->assertEquals(0, sizeof($entries));
2017-06-25 12:23:28 +00:00
}
/**
* Returns the full path to the given file name.
*
* @param string $file file name
*/
private function getTestFileName($file) {
return dirname(dirname(__FILE__)) . '/resources/pdf/' . $file;
2017-06-25 07:47:32 +00:00
}
2017-07-02 07:00:14 +00:00
/**
* Tests if the output is the same as the original PDF.
*/
public function testWrite() {
$file = $this->getTestFileName('writer.xml');
// read input XML
$fileHandle = fopen($file, "r");
$originalXML = fread($fileHandle, 1000000);
fclose($fileHandle);
// read structure
$reader = $this->getMockBuilder('\LAM\PDF\PDFStructureReader')
2020-05-07 18:22:41 +00:00
->setConstructorArgs(array('test'))
2017-07-02 07:00:14 +00:00
->setMethods(array('getFileName'))
->getMock();
$reader->method('getFileName')->willReturn($file);
$structure = $reader->read('type', 'name');
// create writer and get output XML
2020-05-07 18:27:50 +00:00
$writer = new PDFStructureWriter('test');
2017-07-02 07:00:14 +00:00
$xml = $writer->getXML($structure);
// compare
$this->assertEquals($originalXML, $xml);
}
2020-05-07 19:31:21 +00:00
/**
* Tests PDFTextSection
*/
public function testExportPDFTextSection() {
2020-05-07 19:10:47 +00:00
$section = new PDFTextSection('sometext');
$data = $section->export();
$this->assertEquals('sometext', $data);
}
2020-05-07 19:31:21 +00:00
/**
* Tests PDFEntrySection
*/
public function testExportPDFEntrySection() {
2020-05-07 19:10:47 +00:00
$section = new PDFEntrySection('mytitle');
$section->setEntries(array(new PDFSectionEntry('key1'), new PDFSectionEntry('key2')));
$data = $section->export();
$expected = array(
'title' => 'mytitle',
'entries' => array('key1', 'key2')
);
$this->assertEquals($expected, $data);
}
2020-05-07 19:31:21 +00:00
/**
* Tests PDFStructure
*/
public function testExportPDFStructure() {
2020-05-07 19:10:47 +00:00
$structure = new PDFStructure();
$structure->setFoldingMarks(PDFStructure::FOLDING_STANDARD);
$structure->setLogo('somelogo');
$structure->setTitle('mytitle');
$entrySection = new PDFEntrySection('sometitle');
$entrySection->setEntries(array(new PDFSectionEntry('key1')));
$structure->setSections(array(
new PDFTextSection('sometext'),
$entrySection
));
$data = $structure->export();
$expected = array(
'title' => 'mytitle',
'foldingMarks' => PDFStructure::FOLDING_STANDARD,
'logo' => 'somelogo',
'sections' => array(
array(
'type' => 'text',
'data' => 'sometext'
),
array(
'type' => 'entry',
'data' => array(
'title' => 'sometitle',
'entries' => array('key1')
)
)
)
);
$this->assertEquals($expected, $data);
}
2020-05-12 18:58:56 +00:00
/**
* Tests import in PDFEntrySection.
*/
public function testImportPDFEntrySection() {
$data = array(
'title' => 'mytitle',
'entries' => array('e1', 'e2')
);
$section = new PDFEntrySection(null);
$section->import($data);
$this->assertEquals('mytitle', $section->getTitle());
$entries = $section->getEntries();
$this->assertEquals(2, sizeof($entries));
$this->assertEquals('e1', ($entries[0]->getKey()));
$this->assertEquals('e2', ($entries[1]->getKey()));
}
/**
* Tests the import in PDFStructure.
*/
public function testImportPDFStructure() {
$data = array(
'title' => 'mytitle',
'foldingMarks' => PDFStructure::FOLDING_STANDARD,
'logo' => 'logo',
'sections' => array(
array(
'type' => 'text',
'data' => 'textvalue'
),
array(
'type' => 'entry',
'data' => array(
'title' => 'etitle',
'entries' => array('e1', 'e2')
)
),
)
);
$structure = new PDFStructure();
$structure->import($data);
$this->assertEquals('mytitle', $structure->getTitle());
$this->assertEquals(PDFStructure::FOLDING_STANDARD, $structure->getFoldingMarks());
$this->assertEquals('logo', $structure->getLogo());
$sections = $structure->getSections();
$this->assertEquals(2, sizeof($sections));
$this->assertTrue($sections[0] instanceof PDFTextSection);
$this->assertEquals('textvalue', $sections[0]->getText());
$this->assertTrue($sections[1] instanceof PDFEntrySection);
$entries = $sections[1]->getEntries();
$this->assertEquals(2, sizeof($entries));
$this->assertEquals('e1', $entries[0]->getKey());
$this->assertEquals('e2', $entries[1]->getKey());
}
2020-05-07 19:10:47 +00:00
}
2017-06-25 07:47:32 +00:00
?>