1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Dynamic\Locator\Tests\Control; |
4
|
|
|
|
5
|
|
|
use DOMDocument; |
6
|
|
|
use Dynamic\Locator\Control\APIController; |
7
|
|
|
use Dynamic\Locator\Location; |
8
|
|
|
use SilverStripe\Control\HTTPRequest; |
9
|
|
|
use SilverStripe\Control\Session; |
10
|
|
|
use SilverStripe\Core\Config\Config; |
11
|
|
|
use SilverStripe\Dev\FunctionalTest; |
12
|
|
|
use SilverStripe\View\ViewableData; |
13
|
|
|
|
14
|
|
|
/** |
15
|
|
|
* Class APIControllerTest |
16
|
|
|
* @package Dynamic\Locator\Tests\Control |
17
|
|
|
*/ |
18
|
|
|
class APIControllerTest extends FunctionalTest |
19
|
|
|
{ |
20
|
|
|
/** |
21
|
|
|
* @var string |
22
|
|
|
*/ |
23
|
|
|
protected static $fixture_file = '../fixtures.yml'; |
24
|
|
|
|
25
|
|
|
/** |
26
|
|
|
* @var bool |
27
|
|
|
*/ |
28
|
|
|
protected static $use_draft_site = true; |
29
|
|
|
|
30
|
|
|
/** |
31
|
|
|
* |
32
|
|
|
*/ |
33
|
|
|
public function testIndex() |
34
|
|
|
{ |
35
|
|
|
$controller = APIController::create(); |
36
|
|
|
$this->assertInstanceOf(ViewableData::class, $controller->index($controller->getRequest())); |
37
|
|
|
} |
38
|
|
|
|
39
|
|
|
/** |
40
|
|
|
* |
41
|
|
|
*/ |
42
|
|
|
public function testXml() |
43
|
|
|
{ |
44
|
|
|
/** @var APIController $controller */ |
45
|
|
|
$controller = APIController::create(); |
46
|
|
|
$page = $this->get($controller->Link('xml')); |
47
|
|
|
|
48
|
|
|
$this->assertEquals(200, $page->getStatusCode()); |
49
|
|
|
$this->assertEquals('application/xml', $page->getHeader('content-type')); |
50
|
|
|
|
51
|
|
|
$dom = new DOMDocument(); |
52
|
|
|
// true if it loads, false if it doesn't |
53
|
|
|
$valid = $dom->loadXML($page->getBody()); |
54
|
|
|
$this->assertTrue($valid); |
55
|
|
|
} |
56
|
|
|
|
57
|
|
|
/** |
58
|
|
|
* |
59
|
|
|
*/ |
60
|
|
|
public function testJson() |
61
|
|
|
{ |
62
|
|
|
/** @var APIController $controller */ |
63
|
|
|
$controller = APIController::create(); |
64
|
|
|
$page = $this->get($controller->Link('json')); |
65
|
|
|
|
66
|
|
|
$this->assertEquals(200, $page->getStatusCode()); |
67
|
|
|
$this->assertEquals('application/json', $page->getHeader('content-type')); |
68
|
|
|
|
69
|
|
|
$json = json_decode($page->getBody()); |
70
|
|
|
// if it is null its not valid |
71
|
|
|
$this->assertNotNull($json); |
72
|
|
|
} |
73
|
|
|
|
74
|
|
|
/** |
75
|
|
|
* |
76
|
|
|
*/ |
77
|
|
|
public function testGetLocations() |
78
|
|
|
{ |
79
|
|
|
/** @var APIController $controller */ |
80
|
|
|
$controller = APIController::create(); |
81
|
|
|
|
82
|
|
|
$filter = Config::inst()->get(APIController::class, 'base_filter'); |
83
|
|
|
$filterAny = Config::inst()->get(APIController::class, 'base_filter_any'); |
84
|
|
|
$exclude = Config::inst()->get(APIController::class, 'base_exclude'); |
85
|
|
|
|
86
|
|
|
$locations = Location::get()->filter($filter)->filterAny($filterAny)->exclude($exclude); |
87
|
|
|
$this->assertEquals($locations->count(), $controller->getLocationList($this->createRequest())->count()); |
88
|
|
|
} |
89
|
|
|
|
90
|
|
|
/** |
91
|
|
|
* |
92
|
|
|
*/ |
93
|
|
|
public function testGetLocationList() |
94
|
|
|
{ |
95
|
|
|
/** @var APIController $controller */ |
96
|
|
|
$controller = APIController::create(); |
97
|
|
|
|
98
|
|
|
$filter = Config::inst()->get(APIController::class, 'base_filter'); |
99
|
|
|
$filterAny = Config::inst()->get(APIController::class, 'base_filter_any'); |
100
|
|
|
$exclude = Config::inst()->get(APIController::class, 'base_exclude'); |
101
|
|
|
|
102
|
|
|
$locations = Location::get()->filter($filter)->filterAny($filterAny)->exclude($exclude); |
103
|
|
|
$locationsB = $locations->limit(2); |
104
|
|
|
|
105
|
|
|
$this->assertEquals($locations->count(), $controller->getLocationList($this->createRequest(array( |
106
|
|
|
'Limit' => 2, |
107
|
|
|
)))->count()); |
108
|
|
|
|
109
|
|
|
$this->assertEquals($locationsB->count(), $controller->getLocationList($this->createRequest(array( |
110
|
|
|
'Limit' => -50, |
111
|
|
|
)))->count()); |
112
|
|
|
} |
113
|
|
|
|
114
|
|
|
/** |
115
|
|
|
* |
116
|
|
|
*/ |
117
|
|
|
public function testGetFilter() |
118
|
|
|
{ |
119
|
|
|
/** @var APIController $controller */ |
120
|
|
|
$controller = APIController::create(); |
121
|
|
|
$this->assertEquals(array(), $controller->getFilter($this->createRequest())); |
122
|
|
|
|
123
|
|
|
$this->assertEquals(array( |
124
|
|
|
'Categories.ID' => '1', |
125
|
|
|
), $controller->getFilter($this->createRequest(array( |
126
|
|
|
'CategoryID' => '1', |
127
|
|
|
)))); |
128
|
|
|
} |
129
|
|
|
|
130
|
|
|
/** |
131
|
|
|
* |
132
|
|
|
*/ |
133
|
|
|
public function testGetFilterAny() |
134
|
|
|
{ |
135
|
|
|
/** @var APIController $controller */ |
136
|
|
|
$controller = APIController::create(); |
137
|
|
|
$request = $this->createRequest(); |
138
|
|
|
|
139
|
|
|
$this->assertEquals(array(), $controller->getFilterAny($request)); |
140
|
|
|
|
141
|
|
|
$controller->config()->update('base_filter_any', array( |
142
|
|
|
'Any' => 'one', |
143
|
|
|
)); |
144
|
|
|
|
145
|
|
|
$this->assertEquals(array( |
146
|
|
|
'Any' => 'one', |
147
|
|
|
), $controller->getFilterAny($request)); |
148
|
|
|
} |
149
|
|
|
|
150
|
|
|
/** |
151
|
|
|
* |
152
|
|
|
*/ |
153
|
|
|
public function testGetExclude() |
154
|
|
|
{ |
155
|
|
|
/** @var APIController $controller */ |
156
|
|
|
$controller = APIController::create(); |
157
|
|
|
$request = $this->createRequest(); |
158
|
|
|
|
159
|
|
|
$this->assertEquals(array( |
160
|
|
|
'Lat' => 0, |
161
|
|
|
'Lng' => 0, |
162
|
|
|
), $controller->getExclude($request)); |
163
|
|
|
|
164
|
|
|
$controller->config()->update('base_exclude', array( |
165
|
|
|
'Any' => 'one', |
166
|
|
|
)); |
167
|
|
|
|
168
|
|
|
$this->assertEquals(array( |
169
|
|
|
'Any' => 'one', |
170
|
|
|
'Lat' => 0, |
171
|
|
|
'Lng' => 0, |
172
|
|
|
), $controller->getExclude($request)); |
173
|
|
|
} |
174
|
|
|
|
175
|
|
|
/** |
176
|
|
|
* |
177
|
|
|
*/ |
178
|
|
|
public function testGetLimit() |
179
|
|
|
{ |
180
|
|
|
/** @var APIController $controller */ |
181
|
|
|
$controller = APIController::create(); |
182
|
|
|
|
183
|
|
|
$this->assertEquals(50, $controller->getLimit($this->createRequest())); |
184
|
|
|
|
185
|
|
|
$this->assertEquals(50, $controller->getLimit( |
186
|
|
|
$this->createRequest(array( |
187
|
|
|
'Limit' => 109, |
188
|
|
|
)) |
189
|
|
|
)); |
190
|
|
|
|
191
|
|
|
$this->assertEquals(25, $controller->getLimit( |
192
|
|
|
$this->createRequest(array( |
193
|
|
|
'Limit' => 25, |
194
|
|
|
)) |
195
|
|
|
)); |
196
|
|
|
} |
197
|
|
|
|
198
|
|
|
/** |
199
|
|
|
* @param array $getVars |
200
|
|
|
* @return \SilverStripe\Control\HTTPRequest |
201
|
|
|
*/ |
202
|
|
|
private function createRequest($getVars = array()) |
203
|
|
|
{ |
204
|
|
|
$link = APIController::create()->Link(); |
205
|
|
|
|
206
|
|
|
$request = new HTTPRequest('GET', $link, $getVars); |
207
|
|
|
$session = new Session([]); |
208
|
|
|
$request->setSession($session); |
209
|
|
|
|
210
|
|
|
return $request; |
211
|
|
|
} |
212
|
|
|
} |
213
|
|
|
|