Completed
Push — master ( 55524f...247b53 )
by Jeroen De
11:41 queued 10:26
created

testLeafletImageLayersIgnoresNotFoundImages()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 8

Duplication

Lines 0
Ratio 0 %

Importance

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