Addition of a parser test for page= parameter of image inclusion
authorTpt <thomaspt@hotmail.fr>
Wed, 18 Dec 2013 17:56:15 +0000 (18:56 +0100)
committerTpt <thomaspt@hotmail.fr>
Tue, 29 Apr 2014 17:55:44 +0000 (19:55 +0200)
Support for DjVu is detected and parser tests that rely on it are disabled if needed.

Introduce DjVuSupport to easily detect DjVu support in unit tests

Change-Id: I53fd7b54e765d5f349abe74481bbc6f62f2b349e

tests/TestsAutoLoader.php
tests/parser/parserTest.inc
tests/parser/parserTests.txt
tests/phpunit/includes/media/DjVuTest.php
tests/phpunit/includes/parser/NewParserTest.php
tests/phpunit/mocks/media/MockDjVuHandler.php [new file with mode: 0644]
tests/testHelpers.inc

index 29c3269..a5eb29b 100644 (file)
@@ -34,6 +34,7 @@ $wgAutoloadClasses += array(
        'TestFileIterator' => "$testDir/testHelpers.inc",
        'TestRecorder' => "$testDir/testHelpers.inc",
        'ITestRecorder' => "$testDir/testHelpers.inc",
+       'DjVuSupport' => "$testDir/testHelpers.inc",
 
        # tests/phpunit
        'MediaWikiTestCase' => "$testDir/phpunit/MediaWikiTestCase.php",
@@ -92,6 +93,7 @@ $wgAutoloadClasses += array(
        'MockBitmapHandler' => "$testDir/phpunit/mocks/media/MockBitmapHandler.php",
        'MockImageHandler' => "$testDir/phpunit/mocks/media/MockImageHandler.php",
        'MockSvgHandler' => "$testDir/phpunit/mocks/media/MockSvgHandler.php",
+       'MockDjVuHandler' => "$testDir/phpunit/mocks/media/MockDjVuHandler.php",
 
        # tests/parser
        'NewParserTest' => "$testDir/phpunit/includes/parser/NewParserTest.php",
index 0899d46..b4e903d 100644 (file)
@@ -64,6 +64,11 @@ class ParserTest {
         */
        private $dbClone;
 
+       /**
+        * @var DjVuSupport
+        */
+       private $djVuSupport;
+
        /**
         * @var string $oldTablePrefix Original table prefix
         */
@@ -136,6 +141,8 @@ class ParserTest {
                $this->runDisabled = isset( $options['run-disabled'] );
                $this->runParsoid = isset( $options['run-parsoid'] );
 
+               $this->djVuSupport = new DjVuSupport();
+
                $this->hooks = array();
                $this->functionHooks = array();
                self::setUp();
@@ -549,6 +556,12 @@ class ParserTest {
                $user = $context->getUser();
                $options = ParserOptions::newFromContext( $context );
 
+               if ( isset( $opts['djvu'] ) ) {
+                       if ( !$this->djVuSupport->isEnabled() ) {
+                               return $this->showSkipped();
+                       }
+               }
+
                if ( isset( $opts['title'] ) ) {
                        $titleText = $opts['title'];
                } else {
@@ -1014,6 +1027,44 @@ class ParserTest {
                        'sha1' => wfBaseConvert( '3', 16, 36, 31 ),
                        'fileExists' => true
                ), $this->db->timestamp( '20010115123500' ), $user );
+
+               # A DjVu file
+               $image = wfLocalFile( Title::makeTitle( NS_FILE, 'LoremIpsum.djvu' ) );
+               $image->recordUpload2( '', 'Upload a DjVu', 'A DjVu', array(
+                       'size' => 3249,
+                       'width' => 2480,
+                       'height' => 3508,
+                       'media_type' => MEDIATYPE_BITMAP,
+                       'mime' => 'image/vnd.djvu',
+                       'metadata' => '<?xml version="1.0" ?>
+<!DOCTYPE DjVuXML PUBLIC "-//W3C//DTD DjVuXML 1.1//EN" "pubtext/DjVuXML-s.dtd">
+<DjVuXML>
+<HEAD></HEAD>
+<BODY><OBJECT height="3508" width="2480">
+<PARAM name="DPI" value="300" />
+<PARAM name="GAMMA" value="2.2" />
+</OBJECT>
+<OBJECT height="3508" width="2480">
+<PARAM name="DPI" value="300" />
+<PARAM name="GAMMA" value="2.2" />
+</OBJECT>
+<OBJECT height="3508" width="2480">
+<PARAM name="DPI" value="300" />
+<PARAM name="GAMMA" value="2.2" />
+</OBJECT>
+<OBJECT height="3508" width="2480">
+<PARAM name="DPI" value="300" />
+<PARAM name="GAMMA" value="2.2" />
+</OBJECT>
+<OBJECT height="3508" width="2480">
+<PARAM name="DPI" value="300" />
+<PARAM name="GAMMA" value="2.2" />
+</OBJECT>
+</BODY>
+</DjVuXML>',
+                       'sha1' => wfBaseConvert( '', 16, 36, 31 ),
+                       'fileExists' => true
+               ), $this->db->timestamp( '20010115123600' ), $user );
        }
 
        public function teardownDatabase() {
@@ -1092,6 +1143,9 @@ class ParserTest {
                        '<?xml version="1.0" encoding="utf-8"?>' .
                        '<svg xmlns="http://www.w3.org/2000/svg"' .
                        ' version="1.1" width="240" height="180"/>' );
+               wfMkdirParents( $dir . '/5/5f', null, __METHOD__ );
+               copy( "$IP/tests/phpunit/data/media/LoremIpsum.djvu", "$dir/5/5f/LoremIpsum.djvu" );
+
                return $dir;
        }
 
@@ -1159,6 +1213,11 @@ class ParserTest {
 
                                "$dir/0/09/Bad.jpg",
 
+                               "$dir/5/5f/LoremIpsum.djvu",
+                               "$dir/thumb/5/5f/LoremIpsum.djvu/page2-2480px-LoremIpsum.djvu.jpg",
+                               "$dir/thumb/5/5f/LoremIpsum.djvu/page2-3720px-LoremIpsum.djvu.jpg",
+                               "$dir/thumb/5/5f/LoremIpsum.djvu/page2-4960px-LoremIpsum.djvu.jpg",
+
                                "$dir/f/ff/Foobar.svg",
                                "$dir/thumb/f/ff/Foobar.svg/180px-Foobar.svg.png",
                                "$dir/thumb/f/ff/Foobar.svg/2000px-Foobar.svg.png",
@@ -1190,6 +1249,11 @@ class ParserTest {
                                "$dir/thumb/f/",
                                "$dir/0/09/",
                                "$dir/0/",
+                               "$dir/5/5f",
+                               "$dir/5",
+                               "$dir/thumb/5/5f/LoremIpsum.djvu",
+                               "$dir/thumb/5/5f",
+                               "$dir/thumb/5",
                                "$dir/thumb",
                                "$dir/math/f/a/5",
                                "$dir/math/f/a",
@@ -1283,6 +1347,19 @@ class ParserTest {
                return false;
        }
 
+       /**
+        * Print a skipped message.
+        *
+        * @return boolean
+        */
+       protected function showSkipped() {
+               if ( $this->showProgress ) {
+                       print $this->term->color( '1;33' ) . 'SKIPPED' . $this->term->reset() . "\n";
+               }
+
+               return true;
+       }
+
        /**
         * Run given strings through a diff and return the (colorized) output.
         * Requires writable /tmp directory and a 'diff' command in the PATH.
index e2bf9f6..c4bd12d 100644 (file)
@@ -7519,7 +7519,7 @@ Magic Word: {{NUMBEROFFILES}}
 !! wikitext
 {{NUMBEROFFILES}}
 !! html
-<p>4
+<p>5
 </p>
 !! end
 
@@ -15305,6 +15305,17 @@ Width-sized image (using px, with preceding whitespace - test regression from r3
 </p>
 !!end
 
+!! test
+Image with page parameter
+!! options
+djvu
+!! wikitext
+[[File:LoremIpsum.djvu|page=2]]
+!! html
+<p><a href="/index.php?title=File:LoremIpsum.djvu&amp;page=2" class="image"><img alt="LoremIpsum.djvu" src="http://example.com/images/thumb/5/5f/LoremIpsum.djvu/page2-2480px-LoremIpsum.djvu.jpg" width="2480" height="3508" srcset="http://example.com/images/thumb/5/5f/LoremIpsum.djvu/page2-3720px-LoremIpsum.djvu.jpg 1.5x, http://example.com/images/thumb/5/5f/LoremIpsum.djvu/page2-4960px-LoremIpsum.djvu.jpg 2x" /></a>
+</p>
+!! end
+
 !! test
 Another italics / bold test
 !! wikitext
index c61ed22..537c124 100644 (file)
@@ -20,20 +20,13 @@ class DjVuTest extends MediaWikiTestCase {
        protected $handler;
 
        protected function setUp() {
-               global $wgDjvuRenderer, $wgDjvuDump, $wgDjvuToXML;
                parent::setUp();
 
                //cli tool setup
-               $wgDjvuRenderer = $wgDjvuRenderer ? $wgDjvuRenderer : '/usr/bin/ddjvu';
-               $wgDjvuDump = $wgDjvuDump ? $wgDjvuDump : '/usr/bin/djvudump';
-               $wgDjvuToXML = $wgDjvuToXML ? $wgDjvuToXML : '/usr/bin/djvutoxml';
-               if (
-                       !$this->checkIfToolExists( $wgDjvuRenderer ) ||
-                       !$this->checkIfToolExists( $wgDjvuDump ) ||
-                       !$this->checkIfToolExists( $wgDjvuToXML )
-               ) {
-                       $this->markTestSkipped( 'This test needs the installation of the '
-                               . 'ddjvu, djvutoxml and djvudump tools' );
+               $djvuSupport = new DjVuSupport();
+
+               if ( !$djvuSupport->isEnabled() ) {
+                       $this->markTestSkipped( 'This test needs the installation of the ddjvu, djvutoxml and djvudump tools' );
                }
 
                //file repo setup
@@ -53,19 +46,6 @@ class DjVuTest extends MediaWikiTestCase {
                $this->handler = new DjVuHandler();
        }
 
-       /**
-        * Check if a tool exist
-        *
-        * @param string $path path to the tool
-        * @return bool
-        */
-       protected function checkIfToolExists( $path ) {
-               wfSuppressWarnings();
-               $result = file_exists( $path );
-               wfRestoreWarnings();
-               return $result;
-       }
-
        protected function dataFile( $name, $type ) {
                return new UnregisteredLocalFile(
                        false,
index f3c499b..66ed020 100644 (file)
@@ -31,6 +31,11 @@ class NewParserTest extends MediaWikiTestCase {
        public $fuzzSeed = 0;
        public $memoryLimit = 50;
 
+       /**
+        * @var DjVuSupport
+        */
+       private $djVuSupport;
+
        protected $file = false;
 
        public static function setUpBeforeClass() {
@@ -130,6 +135,9 @@ class NewParserTest extends MediaWikiTestCase {
                // Vector images have to be handled slightly differently
                $tmpGlobals['wgMediaHandlers']['image/svg+xml'] = 'MockSvgHandler';
 
+               // DjVu images have to be handled slightly differently
+               $tmpGlobals['wgMediaHandlers']['image/vnd.djvu'] = 'MockDjVuHandler';
+
                $tmpHooks = $wgHooks;
                $tmpHooks['ParserTestParser'][] = 'ParserTestParserHook::setup';
                $tmpHooks['ParserGetVariableValueTs'][] = 'ParserTest::getFakeTimestamp';
@@ -138,6 +146,9 @@ class NewParserTest extends MediaWikiTestCase {
                # proper precedence when resolving links. (bug 51680)
                $tmpGlobals['wgExtraNamespaces'] = array( 100 => 'MemoryAlpha' );
 
+               //DjVu support
+               $this->djVuSupport = new DjVuSupport();
+
                $this->setMwGlobals( $tmpGlobals );
 
                $this->savedWeirdGlobals['image_alias'] = $wgNamespaceAliases['Image'];
@@ -273,6 +284,47 @@ class NewParserTest extends MediaWikiTestCase {
                                        'fileExists'  => true
                        ), $this->db->timestamp( '20010115123500' ), $user );
                }
+
+               # A DjVu file
+               $image = wfLocalFile( Title::makeTitle( NS_FILE, 'LoremIpsum.djvu' ) );
+               if ( !$this->db->selectField( 'image', '1', array( 'img_name' => $image->getName() ) ) ) {
+                       $image->recordUpload2( '', 'Upload a DjVu', 'A DjVu', array(
+                               'size' => 3249,
+                               'width' => 2480,
+                               'height' => 3508,
+                               'bits' => 24,
+                               'media_type' => MEDIATYPE_BITMAP,
+                               'mime' => 'image/vnd.djvu',
+                               'metadata' => '<?xml version="1.0" ?>
+<!DOCTYPE DjVuXML PUBLIC "-//W3C//DTD DjVuXML 1.1//EN" "pubtext/DjVuXML-s.dtd">
+<DjVuXML>
+<HEAD></HEAD>
+<BODY><OBJECT height="3508" width="2480">
+<PARAM name="DPI" value="300" />
+<PARAM name="GAMMA" value="2.2" />
+</OBJECT>
+<OBJECT height="3508" width="2480">
+<PARAM name="DPI" value="300" />
+<PARAM name="GAMMA" value="2.2" />
+</OBJECT>
+<OBJECT height="3508" width="2480">
+<PARAM name="DPI" value="300" />
+<PARAM name="GAMMA" value="2.2" />
+</OBJECT>
+<OBJECT height="3508" width="2480">
+<PARAM name="DPI" value="300" />
+<PARAM name="GAMMA" value="2.2" />
+</OBJECT>
+<OBJECT height="3508" width="2480">
+<PARAM name="DPI" value="300" />
+<PARAM name="GAMMA" value="2.2" />
+</OBJECT>
+</BODY>
+</DjVuXML>',
+                               'sha1' => wfBaseConvert( '', 16, 36, 31 ),
+                               'fileExists' => true
+                       ), $this->db->timestamp( '20140115123600' ), $user );
+               }
        }
 
        //ParserTest setup/teardown functions
@@ -446,6 +498,10 @@ class NewParserTest extends MediaWikiTestCase {
                $backend->store( array(
                        'src' => "$IP/skins/monobook/headbg.jpg", 'dst' => "$base/local-public/0/09/Bad.jpg"
                ) );
+               $backend->prepare( array( 'dir' => "$base/local-public/5/5f" ) );
+               $backend->store( array(
+                       'src' => "$IP/tests/phpunit/data/media/LoremIpsum.djvu", 'dst' => "$base/local-public/5/5f/LoremIpsum.djvu"
+               ) );
 
                // No helpful SVG file to copy, so make one ourselves
                $data = '<?xml version="1.0" encoding="utf-8"?>' .
@@ -525,6 +581,11 @@ class NewParserTest extends MediaWikiTestCase {
 
                                "$base/local-public/0/09/Bad.jpg",
 
+                               "$base/local-public/5/5f/LoremIpsum.djvu",
+                               "$base/local-thumb/5/5f/LoremIpsum.djvu/page2-2480px-LoremIpsum.djvu.jpg",
+                               "$base/local-thumb/5/5f/LoremIpsum.djvu/page2-3720px-LoremIpsum.djvu.jpg",
+                               "$base/local-thumb/5/5f/LoremIpsum.djvu/page2-4960px-LoremIpsum.djvu.jpg",
+
                                "$base/local-public/f/ff/Foobar.svg",
                                "$base/local-thumb/f/ff/Foobar.svg/180px-Foobar.svg.png",
                                "$base/local-thumb/f/ff/Foobar.svg/2000px-Foobar.svg.png",
@@ -638,6 +699,11 @@ class NewParserTest extends MediaWikiTestCase {
                                        . "Current configuration is:\n\$wgTexvc = '$wgTexvc'" );
                        }
                }
+               if ( isset( $opts['djvu'] ) ) {
+                       if ( !$this->djVuSupport->isEnabled() ) {
+                               $this->markTestSkipped( "SKIPPED: djvu binaries do not exist or are not executable.\n" );
+                       }
+               }
 
                if ( isset( $opts['pst'] ) ) {
                        $out = $parser->preSaveTransform( $input, $title, $user, $options );
diff --git a/tests/phpunit/mocks/media/MockDjVuHandler.php b/tests/phpunit/mocks/media/MockDjVuHandler.php
new file mode 100644 (file)
index 0000000..31cb13d
--- /dev/null
@@ -0,0 +1,49 @@
+<?php
+/**
+ * Fake handler for DjVu images.
+ *
+ * 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.,
+ * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
+ * http://www.gnu.org/copyleft/gpl.html
+ *
+ * @file
+ * @ingroup Media
+ */
+
+class MockDjVuHandler extends DjVuHandler {
+       function doTransform( $image, $dstPath, $dstUrl, $params, $flags = 0 ) {
+               if ( !$this->normaliseParams( $image, $params ) ) {
+                       return new TransformParameterError( $params );
+               }
+               $width = $params['width'];
+               $height = $params['height'];
+               $page = $params['page'];
+               if ( $page > $this->pageCount( $image ) ) {
+                       return new MediaTransformError(
+                               'thumbnail_error',
+                               $width,
+                               $height,
+                               wfMessage( 'djvu_page_error' )->text()
+                       );
+               }
+
+               $params = array(
+                       'width' => $width,
+                       'height' => $height,
+                       'page' => $page
+               );
+
+               return new ThumbnailImage( $image, $dstUrl, $dstPath, $params );
+       }
+}
index 6ff2b24..818b24e 100644 (file)
@@ -664,3 +664,37 @@ class DelayedParserTest {
        }
 
 }
+
+/**
+ * Initialize and detect the DjVu files support
+ */
+class DjVuSupport {
+
+       /**
+        * Initialises DjVu tools global with default values
+        */
+       public function __construct() {
+               global $wgDjvuRenderer, $wgDjvuDump, $wgDjvuToXML, $wgFileExtensions;
+
+               $wgDjvuRenderer = $wgDjvuRenderer ? $wgDjvuRenderer : '/usr/bin/ddjvu';
+               $wgDjvuDump = $wgDjvuDump ? $wgDjvuDump : '/usr/bin/djvudump';
+               $wgDjvuToXML = $wgDjvuToXML ? $wgDjvuToXML : '/usr/bin/djvutoxml';
+
+               if ( !in_array( 'djvu', $wgFileExtensions ) ) {
+                       $wgFileExtensions[] = 'djvu';
+               }
+       }
+
+       /**
+        * Returns if the DjVu tools are usable
+        *
+        * @return bool
+        */
+       public function isEnabled() {
+               global $wgDjvuRenderer, $wgDjvuDump, $wgDjvuToXML;
+
+               return is_executable( $wgDjvuRenderer )
+                       && is_executable( $wgDjvuDump )
+                       && is_executable( $wgDjvuToXML );
+       }
+}