Completed
Push — master ( be79d2...7ec48f )
by Jeroen De
03:22
created

DisplayMapTest   A

Complexity

Total Complexity 29

Size/Duplication

Total Lines 256
Duplicated Lines 0 %

Coupling/Cohesion

Components 2
Dependencies 1

Importance

Changes 0
Metric Value
wmc 29
lcom 2
cbo 1
dl 0
loc 256
rs 10
c 0
b 0
f 0

28 Methods

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