1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
class MapUtilTest extends SapphireTest |
4
|
|
|
{ |
5
|
|
|
/* |
6
|
|
|
Other tests: |
7
|
|
|
1) List, ArrayList, DataList, null for get_map |
8
|
|
|
2) Negative and zero map sizes |
9
|
|
|
3) Invalid map type |
10
|
|
|
|
11
|
|
|
|
12
|
|
|
public function setUpOnce() { |
13
|
|
|
$this->requiredExtensions = array( |
14
|
|
|
'Member' => array('MapExtension') |
15
|
|
|
); |
16
|
|
|
parent::setupOnce(); |
17
|
|
|
} |
18
|
|
|
*/ |
19
|
11 |
|
public function setUp() |
20
|
|
|
{ |
21
|
11 |
|
MapUtil::reset(); |
22
|
11 |
|
parent::setUp(); |
23
|
11 |
|
} |
24
|
|
|
|
25
|
1 |
|
public function test_set_api_key_string() |
|
|
|
|
26
|
|
|
{ |
27
|
1 |
|
MapUtil::set_api_key('PRETENDAPIKEY'); |
28
|
1 |
|
$html = $this->htmlForMap(); |
29
|
1 |
|
$this->assertContains( |
30
|
1 |
|
' data-google-map-key="PRETENDAPIKEY" data-google-map-lang="en"', |
31
|
|
|
$html |
32
|
1 |
|
); |
33
|
|
|
|
34
|
1 |
|
$html = $this->htmlForMap(); |
35
|
1 |
|
$this->assertNotContains( |
36
|
1 |
|
' data-google-map-key="PRETENDAPIKEY" data-google-map-lang="en"', |
37
|
|
|
$html |
38
|
1 |
|
); |
39
|
1 |
|
} |
40
|
|
|
|
41
|
1 |
|
public function test_set_api_key_host_array() |
|
|
|
|
42
|
|
|
{ |
43
|
1 |
|
$url = Director::absoluteBaseURL(); |
44
|
|
|
// remove http and https |
45
|
1 |
|
$url = str_replace('http://', '', $url); |
46
|
1 |
|
$url = str_replace('https://', '', $url); |
47
|
1 |
|
$parts = explode('/', $url); |
48
|
1 |
|
$host = $parts[0]; |
49
|
1 |
|
$key = array($host => 'PRETENDAPIKEY'); |
50
|
1 |
|
MapUtil::set_api_key($key); |
|
|
|
|
51
|
1 |
|
$html = $this->htmlForMap(); |
52
|
1 |
|
$this->assertContains( |
53
|
1 |
|
' data-google-map-key="PRETENDAPIKEY" data-google-map-lang="en"', |
54
|
|
|
$html |
55
|
1 |
|
); |
56
|
|
|
|
57
|
1 |
|
$html = $this->htmlForMap(); |
58
|
1 |
|
$this->assertNotContains( |
59
|
1 |
|
' data-google-map-key="PRETENDAPIKEY" data-google-map-lang="en"', |
60
|
|
|
$html |
61
|
1 |
|
); |
62
|
1 |
|
} |
63
|
|
|
|
64
|
1 |
|
public function test_set_map_size() |
|
|
|
|
65
|
|
|
{ |
66
|
1 |
|
MapUtil::set_map_size('890px', '24em'); |
67
|
1 |
|
$html = $this->htmlForMap(); |
68
|
1 |
|
$this->assertContains(' style="width:890px; height: 24em;"', $html); |
69
|
1 |
|
} |
70
|
|
|
|
71
|
1 |
|
public function testSanitizeEmptyString() |
72
|
|
|
{ |
73
|
1 |
|
$this->assertEquals( |
74
|
1 |
|
'', |
75
|
1 |
|
MapUtil::sanitize('') |
76
|
1 |
|
); |
77
|
1 |
|
} |
78
|
|
|
|
79
|
1 |
|
public function testSanitizeAlreadySanitized() |
80
|
|
|
{ |
81
|
1 |
|
$this->assertEquals( |
82
|
1 |
|
'This is already sanitized', |
83
|
1 |
|
MapUtil::sanitize('This is already sanitized') |
84
|
1 |
|
); |
85
|
1 |
|
} |
86
|
|
|
|
87
|
1 |
|
public function testSanitizeSlashN() |
88
|
|
|
{ |
89
|
1 |
|
$this->assertEquals( |
90
|
1 |
|
'String to be sanitized', |
91
|
1 |
|
MapUtil::sanitize("String\n to be sanitized") |
92
|
1 |
|
); |
93
|
1 |
|
} |
94
|
|
|
|
95
|
1 |
|
public function testSanitizeSlashT() |
96
|
|
|
{ |
97
|
1 |
|
$this->assertEquals( |
98
|
1 |
|
'String to be sanitized', |
99
|
1 |
|
MapUtil::sanitize("String\t to be sanitized") |
100
|
1 |
|
); |
101
|
1 |
|
} |
102
|
|
|
|
103
|
1 |
|
public function testSanitizeSlashR() |
104
|
|
|
{ |
105
|
1 |
|
$this->assertEquals( |
106
|
1 |
|
'String to be sanitized', |
107
|
1 |
|
MapUtil::sanitize("String\r to be sanitized") |
108
|
1 |
|
); |
109
|
1 |
|
} |
110
|
|
|
|
111
|
|
|
/** |
112
|
|
|
* A single marker for the Member should appear in the UJS map data. |
113
|
|
|
*/ |
114
|
1 |
|
public function testSingularMappableItemMarkerUJSExists() |
115
|
|
|
{ |
116
|
1 |
|
Member::add_extension('MapExtension'); |
117
|
1 |
|
$member = new Member(); |
118
|
1 |
|
$member->Lat = 12.847; |
119
|
1 |
|
$member->Lon = 29.24; |
120
|
|
|
|
121
|
|
|
// because we are not writing, set this manually |
122
|
1 |
|
$member->MapPinEdited = true; |
123
|
1 |
|
$list = new ArrayList(); |
124
|
1 |
|
$list->push($member); |
125
|
1 |
|
$map = MapUtil::get_map($list, array()); |
126
|
1 |
|
$html = $map->forTemplate(); |
127
|
1 |
|
$markerExpected = 'data-mapmarkers=\'[{"latitude":12.847,"longitude":29.24,"html":"MEMBER: ","category":"default","icon":false}]\''; |
128
|
1 |
|
$this->assertContains($markerExpected, $html); |
129
|
1 |
|
Member::remove_extension('MapExtension'); |
130
|
1 |
|
} |
131
|
|
|
|
132
|
5 |
|
private function htmlForMap() |
133
|
|
|
{ |
134
|
5 |
|
$map = MapUtil::get_map(new ArrayList(), array()); |
135
|
5 |
|
$html = $map->forTemplate(); |
136
|
|
|
|
137
|
5 |
|
return $html; |
138
|
|
|
} |
139
|
|
|
|
140
|
1 |
|
public function test_set_center() |
|
|
|
|
141
|
|
|
{ |
142
|
1 |
|
MapUtil::set_center('Klong Tan, Bangkok, Thailand'); |
143
|
1 |
|
$html = $this->htmlForMap(); |
144
|
|
|
//coordinates of Klong Tan in Bangkok |
145
|
1 |
|
$expected = 'data-centre=\'{"lat":13.7243075,"lng":100.5718086}'; |
146
|
1 |
|
$this->assertContains($expected, $html); |
147
|
1 |
|
} |
148
|
|
|
|
149
|
1 |
|
public function test_set_map_type() |
|
|
|
|
150
|
|
|
{ |
151
|
|
|
$mapTypes = array( |
152
|
1 |
|
'road' => 'road', |
153
|
1 |
|
'satellite' => 'satellite', |
154
|
1 |
|
'hybrid' => 'hybrid', |
155
|
1 |
|
'terrain' => 'terrain', |
156
|
1 |
|
'google.maps.MapTypeId.ROADMAP' => 'road', |
157
|
1 |
|
'google.maps.MapTypeId.SATELLITE' => 'satellite', |
158
|
1 |
|
'google.maps.MapTypeId.G_HYBRID_MAP' => 'hybrid', |
159
|
1 |
|
'google.maps.MapTypeId.G_PHYSICAL_MAP' => 'terrain', |
160
|
1 |
|
'custom_layer' => 'custom_layer', |
161
|
|
|
|
162
|
1 |
|
); |
163
|
|
|
|
164
|
1 |
|
foreach (array_keys($mapTypes) as $mapType) { |
165
|
1 |
|
MapUtil::set_map_type($mapType); |
166
|
1 |
|
$expected = "data-maptype='".$mapTypes[$mapType]."'"; |
167
|
1 |
|
$html = $this->htmlForMap(); |
168
|
1 |
|
$this->assertContains($expected, $html); |
169
|
1 |
|
} |
170
|
1 |
|
} |
171
|
|
|
} |
172
|
|
|
|