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