Completed
Push — master ( 247b53...16b162 )
by Jeroen De
03:24
created

DisplayMapTest::testLeafletImageLayer()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 15

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 15
c 0
b 0
f 0
rs 9.7666
cc 1
nc 1
nop 0
1
<?php
2
3
declare( strict_types = 1 );
4
5
namespace Maps\Tests\Integration\Parser;
6
7
use Maps\MediaWiki\Content\GeoJsonContent;
8
use Maps\Tests\MapsTestFactory;
9
use Maps\Tests\TestDoubles\ImageValueObject;
10
use PHPUnit\Framework\TestCase;
11
12
/**
13
 * @licence GNU GPL v2+
14
 * @author Jeroen De Dauw < [email protected] >
15
 */
16
class DisplayMapTest extends TestCase {
17
18
	private $originalHeight;
19
	private $originalWidth;
20
21
	public function setUp(): void {
22
		$this->originalHeight = $GLOBALS['egMapsMapHeight'];
23
		$this->originalWidth = $GLOBALS['egMapsMapWidth'];
24
	}
25
26
	public function tearDown(): void {
27
		$GLOBALS['egMapsMapHeight'] = $this->originalHeight;
28
		$GLOBALS['egMapsMapWidth'] = $this->originalWidth;
29
	}
30
31
	public function testMapIdIsSet() {
32
		$this->assertContains(
33
			'id="map_leaflet_',
34
			$this->parse( '{{#display_map:1,1|service=leaflet}}' )
35
		);
36
	}
37
38
	private function parse( string $textToParse ): string {
39
		$parser = new \Parser();
40
41
		return $parser->parse( $textToParse, \Title::newMainPage(), new \ParserOptions() )->getText();
42
	}
43
44
	public function testServiceSelectionWorks() {
45
		$this->assertContains(
46
			'maps-googlemaps3',
47
			$this->parse( '{{#display_map:1,1|service=google}}' )
48
		);
49
	}
50
51
	public function testSingleCoordinatesAreIncluded() {
52
		$this->assertContains(
53
			'"lat":1,"lon":1',
54
			$this->parse( '{{#display_map:1,1}}' )
55
		);
56
	}
57
58
	public function testMultipleCoordinatesAreIncluded() {
59
		$result = $this->parse( '{{#display_map:1,1; 4,2}}' );
60
61
		$this->assertContains( '"lat":1,"lon":1', $result );
62
		$this->assertContains( '"lat":4,"lon":2', $result );
63
	}
64
65
	public function testWhenValidZoomIsSpecified_itGetsUsed() {
66
		$this->assertContains(
67
			'"zoom":5',
68
			$this->parse( '{{#display_map:1,1|service=google|zoom=5}}' )
69
		);
70
	}
71
72
	public function testWhenZoomIsNotSpecifiedAndThereIsOnlyOneLocation_itIsDefaulted() {
73
		$this->assertContains(
74
			'"zoom":' . $GLOBALS['egMapsGMaps3Zoom'],
75
			$this->parse( '{{#display_map:1,1|service=google}}' )
76
		);
77
	}
78
79
	public function testWhenZoomIsNotSpecifiedAndThereAreMultipleLocations_itIsDefaulted() {
80
		$this->assertContains(
81
			'"zoom":false',
82
			$this->parse( '{{#display_map:1,1;2,2|service=google}}' )
83
		);
84
	}
85
86
	public function testWhenZoomIsInvalid_itIsDefaulted() {
87
		$this->assertContains(
88
			'"zoom":' . $GLOBALS['egMapsGMaps3Zoom'],
89
			$this->parse( '{{#display_map:1,1|service=google|zoom=tomato}}' )
90
		);
91
	}
92
93
	public function testTagIsRendered() {
94
		$this->assertContains(
95
			'"lat":1,"lon":1',
96
			$this->parse( '<display_map>1,1</display_map>' )
97
		);
98
	}
99
100
	public function testTagServiceParameterIsUsed() {
101
		$this->assertContains(
102
			'maps-googlemaps3',
103
			$this->parse( '<display_map service="google">1,1</display_map>' )
104
		);
105
	}
106
107
	public function testWhenThereAreNoLocations_locationsArrayIsEmpty() {
108
		$this->assertContains(
109
			'"locations":[]',
110
			$this->parse( '{{#display_map:}}' )
111
		);
112
	}
113
114
	public function testLocationTitleGetsIncluded() {
115
		$this->assertContains(
116
			'"title":"title',
117
			$this->parse( '{{#display_map:1,1~title}}' )
118
		);
119
	}
120
121
	public function testLocationDescriptionGetsIncluded() {
122
		$this->assertContains(
123
			'such description',
124
			$this->parse( '{{#display_map:1,1~title~such description}}' )
125
		);
126
	}
127
128
	public function testRectangleDisplay() {
129
		$this->assertContains(
130
			'"title":"title',
131
			$this->parse( '{{#display_map:rectangles=1,1:2,2~title}}' )
132
		);
133
	}
134
135
	public function testCircleDisplay() {
136
		$this->assertContains(
137
			'"title":"title',
138
			$this->parse( '{{#display_map:circles=1,1:2~title}}' )
139
		);
140
	}
141
142
	public function testRectangleFillOpacityIsUsed() {
143
		$this->assertContains(
144
			'"fillOpacity":"fill opacity"',
145
			$this->parse( '{{#display_map:rectangles=1,1:2,2~title~text~color~opacity~thickness~fill color~fill opacity}}' )
146
		);
147
	}
148
149
	public function testRectangleFillColorIsUsed() {
150
		$this->assertContains(
151
			'"fillColor":"fill color"',
152
			$this->parse( '{{#display_map:rectangles=1,1:2,2~title~text~color~opacity~thickness~fill color~fill opacity}}' )
153
		);
154
	}
155
156
	public function testServiceSelectionWorksWhenItIsPrecededByMultipleParameters() {
157
		$this->assertContains(
158
			'maps-googlemaps3',
159
			$this->parse(
160
				"{{#display_map:rectangles=\n  1,1:2,2~title~text~color\n| scrollwheelzoom=off\n| service = google}}"
161
			)
162
		);
163
	}
164
165
	public function testDimensionDefaultsAsInteger() {
166
		$GLOBALS['egMapsMapHeight'] = 420;
167
		$GLOBALS['egMapsMapWidth'] = 230;
168
169
		$this->assertContains(
170
			'height: 420px;',
171
			$this->parse( '{{#display_map:1,1}}' )
172
		);
173
174
		$this->assertContains(
175
			'width: 230px;',
176
			$this->parse( '{{#display_map:1,1}}' )
177
		);
178
	}
179
180
	// TODO: need DI to test
181
//	public function testWhenLocationHasVisitedIconModifier_itIsUsed() {
182
//		$this->assertContains(
183
//			'"visitedicon":"VisitedIcon.png"',
184
//			$this->parse( '{{#display_map:1,1~title~text~icon~group~inline label~VisitedIcon.png}}' )
185
//		);
186
//	}
187
//
188
//	public function testWhenLocationHasVisitedIconModifierWithNamespacePrefix_thePrefixGetsRemoved() {
189
//		$this->assertContains(MapsMapperTest
190
//			'"visitedicon":"VisitedIcon.png"',
191
//			$this->parse( '{{#display_map:1,1~title~text~icon~group~inline label~File:VisitedIcon.png}}' )
192
//		);
193
//	}
194
//
195
//	public function testWhenVisitedIconParameterIsProvidedWithNamespacePrefix_thePrefixGetsRemoved() {
196
//		$this->assertContains(
197
//			'"visitedicon":"VisitedIcon.png"',
198
//			$this->parse( '{{#display_map:1,1|visitedicon=File:VisitedIcon.png}}' )
199
//		);
200
//	}
201
//
202
//	public function testWhenLocationHasIconModifierWithNamespacePrefix_thePrefixGetsRemoved() {
203
//		$this->assertContains(
204
//			'"icon":"Icon.png"',
205
//			$this->parse( '{{#display_map:1,1~title~text~File:Icon.png}}' )
206
//		);
207
//	}
208
209
	public function testWhenIconParameterIsProvidedButEmpty_itIsDefaulted() {
210
		$this->assertContains(
211
			'"icon":"","inlineLabel":"Ghent',
212
			$this->parse(
213
				"{{#display_map:Gent, Belgie~The city Ghent~Ghent is awesome~ ~ ~Ghent}}"
214
			)
215
		);
216
	}
217
218
	public function testWhenLocationHasNoTitleAndText_textFieldIsEmptyString() {
219
		$this->assertContains(
220
			'"text":""',
221
			$this->parse( '{{#display_map:1,1}}' )
222
		);
223
	}
224
225
	public function testGeoJsonSourceForFile() {
226
		$this->skipOn131();
227
228
		$this->assertContains(
229
			'"GeoJsonSource":null,',
230
			$this->parse(
231
				"{{#display_map:geojson=404}}"
232
			)
233
		);
234
	}
235
236
	private function skipOn131() {
237
		if ( version_compare( $GLOBALS['wgVersion'], '1.32c', '<' ) ) {
238
			$this->markTestSkipped();
239
		}
240
	}
241
242
	public function testGeoJsonSourceForPage() {
243
		$this->skipOn131();
244
245
		$page = new \WikiPage( \Title::newFromText( 'GeoJson:TestPageSource' ) );
246
		$page->doEditContent(
247
			new GeoJsonContent( json_encode( [
248
				'type' => 'FeatureCollection',
249
				'features' => []
250
			] ) ),
251
			''
252
		);
253
254
		$this->assertContains(
255
			'"GeoJsonSource":"TestPageSource",',
256
			$this->parse(
257
				"{{#display_map:geojson=TestPageSource}}"
258
			)
259
		);
260
	}
261
262
	public function testGoogleMapsKmlFiltersInvalidFileNames() {
263
		$this->assertContains(
264
			'"kml":["ValidFile.kml"],',
265
			$this->parse(
266
				"{{#display_map:service=google|kml=, ,ValidFile.kml ,}}"
267
			)
268
		);
269
	}
270
271
	public function testLeafletImageLayersIgnoresNotFoundImages() {
272
		$this->assertContains(
273
			'"imageLayers":[]',
274
			$this->parse(
275
				"{{#display_map:image layers=404.png}}"
276
			)
277
		);
278
	}
279
280
	public function testLeafletImageLayersIgnoresImageUrls() {
281
		$this->assertContains(
282
			'"imageLayers":[]',
283
			$this->parse(
284
				"{{#display_map:image layers=https://user-images.githubusercontent.com/62098559/76514021-3fa9be80-647d-11ea-82ae-715420a5c432.png}}"
285
			)
286
		);
287
	}
288
289
	public function testLeafletImageLayer() {
290
		$factory = MapsTestFactory::newTestInstance();
291
292
		$factory->imageRepo->addImage(
293
			'MyImage.png',
294
			new ImageValueObject( '/tmp/example/image.png', 40, 20 )
295
		);
296
297
		$html = $this->parse( "{{#display_map:image layers=MyImage.png}}" );
298
299
		$this->assertContains( '"name":"MyImage.png"', $html );
300
		$this->assertContains( '"url":"/tmp/example/image.png"', $html );
301
		$this->assertContains( '"width":100', $html );
302
		$this->assertContains( '"height":50', $html );
303
	}
304
305
}
306