MediaWiki REL1_31
ResourceLoaderImageTest.php
Go to the documentation of this file.
1<?php
2
7
8 protected $imagesPath;
9
10 protected function setUp() {
11 parent::setUp();
12 $this->imagesPath = __DIR__ . '/../../data/resourceloader';
13 }
14
15 protected function getTestImage( $name ) {
17 $fileDescriptor = is_string( $options ) ? $options : $options['file'];
18 $allowedVariants = ( is_array( $options ) && isset( $options['variants'] ) ) ?
19 $options['variants'] : [];
20 $variants = array_fill_keys( $allowedVariants, [ 'color' => 'red' ] );
22 $name,
23 'test',
24 $fileDescriptor,
25 $this->imagesPath,
26 $variants
27 );
28 }
29
30 public static function provideGetPath() {
31 return [
32 [ 'abc', 'en', 'abc.gif' ],
33 [ 'abc', 'he', 'abc.gif' ],
34 [ 'def', 'en', 'def.svg' ],
35 [ 'def', 'he', 'def.svg' ],
36 [ 'ghi', 'en', 'ghi.svg' ],
37 [ 'ghi', 'he', 'jkl.svg' ],
38 [ 'mno', 'en', 'mno-ltr.svg' ],
39 [ 'mno', 'ar', 'mno-rtl.svg' ],
40 [ 'mno', 'he', 'mno-ltr.svg' ],
41 [ 'pqr', 'en', 'pqr-b.svg' ],
42 [ 'pqr', 'en-gb', 'pqr-b.svg' ],
43 [ 'pqr', 'de', 'pqr-f.svg' ],
44 [ 'pqr', 'de-formal', 'pqr-f.svg' ],
45 [ 'pqr', 'ar', 'pqr-f.svg' ],
46 [ 'pqr', 'fr', 'pqr-a.svg' ],
47 [ 'pqr', 'he', 'pqr-a.svg' ],
48 ];
49 }
50
55 public function testGetPath( $imageName, $languageCode, $path ) {
56 static $dirMap = [
57 'en' => 'ltr',
58 'en-gb' => 'ltr',
59 'de' => 'ltr',
60 'de-formal' => 'ltr',
61 'fr' => 'ltr',
62 'he' => 'rtl',
63 'ar' => 'rtl',
64 ];
65 static $contexts = [];
66
67 $image = $this->getTestImage( $imageName );
69 'lang' => $languageCode,
70 'dir' => $dirMap[$languageCode],
71 ] );
72
73 $this->assertEquals( $image->getPath( $context ), $this->imagesPath . '/' . $path );
74 }
75
80 public function testGetExtension() {
81 $image = $this->getTestImage( 'def' );
82 $this->assertEquals( $image->getExtension(), 'svg' );
83 $this->assertEquals( $image->getExtension( 'original' ), 'svg' );
84 $this->assertEquals( $image->getExtension( 'rasterized' ), 'png' );
85 $image = $this->getTestImage( 'abc' );
86 $this->assertEquals( $image->getExtension(), 'gif' );
87 $this->assertEquals( $image->getExtension( 'original' ), 'gif' );
88 $this->assertEquals( $image->getExtension( 'rasterized' ), 'gif' );
89 }
90
96 public function testGetImageData() {
98
99 $image = $this->getTestImage( 'def' );
100 $data = file_get_contents( $this->imagesPath . '/def.svg' );
101 $dataConstructive = file_get_contents( $this->imagesPath . '/def_variantize.svg' );
102 $this->assertEquals( $image->getImageData( $context, null, 'original' ), $data );
103 $this->assertEquals(
104 $image->getImageData( $context, 'destructive', 'original' ),
105 $dataConstructive
106 );
107 // Stub, since we don't know if we even have a SVG handler, much less what exactly it'll output
108 $this->assertEquals( $image->getImageData( $context, null, 'rasterized' ), 'RASTERIZESTUB' );
109
110 $image = $this->getTestImage( 'abc' );
111 $data = file_get_contents( $this->imagesPath . '/abc.gif' );
112 $this->assertEquals( $image->getImageData( $context, null, 'original' ), $data );
113 $this->assertEquals( $image->getImageData( $context, null, 'rasterized' ), $data );
114 }
115
119 public function testMassageSvgPathdata() {
120 $image = $this->getTestImage( 'ghi' );
121 $data = file_get_contents( $this->imagesPath . '/ghi.svg' );
122 $dataMassaged = file_get_contents( $this->imagesPath . '/ghi_massage.svg' );
123 $this->assertEquals( $image->massageSvgPathdata( $data ), $dataMassaged );
124 }
125}
126
128 // Make some protected methods public
129 public function massageSvgPathdata( $svg ) {
130 return parent::massageSvgPathdata( $svg );
131 }
132 // Stub, since we don't know if we even have a SVG handler, much less what exactly it'll output
133 public function rasterize( $svg ) {
134 return 'RASTERIZESTUB';
135 }
136}
testGetImageData()
ResourceLoaderImage::getImageData ResourceLoaderImage::variantize ResourceLoaderImage::massageSvgPath...
testGetPath( $imageName, $languageCode, $path)
ResourceLoaderImage::getPath provideGetPath.
testGetExtension()
ResourceLoaderImage::getExtension ResourceLoaderImage::getMimeType.
testMassageSvgPathdata()
ResourceLoaderImage::massageSvgPathdata.
massageSvgPathdata( $svg)
Massage the SVG image data for converters which don't understand some path data syntax.
rasterize( $svg)
Convert passed image data, which is assumed to be SVG, to PNG.
Class encapsulating an image used in a ResourceLoaderImageModule.
getResourceLoaderContext( $options=[], ResourceLoader $rl=null)
this hook is for auditing only or null if authentication failed before getting that far or null if we can t even determine that probably a stub it is not rendered in wiki pages or galleries in category pages allow injecting custom HTML after the section Any uses of the hook need to handle escaping see BaseTemplate::getToolbox and BaseTemplate::makeListItem for details on the format of individual items inside of this array or by returning and letting standard HTTP rendering take place modifiable or by returning false and taking over the output modifiable modifiable after all normalizations have been except for the $wgMaxImageArea check $image
Definition hooks.txt:895
null means default in associative array with keys and values unescaped Should be merged with default with a value of false meaning to suppress the attribute in associative array with keys and values unescaped & $options
Definition hooks.txt:2001
do that in ParserLimitReportFormat instead use this to modify the parameters of the image all existing parser cache entries will be invalid To avoid you ll need to handle that somehow(e.g. with the RejectParserCacheValue hook) because MediaWiki won 't do it for you. & $defaults also a ContextSource after deleting those rows but within the same transaction you ll probably need to make sure the header is varied on and they can depend only on the ResourceLoaderContext $context
Definition hooks.txt:2811
Allows to change the fields on the form that will be generated $name
Definition hooks.txt:302