Merge "Optimized SqlBagOStuff::getBatch() a bit."
[lhc/web/wiklou.git] / tests / phpunit / includes / api / ApiOptionsTest.php
1 <?php
2
3 /**
4 * @group API
5 */
6 class ApiOptionsTest extends MediaWikiLangTestCase {
7
8 private $mTested, $mApiMainMock, $mUserMock, $mContext, $mSession;
9
10 private static $Success = array( 'options' => 'success' );
11
12 function setUp() {
13 parent::setUp();
14
15 $this->mUserMock = $this->getMockBuilder( 'User' )
16 ->disableOriginalConstructor()
17 ->getMock();
18
19 $this->mApiMainMock = $this->getMockBuilder( 'ApiBase' )
20 ->disableOriginalConstructor()
21 ->getMock();
22
23 // Create a new context
24 $this->mContext = new DerivativeContext( new RequestContext() );
25 $this->mContext->setUser( $this->mUserMock );
26
27 $this->mApiMainMock->expects( $this->any() )
28 ->method( 'getContext' )
29 ->will( $this->returnValue( $this->mContext ) );
30
31 $this->mApiMainMock->expects( $this->any() )
32 ->method( 'getResult' )
33 ->will( $this->returnValue( new ApiResult( $this->mApiMainMock ) ) );
34
35
36 // Empty session
37 $this->mSession = array();
38
39 $this->mTested = new ApiOptions( $this->mApiMainMock, 'options' );
40 }
41
42 private function getSampleRequest( $custom = array() ) {
43 $request = array(
44 'token' => '123ABC',
45 'change' => null,
46 'optionname' => null,
47 'optionvalue' => null,
48 );
49 return array_merge( $request, $custom );
50 }
51
52 private function executeQuery( $request ) {
53 $this->mContext->setRequest( new FauxRequest( $request, true, $this->mSession ) );
54 $this->mTested->execute();
55 return $this->mTested->getResult()->getData();
56 }
57
58 /**
59 * @expectedException UsageException
60 */
61 public function testNoToken() {
62 $request = $this->getSampleRequest( array( 'token' => null ) );
63
64 $this->executeQuery( $request );
65 }
66
67 public function testAnon() {
68 $this->mUserMock->expects( $this->once() )
69 ->method( 'isAnon' )
70 ->will( $this->returnValue( true ) );
71
72 try {
73 $request = $this->getSampleRequest();
74
75 $this->executeQuery( $request );
76 } catch ( UsageException $e ) {
77 $this->assertEquals( 'notloggedin', $e->getCodeString() );
78 $this->assertEquals( 'Anonymous users cannot change preferences', $e->getMessage() );
79 return;
80 }
81 $this->fail( "UsageException was not thrown" );
82 }
83
84 public function testNoOptionname() {
85 try {
86 $request = $this->getSampleRequest( array( 'optionvalue' => '1' ) );
87
88 $this->executeQuery( $request );
89 } catch ( UsageException $e ) {
90 $this->assertEquals( 'nooptionname', $e->getCodeString() );
91 $this->assertEquals( 'The optionname parameter must be set', $e->getMessage() );
92 return;
93 }
94 $this->fail( "UsageException was not thrown" );
95 }
96
97 public function testNoChanges() {
98 $this->mUserMock->expects( $this->never() )
99 ->method( 'resetOptions' );
100
101 $this->mUserMock->expects( $this->never() )
102 ->method( 'setOption' );
103
104 $this->mUserMock->expects( $this->never() )
105 ->method( 'saveSettings' );
106
107 try {
108 $request = $this->getSampleRequest();
109
110 $this->executeQuery( $request );
111 } catch ( UsageException $e ) {
112 $this->assertEquals( 'nochanges', $e->getCodeString() );
113 $this->assertEquals( 'No changes were requested', $e->getMessage() );
114 return;
115 }
116 $this->fail( "UsageException was not thrown" );
117 }
118
119 public function testReset() {
120 $this->mUserMock->expects( $this->once() )
121 ->method( 'resetOptions' );
122
123 $this->mUserMock->expects( $this->never() )
124 ->method( 'setOption' );
125
126 $this->mUserMock->expects( $this->once() )
127 ->method( 'saveSettings' );
128
129 $request = $this->getSampleRequest( array( 'reset' => '' ) );
130
131 $response = $this->executeQuery( $request );
132
133 $this->assertEquals( self::$Success, $response );
134 }
135
136 public function testOptionWithValue() {
137 $this->mUserMock->expects( $this->never() )
138 ->method( 'resetOptions' );
139
140 $this->mUserMock->expects( $this->once() )
141 ->method( 'setOption' )
142 ->with( $this->equalTo( 'name' ), $this->equalTo( 'value' ) );
143
144 $this->mUserMock->expects( $this->once() )
145 ->method( 'saveSettings' );
146
147 $request = $this->getSampleRequest( array( 'optionname' => 'name', 'optionvalue' => 'value' ) );
148
149 $response = $this->executeQuery( $request );
150
151 $this->assertEquals( self::$Success, $response );
152 }
153
154 public function testOptionResetValue() {
155 $this->mUserMock->expects( $this->never() )
156 ->method( 'resetOptions' );
157
158 $this->mUserMock->expects( $this->once() )
159 ->method( 'setOption' )
160 ->with( $this->equalTo( 'name' ), $this->equalTo( null ) );
161
162 $this->mUserMock->expects( $this->once() )
163 ->method( 'saveSettings' );
164
165 $request = $this->getSampleRequest( array( 'optionname' => 'name' ) );
166 $response = $this->executeQuery( $request );
167
168 $this->assertEquals( self::$Success, $response );
169 }
170
171 public function testChange() {
172 $this->mUserMock->expects( $this->never() )
173 ->method( 'resetOptions' );
174
175 $this->mUserMock->expects( $this->at( 1 ) )
176 ->method( 'setOption' )
177 ->with( $this->equalTo( 'willBeNull' ), $this->equalTo( null ) );
178
179 $this->mUserMock->expects( $this->at( 2 ) )
180 ->method( 'setOption' )
181 ->with( $this->equalTo( 'willBeEmpty' ), $this->equalTo( '' ) );
182
183 $this->mUserMock->expects( $this->at( 3 ) )
184 ->method( 'setOption' )
185 ->with( $this->equalTo( 'willBeHappy' ), $this->equalTo( 'Happy' ) );
186
187 $this->mUserMock->expects( $this->once() )
188 ->method( 'saveSettings' );
189
190 $request = $this->getSampleRequest( array( 'change' => 'willBeNull|willBeEmpty=|willBeHappy=Happy' ) );
191
192 $response = $this->executeQuery( $request );
193
194 $this->assertEquals( self::$Success, $response );
195 }
196
197 public function testResetChangeOption() {
198 $this->mUserMock->expects( $this->once() )
199 ->method( 'resetOptions' );
200
201 $this->mUserMock->expects( $this->at( 2 ) )
202 ->method( 'setOption' )
203 ->with( $this->equalTo( 'willBeHappy' ), $this->equalTo( 'Happy' ) );
204
205 $this->mUserMock->expects( $this->at( 3 ) )
206 ->method( 'setOption' )
207 ->with( $this->equalTo( 'name' ), $this->equalTo( 'value' ) );
208
209 $this->mUserMock->expects( $this->once() )
210 ->method( 'saveSettings' );
211
212 $args = array(
213 'reset' => '',
214 'change' => 'willBeHappy=Happy',
215 'optionname' => 'name',
216 'optionvalue' => 'value'
217 );
218
219 $response = $this->executeQuery( $this->getSampleRequest( $args ) );
220
221 $this->assertEquals( self::$Success, $response );
222 }
223 }