r62078 MediaWiki - Code Review archive

Repository:MediaWiki
Revision:r62077‎ | r62078 | r62079 >
Date:05:45, 7 February 2010
Author:mah
Status:deferred
Tags:
Comment:
Test cases working with “make tap” but not just “phpunit” right now.
“make tap” spawns a new php instance for each test file.

Found some problems with date/time parsing on older 32bit php. I
forgot the year 2999 isn't a valid epoch date.
Modified paths:
  • /trunk/phase3/includes/HttpFunctions.php (modified) (history)
  • /trunk/phase3/maintenance/tests/ApiSetup.php (added) (history)
  • /trunk/phase3/maintenance/tests/ApiTest.php (added) (history)
  • /trunk/phase3/maintenance/tests/HttpTest.php (modified) (history)
  • /trunk/phase3/maintenance/tests/MediaWikiAPITest.php (deleted) (history)
  • /trunk/phase3/maintenance/tests/MediaWikiAPI_Setup.php (deleted) (history)

Diff [purge]

Index: trunk/phase3/maintenance/tests/MediaWikiAPITest.php
@@ -1,74 +0,0 @@
2 -<?php
3 -
4 -require_once( "MediaWikiAPI_Setup.php" );
5 -
6 -class MediaWikiAPITest extends MediaWikiAPI_Setup {
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 -}
Index: trunk/phase3/maintenance/tests/MediaWikiAPI_Setup.php
@@ -1,42 +0,0 @@
2 -<?php
3 -
4 -abstract class MediaWikiAPI_Setup 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 -}
Index: trunk/phase3/maintenance/tests/ApiSetup.php
@@ -0,0 +1,42 @@
 2+<?php
 3+
 4+abstract class ApiSetup 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/maintenance/tests/ApiSetup.php
___________________________________________________________________
Name: svn:eol-style
144 + native
Name: svn:eol-syle
245 + native
Index: trunk/phase3/maintenance/tests/ApiTest.php
@@ -0,0 +1,74 @@
 2+<?php
 3+
 4+require_once( "ApiSetup.php" );
 5+
 6+class ApiTest extends ApiSetup {
 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+ $this->markTestIncomplete("Need to make sure cookie/domain handling is correct");
 72+ $this->assertRegexp( '/_session=[^;]*; .*UserID=[0-9]*; .*UserName=' . self::$userName . '; .*Token=/',
 73+ $cj->serializeToHttpRequest( $wgScriptPath, $wgServerName ) );
 74+ }
 75+}
Property changes on: trunk/phase3/maintenance/tests/ApiTest.php
___________________________________________________________________
Name: svn:eol-style
176 + native
Name: svn:eol-syle
277 + native
Index: trunk/phase3/maintenance/tests/HttpTest.php
@@ -353,7 +353,7 @@
354354 "path" => "/path/",
355355 ) );
356356
357 - $this->assertFalse($c->canServeDomain("example.com"));
 357+ $this->assertTrue($c->canServeDomain("example.com"));
