MediaWiki  REL1_20
ApiOptionsTest.php
Go to the documentation of this file.
00001 <?php
00002 
00007 class ApiOptionsTest extends MediaWikiLangTestCase {
00008 
00009         private $mTested, $mApiMainMock, $mUserMock, $mContext, $mSession;
00010 
00011         private $mOldGetPreferencesHooks = false;
00012 
00013         private static $Success = array( 'options' => 'success' );
00014 
00015         function setUp() {
00016                 parent::setUp();
00017 
00018                 $this->mUserMock = $this->getMockBuilder( 'User' )
00019                         ->disableOriginalConstructor()
00020                         ->getMock();
00021 
00022                 $this->mApiMainMock = $this->getMockBuilder( 'ApiBase' )
00023                         ->disableOriginalConstructor()
00024                         ->getMock();
00025 
00026                 // Set up groups
00027                 $this->mUserMock->expects( $this->any() )
00028                         ->method( 'getEffectiveGroups' )->will( $this->returnValue( array( '*', 'user')) );
00029 
00030                 // Create a new context
00031                 $this->mContext = new DerivativeContext( new RequestContext() );
00032                 $this->mContext->getContext()->setTitle( Title::newFromText( 'Test' ) );
00033                 $this->mContext->setUser( $this->mUserMock );
00034 
00035                 $this->mApiMainMock->expects( $this->any() )
00036                         ->method( 'getContext' )
00037                         ->will( $this->returnValue( $this->mContext ) );
00038 
00039                 $this->mApiMainMock->expects( $this->any() )
00040                         ->method( 'getResult' )
00041                         ->will( $this->returnValue( new ApiResult( $this->mApiMainMock ) ) );
00042 
00043 
00044                 // Empty session
00045                 $this->mSession = array();
00046 
00047                 $this->mTested = new ApiOptions( $this->mApiMainMock, 'options' );
00048 
00049                 global $wgHooks;
00050                 if ( !isset( $wgHooks['GetPreferences'] ) ) {
00051                         $wgHooks['GetPreferences'] = array();
00052                 }
00053                 $this->mOldGetPreferencesHooks = $wgHooks['GetPreferences'];
00054                 $wgHooks['GetPreferences'][] = array( $this, 'hookGetPreferences' );
00055         }
00056 
00057         public function tearDown() {
00058                 global $wgHooks;
00059 
00060                 if ( $this->mOldGetPreferencesHooks !== false ) {
00061                         $wgHooks['GetPreferences'] = $this->mOldGetPreferencesHooks;
00062                         $this->mOldGetPreferencesHooks = false;
00063                 }
00064 
00065                 parent::tearDown();
00066         }
00067 
00068         public function hookGetPreferences( $user, &$preferences ) {
00069                 foreach ( array( 'name', 'willBeNull', 'willBeEmpty', 'willBeHappy' ) as $k ) {
00070                         $preferences[$k] = array(
00071                                 'type' => 'text',
00072                                 'section' => 'test',
00073                                 'label' => '&#160;',
00074                         );
00075                 }
00076 
00077                 return true;
00078         }
00079 
00080         private function getSampleRequest( $custom = array() ) {
00081                 $request = array(
00082                         'token' => '123ABC',
00083                         'change' => null,
00084                         'optionname' => null,
00085                         'optionvalue' => null,
00086                 );
00087                 return array_merge( $request, $custom );
00088         }
00089 
00090         private function executeQuery( $request ) {
00091                 $this->mContext->setRequest( new FauxRequest( $request, true, $this->mSession ) );
00092                 $this->mTested->execute();
00093                 return $this->mTested->getResult()->getData();
00094         }
00095 
00099         public function testNoToken() {
00100                 $request = $this->getSampleRequest( array( 'token' => null ) );
00101 
00102                 $this->executeQuery( $request );
00103         }
00104 
00105         public function testAnon() {
00106                 $this->mUserMock->expects( $this->once() )
00107                         ->method( 'isAnon' )
00108                         ->will( $this->returnValue( true ) );
00109 
00110                 try {
00111                         $request = $this->getSampleRequest();
00112 
00113                         $this->executeQuery( $request );
00114                 } catch ( UsageException $e ) {
00115                         $this->assertEquals( 'notloggedin', $e->getCodeString() );
00116                         $this->assertEquals( 'Anonymous users cannot change preferences', $e->getMessage() );
00117                         return;
00118                 }
00119                 $this->fail( "UsageException was not thrown" );
00120         }
00121 
00122         public function testNoOptionname() {
00123                 try {
00124                         $request = $this->getSampleRequest( array( 'optionvalue' => '1' ) );
00125 
00126                         $this->executeQuery( $request );
00127                 } catch ( UsageException $e ) {
00128                         $this->assertEquals( 'nooptionname', $e->getCodeString() );
00129                         $this->assertEquals( 'The optionname parameter must be set', $e->getMessage() );
00130                         return;
00131                 }
00132                 $this->fail( "UsageException was not thrown" );
00133         }
00134 
00135         public function testNoChanges() {
00136                 $this->mUserMock->expects( $this->never() )
00137                         ->method( 'resetOptions' );
00138 
00139                 $this->mUserMock->expects( $this->never() )
00140                         ->method( 'setOption' );
00141 
00142                 $this->mUserMock->expects( $this->never() )
00143                         ->method( 'saveSettings' );
00144 
00145                 try {
00146                         $request = $this->getSampleRequest();
00147 
00148                         $this->executeQuery( $request );
00149                 } catch ( UsageException $e ) {
00150                         $this->assertEquals( 'nochanges', $e->getCodeString() );
00151                         $this->assertEquals( 'No changes were requested', $e->getMessage() );
00152                         return;
00153                 }
00154                 $this->fail( "UsageException was not thrown" );
00155         }
00156 
00157         public function testReset() {
00158                 $this->mUserMock->expects( $this->once() )
00159                         ->method( 'resetOptions' );
00160 
00161                 $this->mUserMock->expects( $this->never() )
00162                         ->method( 'setOption' );
00163 
00164                 $this->mUserMock->expects( $this->once() )
00165                         ->method( 'saveSettings' );
00166 
00167                 $request = $this->getSampleRequest( array( 'reset' => '' ) );
00168 
00169                 $response = $this->executeQuery( $request );
00170 
00171                 $this->assertEquals( self::$Success, $response );
00172         }
00173 
00174         public function testOptionWithValue() {
00175                 $this->mUserMock->expects( $this->never() )
00176                         ->method( 'resetOptions' );
00177 
00178                 $this->mUserMock->expects( $this->once() )
00179                         ->method( 'setOption' )
00180                         ->with( $this->equalTo( 'name' ), $this->equalTo( 'value' ) );
00181 
00182                 $this->mUserMock->expects( $this->once() )
00183                         ->method( 'saveSettings' );
00184 
00185                 $request = $this->getSampleRequest( array( 'optionname' => 'name', 'optionvalue' => 'value' ) );
00186 
00187                 $response = $this->executeQuery( $request );
00188 
00189                 $this->assertEquals( self::$Success, $response );
00190         }
00191 
00192         public function testOptionResetValue() {
00193                 $this->mUserMock->expects( $this->never() )
00194                         ->method( 'resetOptions' );
00195 
00196                 $this->mUserMock->expects( $this->once() )
00197                         ->method( 'setOption' )
00198                         ->with( $this->equalTo( 'name' ), $this->equalTo( null ) );
00199 
00200                 $this->mUserMock->expects( $this->once() )
00201                         ->method( 'saveSettings' );
00202 
00203                 $request = $this->getSampleRequest( array( 'optionname' => 'name' ) );
00204                 $response = $this->executeQuery( $request );
00205 
00206                 $this->assertEquals( self::$Success, $response );
00207         }
00208 
00209         public function testChange() {
00210                 $this->mUserMock->expects( $this->never() )
00211                         ->method( 'resetOptions' );
00212 
00213                 $this->mUserMock->expects( $this->at( 1 ) )
00214                         ->method( 'getOptions' );
00215 
00216                 $this->mUserMock->expects( $this->at( 2 ) )
00217                         ->method( 'setOption' )
00218                         ->with( $this->equalTo( 'willBeNull' ), $this->equalTo( null ) );
00219 
00220                 $this->mUserMock->expects( $this->at( 3 ) )
00221                         ->method( 'getOptions' );
00222 
00223                 $this->mUserMock->expects( $this->at( 4 ) )
00224                         ->method( 'setOption' )
00225                         ->with( $this->equalTo( 'willBeEmpty' ), $this->equalTo( '' ) );
00226 
00227                 $this->mUserMock->expects( $this->at( 5 ) )
00228                         ->method( 'getOptions' );
00229 
00230                 $this->mUserMock->expects( $this->at( 6 ) )
00231                         ->method( 'setOption' )
00232                         ->with( $this->equalTo( 'willBeHappy' ), $this->equalTo( 'Happy' ) );
00233 
00234                 $this->mUserMock->expects( $this->once() )
00235                         ->method( 'saveSettings' );
00236 
00237                 $request = $this->getSampleRequest( array( 'change' => 'willBeNull|willBeEmpty=|willBeHappy=Happy' ) );
00238 
00239                 $response = $this->executeQuery( $request );
00240 
00241                 $this->assertEquals( self::$Success, $response );
00242         }
00243 
00244         public function testResetChangeOption() {
00245                 $this->mUserMock->expects( $this->once() )
00246                         ->method( 'resetOptions' );
00247 
00248                 $this->mUserMock->expects( $this->at( 2 ) )
00249                         ->method( 'getOptions' );
00250 
00251                 $this->mUserMock->expects( $this->at( 3 ) )
00252                         ->method( 'setOption' )
00253                         ->with( $this->equalTo( 'willBeHappy' ), $this->equalTo( 'Happy' ) );
00254 
00255                 $this->mUserMock->expects( $this->at( 4 ) )
00256                         ->method( 'getOptions' );
00257 
00258                 $this->mUserMock->expects( $this->at( 5 ) )
00259                         ->method( 'setOption' )
00260                         ->with( $this->equalTo( 'name' ), $this->equalTo( 'value' ) );
00261 
00262                 $this->mUserMock->expects( $this->once() )
00263                         ->method( 'saveSettings' );
00264 
00265                 $args = array(
00266                         'reset' => '',
00267                         'change' => 'willBeHappy=Happy',
00268                         'optionname' => 'name',
00269                         'optionvalue' => 'value'
00270                 );
00271 
00272                 $response = $this->executeQuery( $this->getSampleRequest( $args ) );
00273 
00274                 $this->assertEquals( self::$Success, $response );
00275         }
00276 }