Completed
Push — AUTOMATED_TESTING ( 6d2161...4cfed4 )
by Gordon
144:02 queued 110:40
created

MapExtensionTest::testBasicMapWithLayer()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 19
Code Lines 15

Duplication

Lines 0
Ratio 0 %
Metric Value
dl 0
loc 19
rs 9.4286
cc 1
eloc 15
nc 1
nop 0
1
<?php
2
3
class MapExtensionTest extends SapphireTest {
4
	protected static $fixture_file = 'mappable/tests/mapextensions.yml';
5
6
	public function setUpOnce() {
7
		$this->requiredExtensions = array(
8
			'Member' => array('MapExtension', 'MapLayerExtension')
9
		);
10
		parent::setupOnce();
11
	}
12
13
14
	public function testUpdateCMSFields() {
15
16
	}
17
18
19
	public function testGetMappableLatitude() {
20
		$instance = $this->getInstance();
21
		$instance->Lat = 42.1;
22
		$instance->write();
23
		$this->assertEquals(
24
			42.1,
25
			$instance->getMappableLatitude()
26
		);
27
	}
28
29
30
	public function testGetMappableLongitude() {
31
		$instance = $this->getInstance();
32
		$instance->Lon = 42.1;
33
		$this->assertEquals(
34
			42.1,
35
			$instance->getMappableLongitude()
36
		);
37
	}
38
39
40
	public function testgetMappableMapContent() {
41
42
	}
43
44
45
	public function testonBeforeWriteMapPinNotEdited() {
46
		$instance = $this->getInstance();
47
		$this->Lat = 0;
48
		$this->Lon = 0;
49
		$instance->write();
50
		$this->assertFalse($instance->MapPinEdited);
51
	}
52
53
	public function testonBeforeWriteMapPinEdited() {
54
		$instance = $this->getInstance();
55
56
		// north
57
		$this->showMapPinEdited($instance, 10, 0);
58
59
		// north west
60
		$this->showMapPinEdited($instance, 10, -10);
61
62
		// west
63
		$this->showMapPinEdited($instance, 0, -10);
64
65
		// south west
66
		$this->showMapPinEdited($instance, -10, -10);
67
68
		// south
69
		$this->showMapPinEdited($instance, -10, 0);
70
71
		// south east
72
		$this->showMapPinEdited($instance, -10, 10);
73
74
		// east
75
		$this->showMapPinEdited($instance, 0, 10);
76
77
		// north east
78
		$this->showMapPinEdited($instance, 10, 10);
79
80
	}
81
82
83
	// FIXME - use an actual file here
84
	public function testGetMappableMapPin() {
85
		$instance = $this->getInstance();
86
87
		// no icon, so return false
88
		$this->assertFalse($instance->getMappableMapPin());
89
90
		// add a default map icon
91
		$this->addMapPinToInstance($instance);
92
93
		$pin1 = $instance->getMappableMapPin();
94
		$this->assertStringEndsWith('mappable/tests/images/mapicontest.png', $pin1);
0 ignored issues
show
Bug introduced by
The method assertStringEndsWith() does not seem to exist on object<MapExtensionTest>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
95
96
		// Test the cached pin - this is the case of a layer providing a cached pin
97
		// so that repeated calls are not made to load the same icon
98
		$iconURL =
99
			'https://cdn3.iconfinder.com/data/icons/iconic-1/32/map_pin_fill-512.png';
100
101
		// Set mappable as having no icon, use the cached one instead
102
		// This simulates using a common icon to avoid DB queries
103
		$instance->MapPinIconID = 0;
104
		$instance->write();
105
		$instance->CachedMapPinURL = $iconURL;
106
		$pin2 = $instance->getMappableMapPin();
107
		$this->assertEquals($iconURL, $pin2);
108
	}
109
110
111
112
113
114
	public function testHasGeoWest() {
115
		$instance = $this->getInstance();
116
		$instance->Lon = -20;
117
		$this->assertTrue($instance->HasGeo());
118
	}
119
120
	public function testHasGeoEast() {
121
		$instance = $this->getInstance();
122
		$instance->Lon = 20;
123
		$this->assertTrue($instance->HasGeo());
124
	}
125
126
	public function testHasGeoNorth() {
127
		$instance = $this->getInstance();
128
		$instance->Lat = 20;
129
		$this->assertTrue($instance->HasGeo());
130
	}
131
132
	public function testHasGeoNorthWest() {
133
		$instance = $this->getInstance();
134
		$instance->Lat = 20;
135
		$instance->Lon = -20;
136
		$this->assertTrue($instance->HasGeo());
137
	}
138
139
	public function testHasGeoNortEast() {
140
		$instance = $this->getInstance();
141
		$instance->Lat = 20;
142
		$instance->Lon = 20;
143
		$this->assertTrue($instance->HasGeo());
144
	}
145
146
	public function testHasGeoSouth() {
147
		$instance = $this->getInstance();
148
		$instance->Lat = -20;
149
		$this->assertTrue($instance->HasGeo());
150
	}
151
152
	public function testHasGeoSouthWest() {
153
		$instance = $this->getInstance();
154
		$instance->Lat = -20;
155
		$instance->Lon = -20;
156
		$this->assertTrue($instance->HasGeo());
157
	}
158
159
	public function testHasGeoSouthEast() {
160
		$instance = $this->getInstance();
161
		$instance->Lat = -20;
162
		$instance->Lon = 20;
163
		$this->assertTrue($instance->HasGeo());
164
	}
165
166
	public function testHasGeoOrigin() {
167
		$instance = $this->getInstance();
168
		$instance->Lat = 0;
169
		$instance->Lon = 0;
170
		$this->assertFalse($instance->HasGeo());
171
	}
172
173
	public function testHasGeoOriginMapLayerExtension() {
174
		$instance = $this->getInstance();
175
176
		// assert that origin has no geo (until layers added)
177
		$instance->Lat = 0;
178
		$instance->Lon = 0;
179
		$this->assertFalse($instance->HasGeo());
180
181
		$this->addLayerToInstance($instance);
182
183
		// assert has geo even when at the origin of 0,0
184
		$this->assertTrue($instance->HasGeo());
185
	}
186
187
	public function testBasicMap() {
188
		$instance = $this->getInstance();
189
		$instance->Lat = 37.1;
190
		$instance->Lon = 28;
191
		$instance->Zoom = 12;
192
		$instance->MapPinEdited = true;
193
		$html = $instance->BasicMap()->forTemplate();
194
195
		$expected = "data-centre='{\"lat\":37.1,\"lng\":28}'";
196
		$this->assertContains($expected, $html);
197
		$expected = "data-mapmarkers='[{\"latitude\":37.1,\"longitude\":28,\"html\":\"MEMBER: \",\"category\":\"default\",\"icon\":false}]'";
198
		$this->assertContains($expected, $html);
199
200
		// This is only set automatically with layers
201
		$expected = 'data-enableautocentrezoom=false';
202
		$this->assertContains($expected, $html);
203
	}
204
205
	public function testBasicMapWithLayer() {
206
		$instance = $this->getInstance();
207
		$instance->Lat = 37.1;
208
		$instance->Lon = 28;
209
		$instance->Zoom = 12;
210
		$instance->MapPinEdited = true;
211
		$instance->write();
212
		$this->addLayerToInstance($instance);
213
		$html = $instance->BasicMap()->forTemplate();
214
215
		$expected = "data-centre='{\"lat\":37.1,\"lng\":28}'";
216
		$this->assertContains($expected, $html);
217
		$expected = "data-mapmarkers='[{\"latitude\":37.1,\"longitude\":28,\"html\":\"MEMBER: \",\"category\":\"default\",\"icon\":false}]'";
218
		$this->assertContains($expected, $html);
219
220
		// This is only set automatically with layers
221
		$expected = 'data-enableautocentrezoom=1';
222
		$this->assertContains($expected, $html);
223
	}
224
225
226
	public function testGetMapField() {
227
		$instance = $this->getInstance();
228
		$this->Lat = 37.1;
229
		$this->Lon = 28;
230
		$this->Zoom = 12;
231
		$mapField  = $instance->getMapField();
232
		$this->assertInstanceOf('LatLongField', $mapField);
233
	}
234
235
236
	public function testUseCompressedAssets() {
237
		$original = Config::inst()->get('Mappable', 'use_compressed_assets');
238
239
		$instance = $this->getInstance();
240
		Config::inst()->update('Mappable', 'use_compressed_assets', false);
241
		$this->assertFalse($instance->UseCompressedAssets());
242
243
		$instance = $this->getInstance();
244
		Config::inst()->update('Mappable', 'use_compressed_assets', true);
245
		$this->assertTrue($instance->UseCompressedAssets());
246
247
		Config::inst()->update('Mappable', 'use_compressed_assets', $original);
248
	}
249
250
251
	private function getInstance() {
252
		$instance = new Member();
253
		return $instance;
254
	}
255
256
	/*
257
	Add a map layer to an existing instance
258
	 */
259
	private function addLayerToInstance(&$instance) {
260
		// Create a layer
261
		$kmlFile = new File(array('Name' => 'example.kml', 'Filename' => 'mappable/tests/kml/example.kml'));
262
		$layer = new MapLayer();
263
		$layer->Title = 'Example KML Layer';
0 ignored issues
show
Documentation introduced by
The property Title does not exist on object<MapLayer>. Since you implemented __set, maybe consider adding a @property annotation.

Since your code implements the magic setter _set, this function will be called for any write access on an undefined variable. You can add the @property annotation to your class or interface to document the existence of this variable.

<?php

/**
 * @property int $x
 * @property int $y
 * @property string $text
 */
class MyLabel
{
    private $properties;

    private $allowedProperties = array('x', 'y', 'text');

    public function __get($name)
    {
        if (isset($properties[$name]) && in_array($name, $this->allowedProperties)) {
            return $properties[$name];
        } else {
            return null;
        }
    }

    public function __set($name, $value)
    {
        if (in_array($name, $this->allowedProperties)) {
            $properties[$name] = $value;
        } else {
            throw new \LogicException("Property $name is not defined.");
        }
    }

}

Since the property has write access only, you can use the @property-write annotation instead.

Of course, you may also just have mistyped another name, in which case you should fix the error.

See also the PhpDoc documentation for @property.

Loading history...
264
		$layer->KmlFile = $kmlFile;
0 ignored issues
show
Documentation introduced by
The property KmlFile does not exist on object<MapLayer>. Since you implemented __set, maybe consider adding a @property annotation.

Since your code implements the magic setter _set, this function will be called for any write access on an undefined variable. You can add the @property annotation to your class or interface to document the existence of this variable.

<?php

/**
 * @property int $x
 * @property int $y
 * @property string $text
 */
class MyLabel
{
    private $properties;

    private $allowedProperties = array('x', 'y', 'text');

    public function __get($name)
    {
        if (isset($properties[$name]) && in_array($name, $this->allowedProperties)) {
            return $properties[$name];
        } else {
            return null;
        }
    }

    public function __set($name, $value)
    {
        if (in_array($name, $this->allowedProperties)) {
            $properties[$name] = $value;
        } else {
            throw new \LogicException("Property $name is not defined.");
        }
    }

}

Since the property has write access only, you can use the @property-write annotation instead.

Of course, you may also just have mistyped another name, in which case you should fix the error.

See also the PhpDoc documentation for @property.

Loading history...
265
		$layer->write();
266
267
		// add the layer to the Member object
268
		$layers = $instance->MapLayers();
269
		$layers->add($layer);
270
	}
271
272
	private function addMapPinToInstance(&$instance) {
273
		// Create a pin
274
		$imageFile = new Image(array('Name' => 'mapicontest.png', 'Filename' => 'mappable/tests/images/mapicontest.png'));
275
		$imageFile->write();
276
		$instance->MapPinIconID = $imageFile->ID;
277
		$instance->write();
278
	}
279
280
	/**
281
	 * @param integer $lat
282
	 * @param integer $lon
283
	 */
284
	private function showMapPinEdited(&$instance, $lat, $lon) {
285
		$instance->Lat = $lat;
286
		$instance->Long = $lon;
287
		$instance->write();
288
		$this->assertTrue($instance->MapPinEdited);
289
	}
290
291
292
293
}
294