From d469b379554f0961b0053651a88501b2e3bf8f92 Mon Sep 17 00:00:00 2001 From: =?utf8?q?Bartosz=20Dziewo=C5=84ski?= Date: Sun, 29 Mar 2015 19:53:47 +0200 Subject: [PATCH] ResourceLoaderImage: Add basic tests Also remove some wrappers from ResourceLoaderImageModule tests that are no longer necessary, since the files they were mocking now exist. Bug: T86334 Change-Id: If02e58716ce8e6c8327c8939c6c6425bd48bb560 --- tests/TestsAutoLoader.php | 6 +- tests/phpunit/data/resourceloader/add.gif | Bin 0 -> 74 bytes tests/phpunit/data/resourceloader/bold-a.svg | 6 + tests/phpunit/data/resourceloader/bold-b.svg | 6 + tests/phpunit/data/resourceloader/bold-f.svg | 6 + .../phpunit/data/resourceloader/help-ltr.svg | 10 ++ .../phpunit/data/resourceloader/help-rtl.svg | 10 ++ tests/phpunit/data/resourceloader/next.svg | 4 + .../data/resourceloader/next_massage.svg | 4 + tests/phpunit/data/resourceloader/prev.svg | 4 + tests/phpunit/data/resourceloader/remove.svg | 6 + .../data/resourceloader/remove_variantize.svg | 6 + .../ResourceLoaderImageModuleTest.php | 142 +++++++----------- .../ResourceLoaderImageTest.php | 121 +++++++++++++++ 14 files changed, 241 insertions(+), 90 deletions(-) create mode 100644 tests/phpunit/data/resourceloader/add.gif create mode 100644 tests/phpunit/data/resourceloader/bold-a.svg create mode 100644 tests/phpunit/data/resourceloader/bold-b.svg create mode 100644 tests/phpunit/data/resourceloader/bold-f.svg create mode 100644 tests/phpunit/data/resourceloader/help-ltr.svg create mode 100644 tests/phpunit/data/resourceloader/help-rtl.svg create mode 100644 tests/phpunit/data/resourceloader/next.svg create mode 100644 tests/phpunit/data/resourceloader/next_massage.svg create mode 100644 tests/phpunit/data/resourceloader/prev.svg create mode 100644 tests/phpunit/data/resourceloader/remove.svg create mode 100644 tests/phpunit/data/resourceloader/remove_variantize.svg create mode 100644 tests/phpunit/includes/resourceloader/ResourceLoaderImageTest.php diff --git a/tests/TestsAutoLoader.php b/tests/TestsAutoLoader.php index b0f29d5d7d..def23dad9c 100644 --- a/tests/TestsAutoLoader.php +++ b/tests/TestsAutoLoader.php @@ -82,9 +82,13 @@ $wgAutoloadClasses += array( # tests/phpunit/includes/diff 'FakeDiffOp' => "$testDir/phpunit/includes/diff/FakeDiffOp.php", - # tests/phpunit/includes/passwords + # tests/phpunit/includes/password 'PasswordTestCase' => "$testDir/phpunit/includes/password/PasswordTestCase.php", + # tests/phpunit/includes/resourceloader + 'ResourceLoaderImageModuleTest' => "$testDir/phpunit/includes/resourceloader/ResourceLoaderImageModuleTest.php", + 'ResourceLoaderImageModuleTestable' => "$testDir/phpunit/includes/resourceloader/ResourceLoaderImageModuleTest.php", + # tests/phpunit/languages 'LanguageClassesTestCase' => "$testDir/phpunit/languages/LanguageClassesTestCase.php", diff --git a/tests/phpunit/data/resourceloader/add.gif b/tests/phpunit/data/resourceloader/add.gif new file mode 100644 index 0000000000000000000000000000000000000000..5f454ca16cdd1cec5bbfafb3ca5ccac071cd11b4 GIT binary patch literal 74 zcmZ?wbhEHblwgoxXkcUjg8%>jEB<6*V#shAj ZA4J}=2vVtRoiQu#KKJoYKId5(tO5DU7|H+u literal 0 HcmV?d00001 diff --git a/tests/phpunit/data/resourceloader/bold-a.svg b/tests/phpunit/data/resourceloader/bold-a.svg new file mode 100644 index 0000000000..4b82877979 --- /dev/null +++ b/tests/phpunit/data/resourceloader/bold-a.svg @@ -0,0 +1,6 @@ + + + + + + diff --git a/tests/phpunit/data/resourceloader/bold-b.svg b/tests/phpunit/data/resourceloader/bold-b.svg new file mode 100644 index 0000000000..4f64820368 --- /dev/null +++ b/tests/phpunit/data/resourceloader/bold-b.svg @@ -0,0 +1,6 @@ + + + + + + diff --git a/tests/phpunit/data/resourceloader/bold-f.svg b/tests/phpunit/data/resourceloader/bold-f.svg new file mode 100644 index 0000000000..357d2e5d34 --- /dev/null +++ b/tests/phpunit/data/resourceloader/bold-f.svg @@ -0,0 +1,6 @@ + + + + + + diff --git a/tests/phpunit/data/resourceloader/help-ltr.svg b/tests/phpunit/data/resourceloader/help-ltr.svg new file mode 100644 index 0000000000..bb2545c58b --- /dev/null +++ b/tests/phpunit/data/resourceloader/help-ltr.svg @@ -0,0 +1,10 @@ + + + + + + + + + + diff --git a/tests/phpunit/data/resourceloader/help-rtl.svg b/tests/phpunit/data/resourceloader/help-rtl.svg new file mode 100644 index 0000000000..255ae95b08 --- /dev/null +++ b/tests/phpunit/data/resourceloader/help-rtl.svg @@ -0,0 +1,10 @@ + + + + + + + + + + diff --git a/tests/phpunit/data/resourceloader/next.svg b/tests/phpunit/data/resourceloader/next.svg new file mode 100644 index 0000000000..02b4e38798 --- /dev/null +++ b/tests/phpunit/data/resourceloader/next.svg @@ -0,0 +1,4 @@ + + + + diff --git a/tests/phpunit/data/resourceloader/next_massage.svg b/tests/phpunit/data/resourceloader/next_massage.svg new file mode 100644 index 0000000000..bbd1a8d66a --- /dev/null +++ b/tests/phpunit/data/resourceloader/next_massage.svg @@ -0,0 +1,4 @@ + + + + diff --git a/tests/phpunit/data/resourceloader/prev.svg b/tests/phpunit/data/resourceloader/prev.svg new file mode 100644 index 0000000000..f31ec095c3 --- /dev/null +++ b/tests/phpunit/data/resourceloader/prev.svg @@ -0,0 +1,4 @@ + + + + diff --git a/tests/phpunit/data/resourceloader/remove.svg b/tests/phpunit/data/resourceloader/remove.svg new file mode 100644 index 0000000000..6ad791744e --- /dev/null +++ b/tests/phpunit/data/resourceloader/remove.svg @@ -0,0 +1,6 @@ + + + + + + diff --git a/tests/phpunit/data/resourceloader/remove_variantize.svg b/tests/phpunit/data/resourceloader/remove_variantize.svg new file mode 100644 index 0000000000..bcbe87120e --- /dev/null +++ b/tests/phpunit/data/resourceloader/remove_variantize.svg @@ -0,0 +1,6 @@ + + + + + + diff --git a/tests/phpunit/includes/resourceloader/ResourceLoaderImageModuleTest.php b/tests/phpunit/includes/resourceloader/ResourceLoaderImageModuleTest.php index a316a17ca0..841066342d 100644 --- a/tests/phpunit/includes/resourceloader/ResourceLoaderImageModuleTest.php +++ b/tests/phpunit/includes/resourceloader/ResourceLoaderImageModuleTest.php @@ -5,67 +5,67 @@ */ class ResourceLoaderImageModuleTest extends ResourceLoaderTestCase { - public static function providerGetModules() { - $commonVariants = array( - 'invert' => array( - 'color' => '#FFFFFF', - 'global' => true, - ), - 'primary' => array( - 'color' => '#598AD1', + public static $commonImageData = array( + 'add' => 'add.gif', + 'remove' => array( + 'file' => 'remove.svg', + 'variants' => array( 'destructive' ), + ), + 'next' => array( + 'file' => array( + 'ltr' => 'next.svg', + 'rtl' => 'prev.svg' ), - 'constructive' => array( - 'color' => '#00C697', + ), + 'help' => array( + 'file' => array( + 'ltr' => 'help-ltr.svg', + 'rtl' => 'help-rtl.svg', + 'lang' => array( + 'he' => 'help-ltr.svg', + ) ), - 'destructive' => array( - 'color' => '#E81915', + ), + 'bold' => array( + 'file' => array( + 'default' => 'bold-a.svg', + 'lang' => array( + 'en' => 'bold-b.svg', + 'de' => 'bold-f.svg', + ) ), - ); + ) + ); - $commonImageData = array( - 'advanced' => 'advanced.svg', - 'remove' => array( - 'file' => 'remove.svg', - 'variants' => array( 'destructive' ), - ), - 'next' => array( - 'file' => array( - 'ltr' => 'next.svg', - 'rtl' => 'prev.svg' - ), - ), - 'help' => array( - 'file' => array( - 'ltr' => 'help-ltr.svg', - 'rtl' => 'help-rtl.svg', - 'lang' => array( - 'he' => 'help-ltr.svg', - ) - ), - ), - 'bold' => array( - 'file' => array( - 'default' => 'bold-a.svg', - 'lang' => array( - 'en' => 'bold-b.svg', - 'de' => 'bold-f.svg', - ) - ), - ) - ); + public static $commonImageVariants = array( + 'invert' => array( + 'color' => '#FFFFFF', + 'global' => true, + ), + 'primary' => array( + 'color' => '#598AD1', + ), + 'constructive' => array( + 'color' => '#00C697', + ), + 'destructive' => array( + 'color' => '#E81915', + ), + ); + public static function providerGetModules() { return array( array( array( 'class' => 'ResourceLoaderImageModule', 'prefix' => 'oo-ui-icon', - 'variants' => $commonVariants, - 'images' => $commonImageData, + 'variants' => self::$commonImageVariants, + 'images' => self::$commonImageData, ), - '.oo-ui-icon-advanced { + '.oo-ui-icon-add { ... } -.oo-ui-icon-advanced-invert { +.oo-ui-icon-add-invert { ... } .oo-ui-icon-remove { @@ -101,13 +101,13 @@ class ResourceLoaderImageModuleTest extends ResourceLoaderTestCase { 'class' => 'ResourceLoaderImageModule', 'selectorWithoutVariant' => '.mw-ui-icon-{name}:after, .mw-ui-icon-{name}:before', 'selectorWithVariant' => '.mw-ui-icon-{name}-{variant}:after, .mw-ui-icon-{name}-{variant}:before', - 'variants' => $commonVariants, - 'images' => $commonImageData, + 'variants' => self::$commonImageVariants, + 'images' => self::$commonImageData, ), - '.mw-ui-icon-advanced:after, .mw-ui-icon-advanced:before { + '.mw-ui-icon-add:after, .mw-ui-icon-add:before { ... } -.mw-ui-icon-advanced-invert:after, .mw-ui-icon-advanced-invert:before { +.mw-ui-icon-add-invert:after, .mw-ui-icon-add-invert:before { ... } .mw-ui-icon-remove:after, .mw-ui-icon-remove:before { @@ -146,7 +146,7 @@ class ResourceLoaderImageModuleTest extends ResourceLoaderTestCase { * @covers ResourceLoaderImageModule::getStyles */ public function testGetStyles( $module, $expected ) { - $module = new ResourceLoaderImageModuleTestable( $module ); + $module = new ResourceLoaderImageModuleTestable( $module, __DIR__ . '/../../data/resourceloader' ); $styles = $module->getStyles( $this->getResourceLoaderContext() ); $this->assertEquals( $expected, $styles['all'] ); } @@ -159,40 +159,4 @@ class ResourceLoaderImageModuleTestable extends ResourceLoaderImageModule { protected function getCssDeclarations( $primary, $fallback ) { return array( '...' ); } - - /** - * Return mock ResourceLoaderImages that don't call file_get_contents and such. - */ - public function getImages() { - $images = parent::getImages(); - foreach ( $images as $name => &$image ) { - $image = new ResourceLoaderImageWrapper( $image ); - } - return $images; - } -} - -/** - * Wraps a ResourceLoaderImage not to call file_get_contents and such. - */ -class ResourceLoaderImageWrapper extends ResourceLoaderImage { - public function __construct( ResourceLoaderImage $image ) { - $this->image = $image; - } - - public function getUrl( ResourceLoaderContext $context, $script, $variant, $format ) { - return null; - } - - public function getDataUri( ResourceLoaderContext $context, $variant, $format ) { - return null; - } - - public function __call( $method, $arguments ) { - return call_user_func_array( array( $this->image, $method ), $arguments ); - } - - public function __get( $name ) { - return $this->image->$name; - } } diff --git a/tests/phpunit/includes/resourceloader/ResourceLoaderImageTest.php b/tests/phpunit/includes/resourceloader/ResourceLoaderImageTest.php new file mode 100644 index 0000000000..404ae9db32 --- /dev/null +++ b/tests/phpunit/includes/resourceloader/ResourceLoaderImageTest.php @@ -0,0 +1,121 @@ +imagesPath = __DIR__ . '/../../data/resourceloader'; + } + + protected function getTestImage( $name ) { + $options = ResourceLoaderImageModuleTest::$commonImageData[$name]; + $fileDescriptor = is_string( $options ) ? $options : $options['file']; + $allowedVariants = is_array( $options ) && isset( $options['variants'] ) ? $options['variants'] : array(); + $variants = array_fill_keys( $allowedVariants, array( 'color' => 'red' ) ); + return new ResourceLoaderImageTestable( $name, 'test', $fileDescriptor, $this->imagesPath, $variants ); + } + + public static function provideGetPath() { + return array( + array( 'add', 'en', 'add.gif' ), + array( 'add', 'he', 'add.gif' ), + array( 'remove', 'en', 'remove.svg' ), + array( 'remove', 'he', 'remove.svg' ), + array( 'next', 'en', 'next.svg' ), + array( 'next', 'he', 'prev.svg' ), + array( 'help', 'en', 'help-ltr.svg' ), + array( 'help', 'ar', 'help-rtl.svg' ), + array( 'help', 'he', 'help-ltr.svg' ), + array( 'bold', 'en', 'bold-b.svg' ), + array( 'bold', 'de', 'bold-f.svg' ), + array( 'bold', 'fr', 'bold-a.svg' ), + array( 'bold', 'he', 'bold-a.svg' ), + ); + } + + /** + * @covers ResourceLoaderImage::getPath + * @dataProvider provideGetPath + */ + public function testGetPath( $imageName, $languageCode, $path ) { + static $dirMap = array( + 'en' => 'ltr', + 'de' => 'ltr', + 'fr' => 'ltr', + 'he' => 'rtl', + 'ar' => 'rtl', + ); + static $contexts = array(); + + $image = $this->getTestImage( $imageName ); + $context = $this->getResourceLoaderContext( $languageCode, $dirMap[$languageCode] ); + + $this->assertEquals( $image->getPath( $context ), $this->imagesPath . '/' . $path ); + } + + /** + * @covers ResourceLoaderImage::getExtension + * @covers ResourceLoaderImage::getMimeType + */ + public function testGetExtension() { + $image = $this->getTestImage( 'remove' ); + $this->assertEquals( $image->getExtension(), 'svg' ); + $this->assertEquals( $image->getExtension( 'original' ), 'svg' ); + $this->assertEquals( $image->getExtension( 'rasterized' ), 'png' ); + $image = $this->getTestImage( 'add' ); + $this->assertEquals( $image->getExtension(), 'gif' ); + $this->assertEquals( $image->getExtension( 'original' ), 'gif' ); + $this->assertEquals( $image->getExtension( 'rasterized' ), 'gif' ); + } + + /** + * @covers ResourceLoaderImage::getImageData + * @covers ResourceLoaderImage::variantize + * @covers ResourceLoaderImage::massageSvgPathdata + */ + public function testGetImageData() { + $context = $this->getResourceLoaderContext( 'en', 'ltr' ); + + $image = $this->getTestImage( 'remove' ); + $data = file_get_contents( $this->imagesPath . '/remove.svg' ); + $dataConstructive = file_get_contents( $this->imagesPath . '/remove_variantize.svg' ); + $this->assertEquals( $image->getImageData( $context, null, 'original' ), $data ); + $this->assertEquals( $image->getImageData( $context, 'destructive', 'original' ), $dataConstructive ); + // Stub, since we don't know if we even have a SVG handler, much less what exactly it'll output + $this->assertEquals( $image->getImageData( $context, null, 'rasterized' ), 'RASTERIZESTUB' ); + + $image = $this->getTestImage( 'add' ); + $data = file_get_contents( $this->imagesPath . '/add.gif' ); + $this->assertEquals( $image->getImageData( $context, null, 'original' ), $data ); + $this->assertEquals( $image->getImageData( $context, null, 'rasterized' ), $data ); + } + + /** + * @covers ResourceLoaderImage::massageSvgPathdata + */ + public function testMassageSvgPathdata() { + $image = $this->getTestImage( 'next' ); + $data = file_get_contents( $this->imagesPath . '/next.svg' ); + $dataMassaged = file_get_contents( $this->imagesPath . '/next_massage.svg' ); + $this->assertEquals( $image->massageSvgPathdata( $data ), $dataMassaged ); + } +} + +class ResourceLoaderImageTestable extends ResourceLoaderImage { + // Make some protected methods public + public function getPath( ResourceLoaderContext $context ) { + return parent::getPath( $context ); + } + public function massageSvgPathdata( $svg ) { + return parent::massageSvgPathdata( $svg ); + } + // Stub, since we don't know if we even have a SVG handler, much less what exactly it'll output + public function rasterize( $svg ) { + return 'RASTERIZESTUB'; + } +} -- 2.20.1