Remove messages.inc, rebuildLanguage.php, writeMessagesArray.inc
[mediawiki.git] / tests / phpunit / includes / api / ApiTestCaseUpload.php
blob40407dc6f18bac094d37c530518b6398f2c4d6d7
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 protected function setUp() {
12 parent::setUp();
14 $this->setMwGlobals( array(
15 'wgEnableUploads' => true,
16 'wgEnableAPI' => true,
17 ) );
19 wfSetupSession();
21 $this->clearFakeUploads();
24 protected function tearDown() {
25 $this->clearTempUpload();
27 parent::tearDown();
30 /**
31 * Helper function -- remove files and associated articles by Title
33 * @param Title $title title to be removed
35 * @return bool
37 public function deleteFileByTitle( $title ) {
38 if ( $title->exists() ) {
39 $file = wfFindFile( $title, array( 'ignoreRedirect' => true ) );
40 $noOldArchive = ""; // yes this really needs to be set this way
41 $comment = "removing for test";
42 $restrictDeletedVersions = false;
43 $status = FileDeleteForm::doDelete(
44 $title,
45 $file,
46 $noOldArchive,
47 $comment,
48 $restrictDeletedVersions
51 if ( !$status->isGood() ) {
52 return false;
55 $page = WikiPage::factory( $title );
56 $page->doDeleteArticle( "removing for test" );
58 // see if it now doesn't exist; reload
59 $title = Title::newFromText( $title->getText(), NS_FILE );
62 return !( $title && $title instanceof Title && $title->exists() );
65 /**
66 * Helper function -- remove files and associated articles with a particular filename
68 * @param string $fileName filename to be removed
70 * @return bool
72 public function deleteFileByFileName( $fileName ) {
73 return $this->deleteFileByTitle( Title::newFromText( $fileName, NS_FILE ) );
76 /**
77 * Helper function -- given a file on the filesystem, find matching
78 * content in the db (and associated articles) and remove them.
80 * @param string $filePath path to file on the filesystem
82 * @return bool
84 public function deleteFileByContent( $filePath ) {
85 $hash = FSFile::getSha1Base36FromPath( $filePath );
86 $dupes = RepoGroup::singleton()->findBySha1( $hash );
87 $success = true;
88 foreach ( $dupes as $dupe ) {
89 $success &= $this->deleteFileByTitle( $dupe->getTitle() );
92 return $success;
95 /**
96 * Fake an upload by dumping the file into temp space, and adding info to $_FILES.
97 * (This is what PHP would normally do).
99 * @param string $fieldName name this would have in the upload form
100 * @param string $fileName name to title this
101 * @param string $type mime type
102 * @param string $filePath path where to find file contents
104 * @throws Exception
105 * @return bool
107 function fakeUploadFile( $fieldName, $fileName, $type, $filePath ) {
108 $tmpName = tempnam( wfTempDir(), "" );
109 if ( !file_exists( $filePath ) ) {
110 throw new Exception( "$filePath doesn't exist!" );
113 if ( !copy( $filePath, $tmpName ) ) {
114 throw new Exception( "couldn't copy $filePath to $tmpName" );
117 clearstatcache();
118 $size = filesize( $tmpName );
119 if ( $size === false ) {
120 throw new Exception( "couldn't stat $tmpName" );
123 $_FILES[$fieldName] = array(
124 'name' => $fileName,
125 'type' => $type,
126 'tmp_name' => $tmpName,
127 'size' => $size,
128 'error' => null
131 return true;
134 function fakeUploadChunk( $fieldName, $fileName, $type, & $chunkData ) {
135 $tmpName = tempnam( wfTempDir(), "" );
136 // copy the chunk data to temp location:
137 if ( !file_put_contents( $tmpName, $chunkData ) ) {
138 throw new Exception( "couldn't copy chunk data to $tmpName" );
141 clearstatcache();
142 $size = filesize( $tmpName );
143 if ( $size === false ) {
144 throw new Exception( "couldn't stat $tmpName" );
147 $_FILES[$fieldName] = array(
148 'name' => $fileName,
149 'type' => $type,
150 'tmp_name' => $tmpName,
151 'size' => $size,
152 'error' => null
156 function clearTempUpload() {
157 if ( isset( $_FILES['file']['tmp_name'] ) ) {
158 $tmp = $_FILES['file']['tmp_name'];
159 if ( file_exists( $tmp ) ) {
160 unlink( $tmp );
166 * Remove traces of previous fake uploads
168 function clearFakeUploads() {
169 $_FILES = array();