r75881 MediaWiki - Code Review archive

Repository:MediaWiki
Revision:r75880‎ | r75881 | r75882 >
Date:21:28, 2 November 2010
Author:neilk
Status:deferred
Tags:
Comment:
better organization for ApiUploadTest, removed unnecessary apiUrl
Modified paths:
  • /branches/uploadwizard/phase3/maintenance/tests/phpunit/includes/api/ApiUploadTest.php (modified) (history)

Diff [purge]

Index: branches/uploadwizard/phase3/maintenance/tests/phpunit/includes/api/ApiUploadTest.php
@@ -1,6 +1,11 @@
22 <?php
33
44 /**
 5+ * @group Database
 6+ * @group Destructive
 7+ */
 8+
 9+/**
510 * n.b. Ensure that you can write to the images/ directory as the
611 * user that will run tests.
712 */
@@ -14,15 +19,72 @@
1520
1621 require_once( dirname( __FILE__ ) . '/RandomImageGenerator.php' );
1722
 23+/* Wraps the user object, so we can also retain full access to properties like password if we log in via the API */
 24+class ApiTestUser {
 25+ public $username;
 26+ public $password;
 27+ public $email;
 28+ public $groups;
 29+ public $user;
 30+
 31+ function __construct( $username, $realname = 'Real Name', $email = 'sample@sample.com', $groups = array() ) {
 32+ global $wgMinimalPasswordLength;
 33+
 34+ $this->username = $username;
 35+ $this->realname = $realname;
 36+ $this->email = $email;
 37+ $this->groups = $groups;
 38+
 39+ // don't allow user to hardcode or select passwords -- people sometimes run tests
 40+ // on live wikis. Sometimes we create sysop users in these tests. A sysop user with
 41+ // a known password would be a Bad Thing.
 42+ $this->password = User::randomPassword();
 43+
 44+ $this->user = User::newFromName( $this->username );
 45+ $this->user->load();
 46+
 47+ // In an ideal world we'd have a new wiki (or mock data store) for every single test.
 48+ // But for now, we just need to create or update the user with the desired properties.
 49+ // we particularly need the new password, since we just generated it randomly.
 50+ // In core MediaWiki, there is no functionality to delete users, so this is the best we can do.
 51+ if ( !$this->user->getID() ) {
 52+ // create the user
 53+ $this->user = User::createNew(
 54+ $this->username, array(
 55+ "email" => $this->email,
 56+ "real_name" => $this->realname
 57+ )
 58+ );
 59+ if ( !$this->user ) {
 60+ throw new Exception( "error creating user" );
 61+ }
 62+ }
 63+
 64+ // update the user to use the new random password and other details
 65+ $this->user->setPassword( $this->password );
 66+ $this->user->setEmail( $this->email );
 67+ $this->user->setRealName( $this->realname );
 68+ // remove all groups, replace with any groups specified
 69+ foreach ( $this->user->getGroups() as $group ) {
 70+ $this->user->removeGroup( $group );
 71+ }
 72+ if ( count( $this->groups ) ) {
 73+ foreach ( $this->groups as $group ) {
 74+ $this->user->addGroup( $group );
 75+ }
 76+ }
 77+ $this->user->saveSettings();
 78+
 79+ }
 80+
 81+}
 82+
1883 abstract class ApiTestCase extends PHPUnit_Framework_TestCase {
19 - public static $apiUrl;
2084 public static $users;
2185
2286 function setUp() {
2387 global $wgServer, $wgContLang, $wgAuth, $wgMemc, $wgRequest, $wgUser;
2488
25 - self::$apiUrl = $wgServer . wfScript( 'api' );
26 -
2789 $wgMemc = new FakeMemCachedClient();
2890 $wgContLang = Language::factory( 'en' );
2991 $wgAuth = new StubObject( 'wgAuth', 'AuthPlugin' );
@@ -581,63 +643,3 @@
582644
583645 }
584646
585 -/* Wraps the user object, so we can also retain full access to properties like password if we log in via the API */
586 -class ApiTestUser {
587 - public $username;
588 - public $password;
589 - public $email;
590 - public $groups;
591 - public $user;
592 -
593 - function __construct( $username, $realname = 'Real Name', $email = 'sample@sample.com', $groups = array() ) {
594 - global $wgMinimalPasswordLength;
595 -
596 - $this->username = $username;
597 - $this->realname = $realname;
598 - $this->email = $email;
599 - $this->groups = $groups;
600 -
601 - // don't allow user to hardcode or select passwords -- people sometimes run tests
602 - // on live wikis. Sometimes we create sysop users in these tests. A sysop user with
603 - // a known password would be a Bad Thing.
604 - $this->password = User::randomPassword();
605 -
606 - $this->user = User::newFromName( $this->username );
607 - $this->user->load();
608 -
609 - // In an ideal world we'd have a new wiki (or mock data store) for every single test.
610 - // But for now, we just need to create or update the user with the desired properties.
611 - // we particularly need the new password, since we just generated it randomly.
612 - // In core MediaWiki, there is no functionality to delete users, so this is the best we can do.
613 - if ( !$this->user->getID() ) {
614 - // create the user
615 - $this->user = User::createNew(
616 - $this->username, array(
617 - "email" => $this->email,
618 - "real_name" => $this->realname
619 - )
620 - );
621 - if ( !$this->user ) {
622 - throw new Exception( "error creating user" );
623 - }
624 - }
625 -
626 - // update the user to use the new random password and other details
627 - $this->user->setPassword( $this->password );
628 - $this->user->setEmail( $this->email );
629 - $this->user->setRealName( $this->realname );
630 - // remove all groups, replace with any groups specified
631 - foreach ( $this->user->getGroups() as $group ) {
632 - $this->user->removeGroup( $group );
633 - }
634 - if ( count( $this->groups ) ) {
635 - foreach ( $this->groups as $group ) {
636 - $this->user->addGroup( $group );
637 - }
638 - }
639 - $this->user->saveSettings();
640 -
641 - }
642 -
643 -
644 - }

Status & tagging log