From: Tpt
Date: Wed, 18 Dec 2013 17:56:15 +0000 (+0100)
Subject: Addition of a parser test for page= parameter of image inclusion
X-Git-Tag: 1.31.0-rc.0~15964^2
X-Git-Url: https://git.cyclocoop.org/%242?a=commitdiff_plain;h=17117f74f5d2e92a7a41ab949eb56d657aac6330;p=lhc%2Fweb%2Fwiklou.git
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
---
diff --git a/tests/TestsAutoLoader.php b/tests/TestsAutoLoader.php
index 29c326927f..a5eb29bca0 100644
--- a/tests/TestsAutoLoader.php
+++ b/tests/TestsAutoLoader.php
@@ -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",
diff --git a/tests/parser/parserTest.inc b/tests/parser/parserTest.inc
index 0899d46243..b4e903d633 100644
--- a/tests/parser/parserTest.inc
+++ b/tests/parser/parserTest.inc
@@ -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' => '
+
+
+
+
+
+
+
+
+
+',
+ 'sha1' => wfBaseConvert( '', 16, 36, 31 ),
+ 'fileExists' => true
+ ), $this->db->timestamp( '20010115123600' ), $user );
}
public function teardownDatabase() {
@@ -1092,6 +1143,9 @@ class ParserTest {
'' .
'' );
+ 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.
diff --git a/tests/parser/parserTests.txt b/tests/parser/parserTests.txt
index e2bf9f64fb..c4bd12d50d 100644
--- a/tests/parser/parserTests.txt
+++ b/tests/parser/parserTests.txt
@@ -7519,7 +7519,7 @@ Magic Word: {{NUMBEROFFILES}}
!! wikitext
{{NUMBEROFFILES}}
!! html
-4
+
5
!! end
@@ -15305,6 +15305,17 @@ Width-sized image (using px, with preceding whitespace - test regression from r3
!!end
+!! test
+Image with page parameter
+!! options
+djvu
+!! wikitext
+[[File:LoremIpsum.djvu|page=2]]
+!! html
+
+
+!! end
+
!! test
Another italics / bold test
!! wikitext
diff --git a/tests/phpunit/includes/media/DjVuTest.php b/tests/phpunit/includes/media/DjVuTest.php
index c61ed22ad5..537c124d61 100644
--- a/tests/phpunit/includes/media/DjVuTest.php
+++ b/tests/phpunit/includes/media/DjVuTest.php
@@ -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,
diff --git a/tests/phpunit/includes/parser/NewParserTest.php b/tests/phpunit/includes/parser/NewParserTest.php
index f3c499b58b..66ed020a52 100644
--- a/tests/phpunit/includes/parser/NewParserTest.php
+++ b/tests/phpunit/includes/parser/NewParserTest.php
@@ -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' => '
+
+
+
+
+
+
+
+
+
+',
+ '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 = '' .
@@ -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
index 0000000000..31cb13dc9b
--- /dev/null
+++ b/tests/phpunit/mocks/media/MockDjVuHandler.php
@@ -0,0 +1,49 @@
+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 6ff2b24643..818b24e23c 100644
--- a/tests/testHelpers.inc
+++ b/tests/testHelpers.inc
@@ -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 );
+ }
+}