jenkins-bot has submitted this change and it was merged.

Change subject: Addition of a parser test for page= parameter of image inclusion
......................................................................


Addition of a parser test for page= parameter of image inclusion

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
---
M tests/TestsAutoLoader.php
M tests/parser/parserTest.inc
M tests/parser/parserTests.txt
M tests/phpunit/includes/media/DjVuTest.php
M tests/phpunit/includes/parser/NewParserTest.php
A tests/phpunit/mocks/media/MockDjVuHandler.php
M tests/testHelpers.inc
7 files changed, 244 insertions(+), 25 deletions(-)

Approvals:
  Cscott: Looks good to me, approved
  jenkins-bot: Verified



diff --git a/tests/TestsAutoLoader.php b/tests/TestsAutoLoader.php
index 29c3269..a5eb29b 100644
--- a/tests/TestsAutoLoader.php
+++ b/tests/TestsAutoLoader.php
@@ -34,6 +34,7 @@
        '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 @@
        '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",
diff --git a/tests/parser/parserTest.inc b/tests/parser/parserTest.inc
index 0899d46..b4e903d 100644
--- a/tests/parser/parserTest.inc
+++ b/tests/parser/parserTest.inc
@@ -65,6 +65,11 @@
        private $dbClone;
 
        /**
+        * @var DjVuSupport
+        */
+       private $djVuSupport;
+
+       /**
         * @var string $oldTablePrefix Original table prefix
         */
        private $oldTablePrefix;
@@ -135,6 +140,8 @@
 
                $this->runDisabled = isset( $options['run-disabled'] );
                $this->runParsoid = isset( $options['run-parsoid'] );
+
+               $this->djVuSupport = new DjVuSupport();
 
                $this->hooks = array();
                $this->functionHooks = array();
@@ -548,6 +555,12 @@
 
                $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'];
@@ -1014,6 +1027,44 @@
                        '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 @@
                        '<?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 @@
 
                                "$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 @@
                                "$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",
@@ -1284,6 +1348,19 @@
        }
 
        /**
+        * 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.
         *
diff --git a/tests/parser/parserTests.txt b/tests/parser/parserTests.txt
index e2bf9f6..c4bd12d 100644
--- a/tests/parser/parserTests.txt
+++ b/tests/parser/parserTests.txt
@@ -7519,7 +7519,7 @@
 !! wikitext
 {{NUMBEROFFILES}}
 !! html
-<p>4
+<p>5
 </p>
 !! end
 
@@ -15306,6 +15306,17 @@
 !!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
  ''' ''x'
diff --git a/tests/phpunit/includes/media/DjVuTest.php 
b/tests/phpunit/includes/media/DjVuTest.php
index c61ed22..537c124 100644
--- a/tests/phpunit/includes/media/DjVuTest.php
+++ b/tests/phpunit/includes/media/DjVuTest.php
@@ -20,20 +20,13 @@
        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
@@ -51,19 +44,6 @@
                ) );
 
                $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 ) {
diff --git a/tests/phpunit/includes/parser/NewParserTest.php 
b/tests/phpunit/includes/parser/NewParserTest.php
index f3c499b..66ed020 100644
--- a/tests/phpunit/includes/parser/NewParserTest.php
+++ b/tests/phpunit/includes/parser/NewParserTest.php
@@ -31,6 +31,11 @@
        public $fuzzSeed = 0;
        public $memoryLimit = 50;
 
+       /**
+        * @var DjVuSupport
+        */
+       private $djVuSupport;
+
        protected $file = false;
 
        public static function setUpBeforeClass() {
@@ -130,6 +135,9 @@
                // 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';
@@ -137,6 +145,9 @@
                # add a namespace shadowing a interwiki link, to test
                # proper precedence when resolving links. (bug 51680)
                $tmpGlobals['wgExtraNamespaces'] = array( 100 => 'MemoryAlpha' 
);
+
+               //DjVu support
+               $this->djVuSupport = new DjVuSupport();
 
                $this->setMwGlobals( $tmpGlobals );
 
@@ -272,6 +283,47 @@
                                        'sha1'        => wfBaseConvert( '', 16, 
36, 31 ),
                                        '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 );
                }
        }
 
@@ -446,6 +498,10 @@
                $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"?>' .
@@ -524,6 +580,11 @@
                                "$base/local-public/e/ea/Thumb.png",
 
                                "$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",
@@ -638,6 +699,11 @@
                                        . "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
index 0000000..31cb13d
--- /dev/null
+++ b/tests/phpunit/mocks/media/MockDjVuHandler.php
@@ -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 );
+       }
+}
diff --git a/tests/testHelpers.inc b/tests/testHelpers.inc
index 6ff2b24..818b24e 100644
--- a/tests/testHelpers.inc
+++ b/tests/testHelpers.inc
@@ -664,3 +664,37 @@
        }
 
 }
+
+/**
+ * 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 );
+       }
+}

-- 
To view, visit https://gerrit.wikimedia.org/r/102475
To unsubscribe, visit https://gerrit.wikimedia.org/r/settings

Gerrit-MessageType: merged
Gerrit-Change-Id: I53fd7b54e765d5f349abe74481bbc6f62f2b349e
Gerrit-PatchSet: 25
Gerrit-Project: mediawiki/core
Gerrit-Branch: master
Gerrit-Owner: Tpt <thoma...@hotmail.fr>
Gerrit-Reviewer: Cscott <canan...@wikimedia.org>
Gerrit-Reviewer: GWicke <gwi...@wikimedia.org>
Gerrit-Reviewer: Nemo bis <federicol...@tiscali.it>
Gerrit-Reviewer: Tpt <thoma...@hotmail.fr>
Gerrit-Reviewer: jenkins-bot <>

_______________________________________________
MediaWiki-commits mailing list
MediaWiki-commits@lists.wikimedia.org
https://lists.wikimedia.org/mailman/listinfo/mediawiki-commits

Reply via email to