wiki.techinc.nl/tests/phpunit/includes/upload/UploadFromUrlTest.php

329 lines
8.8 KiB
PHP
Raw Normal View History

<?php
/**
* @group Broken
* @group Upload
* @group Database
*
* @covers UploadFromUrl
*/
class UploadFromUrlTest extends ApiTestCase {
Clean and repair many phpunit tests (+ fix implied configuration) This commit depends on the introduction of MediaWikiTestCase::setMwGlobals in change Iccf6ea81f4. Various tests already set their globals, but forgot to restore them afterwards, or forgot to call the parent setUp, tearDown... Either way they won't have to anymore with setMwGlobals. Consistent use of function characteristics: * protected function setUp * protected function tearDown * public static function (provide..) (Matching the function signature with PHPUnit/Framework/TestCase.php) Replaces: * public function (setUp|tearDown)\( * protected function $1( * \tfunction (setUp|tearDown)\( * \tprotected function $1( * \tfunction (data|provide)\( * \tpublic static function $1\( Also renamed a few "data#", "provider#" and "provides#" functions to "provide#" for consistency. This also removes confusion where the /media tests had a few private methods called dataFile(), which were sometimes expected to be data providers. Fixes: TimestampTest often failed due to a previous test setting a different language (it tests "1 hour ago" so need to make sure it is set to English). MWNamespaceTest became a lot cleaner now that it executes with a known context. Though the now-redundant code that was removed didn't work anyway because wgContentNamespaces isn't keyed by namespace id, it had them was values... FileBackendTest: * Fixed: "PHP Fatal: Using $this when not in object context" HttpTest * Added comment about: "PHP Fatal: Call to protected MWHttpRequest::__construct()" (too much unrelated code to fix in this commit) ExternalStoreTest * Add an assertTrue as well, without it the test is useless because regardless of whether wgExternalStores is true or false it only uses it if it is an array. Change-Id: I9d2b148e57bada64afeb7d5a99bec0e58f8e1561
2012-10-08 10:56:20 +00:00
protected function setUp() {
parent::setUp();
$this->setMwGlobals( array(
'wgEnableUploads' => true,
'wgAllowCopyUploads' => true,
'wgAllowAsyncCopyUploads' => true,
) );
wfSetupSession();
if ( wfLocalFile( 'UploadFromUrlTest.png' )->exists() ) {
$this->deleteFile( 'UploadFromUrlTest.png' );
}
}
protected function doApiRequest( array $params, array $unused = null,
$appendModule = false, User $user = null
) {
$sessionId = session_id();
session_write_close();
$req = new FauxRequest( $params, true, $_SESSION );
$module = new ApiMain( $req, true );
$module->execute();
wfSetupSession( $sessionId );
return array( $module->getResultData(), $req );
}
/**
* Ensure that the job queue is empty before continuing
*/
public function testClearQueue() {
$job = JobQueueGroup::singleton()->pop();
while ( $job ) {
$job = JobQueueGroup::singleton()->pop();
}
$this->assertFalse( $job );
}
/**
* @depends testClearQueue
*/
public function testSetupUrlDownload( $data ) {
$token = $this->user->getEditToken();
$exception = false;
try {
$this->doApiRequest( array(
'action' => 'upload',
) );
} catch ( UsageException $e ) {
$exception = true;
$this->assertEquals( "The token parameter must be set", $e->getMessage() );
}
$this->assertTrue( $exception, "Got exception" );
$exception = false;
try {
$this->doApiRequest( array(
'action' => 'upload',
'token' => $token,
), $data );
} catch ( UsageException $e ) {
$exception = true;
$this->assertEquals( "One of the parameters sessionkey, file, url, statuskey is required",
$e->getMessage() );
}
$this->assertTrue( $exception, "Got exception" );
$exception = false;
try {
$this->doApiRequest( array(
'action' => 'upload',
'url' => 'http://www.example.com/test.png',
'token' => $token,
), $data );
} catch ( UsageException $e ) {
$exception = true;
$this->assertEquals( "The filename parameter must be set", $e->getMessage() );
}
$this->assertTrue( $exception, "Got exception" );
$this->user->removeGroup( 'sysop' );
$exception = false;
try {
$this->doApiRequest( array(
'action' => 'upload',
'url' => 'http://www.example.com/test.png',
'filename' => 'UploadFromUrlTest.png',
'token' => $token,
), $data );
} catch ( UsageException $e ) {
$exception = true;
$this->assertEquals( "Permission denied", $e->getMessage() );
}
$this->assertTrue( $exception, "Got exception" );
$this->user->addGroup( 'sysop' );
$data = $this->doApiRequest( array(
'action' => 'upload',
'url' => 'http://bits.wikimedia.org/skins-1.5/common/images/poweredby_mediawiki_88x31.png',
'asyncdownload' => 1,
'filename' => 'UploadFromUrlTest.png',
'token' => $token,
), $data );
$this->assertEquals( $data[0]['upload']['result'], 'Queued', 'Queued upload' );
$job = JobQueueGroup::singleton()->pop();
$this->assertThat( $job, $this->isInstanceOf( 'UploadFromUrlJob' ), 'Queued upload inserted' );
}
/**
* @depends testClearQueue
*/
public function testAsyncUpload( $data ) {
$token = $this->user->getEditToken();
$this->user->addGroup( 'users' );
$data = $this->doAsyncUpload( $token, true );
$this->assertEquals( $data[0]['upload']['result'], 'Success' );
$this->assertEquals( $data[0]['upload']['filename'], 'UploadFromUrlTest.png' );
$this->assertTrue( wfLocalFile( $data[0]['upload']['filename'] )->exists() );
$this->deleteFile( 'UploadFromUrlTest.png' );
return $data;
}
/**
* @depends testClearQueue
*/
public function testAsyncUploadWarning( $data ) {
$token = $this->user->getEditToken();
$this->user->addGroup( 'users' );
$data = $this->doAsyncUpload( $token );
$this->assertEquals( $data[0]['upload']['result'], 'Warning' );
$this->assertTrue( isset( $data[0]['upload']['sessionkey'] ) );
$data = $this->doApiRequest( array(
'action' => 'upload',
'sessionkey' => $data[0]['upload']['sessionkey'],
'filename' => 'UploadFromUrlTest.png',
'ignorewarnings' => 1,
'token' => $token,
) );
$this->assertEquals( $data[0]['upload']['result'], 'Success' );
$this->assertEquals( $data[0]['upload']['filename'], 'UploadFromUrlTest.png' );
$this->assertTrue( wfLocalFile( $data[0]['upload']['filename'] )->exists() );
$this->deleteFile( 'UploadFromUrlTest.png' );
return $data;
}
/**
* @depends testClearQueue
*/
public function testSyncDownload( $data ) {
$token = $this->user->getEditToken();
$job = JobQueueGroup::singleton()->pop();
$this->assertFalse( $job, 'Starting with an empty jobqueue' );
$this->user->addGroup( 'users' );
$data = $this->doApiRequest( array(
'action' => 'upload',
'filename' => 'UploadFromUrlTest.png',
'url' => 'http://bits.wikimedia.org/skins-1.5/common/images/poweredby_mediawiki_88x31.png',
'ignorewarnings' => true,
'token' => $token,
), $data );
$job = JobQueueGroup::singleton()->pop();
$this->assertFalse( $job );
$this->assertEquals( 'Success', $data[0]['upload']['result'] );
$this->deleteFile( 'UploadFromUrlTest.png' );
return $data;
}
public function testLeaveMessage() {
$token = $this->user->user->getEditToken();
$talk = $this->user->user->getTalkPage();
if ( $talk->exists() ) {
$page = WikiPage::factory( $talk );
$page->doDeleteArticle( '' );
}
$this->assertFalse(
(bool)$talk->getArticleID( Title::GAID_FOR_UPDATE ),
'User talk does not exist'
);
$this->doApiRequest( array(
'action' => 'upload',
'filename' => 'UploadFromUrlTest.png',
'url' => 'http://bits.wikimedia.org/skins-1.5/common/images/poweredby_mediawiki_88x31.png',
'asyncdownload' => 1,
'token' => $token,
'leavemessage' => 1,
'ignorewarnings' => 1,
) );
$job = JobQueueGroup::singleton()->pop();
$this->assertEquals( 'UploadFromUrlJob', get_class( $job ) );
$job->run();
$this->assertTrue( wfLocalFile( 'UploadFromUrlTest.png' )->exists() );
$this->assertTrue( (bool)$talk->getArticleID( Title::GAID_FOR_UPDATE ), 'User talk exists' );
$this->deleteFile( 'UploadFromUrlTest.png' );
$exception = false;
try {
$this->doApiRequest( array(
'action' => 'upload',
'filename' => 'UploadFromUrlTest.png',
'url' => 'http://bits.wikimedia.org/skins-1.5/common/images/poweredby_mediawiki_88x31.png',
'asyncdownload' => 1,
'token' => $token,
'leavemessage' => 1,
) );
} catch ( UsageException $e ) {
$exception = true;
$this->assertEquals(
'Using leavemessage without ignorewarnings is not supported',
$e->getMessage()
);
}
$this->assertTrue( $exception );
$job = JobQueueGroup::singleton()->pop();
$this->assertFalse( $job );
return;
/*
// Broken until using leavemessage with ignorewarnings is supported
$talkRev = Revision::newFromTitle( $talk );
$talkSize = $talkRev->getSize();
$job->run();
$this->assertFalse( wfLocalFile( 'UploadFromUrlTest.png' )->exists() );
$talkRev = Revision::newFromTitle( $talk );
$this->assertTrue( $talkRev->getSize() > $talkSize, 'New message left' );
*/
}
/**
* Helper function to perform an async upload, execute the job and fetch
* the status
*
* @param string $token
* @param bool $ignoreWarnings
* @param bool $leaveMessage
* @return array The result of action=upload&statuskey=key
*/
private function doAsyncUpload( $token, $ignoreWarnings = false, $leaveMessage = false ) {
$params = array(
'action' => 'upload',
'filename' => 'UploadFromUrlTest.png',
'url' => 'http://bits.wikimedia.org/skins-1.5/common/images/poweredby_mediawiki_88x31.png',
'asyncdownload' => 1,
'token' => $token,
);
if ( $ignoreWarnings ) {
$params['ignorewarnings'] = 1;
}
if ( $leaveMessage ) {
$params['leavemessage'] = 1;
}
$data = $this->doApiRequest( $params );
$this->assertEquals( $data[0]['upload']['result'], 'Queued' );
$this->assertTrue( isset( $data[0]['upload']['statuskey'] ) );
$statusKey = $data[0]['upload']['statuskey'];
$job = JobQueueGroup::singleton()->pop();
$this->assertEquals( 'UploadFromUrlJob', get_class( $job ) );
$status = $job->run();
$this->assertTrue( $status );
$data = $this->doApiRequest( array(
'action' => 'upload',
'statuskey' => $statusKey,
'token' => $token,
) );
return $data;
}
protected function deleteFile( $name ) {
$t = Title::newFromText( $name, NS_FILE );
$this->assertTrue( $t->exists(), "File '$name' exists" );
if ( $t->exists() ) {
$file = wfFindFile( $name, array( 'ignoreRedirect' => true ) );
$empty = "";
FileDeleteForm::doDelete( $t, $file, $empty, "none", true );
$page = WikiPage::factory( $t );
$page->doDeleteArticle( "testing" );
}
$t = Title::newFromText( $name, NS_FILE );
$this->assertFalse( $t->exists(), "File '$name' was deleted" );
}
}