Merge "mediawiki.action.edit.preview: New module replacing preview.js"
[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 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 public function testReset() {
109 $this->mUserMock->expects( $this->once() )
110 ->method( 'resetOptions' );
111
112 $this->mUserMock->expects( $this->never() )
113 ->method( 'setOption' );
114
115 $this->mUserMock->expects( $this->once() )
116 ->method( 'saveSettings' );
117
118 $request = $this->getSampleRequest( array( 'reset' => '' ) );
119
120 $response = $this->executeQuery( $request );
121
122 $this->assertEquals( self::$Success, $response );
123 }
124
125 public function testOptionWithValue() {
126 $this->mUserMock->expects( $this->never() )
127 ->method( 'resetOptions' );
128
129 $this->mUserMock->expects( $this->once() )
130 ->method( 'setOption' )
131 ->with( $this->equalTo( 'name' ), $this->equalTo( 'value' ) );
132
133 $this->mUserMock->expects( $this->once() )
134 ->method( 'saveSettings' );
135
136 $request = $this->getSampleRequest( array( 'optionname' => 'name', 'optionvalue' => 'value' ) );
137
138 $response = $this->executeQuery( $request );
139
140 $this->assertEquals( self::$Success, $response );
141 }
142
143 public function testOptionResetValue() {
144 $this->mUserMock->expects( $this->never() )
145 ->method( 'resetOptions' );
146
147 $this->mUserMock->expects( $this->once() )
148 ->method( 'setOption' )
149 ->with( $this->equalTo( 'name' ), $this->equalTo( null ) );
150
151 $this->mUserMock->expects( $this->once() )
152 ->method( 'saveSettings' );
153
154 $request = $this->getSampleRequest( array( 'optionname' => 'name' ) );
155 $response = $this->executeQuery( $request );
156
157 $this->assertEquals( self::$Success, $response );
158 }
159
160 public function testChange() {
161 $this->mUserMock->expects( $this->never() )
162 ->method( 'resetOptions' );
163
164 $this->mUserMock->expects( $this->at( 1 ) )
165 ->method( 'setOption' )
166 ->with( $this->equalTo( 'willBeNull' ), $this->equalTo( null ) );
167
168 $this->mUserMock->expects( $this->at( 2 ) )
169 ->method( 'setOption' )
170 ->with( $this->equalTo( 'willBeEmpty' ), $this->equalTo( '' ) );
171
172 $this->mUserMock->expects( $this->at( 3 ) )
173 ->method( 'setOption' )
174 ->with( $this->equalTo( 'willBeHappy' ), $this->equalTo( 'Happy' ) );
175
176 $this->mUserMock->expects( $this->once() )
177 ->method( 'saveSettings' );
178
179 $request = $this->getSampleRequest( array( 'change' => 'willBeNull|willBeEmpty=|willBeHappy=Happy' ) );
180
181 $response = $this->executeQuery( $request );
182
183 $this->assertEquals( self::$Success, $response );
184 }
185
186 public function testResetChangeOption() {
187 $this->mUserMock->expects( $this->once() )
188 ->method( 'resetOptions' );
189
190 $this->mUserMock->expects( $this->at( 2 ) )
191 ->method( 'setOption' )
192 ->with( $this->equalTo( 'willBeHappy' ), $this->equalTo( 'Happy' ) );
193
194 $this->mUserMock->expects( $this->at( 3 ) )
195 ->method( 'setOption' )
196 ->with( $this->equalTo( 'name' ), $this->equalTo( 'value' ) );
197
198 $this->mUserMock->expects( $this->once() )
199 ->method( 'saveSettings' );
200
201 $args = array(
202 'reset' => '',
203 'change' => 'willBeHappy=Happy',
204 'optionname' => 'name',
205 'optionvalue' => 'value'
206 );
207
208 $response = $this->executeQuery( $this->getSampleRequest( $args ) );
209
210 $this->assertEquals( self::$Success, $response );
211 }
212 }