Completed
Pull Request — master (#35)
by Robbie
01:20
created
tests/SpellControllerTest.php 1 patch
Indentation   +165 added lines, -165 removed lines patch added patch discarded remove patch
@@ -17,169 +17,169 @@
 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
-     * Ensure that invalid input is correctly rejected
123
-     */
124
-    public function testInputRejection()
125
-    {
126
-        // Disable security ID and permissions for this test
127
-        Config::modify()->set(SpellController::class, 'enable_security_token', false);
128
-        Config::modify()->set(SpellController::class, 'required_permission', false);
129
-        $invalidRequest = _t('SilverStripe\\SpellCheck\\Handling\\SpellController.InvalidRequest', 'Invalid request');
130
-
131
-        // Test spellcheck acceptance
132
-        $mockData = [
133
-            'method' => 'spellcheck',
134
-            'lang' => 'en_NZ',
135
-            'text' => 'Collor is everywhere',
136
-        ];
137
-        $response = $this->post('spellcheck', ['ajax' => true] + $mockData);
138
-        $this->assertEquals(200, $response->getStatusCode());
139
-        $jsonBody = json_decode($response->getBody());
140
-        $this->assertNotEmpty($jsonBody->words);
141
-        $this->assertNotEmpty($jsonBody->words->collor);
142
-        $this->assertEquals(['collar', 'colour'], $jsonBody->words->collor);
143
-
144
-        // Test non-ajax rejection
145
-        $response = $this->post('spellcheck', $mockData);
146
-        $this->assertEquals(400, $response->getStatusCode());
147
-        $jsonBody = json_decode($response->getBody());
148
-        $this->assertEquals($invalidRequest, $jsonBody->error);
149
-
150
-        // Test incorrect method
151
-        $dataInvalidMethod = $mockData;
152
-        $dataInvalidMethod['method'] = 'validate';
153
-        $response = $this->post('spellcheck', ['ajax' => true] + $dataInvalidMethod);
154
-        $this->assertEquals(400, $response->getStatusCode());
155
-        $jsonBody = json_decode($response->getBody());
156
-        $this->assertEquals(
157
-            _t(
158
-                'SilverStripe\\SpellCheck\\Handling\\.UnsupportedMethod',
159
-                "Unsupported method '{method}'",
160
-                array('method' => 'validate')
161
-            ),
162
-            $jsonBody->error
163
-        );
164
-
165
-        // Test missing method
166
-        $dataNoMethod = $mockData;
167
-        unset($dataNoMethod['method']);
168
-        $response = $this->post('spellcheck', ['ajax' => true] + $dataNoMethod);
169
-        $this->assertEquals(400, $response->getStatusCode());
170
-        $jsonBody = json_decode($response->getBody());
171
-        $this->assertEquals($invalidRequest, $jsonBody->error);
172
-
173
-        // Test unsupported locale
174
-        $dataWrongLocale = $mockData;
175
-        $dataWrongLocale['lang'] = 'de_DE';
176
-
177
-        $response = $this->post('spellcheck', ['ajax' => true] + $dataWrongLocale);
178
-        $this->assertEquals(400, $response->getStatusCode());
179
-        $jsonBody = json_decode($response->getBody());
180
-        $this->assertEquals(_t(
181
-            'SilverStripe\\SpellCheck\\Handling\\.InvalidLocale',
182
-            'Not supported locale'
183
-        ), $jsonBody->error);
184
-    }
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
+	 * Ensure that invalid input is correctly rejected
123
+	 */
124
+	public function testInputRejection()
125
+	{
126
+		// Disable security ID and permissions for this test
127
+		Config::modify()->set(SpellController::class, 'enable_security_token', false);
128
+		Config::modify()->set(SpellController::class, 'required_permission', false);
129
+		$invalidRequest = _t('SilverStripe\\SpellCheck\\Handling\\SpellController.InvalidRequest', 'Invalid request');
130
+
131
+		// Test spellcheck acceptance
132
+		$mockData = [
133
+			'method' => 'spellcheck',
134
+			'lang' => 'en_NZ',
135
+			'text' => 'Collor is everywhere',
136
+		];
137
+		$response = $this->post('spellcheck', ['ajax' => true] + $mockData);
138
+		$this->assertEquals(200, $response->getStatusCode());
139
+		$jsonBody = json_decode($response->getBody());
140
+		$this->assertNotEmpty($jsonBody->words);
141
+		$this->assertNotEmpty($jsonBody->words->collor);
142
+		$this->assertEquals(['collar', 'colour'], $jsonBody->words->collor);
143
+
144
+		// Test non-ajax rejection
145
+		$response = $this->post('spellcheck', $mockData);
146
+		$this->assertEquals(400, $response->getStatusCode());
147
+		$jsonBody = json_decode($response->getBody());
148
+		$this->assertEquals($invalidRequest, $jsonBody->error);
149
+
150
+		// Test incorrect method
151
+		$dataInvalidMethod = $mockData;
152
+		$dataInvalidMethod['method'] = 'validate';
153
+		$response = $this->post('spellcheck', ['ajax' => true] + $dataInvalidMethod);
154
+		$this->assertEquals(400, $response->getStatusCode());
155
+		$jsonBody = json_decode($response->getBody());
156
+		$this->assertEquals(
157
+			_t(
158
+				'SilverStripe\\SpellCheck\\Handling\\.UnsupportedMethod',
159
+				"Unsupported method '{method}'",
160
+				array('method' => 'validate')
161
+			),
162
+			$jsonBody->error
163
+		);
164
+
165
+		// Test missing method
166
+		$dataNoMethod = $mockData;
167
+		unset($dataNoMethod['method']);
168
+		$response = $this->post('spellcheck', ['ajax' => true] + $dataNoMethod);
169
+		$this->assertEquals(400, $response->getStatusCode());
170
+		$jsonBody = json_decode($response->getBody());
171
+		$this->assertEquals($invalidRequest, $jsonBody->error);
172
+
173
+		// Test unsupported locale
174
+		$dataWrongLocale = $mockData;
175
+		$dataWrongLocale['lang'] = 'de_DE';
176
+
177
+		$response = $this->post('spellcheck', ['ajax' => true] + $dataWrongLocale);
178
+		$this->assertEquals(400, $response->getStatusCode());
179
+		$jsonBody = json_decode($response->getBody());
180
+		$this->assertEquals(_t(
181
+			'SilverStripe\\SpellCheck\\Handling\\.InvalidLocale',
182
+			'Not supported locale'
183
+		), $jsonBody->error);
184
+	}
185 185
 }
Please login to merge, or discard this patch.