Completed
Push — master ( 09c22d...214c5a )
by Damian
08:47 queued 06:04
created

SpellControllerTest::setUpOnce()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 7
Code Lines 4

Duplication

Lines 0
Ratio 0 %
Metric Value
cc 2
eloc 4
nc 2
nop 0
dl 0
loc 7
rs 9.4285
1
<?php
2
3
/**
4
 * Tests the {@see SpellController} class
5
 */
6
class SpellControllerTest extends FunctionalTest {
0 ignored issues
show
Coding Style Compatibility introduced by
PSR1 recommends that each class must be in a namespace of at least one level to avoid collisions.

You can fix this by adding a namespace to your class:

namespace YourVendor;

class YourClass { }

When choosing a vendor namespace, try to pick something that is not too generic to avoid conflicts with other libraries.

Loading history...
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");
0 ignored issues
show
Bug introduced by
The method markTestSkipped() does not seem to exist on object<SpellControllerTest>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
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());
0 ignored issues
show
Bug introduced by
The method assertEquals() does not seem to exist on object<SpellControllerTest>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
81
		$jsonBody = json_decode($response->getBody());
82
		$this->assertEquals($tokenError, $jsonBody->error->errstr);
0 ignored issues
show
Bug introduced by
The method assertEquals() does not seem to exist on object<SpellControllerTest>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
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);
0 ignored issues
show
Bug introduced by
The method assertNotEquals() does not seem to exist on object<SpellControllerTest>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
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);
0 ignored issues
show
Bug introduced by
The method assertNotEquals() does not seem to exist on object<SpellControllerTest>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
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);
0 ignored issues
show
Bug introduced by
The method assertNotEquals() does not seem to exist on object<SpellControllerTest>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
113
114
		// Test insufficient permissions
115
		$this->logInWithPermission('CMS_ACCESS_CMSMain');
116
		$response = $this->get('spellcheck');
117
		$this->assertEquals(403, $response->getStatusCode());
0 ignored issues
show
Bug introduced by
The method assertEquals() does not seem to exist on object<SpellControllerTest>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
118
		$jsonBody = json_decode($response->getBody());
119
		$this->assertEquals($securityError, $jsonBody->error->errstr);
0 ignored issues
show
Bug introduced by
The method assertEquals() does not seem to exist on object<SpellControllerTest>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
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);
0 ignored issues
show
Bug introduced by
The method assertNotEquals() does not seem to exist on object<SpellControllerTest>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
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());
0 ignored issues
show
Bug introduced by
The method assertEquals() does not seem to exist on object<SpellControllerTest>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
148
		$jsonBody = json_decode($response->getBody());
149
		$this->assertEquals('c0', $jsonBody->id);
0 ignored issues
show
Bug introduced by
The method assertEquals() does not seem to exist on object<SpellControllerTest>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
150
		$this->assertEquals(array("collor", "color", "onee"), $jsonBody->result);
0 ignored issues
show
Bug introduced by
The method assertEquals() does not seem to exist on object<SpellControllerTest>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
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());
0 ignored issues
show
Bug introduced by
The method assertEquals() does not seem to exist on object<SpellControllerTest>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
164
		$jsonBody = json_decode($response->getBody());
165
		$this->assertEquals('c1', $jsonBody->id);
0 ignored issues
show
Bug introduced by
The method assertEquals() does not seem to exist on object<SpellControllerTest>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
166
		$this->assertEquals(array('collar', 'colour'), $jsonBody->result);
0 ignored issues
show
Bug introduced by
The method assertEquals() does not seem to exist on object<SpellControllerTest>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
167
168
		// Test non-ajax rejection
169
		$response = $this->post('spellcheck', array('json_data' => json_encode($dataCheckWords)));
170
		$this->assertEquals(400,  $response->getStatusCode());
0 ignored issues
show
Bug introduced by
The method assertEquals() does not seem to exist on object<SpellControllerTest>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
171
		$jsonBody = json_decode($response->getBody());
172
		$this->assertEquals($invalidRequest, $jsonBody->error->errstr);
0 ignored issues
show
Bug introduced by
The method assertEquals() does not seem to exist on object<SpellControllerTest>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
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());
0 ignored issues
show
Bug introduced by
The method assertEquals() does not seem to exist on object<SpellControllerTest>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
179
		$jsonBody = json_decode($response->getBody());
180
		$this->assertEquals(
0 ignored issues
show
Bug introduced by
The method assertEquals() does not seem to exist on object<SpellControllerTest>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
181
			_t('SpellController.UnsupportedMethod', "Unsupported method '{method}'", array('method' => 'validate')),
0 ignored issues
show
Documentation introduced by
array('method' => 'validate') is of type array<string,string,{"method":"string"}>, but the function expects a string.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
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());
0 ignored issues
show
Bug introduced by
The method assertEquals() does not seem to exist on object<SpellControllerTest>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
190
		$jsonBody = json_decode($response->getBody());
191
		$this->assertEquals($invalidRequest, $jsonBody->error->errstr);
0 ignored issues
show
Bug introduced by
The method assertEquals() does not seem to exist on object<SpellControllerTest>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
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());
0 ignored issues
show
Bug introduced by
The method assertEquals() does not seem to exist on object<SpellControllerTest>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
201
		$jsonBody = json_decode($response->getBody());
202
		$this->assertEquals(_t('SpellController.InvalidLocale', 'Not supported locale'), $jsonBody->error->errstr);
0 ignored issues
show
Bug introduced by
The method assertEquals() does not seem to exist on object<SpellControllerTest>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
203
	}
204
}
205