r61917 MediaWiki - Code Review archive

Repository:MediaWiki
Revision:r61916‎ | r61917 | r61918 >
Date:07:45, 3 February 2010
Author:mah
Status:ok (Comments)
Tags:
Comment:
Add preliminary API tests. Next step: UploadChunks API testing.
Modified paths:
  • /trunk/phase3/includes/HttpFunctions.php (modified) (history)
  • /trunk/phase3/tests/MediaWikiAPITest.php (added) (history)
  • /trunk/phase3/tests/MediaWikiAPI_TestCase.php (added) (history)

Diff [purge]

Index: trunk/phase3/tests/MediaWikiAPITest.php
@@ -0,0 +1,74 @@
 2+<?php
 3+
 4+require_once("MediaWikiAPI_TestCase.php");
 5+
 6+class MediaWikiAPITest extends MediaWikiAPI_TestCase {
 7+
 8+ function setup() {
 9+ parent::setup();
 10+ }
 11+
 12+ function testApi() {
 13+ /* Haven't thought about test ordering yet -- but this depends on HttpTest.php */
 14+ $resp = Http::get(self::$apiUrl."?format=xml");
 15+
 16+ libxml_use_internal_errors(true);
 17+ $sxe = simplexml_load_string($resp);
 18+ $this->assertNotType("bool", $sxe);
 19+ $this->assertThat($sxe, $this->isInstanceOf("SimpleXMLElement"));
 20+ }
 21+
 22+ function testApiLoginNoName() {
 23+ $resp = Http::post(self::$apiUrl."?action=login&format=xml",
 24+ array("postData" => array(
 25+ "lgname" => "",
 26+ "lgpassword" => self::$passWord)));
 27+ libxml_use_internal_errors(true);
 28+ $sxe = simplexml_load_string($resp);
 29+ $this->assertNotType("bool", $sxe);
 30+ $this->assertThat($sxe, $this->isInstanceOf("SimpleXMLElement"));
 31+ $a = $sxe->login[0]->attributes()->result;
 32+ $this->assertEquals( ' result="NoName"', $a->asXML() );
 33+ }
 34+
 35+ function testApiLoginBadPass() {
 36+ $resp = Http::post(self::$apiUrl."?action=login&format=xml",
 37+ array("postData" => array(
 38+ "lgname" => self::$userName,
 39+ "lgpassword" => "bad")));
 40+ libxml_use_internal_errors(true);
 41+ $sxe = simplexml_load_string($resp);
 42+ $this->assertNotType("bool", $sxe);
 43+ $this->assertThat($sxe, $this->isInstanceOf("SimpleXMLElement"));
 44+ $a = $sxe->login[0]->attributes()->result;
 45+ $this->assertEquals( ' result="WrongPass"', $a->asXML() );
 46+ }
 47+
 48+ function testApiLoginGoodPass() {
 49+ $resp = Http::post(self::$apiUrl."?action=login&format=xml",
 50+ array("postData" => array(
 51+ "lgname" => self::$userName,
 52+ "lgpassword" => self::$passWord)));
 53+ libxml_use_internal_errors(true);
 54+ $sxe = simplexml_load_string($resp);
 55+ $this->assertNotType("bool", $sxe);
 56+ $this->assertThat($sxe, $this->isInstanceOf("SimpleXMLElement"));
 57+ $a = $sxe->login[0]->attributes()->result;
 58+ $this->assertEquals( ' result="Success"', $a->asXML() );
 59+ }
 60+
 61+ function testApiGotCookie() {
 62+ global $wgScriptPath, $wgServerName;
 63+
 64+ $req = HttpRequest::factory(self::$apiUrl."?action=login&format=xml",
 65+ array("method" => "POST",
 66+ "postData" => array(
 67+ "lgname" => self::$userName,
 68+ "lgpassword" => self::$passWord)));
 69+ $req->execute();
 70+ $cj = $req->getCookieJar();
 71+
 72+ $this->assertRegexp( '/_session=[^;]*; .*UserID=[0-9]*; .*UserName='.self::$userName.'; .*Token=/',
 73+ $cj->serializeToHttpRequest($wgScriptPath, $wgServerName) );
 74+ }
 75+}
