loadFile( $from ); $document = new ezcDocumentXhtml(); $document->createFromDocbook( $docbook ); // Store test file, to have something to compare on failure $tempDir = $this->createTempDir( 'docbook_xhtml_' ) . '/'; file_put_contents( $tempDir . basename( $to ), $xml = $document->save() ); $this->assertEquals( file_get_contents( $to ), $xml, 'Document not visited as expected.' ); } public static function getRstTestDocuments() { if ( self::$rstTestDocuments === null ) { // Get a list of all test files from the respektive folder $testFiles = glob( dirname( __FILE__ ) . '/files/xhtml/rst_html/s_*.html' ); // Create array with the test file and the expected result file foreach ( $testFiles as $file ) { self::$rstTestDocuments[] = array( $file, substr( $file, 0, -4 ) . 'xml' ); } } return self::$rstTestDocuments; return array_slice( self::$rstTestDocuments, 0, 27 ); } public static function getMetadataTestDocuments() { if ( self::$metadataTestDocuments === null ) { // Get a list of all test files from the respektive folder $testFiles = glob( dirname( __FILE__ ) . '/files/xhtml/metadata/s_*.html' ); // Create array with the test file and the expected result file foreach ( $testFiles as $file ) { self::$metadataTestDocuments[] = array( $file, substr( $file, 0, -4 ) . 'xml' ); } } return self::$metadataTestDocuments; return array_slice( self::$metadataTestDocuments, 0, 27 ); } public static function getBadTestDocuments() { if ( self::$badTestDocuments === null ) { // Get a list of all test files from the respektive folder $testFiles = glob( dirname( __FILE__ ) . '/files/xhtml/bad_markup/s_*.html' ); // Create array with the test file and the expected result file foreach ( $testFiles as $file ) { self::$badTestDocuments[] = array( $file, substr( $file, 0, -4 ) . 'xml' ); } } return self::$badTestDocuments; return array_slice( self::$badTestDocuments, 6, 1 ); } public static function getTableTestDocuments() { if ( self::$tableTestDocuments === null ) { // Get a list of all test files from the respektive folder $testFiles = glob( dirname( __FILE__ ) . '/files/xhtml/table/s_*.html' ); // Create array with the test file and the expected result file foreach ( $testFiles as $file ) { self::$tableTestDocuments[] = array( $file, substr( $file, 0, -4 ) . 'xml' ); } } return self::$tableTestDocuments; return array_slice( self::$tableTestDocuments, 6, 1 ); } /** * Check docbook for validity * * Check the provided docbook document, that it is valid docbook XML. * * @param DOMDocument $document * @return void */ protected function checkDocbook( DOMDocument $document ) { // Reload document to reassign elements to namespaces. $xml = $document->saveXml(); $document = new DOMDocument(); $document->loadXml( $xml ); $oldSetting = libxml_use_internal_errors( true ); $document->schemaValidate( __DIR__ . '/files/schemas/docbook.xsd' ); // Severity types of XML errors $errorTypes = array( LIBXML_ERR_WARNING => 'Warning', LIBXML_ERR_ERROR => 'Error', LIBXML_ERR_FATAL => 'Fatal error', ); // Get all errors $xmlErrors = libxml_get_errors(); $errors = array(); foreach ( $xmlErrors as $error ) { $errors[] = sprintf( "%s in %d:%d: %s.", $errorTypes[$error->level], $error->line, $error->column, str_replace( '{http://docbook.org/ns/docbook}', 'docbook:', trim( $error->message ) ) ); } libxml_clear_errors(); libxml_use_internal_errors( $oldSetting ); $this->assertEquals( array(), $errors, 'Docbook document is not valid.' ); } /** * @dataProvider getRstTestDocuments */ public function testParseRstFile( $from, $to ) { if ( !is_file( $to ) ) { $this->markTestSkipped( "Comparision file '$to' not yet defined." ); } $document = new ezcDocumentXhtml(); $document->loadFile( $from ); $docbook = $document->getAsDocbook(); $xml = $docbook->save(); // Store test file, to have something to compare on failure $tempDir = $this->createTempDir( 'xhtml_rst_' ) . '/'; file_put_contents( $tempDir . basename( $to ), $xml ); // We need a proper XSD first, the current one does not accept legal // XML. // $this->checkDocbook( $docbook->getDomDocument() ); $this->assertEquals( file_get_contents( $to ), $xml, 'Document not visited as expected.' ); // Remove tempdir, when nothing failed. $this->removeTempDir(); } /** * @dataProvider getMetadataTestDocuments */ public function testExtractMetadata( $from, $to ) { if ( !is_file( $to ) ) { $this->markTestSkipped( "Comparision file '$to' not yet defined." ); } $document = new ezcDocumentXhtml(); $document->loadFile( $from ); $docbook = $document->getAsDocbook(); $xml = $docbook->save(); // Store test file, to have something to compare on failure $tempDir = $this->createTempDir( 'xhtml_metadata_' ) . '/'; file_put_contents( $tempDir . basename( $to ), $xml ); // We need a proper XSD first, the current one does not accept legal // XML. // $this->checkDocbook( $docbook->getDomDocument() ); $this->assertEquals( file_get_contents( $to ), $xml, 'Document not visited as expected.' ); // Remove tempdir, when nothing failed. $this->removeTempDir(); } /** * @dataProvider getBadTestDocuments */ public function testConvertBadMarkup( $from, $to ) { if ( !is_file( $to ) ) { $this->markTestSkipped( "Comparision file '$to' not yet defined." ); } $document = new ezcDocumentXhtml(); $document->setFilters( array( new ezcDocumentXhtmlElementFilter(), new ezcDocumentXhtmlMetadataFilter(), new ezcDocumentXhtmlContentLocatorFilter(), ) ); $document->loadFile( $from ); $docbook = $document->getAsDocbook(); $xml = $docbook->save(); // Store test file, to have something to compare on failure $tempDir = $this->createTempDir( 'xhtml_bad_' ) . '/'; file_put_contents( $tempDir . basename( $to ), $xml ); // We need a proper XSD first, the current one does not accept legal // XML. // $this->checkDocbook( $docbook->getDomDocument() ); $this->assertEquals( file_get_contents( $to ), $xml, 'Document not visited as expected.' ); // Remove tempdir, when nothing failed. $this->removeTempDir(); } public function testXpathFilter() { $from = dirname( __FILE__ ) . '/files/xhtml/xpath/s_004_detect_url_in_texts.html'; $to = dirname( __FILE__ ) . '/files/xhtml/xpath/s_004_detect_url_in_texts.xml'; $document = new ezcDocumentXhtml(); $document->setFilters( array( new ezcDocumentXhtmlXpathFilter( '//div[@class = "content"]' ), new ezcDocumentXhtmlElementFilter(), new ezcDocumentXhtmlMetadataFilter(), ) ); $document->loadFile( $from ); $docbook = $document->getAsDocbook(); $xml = $docbook->save(); // Store test file, to have something to compare on failure $tempDir = $this->createTempDir( 'xpath_filter_' ) . '/'; file_put_contents( $tempDir . basename( $to ), $xml ); // We need a proper XSD first, the current one does not accept legal // XML. // $this->checkDocbook( $docbook->getDomDocument() ); $this->assertEquals( file_get_contents( $to ), $xml, 'Document not visited as expected.' ); // Remove tempdir, when nothing failed. $this->removeTempDir(); } /** * @dataProvider getTableTestDocuments */ public function testTableDetector( $from, $to ) { if ( !is_file( $to ) ) { $this->markTestSkipped( "Comparision file '$to' not yet defined." ); } $document = new ezcDocumentXhtml(); $document->setFilters( array( new ezcDocumentXhtmlElementFilter(), new ezcDocumentXhtmlMetadataFilter(), new ezcDocumentXhtmlContentLocatorFilter(), new ezcDocumentXhtmlTablesFilter(), ) ); $document->loadFile( $from ); $docbook = $document->getAsDocbook(); $xml = $docbook->save(); // Store test file, to have something to compare on failure $tempDir = $this->createTempDir( 'xhtml_table_' ) . '/'; file_put_contents( $tempDir . basename( $to ), $xml ); // We need a proper XSD first, the current one does not accept legal // XML. // $this->checkDocbook( $docbook->getDomDocument() ); $this->assertEquals( file_get_contents( $to ), $xml, 'Document not visited as expected.' ); // Remove tempdir, when nothing failed. $this->removeTempDir(); } } ?>