358358 $this->assertFalse($c->canServeDomain("www.example.net"));
359359 $this->assertTrue($c->canServeDomain("www.example.com"));
360360
@@ -372,7 +372,7 @@
373373 array(
374374 "domain" => ".example.com",
375375 "path" => "/path/",
376 - "expires" => "January 1, 1990",
 376+ "expires" => "-1 day",
377377 ) );
378378 $this->assertFalse($c->isUnExpired());
379379 $this->assertEquals("", $c->serializeToHttpRequest("/path/", "www.example.com"));
@@ -381,12 +381,10 @@
382382 array(
383383 "domain" => ".example.com",
384384 "path" => "/path/",
385 - "expires" => "January 1, 2999",
 385+ "expires" => "+1 day",
386386 ) );
387387 $this->assertTrue($c->isUnExpired());
388388 $this->assertEquals("name=value", $c->serializeToHttpRequest("/path/", "www.example.com"));
389 -
390 -
391389 }
392390
393391 function testCookieJarSetCookie() {
@@ -415,7 +413,7 @@
416414 array(
417415 "domain" => ".example.net",
418416 "path" => "/path/",
419 - "expires" => "January 1, 1999",
 417+ "expires" => "-1 day",
420418 ) );
421419
422420 $this->assertEquals("name4=value", $cj->serializeToHttpRequest("/path/", "www.example.net"));
@@ -426,7 +424,7 @@
427425 array(
428426 "domain" => ".example.net",
429427 "path" => "/path/",
430 - "expires" => "January 1, 2999",
 428+ "expires" => "+1 day",
431429 ) );
432430 $this->assertEquals("name4=value; name5=value", $cj->serializeToHttpRequest("/path/", "www.example.net"));
433431
@@ -434,7 +432,7 @@
435433 array(
436434 "domain" => ".example.net",
437435 "path" => "/path/",
438 - "expires" => "January 1, 1999",
 436+ "expires" => "-1 day",
439437 ) );
440438 $this->assertEquals("name5=value", $cj->serializeToHttpRequest("/path/", "www.example.net"));
441439 }
@@ -442,20 +440,20 @@
443441 function testParseResponseHeader() {
444442 $cj = new CookieJar;
445443
446 - $h[] = "Set-Cookie: name4=value; domain=.example.com; path=/; expires=Mon, 09-Dec-2999 13:46:00 GMT";
 444+ $h[] = "Set-Cookie: name4=value; domain=.example.com; path=/; expires=Mon, 09-Dec-2029 13:46:00 GMT";
447445 $cj->parseCookieResponseHeader( $h[0], "www.example.com" );
448446 $this->assertEquals("name4=value", $cj->serializeToHttpRequest("/", "www.example.com"));
449447
450 - $h[] = "name4=value2; domain=.example.com; path=/path/; expires=Mon, 09-Dec-2999 13:46:00 GMT";
 448+ $h[] = "name4=value2; domain=.example.com; path=/path/; expires=Mon, 09-Dec-2029 13:46:00 GMT";
451449 $cj->parseCookieResponseHeader( $h[1], "www.example.com" );
452450 $this->assertEquals("", $cj->serializeToHttpRequest("/", "www.example.com"));
453451 $this->assertEquals("name4=value2", $cj->serializeToHttpRequest("/path/", "www.example.com"));
454452
455 - $h[] = "name5=value3; domain=.example.com; path=/path/; expires=Mon, 09-Dec-2999 13:46:00 GMT";
 453+ $h[] = "name5=value3; domain=.example.com; path=/path/; expires=Mon, 09-Dec-2029 13:46:00 GMT";
456454 $cj->parseCookieResponseHeader( $h[2], "www.example.com" );
457455 $this->assertEquals("name4=value2; name5=value3", $cj->serializeToHttpRequest("/path/", "www.example.com"));
458456
459 - $h[] = "name6=value3; domain=.example.net; path=/path/; expires=Mon, 09-Dec-2999 13:46:00 GMT";
 457+ $h[] = "name6=value3; domain=.example.net; path=/path/; expires=Mon, 09-Dec-2029 13:46:00 GMT";
460458 $cj->parseCookieResponseHeader( $h[3], "www.example.com" );
461459 $this->assertEquals("", $cj->serializeToHttpRequest("/path/", "www.example.net"));
462460
@@ -463,7 +461,7 @@
464462 $cj->parseCookieResponseHeader( $h[4], "www.example.net" );
465463 $this->assertEquals("", $cj->serializeToHttpRequest("/path/", "www.example.net"));
466464
467 - $h[] = "name6=value4; domain=.example.net; path=/path/; expires=Mon, 09-Dec-2999 13:46:00 GMT";
 465+ $h[] = "name6=value4; domain=.example.net; path=/path/; expires=Mon, 09-Dec-2029 13:46:00 GMT";
468466 $cj->parseCookieResponseHeader( $h[5], "www.example.net" );
469467 $this->assertEquals("name6=value4", $cj->serializeToHttpRequest("/path/", "www.example.net"));
470468 }
Index: trunk/phase3/includes/HttpFunctions.php
@@ -386,10 +386,10 @@
387387 * Parse the cookies in the response headers and store them in the cookie jar.
388388 */
389389 protected function parseCookies() {
 390+ if( !$this->cookieJar ) {
 391+ $this->cookieJar = new CookieJar;
 392+ }
390393 if( isset( $this->respHeaders['set-cookie'] ) ) {
391 - if( !$this->cookieJar ) {
392 - $this->cookieJar = new CookieJar;
393 - }
394394 $url = parse_url( $this->getFinalUrl() );
395395 foreach( $this->respHeaders['set-cookie'] as $cookie ) {
396396 $this->cookieJar->parseCookieResponseHeader( $cookie, $url['host'] );
@@ -453,12 +453,25 @@
454454 $this->path = "/";
455455 }
456456 if( isset( $attr['domain'] ) ) {
457 - $this->domain = $attr['domain'];
 457+ $this->domain = self::parseCookieDomain( $attr['domain'] );
458458 } else {
459459 throw new MWException("You must specify a domain.");
460460 }
461461 }
462462
 463+ public static function parseCookieDomain( $domain ) {
 464+ /* If domain is given, it has to contain at least two dots */
 465+ if ( strrpos( $domain, '.' ) === false
 466+ || strrpos( $domain, '.' ) === strpos( $domain, '.' ) ) {
 467+ return;
 468+ }
 469+ if ( substr( $domain, 0, 1 ) === '.' ) {
 470+ $domain = substr( $domain, 1 );
 471+ }
 472+
 473+ return $domain;
 474+ }
 475+
463476 /**
464477 * Serialize the cookie jar into a format useful for HTTP Request headers.
465478 * @param $path string the path that will be used. Required.
@@ -560,14 +573,6 @@
561574 if( !isset( $attr['domain'] ) ) {
562575 $attr['domain'] = $domain;
563576 } else {
564 - /* If domain is given, it has to contain at least two dots */
565 - if ( strrpos( $attr['domain'], '.' ) === false
566 - || strrpos( $attr['domain'], '.' ) === strpos( $attr['domain'], '.' ) ) {
567 - return;
568 - }
569 - if ( substr( $attr['domain'], 0, 1 ) === '.' ) {
570 - $attr['domain'] = substr( $attr['domain'], 1 );
571 - }
572577 if ( strlen( $attr['domain'] ) < strlen( $domain )
573578 && substr_compare( $domain, $attr['domain'], -strlen( $attr['domain'] ),
574579 strlen( $attr['domain'] ), TRUE ) != 0 ) {

Status & tagging log