Jarry1250 has uploaded a new change for review. https://gerrit.wikimedia.org/r/213789
Change subject: Implement tests for TranslateSvgUtils ...................................................................... Implement tests for TranslateSvgUtils Slightly tidy to make sure param strings (faux templates) actually look vaguely valid, i.e. end with }}. Also formalise the rule that they must appear at the end of the translation. Change-Id: I1cfba0e988e713b9309c485d5ae1bf82b2315376 --- M TranslateSvgUtils.php M tests/phpunit/TranslateSvgTestCase.php A tests/phpunit/TranslateSvgUtilsTest.php 3 files changed, 187 insertions(+), 10 deletions(-) git pull ssh://gerrit.wikimedia.org:29418/mediawiki/extensions/TranslateSvg refs/changes/89/213789/1 diff --git a/TranslateSvgUtils.php b/TranslateSvgUtils.php index 25cdbe8..0805618 100644 --- a/TranslateSvgUtils.php +++ b/TranslateSvgUtils.php @@ -16,15 +16,11 @@ * Function used to determine if a message includes a property string * * @param $message \string Message which may or may not include a property string - * @return true + * @return bool */ public static function hasPropertyString( $message ) { global $wgTranslateSvgTemplateName; - if ( strpos( $message, '{{' . $wgTranslateSvgTemplateName ) !== false ) { - return true; - } else { - return false; - } + return preg_match( '/\{\{' . $wgTranslateSvgTemplateName . '.*\}\}$/', $message ); } /** @@ -67,10 +63,10 @@ if ( $title->getNamespace() === NS_FILE ) { $file = wfFindFile( $title ); return ( $file && $file->getMimeType() === 'image/svg+xml' ); - } else { - // Not a file description page - return false; } + + // Not a file description page + return false; } /** diff --git a/tests/phpunit/TranslateSvgTestCase.php b/tests/phpunit/TranslateSvgTestCase.php index 4f926d3..a0e91df 100644 --- a/tests/phpunit/TranslateSvgTestCase.php +++ b/tests/phpunit/TranslateSvgTestCase.php @@ -48,7 +48,8 @@ // Actually perform upload $bot = User::newFromName( 'TranslateSvg unit tests', false ); $status = $uploader->performUpload( 'testing', 'Created during testing', false, $bot ); - if ( !$status->isGood() ) { + $title = Title::makeTitle( NS_FILE, $name ); + if ( !$status->isGood() || !$title->exists() ) { die( 'Could not upload test file ' . $name ); } diff --git a/tests/phpunit/TranslateSvgUtilsTest.php b/tests/phpunit/TranslateSvgUtilsTest.php new file mode 100644 index 0000000..95037b1 --- /dev/null +++ b/tests/phpunit/TranslateSvgUtilsTest.php @@ -0,0 +1,180 @@ +<?php +/** + * Unit tests. + * + * @file + * @author Harry Burt + * @copyright Copyright © 2014, Harry Burt + * @license GPL-2.0+ + */ +class TranslateSvgUtilsTest extends TranslateSvgTestCase { + public static function setUpBeforeClass() { + parent::setUpBeforeClass(); + self::prepareFile( __DIR__ . '/../data/Speech_bubbles.svg' ); + } + + public function propertyStringProvider () { + global $wgTranslateSvgTemplateName; + $tn = $wgTranslateSvgTemplateName; + return array( + array( "Test", '' ), + array( "Test$tn", '' ), + array( 'Test {{' . $tn, '' ), + array( '{{' . $tn . '}}foo', '' ), + array( '{{' . $tn . '}}', '{{' . $tn. '}}' ), + array( 'Test {{' . $tn. '}}', '{{' . $tn. '}}' ), + array( '{{' . $tn . '|foo=bar}}', '{{' . $tn . '|foo=bar}}' ), + array( 'foo{{' . $tn . '|foo=bar}}', '{{' . $tn . '|foo=bar}}' ) + ); + } + + /** + * @dataProvider propertyStringProvider + */ + public function testHasPropertyString( $message, $expected ) { + $this->assertEquals( $expected !== '', TranslateSvgUtils::hasPropertyString( $message ) ); + } + + /** + * @dataProvider propertyStringProvider + */ + public function testExtractPropertyString( $message, $expected ) { + $this->assertEquals( $expected, TranslateSvgUtils::extractPropertyString( $message ) ); + } + + /** + * @dataProvider propertyStringProvider + */ + public function testStripPropertyString( $message, $expected ) { + $this->assertEquals( str_replace( $expected, '', $message ), TranslateSvgUtils::stripPropertyString( $message ) ); + } + + public function titleProvider () { + return array( + array( NS_FILE, false, true ), + array( NS_MAIN, false, false ), + array( NS_FILE, 'Speech_bubbles.jpg', false ) + ); + } + + /** + * @dataProvider titleProvider + */ + public function testIsSVGFilePage( $ns, $name, $expected ) { + if( !$name ) { + $name = self::$name; // Providers are established before self::$name is set + } + $title = Title::makeTitle( $ns, $name ); + $this->assertEquals( $expected, TranslateSvgUtils::isSVGFilePage( $title ) ); + }/* + if ( $title->getNamespace() === NS_FILE ) { + $file = wfFindFile( $title ); + return ( $file && $file->getMimeType() === 'image/svg+xml' ); + } else { + // Not a file description page + return false; + } + } +*/ + public function paramProvider () { + return array( + array( 'hackingAttempt', 'inject', array( false, false ) ), + array( 'bold', 'yes', array( 'font-weight', 'bold' ) ), + array( 'underline', 'yes', array( 'text-decoration', 'underline' ) ), + array( 'italic', 'no ', array( 'font-style', 'normal' ) ), // Trim value + array( ' sodipodi:role', 'line', array( 'sodipodi:role', 'line' ) ), // Trim parameter + array( 'xml:type', 'test', array( 'xml:type', 'test' ) ), // Custom parameter pass-through + array( 'xml:type', '', array( 'xml:type', false ) ) // '' becomes false + ); + } + /** + * @dataProvider paramProvider + */ + public function testMapToAttribute( $parameter, $value, $expected ) { + global $wgTranslateSvgOptionalProperties; + $wgTranslateSvgOptionalProperties[] = 'xml:type'; + $this->assertArrayEquals( $expected, TranslateSvgUtils::mapToAttribute( $parameter, $value ) ); + } + + public function attribProvider () { + return array( + array( 'hackingAttempt', 'inject', array( false, false ) ), + array( 'font-weight', 'bold', array( 'bold', 'yes' ) ), + array( 'text-decoration', 'underline', array( 'underline', 'yes' ) ), + array( 'font-style', 'normal ', array( 'italic', 'no' ) ), // Trim value + array( ' sodipodi:role', 'line', array( 'sodipodi:role', 'line' ) ), // Trim parameter + array( 'fill', '#ccc', array( 'color', '#ccc' ) ), // fill to color + array( 'xml:type', 'test', array( 'xml:type', 'test' ) ), // Custom parameter pass-through + array( 'xml:type', '', array( 'xml:type', false ) ), // '' becomes false + array( 'font-family', 'Sans', array( 'font-family', 'sans-serif' ) ) // Hard coded mapping + ); + } + /** + * @dataProvider attribProvider + */ + public function testMapFromAttribute( $parameter, $value, $expected ) { + global $wgTranslateSvgOptionalProperties; + $wgTranslateSvgOptionalProperties[] = 'xml:type'; + $this->assertArrayEquals( $expected, TranslateSvgUtils::mapFromAttribute( $parameter, $value ) ); + } + + public function translationProvider () { + global $wgTranslateSvgTemplateName; + $tn = $wgTranslateSvgTemplateName; + return array( + array( + 'Foo{{' . $tn . '|x=|y=|font-family=other|font-size=|units=other|color=|underline=no|italic=no|bold=yes}}', + array( 'text' => 'Foo', 'font-weight' => 'bold', 'text-decoration' => 'normal', 'font-style' => 'normal' ) + ), + array( + 'Foo{{' . $tn . '|x=|y=|font-family=other|font-size=12|units=px|color=|underline=no|italic=no|bold=yes}}', + array( 'text' => 'Foo', 'font-weight' => 'bold', 'text-decoration' => 'normal', 'font-style' => 'normal', 'font-size' => '12px' ) + ) + ); + } + + /** + * @dataProvider translationProvider + */ + public function testTranslationToArray( $translation, $expected ) { + $this->assertArrayEquals( $expected, TranslateSvgUtils::translationToArray( $translation ) ); + } + + /** + * @dataProvider translationProvider + */ + public function testArrayToTranslation( $expected, $array ) { + $this->assertEquals( $expected, TranslateSvgUtils::arrayToTranslation( $array ) ); + } + + public function langProvider () { + return array( + array( 'en_GB', 'en-gb', 'British English' ), + array( 'fr', 'fr', 'français' ), + array( 'fr_BZ', 'fr-bz', 'français' ), // fr-bz doesn't exist in ISO but fr does + array( 'zz_BZ', 'zz-bz', 'zz-bz' ), // neither zz-bz nor zz doesn't exist in ISO + array( 'fallback', 'fallback', 'Deutsch' ) // fallback defined as de + ); + } + + /** + * @dataProvider langProvider + */ + public function testOsToLangCode( $os, $expected ){ + $this->assertEquals( $expected, TranslateSvgUtils::osToLangCode( $os ) ); + } + + /** + * @dataProvider langProvider + */ + public function testLangCodeToOs( $expected, $langCode ){ + $this->assertEquals( $expected, TranslateSvgUtils::langCodeToOs( $langCode ) ); + } + + /** + * @dataProvider langProvider + */ + public function testFetchLanguageName( $os /* unused */, $langCode, $expected ) { + $this->assertEquals( $expected, TranslateSvgUtils::fetchLanguageName( $langCode, 'de', $expected ) ); + } +} \ No newline at end of file -- To view, visit https://gerrit.wikimedia.org/r/213789 To unsubscribe, visit https://gerrit.wikimedia.org/r/settings Gerrit-MessageType: newchange Gerrit-Change-Id: I1cfba0e988e713b9309c485d5ae1bf82b2315376 Gerrit-PatchSet: 1 Gerrit-Project: mediawiki/extensions/TranslateSvg Gerrit-Branch: master Gerrit-Owner: Jarry1250 <jarry1...@gmail.com> _______________________________________________ MediaWiki-commits mailing list MediaWiki-commits@lists.wikimedia.org https://lists.wikimedia.org/mailman/listinfo/mediawiki-commits