f004f3c6bea04f8b9a1785163ecdfb1eeeacf02e
[lhc/web/wiklou.git] / maintenance / tests / phpunit / includes / api / ApiSetup.php
1 <?php
2
3 abstract class ApiTestSetup extends PHPUnit_Framework_TestCase {
4 protected static $user;
5 protected static $sysopUser;
6 protected static $apiUrl;
7
8 function setUp() {
9 global $wgServer, $wgContLang, $wgAuth, $wgMemc, $wgRequest;
10
11 self::$apiUrl = $wgServer . wfScript( 'api' );
12
13 $wgMemc = new FakeMemCachedClient;
14 $wgContLang = Language::factory( 'en' );
15 $wgAuth = new StubObject( 'wgAuth', 'AuthPlugin' );
16 $wgRequest = new FauxRequest( array() );
17 self::setupUser();
18 }
19
20 protected function doApiRequest( $params, $data = null ) {
21 $_SESSION = isset( $data[2] ) ? $data[2] : array();
22
23 $req = new FauxRequest( $params, true, $_SESSION );
24 $module = new ApiMain( $req, true );
25 $module->execute();
26
27 $data[0] = $module->getResultData();
28 $data[1] = $req;
29 $data[2] = $_SESSION;
30
31 return $data;
32 }
33
34 static function setupUser() {
35 if ( self::$user == null || self::$sysopUser == null ) {
36 self::$user = new UserWrapper( 'User for MediaWiki automated tests', User::randomPassword() );
37 self::$sysopUser = new UserWrapper( 'Sysop for MediaWiki automated tests', User::randomPassword(), 'sysop' );
38 }
39
40 $GLOBALS['wgUser'] = self::$sysopUser->user;
41 }
42 }
43
44 class UserWrapper {
45 public $userName, $password, $user;
46
47 public function __construct( $userName, $password, $group = '' ) {
48 $this->userName = $userName;
49 $this->password = $password;
50
51 $this->user = User::newFromName( $this->userName );
52 if ( !$this->user->getID() ) {
53 $this->user = User::createNew( $this->userName, array(
54 "email" => "test@example.com",
55 "real_name" => "Test User" ) );
56 }
57 $this->user->setPassword( $this->password );
58
59 if ( $group !== '' ) {
60 $this->user->addGroup( $group );
61 }
62 $this->user->saveSettings();
63 }
64 }
65