Stylize maintenance folder..
[lhc/web/wiklou.git] / maintenance / tests / UploadFromUrlTest.php
1 <?php
2
3 global $IP;
4 require_once( "ApiSetup.php" );
5 require_once( dirname( dirname( __FILE__ ) ) . "/deleteArchivedFiles.inc" );
6 require_once( dirname( dirname( __FILE__ ) ) . "/deleteArchivedRevisions.inc" );
7
8 class nullClass {
9 public function handleOutput() { }
10 public function purgeRedundantText() { }
11 }
12
13 class UploadFromUrlTest extends ApiSetup {
14
15 function setUp() {
16 global $wgEnableUploads, $wgLocalFileRepo, $wgAllowCopyUploads;
17
18 $wgEnableUploads = true;
19 $wgAllowCopyUploads = true;
20 parent::setup();
21 $wgLocalFileRepo = array(
22 'class' => 'LocalRepo',
23 'name' => 'local',
24 'directory' => 'test-repo',
25 'url' => 'http://example.com/images',
26 'hashLevels' => 2,
27 'transformVia404' => false,
28 );
29
30 ini_set( 'log_errors', 1 );
31 ini_set( 'error_reporting', 1 );
32 ini_set( 'display_errors', 1 );
33 }
34
35 function doApiRequest( $params, $data = null ) {
36 $session = isset( $data[2] ) ? $data[2] : array();
37 $_SESSION = $session;
38
39 $req = new FauxRequest( $params, true, $session );
40 $module = new ApiMain( $req, true );
41 $module->execute();
42
43 return array( $module->getResultData(), $req, $_SESSION );
44 }
45
46 function testClearQueue() {
47 while ( $job = Job::pop() ) { }
48 $this->assertFalse( $job );
49 }
50
51 function testLogin() {
52 $data = $this->doApiRequest( array(
53 'action' => 'login',
54 'lgname' => self::$userName,
55 'lgpassword' => self::$passWord ) );
56 $this->assertArrayHasKey( "login", $data[0] );
57 $this->assertArrayHasKey( "result", $data[0]['login'] );
58 $this->assertEquals( "NeedToken", $data[0]['login']['result'] );
59 $token = $data[0]['login']['token'];
60
61 $data = $this->doApiRequest( array(
62 'action' => 'login',
63 "lgtoken" => $token,
64 "lgname" => self::$userName,
65 "lgpassword" => self::$passWord ) );
66
67 $this->assertArrayHasKey( "login", $data[0] );
68 $this->assertArrayHasKey( "result", $data[0]['login'] );
69 $this->assertEquals( "Success", $data[0]['login']['result'] );
70 $this->assertArrayHasKey( 'lgtoken', $data[0]['login'] );
71
72 return $data;
73 }
74
75 /**
76 * @depends testLogin
77 */
78 function testSetupUrlDownload( $data ) {
79 global $wgUser;
80 $wgUser = User::newFromName( self::$userName );
81 $wgUser->load();
82 $data[2]['wsEditToken'] = $data[2]['wsToken'];
83 $token = md5( $data[2]['wsToken'] ) . EDIT_TOKEN_SUFFIX;
84 $exception = false;
85
86 try {
87 $this->doApiRequest( array(
88 'action' => 'upload',
89 ), $data );
90 } catch ( UsageException $e ) {
91 $exception = true;
92 $this->assertEquals( "The token parameter must be set", $e->getMessage() );
93 }
94 $this->assertTrue( $exception, "Got exception" );
95
96 $exception = false;
97 try {
98 $this->doApiRequest( array(
99 'action' => 'upload',
100 'token' => $token,
101 ), $data );
102 } catch ( UsageException $e ) {
103 $exception = true;
104 $this->assertEquals( "One of the parameters sessionkey, file, url is required",
105 $e->getMessage() );
106 }
107 $this->assertTrue( $exception, "Got exception" );
108
109 $exception = false;
110 try {
111 $this->doApiRequest( array(
112 'action' => 'upload',
113 'url' => 'http://www.example.com/test.png',
114 'token' => $token,
115 ), $data );
116 } catch ( UsageException $e ) {
117 $exception = true;
118 $this->assertEquals( "The filename parameter must be set", $e->getMessage() );
119 }
120 $this->assertTrue( $exception, "Got exception" );
121
122 $wgUser->removeGroup( 'sysop' );
123 $exception = false;
124 try {
125 $this->doApiRequest( array(
126 'action' => 'upload',
127 'url' => 'http://www.example.com/test.png',
128 'filename' => 'Test.png',
129 'token' => $token,
130 ), $data );
131 } catch ( UsageException $e ) {
132 $exception = true;
133 $this->assertEquals( "Permission denied", $e->getMessage() );
134 }
135 $this->assertTrue( $exception, "Got exception" );
136
137 $wgUser->addGroup( '*' );
138 $wgUser->addGroup( 'sysop' );
139 $exception = false;
140 $data = $this->doApiRequest( array(
141 'action' => 'upload',
142 'url' => 'http://bits.wikimedia.org/skins-1.5/common/images/poweredby_mediawiki_88x31.png',
143 'filename' => 'Test.png',
144 'token' => $token,
145 ), $data );
146
147 $this->assertTrue( $data[0]['upload']['queued'], 'Job added' );
148
149 $job = Job::pop();
150 $this->assertThat( $job, $this->isInstanceOf( 'UploadFromUrlJob' ),
151 "Got Job Object" );
152
153 $job = Job::pop_type( 'upload' );
154 $this->assertFalse( $job );
155 }
156
157 /**
158 * @depends testLogin
159 */
160 function testDoDownload( $data ) {
161 global $wgUser;
162 $data[2]['wsEditToken'] = $data[2]['wsToken'];
163 $token = md5( $data[2]['wsToken'] ) . EDIT_TOKEN_SUFFIX;
164
165 $wgUser->addGroup( 'users' );
166 $data = $this->doApiRequest( array(
167 'action' => 'upload',
168 'filename' => 'Test.png',
169 'url' => 'http://bits.wikimedia.org/skins-1.5/common/images/poweredby_mediawiki_88x31.png',
170 'token' => $token,
171 ), $data );
172
173 $job = Job::pop();
174 $this->assertEquals( 'UploadFromUrlJob', get_class( $job ) );
175
176 $status = $job->run();
177 $this->assertTrue( $status->isOk() );
178
179 return $data;
180 }
181
182 /**
183 * @depends testDoDownload
184 */
185 function testVerifyDownload( $data ) {
186 $t = Title::newFromText( "Test.png", NS_FILE );
187
188 $this->assertTrue( $t->exists() );
189 }
190 }