Completed
Push — master ( 4427e6...cf958e )
by Jeroen De
03:32 queued 11s
created

DisplayMapTest   A

Complexity

Total Complexity 24

Size/Duplication

Total Lines 219
Duplicated Lines 0 %

Coupling/Cohesion

Components 2
Dependencies 1

Importance

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

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