Completed
Pull Request — master (#35)
by Robbie
01:25
created
tests/SpellControllerTest.php 1 patch
Indentation   +215 added lines, -215 removed lines patch added patch discarded remove patch
@@ -17,219 +17,219 @@
 block discarded – undo
17 17
  */
18 18
 class SpellControllerTest extends FunctionalTest
19 19
 {
20
-    protected $usesDatabase = true;
21
-
22
-    protected $securityWasEnabled = false;
23
-
24
-    protected function setUp()
25
-    {
26
-        parent::setUp();
27
-
28
-        $this->securityWasEnabled = SecurityToken::is_enabled();
29
-
30
-        // Reset config
31
-        Config::modify()->set(SpellController::class, 'required_permission', 'CMS_ACCESS_CMSMain');
32
-        Config::inst()->remove(SpellController::class, 'locales');
33
-        Config::modify()->set(SpellController::class, 'locales', array('en_US', 'en_NZ', 'fr_FR'));
34
-        Config::modify()->set(SpellController::class, 'enable_security_token', true);
35
-        SecurityToken::enable();
36
-
37
-        // Setup mock for testing provider
38
-        $spellChecker = new SpellProviderStub;
39
-        Injector::inst()->registerService($spellChecker, SpellProvider::class);
40
-    }
41
-
42
-    protected function tearDown()
43
-    {
44
-        if ($this->securityWasEnabled) {
45
-            SecurityToken::enable();
46
-        } else {
47
-            SecurityToken::disable();
48
-        }
49
-
50
-        parent::tearDown();
51
-    }
52
-
53
-    /**
54
-     * Tests security ID check
55
-     */
56
-    public function testSecurityID()
57
-    {
58
-        // Mock token
59
-        $securityToken = SecurityToken::inst();
60
-        $generator = new RandomGenerator();
61
-        $token = $generator->randomToken('sha1');
62
-        $session = array(
63
-            $securityToken->getName() => $token
64
-        );
65
-        $tokenError = _t(
66
-            'SilverStripe\\SpellCheck\\Handling\\SpellController.SecurityMissing',
67
-            'Your session has expired. Please refresh your browser to continue.'
68
-        );
69
-
70
-        // Test request sans token
71
-        $response = $this->get('spellcheck', Injector::inst()->create(Session::class, $session));
72
-        $this->assertEquals(400, $response->getStatusCode());
73
-        $jsonBody = json_decode($response->getBody());
74
-        $this->assertEquals($tokenError, $jsonBody->error);
75
-
76
-        // Test request with correct token (will fail with an unrelated error)
77
-        $response = $this->get(
78
-            'spellcheck/?SecurityID='.urlencode($token),
79
-            Injector::inst()->create(Session::class, $session)
80
-        );
81
-        $jsonBody = json_decode($response->getBody());
82
-        $this->assertNotEquals($tokenError, $jsonBody->error);
83
-
84
-        // Test request with check disabled
85
-        Config::modify()->set(SpellController::class, 'enable_security_token', false);
86
-        $response = $this->get('spellcheck', Injector::inst()->create(Session::class, $session));
87
-        $jsonBody = json_decode($response->getBody());
88
-        $this->assertNotEquals($tokenError, $jsonBody->error);
89
-    }
90
-
91
-    /**
92
-     * Tests permission check
93
-     */
94
-    public function testPermissions()
95
-    {
96
-        // Disable security ID for this test
97
-        Config::modify()->set(SpellController::class, 'enable_security_token', false);
98
-        $securityError = _t('SilverStripe\\SpellCheck\\Handling\\SpellController.SecurityDenied', 'Permission Denied');
99
-
100
-        // Test admin permissions
101
-        Config::modify()->set(SpellController::class, 'required_permission', 'ADMIN');
102
-        $this->logInWithPermission('ADMIN');
103
-        $response = $this->get('spellcheck');
104
-        $jsonBody = json_decode($response->getBody());
105
-        $this->assertNotEquals($securityError, $jsonBody->error);
106
-
107
-        // Test insufficient permissions
108
-        $this->logInWithPermission('CMS_ACCESS_CMSMain');
109
-        $response = $this->get('spellcheck');
110
-        $this->assertEquals(403, $response->getStatusCode());
111
-        $jsonBody = json_decode($response->getBody());
112
-        $this->assertEquals($securityError, $jsonBody->error);
113
-
114
-        // Test disabled permissions
115
-        Config::modify()->set(SpellController::class, 'required_permission', false);
116
-        $response = $this->get('spellcheck');
117
-        $jsonBody = json_decode($response->getBody());
118
-        $this->assertNotEquals($securityError, $jsonBody->error);
119
-    }
120
-
121
-    /**
122
-     * @param string $lang
123
-     * @param int $expectedStatusCode
124
-     * @dataProvider langProvider
125
-     */
126
-    public function testBothLangAndLocaleInputResolveToLocale($lang, $expectedStatusCode)
127
-    {
128
-        $this->logInWithPermission('ADMIN');
129
-        Config::modify()->set(SpellController::class, 'enable_security_token', false);
130
-
131
-        // Test with a language (assumes en_US is the default "en" language locale)
132
-        $mockData = [
133
-            'ajax' => true,
134
-            'method' => 'spellcheck',
135
-            'lang' => $lang,
136
-            'text' => 'Collor is everywhere',
137
-        ];
138
-        $response = $this->post('spellcheck', $mockData);
139
-        $this->assertEquals($expectedStatusCode, $response->getStatusCode());
140
-    }
141
-
142
-    /**
143
-     * @return array[]
144
-     */
145
-    public function langProvider()
146
-    {
147
-        return [
148
-            'english_language' => [
149
-                'en', // assumes en_US is the default locale for "en" language
150
-                200,
151
-            ],
152
-            'english_locale' => [
153
-                'en_NZ',
154
-                200,
155
-            ],
156
-            'invalid_language' => [
157
-                'ru',
158
-                400,
159
-            ],
160
-            'other_valid_language' => [
161
-                'fr', // assumes fr_FR is the default locale for "en" language
162
-                200,
163
-            ],
164
-            'other_valid_locale' => [
165
-                'fr_FR',
166
-                200,
167
-            ],
168
-        ];
169
-    }
170
-
171
-    /**
172
-     * Ensure that invalid input is correctly rejected
173
-     */
174
-    public function testInputRejection()
175
-    {
176
-        // Disable security ID and permissions for this test
177
-        Config::modify()->set(SpellController::class, 'enable_security_token', false);
178
-        Config::modify()->set(SpellController::class, 'required_permission', false);
179
-        $invalidRequest = _t('SilverStripe\\SpellCheck\\Handling\\SpellController.InvalidRequest', 'Invalid request');
180
-
181
-        // Test spellcheck acceptance
182
-        $mockData = [
183
-            'method' => 'spellcheck',
184
-            'lang' => 'en_NZ',
185
-            'text' => 'Collor is everywhere',
186
-        ];
187
-        $response = $this->post('spellcheck', ['ajax' => true] + $mockData);
188
-        $this->assertEquals(200, $response->getStatusCode());
189
-        $jsonBody = json_decode($response->getBody());
190
-        $this->assertNotEmpty($jsonBody->words);
191
-        $this->assertNotEmpty($jsonBody->words->collor);
192
-        $this->assertEquals(['collar', 'colour'], $jsonBody->words->collor);
193
-
194
-        // Test non-ajax rejection
195
-        $response = $this->post('spellcheck', $mockData);
196
-        $this->assertEquals(400, $response->getStatusCode());
197
-        $jsonBody = json_decode($response->getBody());
198
-        $this->assertEquals($invalidRequest, $jsonBody->error);
199
-
200
-        // Test incorrect method
201
-        $dataInvalidMethod = $mockData;
202
-        $dataInvalidMethod['method'] = 'validate';
203
-        $response = $this->post('spellcheck', ['ajax' => true] + $dataInvalidMethod);
204
-        $this->assertEquals(400, $response->getStatusCode());
205
-        $jsonBody = json_decode($response->getBody());
206
-        $this->assertEquals(
207
-            _t(
208
-                'SilverStripe\\SpellCheck\\Handling\\.UnsupportedMethod',
209
-                "Unsupported method '{method}'",
210
-                array('method' => 'validate')
211
-            ),
212
-            $jsonBody->error
213
-        );
214
-
215
-        // Test missing method
216
-        $dataNoMethod = $mockData;
217
-        unset($dataNoMethod['method']);
218
-        $response = $this->post('spellcheck', ['ajax' => true] + $dataNoMethod);
219
-        $this->assertEquals(400, $response->getStatusCode());
220
-        $jsonBody = json_decode($response->getBody());
221
-        $this->assertEquals($invalidRequest, $jsonBody->error);
222
-
223
-        // Test unsupported locale
224
-        $dataWrongLocale = $mockData;
225
-        $dataWrongLocale['lang'] = 'de_DE';
226
-
227
-        $response = $this->post('spellcheck', ['ajax' => true] + $dataWrongLocale);
228
-        $this->assertEquals(400, $response->getStatusCode());
229
-        $jsonBody = json_decode($response->getBody());
230
-        $this->assertEquals(_t(
231
-            'SilverStripe\\SpellCheck\\Handling\\.InvalidLocale',
232
-            'Not supported locale'
233
-        ), $jsonBody->error);
234
-    }
20
+	protected $usesDatabase = true;
21
+
22
+	protected $securityWasEnabled = false;
23
+
24
+	protected function setUp()
25
+	{
26
+		parent::setUp();
27
+
28
+		$this->securityWasEnabled = SecurityToken::is_enabled();
29
+
30
+		// Reset config
31
+		Config::modify()->set(SpellController::class, 'required_permission', 'CMS_ACCESS_CMSMain');
32
+		Config::inst()->remove(SpellController::class, 'locales');
33
+		Config::modify()->set(SpellController::class, 'locales', array('en_US', 'en_NZ', 'fr_FR'));
34
+		Config::modify()->set(SpellController::class, 'enable_security_token', true);
35
+		SecurityToken::enable();
36
+
37
+		// Setup mock for testing provider
38
+		$spellChecker = new SpellProviderStub;
39
+		Injector::inst()->registerService($spellChecker, SpellProvider::class);
40
+	}
41
+
42
+	protected function tearDown()
43
+	{
44
+		if ($this->securityWasEnabled) {
45
+			SecurityToken::enable();
46
+		} else {
47
+			SecurityToken::disable();
48
+		}
49
+
50
+		parent::tearDown();
51
+	}
52
+
53
+	/**
54
+	 * Tests security ID check
55
+	 */
56
+	public function testSecurityID()
57
+	{
58
+		// Mock token
59
+		$securityToken = SecurityToken::inst();
60
+		$generator = new RandomGenerator();
61
+		$token = $generator->randomToken('sha1');
62
+		$session = array(
63
+			$securityToken->getName() => $token
64
+		);
65
+		$tokenError = _t(
66
+			'SilverStripe\\SpellCheck\\Handling\\SpellController.SecurityMissing',
67
+			'Your session has expired. Please refresh your browser to continue.'
68
+		);
69
+
70
+		// Test request sans token
71
+		$response = $this->get('spellcheck', Injector::inst()->create(Session::class, $session));
72
+		$this->assertEquals(400, $response->getStatusCode());
73
+		$jsonBody = json_decode($response->getBody());
74
+		$this->assertEquals($tokenError, $jsonBody->error);
75
+
76
+		// Test request with correct token (will fail with an unrelated error)
77
+		$response = $this->get(
78
+			'spellcheck/?SecurityID='.urlencode($token),
79
+			Injector::inst()->create(Session::class, $session)
80
+		);
81
+		$jsonBody = json_decode($response->getBody());
82
+		$this->assertNotEquals($tokenError, $jsonBody->error);
83
+
84
+		// Test request with check disabled
85
+		Config::modify()->set(SpellController::class, 'enable_security_token', false);
86
+		$response = $this->get('spellcheck', Injector::inst()->create(Session::class, $session));
87
+		$jsonBody = json_decode($response->getBody());
88
+		$this->assertNotEquals($tokenError, $jsonBody->error);
89
+	}
90
+
91
+	/**
92
+	 * Tests permission check
93
+	 */
94
+	public function testPermissions()
95
+	{
96
+		// Disable security ID for this test
97
+		Config::modify()->set(SpellController::class, 'enable_security_token', false);
98
+		$securityError = _t('SilverStripe\\SpellCheck\\Handling\\SpellController.SecurityDenied', 'Permission Denied');
99
+
100
+		// Test admin permissions
101
+		Config::modify()->set(SpellController::class, 'required_permission', 'ADMIN');
102
+		$this->logInWithPermission('ADMIN');
103
+		$response = $this->get('spellcheck');
104
+		$jsonBody = json_decode($response->getBody());
105
+		$this->assertNotEquals($securityError, $jsonBody->error);
106
+
107
+		// Test insufficient permissions
108
+		$this->logInWithPermission('CMS_ACCESS_CMSMain');
109
+		$response = $this->get('spellcheck');
110
+		$this->assertEquals(403, $response->getStatusCode());
111
+		$jsonBody = json_decode($response->getBody());
112
+		$this->assertEquals($securityError, $jsonBody->error);
113
+
114
+		// Test disabled permissions
115
+		Config::modify()->set(SpellController::class, 'required_permission', false);
116
+		$response = $this->get('spellcheck');
117
+		$jsonBody = json_decode($response->getBody());
118
+		$this->assertNotEquals($securityError, $jsonBody->error);
119
+	}
120
+
121
+	/**
122
+	 * @param string $lang
123
+	 * @param int $expectedStatusCode
124
+	 * @dataProvider langProvider
125
+	 */
126
+	public function testBothLangAndLocaleInputResolveToLocale($lang, $expectedStatusCode)
127
+	{
128
+		$this->logInWithPermission('ADMIN');
129
+		Config::modify()->set(SpellController::class, 'enable_security_token', false);
130
+
131
+		// Test with a language (assumes en_US is the default "en" language locale)
132
+		$mockData = [
133
+			'ajax' => true,
134
+			'method' => 'spellcheck',
135
+			'lang' => $lang,
136
+			'text' => 'Collor is everywhere',
137
+		];
138
+		$response = $this->post('spellcheck', $mockData);
139
+		$this->assertEquals($expectedStatusCode, $response->getStatusCode());
140
+	}
141
+
142
+	/**
143
+	 * @return array[]
144
+	 */
145
+	public function langProvider()
146
+	{
147
+		return [
148
+			'english_language' => [
149
+				'en', // assumes en_US is the default locale for "en" language
150
+				200,
151
+			],
152
+			'english_locale' => [
153
+				'en_NZ',
154
+				200,
155
+			],
156
+			'invalid_language' => [
157
+				'ru',
158
+				400,
159
+			],
160
+			'other_valid_language' => [
161
+				'fr', // assumes fr_FR is the default locale for "en" language
162
+				200,
163
+			],
164
+			'other_valid_locale' => [
165
+				'fr_FR',
166
+				200,
167
+			],
168
+		];
169
+	}
170
+
171
+	/**
172
+	 * Ensure that invalid input is correctly rejected
173
+	 */
174
+	public function testInputRejection()
175
+	{
176
+		// Disable security ID and permissions for this test
177
+		Config::modify()->set(SpellController::class, 'enable_security_token', false);
178
+		Config::modify()->set(SpellController::class, 'required_permission', false);
179
+		$invalidRequest = _t('SilverStripe\\SpellCheck\\Handling\\SpellController.InvalidRequest', 'Invalid request');
180
+
181
+		// Test spellcheck acceptance
182
+		$mockData = [
183
+			'method' => 'spellcheck',
184
+			'lang' => 'en_NZ',
185
+			'text' => 'Collor is everywhere',
186
+		];
187
+		$response = $this->post('spellcheck', ['ajax' => true] + $mockData);
188
+		$this->assertEquals(200, $response->getStatusCode());
189
+		$jsonBody = json_decode($response->getBody());
190
+		$this->assertNotEmpty($jsonBody->words);
191
+		$this->assertNotEmpty($jsonBody->words->collor);
192
+		$this->assertEquals(['collar', 'colour'], $jsonBody->words->collor);
193
+
194
+		// Test non-ajax rejection
195
+		$response = $this->post('spellcheck', $mockData);
196
+		$this->assertEquals(400, $response->getStatusCode());
197
+		$jsonBody = json_decode($response->getBody());
198
+		$this->assertEquals($invalidRequest, $jsonBody->error);
199
+
200
+		// Test incorrect method
201
+		$dataInvalidMethod = $mockData;
202
+		$dataInvalidMethod['method'] = 'validate';
203
+		$response = $this->post('spellcheck', ['ajax' => true] + $dataInvalidMethod);
204
+		$this->assertEquals(400, $response->getStatusCode());
205
+		$jsonBody = json_decode($response->getBody());
206
+		$this->assertEquals(
207
+			_t(
208
+				'SilverStripe\\SpellCheck\\Handling\\.UnsupportedMethod',
209
+				"Unsupported method '{method}'",
210
+				array('method' => 'validate')
211
+			),
212
+			$jsonBody->error
213
+		);
214
+
215
+		// Test missing method
216
+		$dataNoMethod = $mockData;
217
+		unset($dataNoMethod['method']);
218
+		$response = $this->post('spellcheck', ['ajax' => true] + $dataNoMethod);
219
+		$this->assertEquals(400, $response->getStatusCode());
220
+		$jsonBody = json_decode($response->getBody());
221
+		$this->assertEquals($invalidRequest, $jsonBody->error);
222
+
223
+		// Test unsupported locale
224
+		$dataWrongLocale = $mockData;
225
+		$dataWrongLocale['lang'] = 'de_DE';
226
+
227
+		$response = $this->post('spellcheck', ['ajax' => true] + $dataWrongLocale);
228
+		$this->assertEquals(400, $response->getStatusCode());
229
+		$jsonBody = json_decode($response->getBody());
230
+		$this->assertEquals(_t(
231
+			'SilverStripe\\SpellCheck\\Handling\\.InvalidLocale',
232
+			'Not supported locale'
233
+		), $jsonBody->error);
234
+	}
235 235
 }
Please login to merge, or discard this patch.