(bug 42915) make MovePage aware of whether redirects are supported.
[lhc/web/wiklou.git] / tests / phpunit / includes / api / ApiOptionsTest.php
1 <?php
2
3 /**
4 * @group API
5 * @group Database
6 */
7 class ApiOptionsTest extends MediaWikiLangTestCase {
8
9 private $mTested, $mUserMock, $mContext, $mSession;
10
11 private $mOldGetPreferencesHooks = false;
12
13 private static $Success = array( 'options' => 'success' );
14
15 protected function setUp() {
16 parent::setUp();
17
18 $this->mUserMock = $this->getMockBuilder( 'User' )
19 ->disableOriginalConstructor()
20 ->getMock();
21
22 // Set up groups
23 $this->mUserMock->expects( $this->any() )
24 ->method( 'getEffectiveGroups' )->will( $this->returnValue( array( '*', 'user')) );
25
26 // Create a new context
27 $this->mContext = new DerivativeContext( new RequestContext() );
28 $this->mContext->getContext()->setTitle( Title::newFromText( 'Test' ) );
29 $this->mContext->setUser( $this->mUserMock );
30
31 $main = new ApiMain( $this->mContext );
32
33 // Empty session
34 $this->mSession = array();
35
36 $this->mTested = new ApiOptions( $main, 'options' );
37
38 global $wgHooks;
39 if ( !isset( $wgHooks['GetPreferences'] ) ) {
40 $wgHooks['GetPreferences'] = array();
41 }
42 $this->mOldGetPreferencesHooks = $wgHooks['GetPreferences'];
43 $wgHooks['GetPreferences'][] = array( $this, 'hookGetPreferences' );
44 }
45
46 protected function tearDown() {
47 global $wgHooks;
48
49 if ( $this->mOldGetPreferencesHooks !== false ) {
50 $wgHooks['GetPreferences'] = $this->mOldGetPreferencesHooks;
51 $this->mOldGetPreferencesHooks = false;
52 }
53
54 parent::tearDown();
55 }
56
57 public function hookGetPreferences( $user, &$preferences ) {
58 foreach ( array( 'name', 'willBeNull', 'willBeEmpty', 'willBeHappy' ) as $k ) {
59 $preferences[$k] = array(
60 'type' => 'text',
61 'section' => 'test',
62 'label' => '&#160;',
63 );
64 }
65
66 return true;
67 }
68
69 private function getSampleRequest( $custom = array() ) {
70 $request = array(
71 'token' => '123ABC',
72 'change' => null,
73 'optionname' => null,
74 'optionvalue' => null,
75 );
76 return array_merge( $request, $custom );
77 }
78
79 private function executeQuery( $request ) {
80 $this->mContext->setRequest( new FauxRequest( $request, true, $this->mSession ) );
81 $this->mTested->execute();
82 return $this->mTested->getResult()->getData();
83 }
84
85 /**
86 * @expectedException UsageException
87 */
88 public function testNoToken() {
89 $request = $this->getSampleRequest( array( 'token' => null ) );
90
91 $this->executeQuery( $request );
92 }
93
94 public function testAnon() {
95 $this->mUserMock->expects( $this->once() )
96 ->method( 'isAnon' )
97 ->will( $this->returnValue( true ) );
98
99 try {
100 $request = $this->getSampleRequest();
101
102 $this->executeQuery( $request );
103 } catch ( UsageException $e ) {
104 $this->assertEquals( 'notloggedin', $e->getCodeString() );
105 $this->assertEquals( 'Anonymous users cannot change preferences', $e->getMessage() );
106 return;
107 }
108 $this->fail( "UsageException was not thrown" );
109 }
110
111 public function testNoOptionname() {
112 try {
113 $request = $this->getSampleRequest( array( 'optionvalue' => '1' ) );
114
115 $this->executeQuery( $request );
116 } catch ( UsageException $e ) {
117 $this->assertEquals( 'nooptionname', $e->getCodeString() );
118 $this->assertEquals( 'The optionname parameter must be set', $e->getMessage() );
119 return;
120 }
121 $this->fail( "UsageException was not thrown" );
122 }
123
124 public function testNoChanges() {
125 $this->mUserMock->expects( $this->never() )
126 ->method( 'resetOptions' );
127
128 $this->mUserMock->expects( $this->never() )
129 ->method( 'setOption' );
130
131 $this->mUserMock->expects( $this->never() )
132 ->method( 'saveSettings' );
133
134 try {
135 $request = $this->getSampleRequest();
136
137 $this->executeQuery( $request );
138 } catch ( UsageException $e ) {
139 $this->assertEquals( 'nochanges', $e->getCodeString() );
140 $this->assertEquals( 'No changes were requested', $e->getMessage() );
141 return;
142 }
143 $this->fail( "UsageException was not thrown" );
144 }
145
146 public function testReset() {
147 $this->mUserMock->expects( $this->once() )
148 ->method( 'resetOptions' );
149
150 $this->mUserMock->expects( $this->never() )
151 ->method( 'setOption' );
152
153 $this->mUserMock->expects( $this->once() )
154 ->method( 'saveSettings' );
155
156 $request = $this->getSampleRequest( array( 'reset' => '' ) );
157
158 $response = $this->executeQuery( $request );
159
160 $this->assertEquals( self::$Success, $response );
161 }
162
163 public function testOptionWithValue() {
164 $this->mUserMock->expects( $this->never() )
165 ->method( 'resetOptions' );
166
167 $this->mUserMock->expects( $this->once() )
168 ->method( 'setOption' )
169 ->with( $this->equalTo( 'name' ), $this->equalTo( 'value' ) );
170
171 $this->mUserMock->expects( $this->once() )
172 ->method( 'saveSettings' );
173
174 $request = $this->getSampleRequest( array( 'optionname' => 'name', 'optionvalue' => 'value' ) );
175
176 $response = $this->executeQuery( $request );
177
178 $this->assertEquals( self::$Success, $response );
179 }
180
181 public function testOptionResetValue() {
182 $this->mUserMock->expects( $this->never() )
183 ->method( 'resetOptions' );
184
185 $this->mUserMock->expects( $this->once() )
186 ->method( 'setOption' )
187 ->with( $this->equalTo( 'name' ), $this->equalTo( null ) );
188
189 $this->mUserMock->expects( $this->once() )
190 ->method( 'saveSettings' );
191
192 $request = $this->getSampleRequest( array( 'optionname' => 'name' ) );
193 $response = $this->executeQuery( $request );
194
195 $this->assertEquals( self::$Success, $response );
196 }
197
198 public function testChange() {
199 $this->mUserMock->expects( $this->never() )
200 ->method( 'resetOptions' );
201
202 $this->mUserMock->expects( $this->at( 1 ) )
203 ->method( 'getOptions' );
204
205 $this->mUserMock->expects( $this->at( 2 ) )
206 ->method( 'setOption' )
207 ->with( $this->equalTo( 'willBeNull' ), $this->equalTo( null ) );
208
209 $this->mUserMock->expects( $this->at( 3 ) )
210 ->method( 'getOptions' );
211
212 $this->mUserMock->expects( $this->at( 4 ) )
213 ->method( 'setOption' )
214 ->with( $this->equalTo( 'willBeEmpty' ), $this->equalTo( '' ) );
215
216 $this->mUserMock->expects( $this->at( 5 ) )
217 ->method( 'getOptions' );
218
219 $this->mUserMock->expects( $this->at( 6 ) )
220 ->method( 'setOption' )
221 ->with( $this->equalTo( 'willBeHappy' ), $this->equalTo( 'Happy' ) );
222
223 $this->mUserMock->expects( $this->once() )
224 ->method( 'saveSettings' );
225
226 $request = $this->getSampleRequest( array( 'change' => 'willBeNull|willBeEmpty=|willBeHappy=Happy' ) );
227
228 $response = $this->executeQuery( $request );
229
230 $this->assertEquals( self::$Success, $response );
231 }
232
233 public function testResetChangeOption() {
234 $this->mUserMock->expects( $this->once() )
235 ->method( 'resetOptions' );
236
237 $this->mUserMock->expects( $this->at( 2 ) )
238 ->method( 'getOptions' );
239
240 $this->mUserMock->expects( $this->at( 3 ) )
241 ->method( 'setOption' )
242 ->with( $this->equalTo( 'willBeHappy' ), $this->equalTo( 'Happy' ) );
243
244 $this->mUserMock->expects( $this->at( 4 ) )
245 ->method( 'getOptions' );
246
247 $this->mUserMock->expects( $this->at( 5 ) )
248 ->method( 'setOption' )
249 ->with( $this->equalTo( 'name' ), $this->equalTo( 'value' ) );
250
251 $this->mUserMock->expects( $this->once() )
252 ->method( 'saveSettings' );
253
254 $args = array(
255 'reset' => '',
256 'change' => 'willBeHappy=Happy',
257 'optionname' => 'name',
258 'optionvalue' => 'value'
259 );
260
261 $response = $this->executeQuery( $this->getSampleRequest( $args ) );
262
263 $this->assertEquals( self::$Success, $response );
264 }
265 }