Property changes on: trunk/phase3/tests/MediaWikiAPITest.php
___________________________________________________________________
Name: svn:eol-syle
176 + native
Index: trunk/phase3/tests/MediaWikiAPI_TestCase.php
@@ -0,0 +1,42 @@
 2+<?php
 3+
 4+abstract class MediaWikiAPI_TestCase extends PHPUnit_Framework_TestCase {
 5+ protected static $userName;
 6+ protected static $passWord;
 7+ protected static $user;
 8+ protected static $apiUrl;
 9+
 10+ function setup() {
 11+ global $wgServerName, $wgServer, $wgContLang, $wgAuth, $wgScriptPath,
 12+ $wgScriptExtension, $wgMemc;
 13+
 14+ if($wgServerName == "localhost" || $wgServer == "http://localhost") {
 15+ $this->markTestIncomplete('This test needs $wgServerName and $wgServer to '.
 16+ 'be set in LocalSettings.php');
 17+ }
 18+ self::$apiUrl = $wgServer.$wgScriptPath."/api".$wgScriptExtension;
 19+
 20+ $wgMemc = new FakeMemCachedClient;
 21+ $wgContLang = Language::factory( 'en' );
 22+ $wgAuth = new StubObject( 'wgAuth', 'AuthPlugin' );
 23+ self::setupUser();
 24+ }
 25+
 26+ static function setupUser() {
 27+ if ( self::$user == NULL ) {
 28+ self::$userName = "Useruser";
 29+ self::$passWord = User::randomPassword();
 30+
 31+ self::$user = User::newFromName(self::$userName);
 32+ if ( !self::$user->getID() ) {
 33+ self::$user = User::createNew(self::$userName, array(
 34+ "password" => self::$passWord,
 35+ "email" => "test@example.com",
 36+ "real_name" => "Test User"));
 37+ } else {
 38+ self::$user->setPassword(self::$passWord);
 39+ }
 40+ self::$user->saveSettings();
 41+ }
 42+ }
 43+}
Property changes on: trunk/phase3/tests/MediaWikiAPI_TestCase.php
___________________________________________________________________
Name: svn:eol-syle
144 + native
Index: trunk/phase3/includes/HttpFunctions.php
@@ -257,7 +257,9 @@
258258 $list = array();
259259
260260 if( $this->cookieJar ) {
261 - $this->reqHeaders['Cookie'] = $this->cookieJar->serializeToHttpRequest();
 261+ $this->reqHeaders['Cookie'] =
 262+ $this->cookieJar->serializeToHttpRequest($this->parsedURL['path'],
 263+ $this->parsedURL['host']);
262264 }
263265 foreach($this->reqHeaders as $name => $value) {
264266 $list[] = "$name: $value";
@@ -553,6 +555,12 @@
554556 $attr[strtolower( $parts[0] )] = true;
555557 }
556558 }
 559+
 560+ if( !isset( $attr['domain'] ) ) {
 561+ $attr['domain'] = $domain;
 562+ } else {
 563+ /* FIXME: Check that domain is valid */
 564+ }
557565 $this->setCookie( $name, $value, $attr );
558566 }
559567 }

Follow-up revisions

RevisionCommit summaryAuthorDate
r61936follow-up r61917 - stylize for coding conventionsmah16:27, 3 February 2010

Comments

#Comment by MaxSem (talk | contribs)   08:18, 3 February 2010

Why can't one run tests on loopback? Btw, coding conventions aren't followed in this commit.

#Comment by MarkAHershberger (talk | contribs)   16:17, 3 February 2010

these tests require cookies be set, which requires that the proper domain be used.

#Comment by Tim Starling (talk | contribs)   09:42, 25 June 2010

Do not modify the primary wiki database in a test case. Users should be able to run "make test" without nonsense appearing in their Special:Listusers. parserTests.php deals with this issue nicely.

#Comment by 😂 (talk | contribs)   14:42, 5 January 2011

Marking this ok now:

  • External requests in tests were either refactored to use FauxRequest or are skipped
  • Tests shouldn't be modifying the DB anymore (recent refactoring has fixed this)

Status & tagging log