4 * Wraps the user object, so we can also retain full access to properties
5 * like password if we log in via the API.
9 * @deprecated Since 1.25. Use TestUser::getUser()->getName()
16 * @deprecated Since 1.25. Use TestUser::getPassword()
23 * @deprecated Since 1.25. Use TestUser::getUser()
29 private function assertNotReal() {
31 if ( $wgDBprefix !== MediaWikiTestCase
::DB_PREFIX
&& $wgDBprefix !== MediaWikiTestCase
::ORA_DB_PREFIX
) {
32 throw new MWException( "Can't create user on real database" );
36 public function __construct( $username, $realname = 'Real Name',
37 $email = 'sample@example.com', $groups = array()
39 $this->assertNotReal();
41 $this->username
= $username;
42 $this->password
= 'TestUser';
44 $this->user
= User
::newFromName( $this->username
);
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
->isLoggedIn() ) {
53 $this->user
= User
::createNew(
54 $this->username
, array(
56 "real_name" => $realname
61 throw new MWException( "Error creating TestUser " . $username );
65 // Update the user to use the password and other details
66 $change = $this->setPassword( $this->password
) ||
67 $this->setEmail( $email ) ||
68 $this->setRealName( $realname );
70 // Adjust groups by adding any missing ones and removing any extras
71 $currentGroups = $this->user
->getGroups();
72 foreach ( array_diff( $groups, $currentGroups ) as $group ) {
73 $this->user
->addGroup( $group );
75 foreach ( array_diff( $currentGroups, $groups ) as $group ) {
76 $this->user
->removeGroup( $group );
79 $this->user
->saveSettings();
84 * @param string $realname
87 private function setRealName( $realname ) {
88 if ( $this->user
->getRealName() !== $realname ) {
89 $this->user
->setRealName( $realname );
97 * @param string $email
100 private function setEmail( $email ) {
101 if ( $this->user
->getEmail() !== $email ) {
102 $this->user
->setEmail( $email );
110 * @param string $password
113 private function setPassword( $password ) {
114 $passwordFactory = $this->user
->getPasswordFactory();
115 $oldDefaultType = $passwordFactory->getDefaultType();
117 // A is unsalted MD5 (thus fast) ... we don't care about security here, this is test only
118 $passwordFactory->setDefaultType( 'A' );
119 $newPassword = $passwordFactory->newFromPlaintext( $password, $this->user
->getPassword() );
122 if ( !$this->user
->getPassword()->equals( $newPassword ) ) {
124 $this->user
->setPassword( $password );
128 $passwordFactory->setDefaultType( $oldDefaultType );
136 public function getUser() {
143 public function getPassword() {
144 return $this->password
;