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