* upgrade patches for oracle 1.17->1.19
[mediawiki.git] / tests / phpunit / includes / api / ApiTestCaseUpload.php
blobe51e72140a7b9e4174faec0da650131ee614df01
1 <?php
3 /**
4 * * Abstract class to support upload tests
5 */
7 abstract class ApiTestCaseUpload extends ApiTestCase {
8 /**
9 * Fixture -- run before every test
11 public function setUp() {
12 global $wgEnableUploads, $wgEnableAPI;
13 parent::setUp();
15 $wgEnableUploads = true;
16 $wgEnableAPI = true;
17 wfSetupSession();
19 $this->clearFakeUploads();
22 /**
23 * Helper function -- remove files and associated articles by Title
24 * @param $title Title: title to be removed
26 public function deleteFileByTitle( $title ) {
27 if ( $title->exists() ) {
28 $file = wfFindFile( $title, array( 'ignoreRedirect' => true ) );
29 $noOldArchive = ""; // yes this really needs to be set this way
30 $comment = "removing for test";
31 $restrictDeletedVersions = false;
32 $status = FileDeleteForm::doDelete( $title, $file, $noOldArchive, $comment, $restrictDeletedVersions );
33 if ( !$status->isGood() ) {
34 return false;
36 $article = new Article( $title );
37 $article->doDeleteArticle( "removing for test" );
39 // see if it now doesn't exist; reload
40 $title = Title::newFromText( $title->getText(), NS_FILE );
42 return ! ( $title && $title instanceof Title && $title->exists() );
45 /**
46 * Helper function -- remove files and associated articles with a particular filename
47 * @param $fileName String: filename to be removed
49 public function deleteFileByFileName( $fileName ) {
50 return $this->deleteFileByTitle( Title::newFromText( $fileName, NS_FILE ) );
54 /**
55 * Helper function -- given a file on the filesystem, find matching content in the db (and associated articles) and remove them.
56 * @param $filePath String: path to file on the filesystem
58 public function deleteFileByContent( $filePath ) {
59 $hash = File::sha1Base36( $filePath );
60 $dupes = RepoGroup::singleton()->findBySha1( $hash );
61 $success = true;
62 foreach ( $dupes as $dupe ) {
63 $success &= $this->deleteFileByTitle( $dupe->getTitle() );
65 return $success;
68 /**
69 * Fake an upload by dumping the file into temp space, and adding info to $_FILES.
70 * (This is what PHP would normally do).
71 * @param $fieldName String: name this would have in the upload form
72 * @param $fileName String: name to title this
73 * @param $type String: mime type
74 * @param $filePath String: path where to find file contents
76 function fakeUploadFile( $fieldName, $fileName, $type, $filePath ) {
77 $tmpName = tempnam( wfTempDir(), "" );
78 if ( !file_exists( $filePath ) ) {
79 throw new Exception( "$filePath doesn't exist!" );
82 if ( !copy( $filePath, $tmpName ) ) {
83 throw new Exception( "couldn't copy $filePath to $tmpName" );
86 clearstatcache();
87 $size = filesize( $tmpName );
88 if ( $size === false ) {
89 throw new Exception( "couldn't stat $tmpName" );
92 $_FILES[ $fieldName ] = array(
93 'name' => $fileName,
94 'type' => $type,
95 'tmp_name' => $tmpName,
96 'size' => $size,
97 'error' => null
100 return true;
105 * Remove traces of previous fake uploads
107 function clearFakeUploads() {
108 $_FILES = array();