Merge "Add LinkTarget interface"
[lhc/web/wiklou.git] / tests / phpunit / includes / upload / UploadFromUrlTest.php
1 <?php
2
3 /**
4 * @group Broken
5 * @group Upload
6 * @group Database
7 *
8 * @covers UploadFromUrl
9 */
10 class UploadFromUrlTest extends ApiTestCase {
11 protected function setUp() {
12 parent::setUp();
13
14 $this->setMwGlobals( array(
15 'wgEnableUploads' => true,
16 'wgAllowCopyUploads' => true,
17 ) );
18 wfSetupSession();
19
20 if ( wfLocalFile( 'UploadFromUrlTest.png' )->exists() ) {
21 $this->deleteFile( 'UploadFromUrlTest.png' );
22 }
23 }
24
25 protected function doApiRequest( array $params, array $unused = null,
26 $appendModule = false, User $user = null
27 ) {
28 $sessionId = session_id();
29 session_write_close();
30
31 $req = new FauxRequest( $params, true, $_SESSION );
32 $module = new ApiMain( $req, true );
33 $module->execute();
34
35 wfSetupSession( $sessionId );
36
37 return array(
38 $module->getResult()->getResultData( null, array( 'Strip' => 'all' ) ),
39 $req
40 );
41 }
42
43 /**
44 * Ensure that the job queue is empty before continuing
45 */
46 public function testClearQueue() {
47 $job = JobQueueGroup::singleton()->pop();
48 while ( $job ) {
49 $job = JobQueueGroup::singleton()->pop();
50 }
51 $this->assertFalse( $job );
52 }
53
54 /**
55 * @depends testClearQueue
56 */
57 public function testSetupUrlDownload( $data ) {
58 $token = $this->user->getEditToken();
59 $exception = false;
60
61 try {
62 $this->doApiRequest( array(
63 'action' => 'upload',
64 ) );
65 } catch ( UsageException $e ) {
66 $exception = true;
67 $this->assertEquals( "The token parameter must be set", $e->getMessage() );
68 }
69 $this->assertTrue( $exception, "Got exception" );
70
71 $exception = false;
72 try {
73 $this->doApiRequest( array(
74 'action' => 'upload',
75 'token' => $token,
76 ), $data );
77 } catch ( UsageException $e ) {
78 $exception = true;
79 $this->assertEquals( "One of the parameters sessionkey, file, url is required",
80 $e->getMessage() );
81 }
82 $this->assertTrue( $exception, "Got exception" );
83
84 $exception = false;
85 try {
86 $this->doApiRequest( array(
87 'action' => 'upload',
88 'url' => 'http://www.example.com/test.png',
89 'token' => $token,
90 ), $data );
91 } catch ( UsageException $e ) {
92 $exception = true;
93 $this->assertEquals( "The filename parameter must be set", $e->getMessage() );
94 }
95 $this->assertTrue( $exception, "Got exception" );
96
97 $this->user->removeGroup( 'sysop' );
98 $exception = false;
99 try {
100 $this->doApiRequest( array(
101 'action' => 'upload',
102 'url' => 'http://www.example.com/test.png',
103 'filename' => 'UploadFromUrlTest.png',
104 'token' => $token,
105 ), $data );
106 } catch ( UsageException $e ) {
107 $exception = true;
108 $this->assertEquals( "Permission denied", $e->getMessage() );
109 }
110 $this->assertTrue( $exception, "Got exception" );
111 }
112
113 /**
114 * @depends testClearQueue
115 */
116 public function testSyncDownload( $data ) {
117 $token = $this->user->getEditToken();
118
119 $job = JobQueueGroup::singleton()->pop();
120 $this->assertFalse( $job, 'Starting with an empty jobqueue' );
121
122 $this->user->addGroup( 'users' );
123 $data = $this->doApiRequest( array(
124 'action' => 'upload',
125 'filename' => 'UploadFromUrlTest.png',
126 'url' => 'http://upload.wikimedia.org/wikipedia/mediawiki/b/bc/Wiki.png',
127 'ignorewarnings' => true,
128 'token' => $token,
129 ), $data );
130
131 $job = JobQueueGroup::singleton()->pop();
132 $this->assertFalse( $job );
133
134 $this->assertEquals( 'Success', $data[0]['upload']['result'] );
135 $this->deleteFile( 'UploadFromUrlTest.png' );
136
137 return $data;
138 }
139
140 protected function deleteFile( $name ) {
141 $t = Title::newFromText( $name, NS_FILE );
142 $this->assertTrue( $t->exists(), "File '$name' exists" );
143
144 if ( $t->exists() ) {
145 $file = wfFindFile( $name, array( 'ignoreRedirect' => true ) );
146 $empty = "";
147 FileDeleteForm::doDelete( $t, $file, $empty, "none", true );
148 $page = WikiPage::factory( $t );
149 $page->doDeleteArticle( "testing" );
150 }
151 $t = Title::newFromText( $name, NS_FILE );
152
153 $this->assertFalse( $t->exists(), "File '$name' was deleted" );
154 }
155 }