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