(second commit to get all files.)
[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( 'Useruser', 'Passpass' );
37 self::$sysopUser = new UserWrapper( 'Useruser1', 'Passpass1', 'sysop' );
38 }
39
40 $GLOBALS['wgUser'] = self::$sysopUser->user;
41
42 }
43
44 function tearDown() {
45 global $wgMemc;
46 $wgMemc = null;
47 }
48 }
49
50 class UserWrapper {
51 public $userName, $password, $user;
52
53 public function __construct( $userName, $password, $group = '' ) {
54 $this->userName = $userName;
55 $this->password = $password;
56
57 $this->user = User::newFromName( $this->userName );
58
59 if ( !$this->user->getID() ) {
60 $this->user = User::createNew( $this->userName, array(
61 "email" => "test@example.com",
62 "real_name" => "Test User" ) );
63 }
64 $this->user->setPassword( $this->password );
65
66 if ( $group !== '' ) {
67 $this->user->addGroup( $group );
68 }
69
70 $this->user->saveSettings();
71 }
72 }