|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
/** |
|
4
|
|
|
* Tests the {@see SpellController} class |
|
5
|
|
|
*/ |
|
6
|
|
|
class SpellControllerTest extends FunctionalTest { |
|
|
|
|
|
|
7
|
|
|
|
|
8
|
|
|
protected $securityWasEnabled = false; |
|
9
|
|
|
|
|
10
|
|
|
public function setUpOnce() { |
|
11
|
|
|
if (class_exists('Phockito')) { |
|
12
|
|
|
Phockito::include_hamcrest(); |
|
13
|
|
|
} |
|
14
|
|
|
|
|
15
|
|
|
parent::setUpOnce(); |
|
16
|
|
|
} |
|
17
|
|
|
|
|
18
|
|
|
public function setUp() { |
|
19
|
|
|
parent::setUp(); |
|
20
|
|
|
Config::nest(); |
|
21
|
|
|
Injector::nest(); |
|
22
|
|
|
$this->securityWasEnabled = SecurityToken::is_enabled(); |
|
23
|
|
|
|
|
24
|
|
|
// Check dependencies |
|
25
|
|
|
if (!class_exists('Phockito')) { |
|
26
|
|
|
$this->skipTest = true; |
|
27
|
|
|
return $this->markTestSkipped("These tests need the Phockito module installed to run"); |
|
|
|
|
|
|
28
|
|
|
} |
|
29
|
|
|
|
|
30
|
|
|
// Reset config |
|
31
|
|
|
Config::inst()->update('SpellController', 'required_permission', 'CMS_ACCESS_CMSMain'); |
|
32
|
|
|
Config::inst()->remove('SpellController', 'locales'); |
|
33
|
|
|
Config::inst()->update('SpellController', 'locales', array('en_US', 'en_NZ', 'fr_FR')); |
|
34
|
|
|
Config::inst()->update('SpellController', 'enable_security_token', true); |
|
35
|
|
|
SecurityToken::enable(); |
|
36
|
|
|
|
|
37
|
|
|
// Setup mock for testing provider |
|
38
|
|
|
$spellChecker = Phockito::mock('SpellProvider'); |
|
39
|
|
|
Phockito::when($spellChecker) |
|
40
|
|
|
->checkWords('en_NZ', array('collor', 'colour', 'color', 'onee', 'correct')) |
|
41
|
|
|
->return(array('collor', 'color', 'onee')); |
|
42
|
|
|
Phockito::when($spellChecker) |
|
43
|
|
|
->checkWords('en_US', array('collor', 'colour', 'color', 'onee', 'correct')) |
|
44
|
|
|
->return(array('collor', 'colour', 'onee')); |
|
45
|
|
|
Phockito::when($spellChecker) |
|
46
|
|
|
->getSuggestions('en_NZ', 'collor') |
|
47
|
|
|
->return(array('collar', 'colour')); |
|
48
|
|
|
Phockito::when($spellChecker) |
|
49
|
|
|
->getSuggestions('en_US', 'collor') |
|
50
|
|
|
->return(array('collar', 'color')); |
|
51
|
|
|
Injector::inst()->registerService($spellChecker, 'SpellProvider'); |
|
52
|
|
|
} |
|
53
|
|
|
|
|
54
|
|
|
public function tearDown() { |
|
55
|
|
|
if($this->securityWasEnabled) SecurityToken::enable(); |
|
56
|
|
|
else SecurityToken::disable(); |
|
57
|
|
|
Injector::unnest(); |
|
58
|
|
|
Config::unnest(); |
|
59
|
|
|
parent::tearDown(); |
|
60
|
|
|
} |
|
61
|
|
|
|
|
62
|
|
|
/** |
|
63
|
|
|
* Tests security ID check |
|
64
|
|
|
*/ |
|
65
|
|
|
public function testSecurityID() { |
|
66
|
|
|
// Mock token |
|
67
|
|
|
$securityToken = SecurityToken::inst(); |
|
68
|
|
|
$generator = new RandomGenerator(); |
|
69
|
|
|
$token = $generator->randomToken('sha1'); |
|
70
|
|
|
$session = array( |
|
71
|
|
|
$securityToken->getName() => $token |
|
72
|
|
|
); |
|
73
|
|
|
$tokenError = _t( |
|
74
|
|
|
'SpellController.SecurityMissing', |
|
75
|
|
|
'Your session has expired. Please refresh your browser to continue.' |
|
76
|
|
|
); |
|
77
|
|
|
|
|
78
|
|
|
// Test request sans token |
|
79
|
|
|
$response = $this->get('spellcheck', Injector::inst()->create('Session', $session)); |
|
80
|
|
|
$this->assertEquals(400, $response->getStatusCode()); |
|
|
|
|
|
|
81
|
|
|
$jsonBody = json_decode($response->getBody()); |
|
82
|
|
|
$this->assertEquals($tokenError, $jsonBody->error->errstr); |
|
|
|
|
|
|
83
|
|
|
|
|
84
|
|
|
// Test request with correct token (will fail with an unrelated error) |
|
85
|
|
|
$response = $this->get( |
|
86
|
|
|
'spellcheck/?SecurityID='.urlencode($token), |
|
87
|
|
|
Injector::inst()->create('Session', $session) |
|
88
|
|
|
); |
|
89
|
|
|
$jsonBody = json_decode($response->getBody()); |
|
90
|
|
|
$this->assertNotEquals($tokenError, $jsonBody->error->errstr); |
|
|
|
|
|
|
91
|
|
|
|
|
92
|
|
|
// Test request with check disabled |
|
93
|
|
|
Config::inst()->update('SpellController', 'enable_security_token', false); |
|
94
|
|
|
$response = $this->get('spellcheck', Injector::inst()->create('Session', $session)); |
|
95
|
|
|
$jsonBody = json_decode($response->getBody()); |
|
96
|
|
|
$this->assertNotEquals($tokenError, $jsonBody->error->errstr); |
|
|
|
|
|
|
97
|
|
|
} |
|
98
|
|
|
|
|
99
|
|
|
/** |
|
100
|
|
|
* Tests permission check |
|
101
|
|
|
*/ |
|
102
|
|
|
public function testPermissions() { |
|
103
|
|
|
// Disable security ID for this test |
|
104
|
|
|
Config::inst()->update('SpellController', 'enable_security_token', false); |
|
105
|
|
|
$securityError = _t('SpellController.SecurityDenied', 'Permission Denied'); |
|
106
|
|
|
|
|
107
|
|
|
// Test admin permissions |
|
108
|
|
|
Config::inst()->update('SpellController', 'required_permission', 'ADMIN'); |
|
109
|
|
|
$this->logInWithPermission('ADMIN'); |
|
110
|
|
|
$response = $this->get('spellcheck'); |
|
111
|
|
|
$jsonBody = json_decode($response->getBody()); |
|
112
|
|
|
$this->assertNotEquals($securityError, $jsonBody->error->errstr); |
|
|
|
|
|
|
113
|
|
|
|
|
114
|
|
|
// Test insufficient permissions |
|
115
|
|
|
$this->logInWithPermission('CMS_ACCESS_CMSMain'); |
|
116
|
|
|
$response = $this->get('spellcheck'); |
|
117
|
|
|
$this->assertEquals(403, $response->getStatusCode()); |
|
|
|
|
|
|
118
|
|
|
$jsonBody = json_decode($response->getBody()); |
|
119
|
|
|
$this->assertEquals($securityError, $jsonBody->error->errstr); |
|
|
|
|
|
|
120
|
|
|
|
|
121
|
|
|
// Test disabled permissions |
|
122
|
|
|
Config::inst()->update('SpellController', 'required_permission', false); |
|
123
|
|
|
$response = $this->get('spellcheck'); |
|
124
|
|
|
$jsonBody = json_decode($response->getBody()); |
|
125
|
|
|
$this->assertNotEquals($securityError, $jsonBody->error->errstr); |
|
|
|
|
|
|
126
|
|
|
} |
|
127
|
|
|
|
|
128
|
|
|
/** |
|
129
|
|
|
* Ensure that invalid input is correctly rejected |
|
130
|
|
|
*/ |
|
131
|
|
|
public function testInputRejection() { |
|
132
|
|
|
// Disable security ID and permissions for this test |
|
133
|
|
|
Config::inst()->update('SpellController', 'enable_security_token', false); |
|
134
|
|
|
Config::inst()->update('SpellController', 'required_permission', false); |
|
135
|
|
|
$invalidRequest = _t('SpellController.InvalidRequest', 'Invalid request'); |
|
136
|
|
|
|
|
137
|
|
|
// Test checkWords acceptance |
|
138
|
|
|
$dataCheckWords = array( |
|
139
|
|
|
'id' => 'c0', |
|
140
|
|
|
'method' => 'checkWords', |
|
141
|
|
|
'params' => array( |
|
142
|
|
|
'en_NZ', |
|
143
|
|
|
array('collor', 'colour', 'color', 'onee', 'correct') |
|
144
|
|
|
) |
|
145
|
|
|
); |
|
146
|
|
|
$response = $this->post('spellcheck', array('ajax' => 1, 'json_data' => json_encode($dataCheckWords))); |
|
147
|
|
|
$this->assertEquals(200, $response->getStatusCode()); |
|
|
|
|
|
|
148
|
|
|
$jsonBody = json_decode($response->getBody()); |
|
149
|
|
|
$this->assertEquals('c0', $jsonBody->id); |
|
|
|
|
|
|
150
|
|
|
$this->assertEquals(array("collor", "color", "onee"), $jsonBody->result); |
|
|
|
|
|
|
151
|
|
|
|
|
152
|
|
|
// Test getSuggestions acceptance |
|
153
|
|
|
$dataGetSuggestions = array( |
|
154
|
|
|
'id' => '//c1//', // Should be reduced to only alphanumeric characters |
|
155
|
|
|
'method' => 'getSuggestions', |
|
156
|
|
|
'params' => array( |
|
157
|
|
|
'en_NZ', |
|
158
|
|
|
'collor' |
|
159
|
|
|
|
|
160
|
|
|
) |
|
161
|
|
|
); |
|
162
|
|
|
$response = $this->post('spellcheck', array('ajax' => 1, 'json_data' => json_encode($dataGetSuggestions))); |
|
163
|
|
|
$this->assertEquals(200, $response->getStatusCode()); |
|
|
|
|
|
|
164
|
|
|
$jsonBody = json_decode($response->getBody()); |
|
165
|
|
|
$this->assertEquals('c1', $jsonBody->id); |
|
|
|
|
|
|
166
|
|
|
$this->assertEquals(array('collar', 'colour'), $jsonBody->result); |
|
|
|
|
|
|
167
|
|
|
|
|
168
|
|
|
// Test non-ajax rejection |
|
169
|
|
|
$response = $this->post('spellcheck', array('json_data' => json_encode($dataCheckWords))); |
|
170
|
|
|
$this->assertEquals(400, $response->getStatusCode()); |
|
|
|
|
|
|
171
|
|
|
$jsonBody = json_decode($response->getBody()); |
|
172
|
|
|
$this->assertEquals($invalidRequest, $jsonBody->error->errstr); |
|
|
|
|
|
|
173
|
|
|
|
|
174
|
|
|
// Test incorrect method |
|
175
|
|
|
$dataInvalidMethod = $dataCheckWords; |
|
176
|
|
|
$dataInvalidMethod['method'] = 'validate'; |
|
177
|
|
|
$response = $this->post('spellcheck', array('ajax' => 1, 'json_data' => json_encode($dataInvalidMethod))); |
|
178
|
|
|
$this->assertEquals(400, $response->getStatusCode()); |
|
|
|
|
|
|
179
|
|
|
$jsonBody = json_decode($response->getBody()); |
|
180
|
|
|
$this->assertEquals( |
|
|
|
|
|
|
181
|
|
|
_t('SpellController.UnsupportedMethod', "Unsupported method '{method}'", array('method' => 'validate')), |
|
|
|
|
|
|
182
|
|
|
$jsonBody->error->errstr |
|
183
|
|
|
); |
|
184
|
|
|
|
|
185
|
|
|
// Test missing method |
|
186
|
|
|
$dataNoMethod = $dataCheckWords; |
|
187
|
|
|
unset($dataNoMethod['method']); |
|
188
|
|
|
$response = $this->post('spellcheck', array('ajax' => 1, 'json_data' => json_encode($dataNoMethod))); |
|
189
|
|
|
$this->assertEquals(400, $response->getStatusCode()); |
|
|
|
|
|
|
190
|
|
|
$jsonBody = json_decode($response->getBody()); |
|
191
|
|
|
$this->assertEquals($invalidRequest, $jsonBody->error->errstr); |
|
|
|
|
|
|
192
|
|
|
|
|
193
|
|
|
// Test unsupported locale |
|
194
|
|
|
$dataWrongLocale = $dataCheckWords; |
|
195
|
|
|
$dataWrongLocale['params'] = array( |
|
196
|
|
|
'de_DE', |
|
197
|
|
|
array('collor', 'colour', 'color', 'onee', 'correct') |
|
198
|
|
|
); |
|
199
|
|
|
$response = $this->post('spellcheck', array('ajax' => 1, 'json_data' => json_encode($dataWrongLocale))); |
|
200
|
|
|
$this->assertEquals(400, $response->getStatusCode()); |
|
|
|
|
|
|
201
|
|
|
$jsonBody = json_decode($response->getBody()); |
|
202
|
|
|
$this->assertEquals(_t('SpellController.InvalidLocale', 'Not supported locale'), $jsonBody->error->errstr); |
|
|
|
|
|
|
203
|
|
|
} |
|
204
|
|
|
} |
|
205
|
|
|
|
You can fix this by adding a namespace to your class:
When choosing a vendor namespace, try to pick something that is not too generic to avoid conflicts with other libraries.