Add PasswordFactory to MediaWikiServices
[lhc/web/wiklou.git] / tests / phpunit / includes / api / ApiLoginTest.php
index 3cf1fde..384d779 100644 (file)
@@ -1,5 +1,6 @@
 <?php
 
+use MediaWiki\MediaWikiServices;
 use Wikimedia\TestingAccessWrapper;
 
 /**
@@ -142,7 +143,7 @@ class ApiLoginTest extends ApiTestCase {
                libxml_use_internal_errors( true );
                $sxe = simplexml_load_string( $req->getContent() );
                $this->assertNotInternalType( "bool", $sxe );
-               $this->assertThat( $sxe, $this->isInstanceOf( "SimpleXMLElement" ) );
+               $this->assertThat( $sxe, $this->isInstanceOf( SimpleXMLElement::class ) );
                $this->assertNotInternalType( "null", $sxe->login[0] );
 
                $a = $sxe->login[0]->attributes()->result[0];
@@ -233,8 +234,7 @@ class ApiLoginTest extends ApiTestCase {
                $this->assertNotEquals( 0, $centralId, 'sanity check' );
 
                $password = 'ngfhmjm64hv0854493hsj5nncjud2clk';
-               $passwordFactory = new PasswordFactory();
-               $passwordFactory->init( RequestContext::getMain()->getConfig() );
+               $passwordFactory = MediaWikiServices::getInstance()->getPasswordFactory();
                // A is unsalted MD5 (thus fast) ... we don't care about security here, this is test only
                $passwordHash = $passwordFactory->newFromPlaintext( $password );
 
@@ -282,4 +282,20 @@ class ApiLoginTest extends ApiTestCase {
                $this->assertEquals( 'Success', $a );
        }
 
+       public function testLoginWithNoSameOriginSecurity() {
+               $this->setTemporaryHook( 'RequestHasSameOriginSecurity',
+                       function () {
+                               return false;
+                       }
+               );
+
+               $result = $this->doApiRequest( [
+                       'action' => 'login',
+               ] )[0]['login'];
+
+               $this->assertSame( [
+                       'result' => 'Aborted',
+                       'reason' => 'Cannot log in when the same-origin policy is not applied.',
+               ], $result );
+       }
 }