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