Merge "DatabaseMssql: Don't duplicate body of makeList()"
[mediawiki.git] / tests / phpunit / includes / api / ApiTestCaseUpload.php
blobd4d96512cd1b3090aafb9e1e02652685104e3594
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 /**
25 * Helper function -- remove files and associated articles by Title
27 * @param Title $title Title to be removed
29 * @return bool
31 public function deleteFileByTitle( $title ) {
32 if ( $title->exists() ) {
33 $file = wfFindFile( $title, array( 'ignoreRedirect' => true ) );
34 $noOldArchive = ""; // yes this really needs to be set this way
35 $comment = "removing for test";
36 $restrictDeletedVersions = false;
37 $status = FileDeleteForm::doDelete(
38 $title,
39 $file,
40 $noOldArchive,
41 $comment,
42 $restrictDeletedVersions
45 if ( !$status->isGood() ) {
46 return false;
49 $page = WikiPage::factory( $title );
50 $page->doDeleteArticle( "removing for test" );
52 // see if it now doesn't exist; reload
53 $title = Title::newFromText( $title->getText(), NS_FILE );
56 return !( $title && $title instanceof Title && $title->exists() );
59 /**
60 * Helper function -- remove files and associated articles with a particular filename
62 * @param string $fileName Filename to be removed
64 * @return bool
66 public function deleteFileByFileName( $fileName ) {
67 return $this->deleteFileByTitle( Title::newFromText( $fileName, NS_FILE ) );
70 /**
71 * Helper function -- given a file on the filesystem, find matching
72 * content in the db (and associated articles) and remove them.
74 * @param string $filePath Path to file on the filesystem
76 * @return bool
78 public function deleteFileByContent( $filePath ) {
79 $hash = FSFile::getSha1Base36FromPath( $filePath );
80 $dupes = RepoGroup::singleton()->findBySha1( $hash );
81 $success = true;
82 foreach ( $dupes as $dupe ) {
83 $success &= $this->deleteFileByTitle( $dupe->getTitle() );
86 return $success;
89 /**
90 * Fake an upload by dumping the file into temp space, and adding info to $_FILES.
91 * (This is what PHP would normally do).
93 * @param string $fieldName Name this would have in the upload form
94 * @param string $fileName Name to title this
95 * @param string $type MIME type
96 * @param string $filePath Path where to find file contents
98 * @throws Exception
99 * @return bool
101 function fakeUploadFile( $fieldName, $fileName, $type, $filePath ) {
102 $tmpName = $this->getNewTempFile();
103 if ( !file_exists( $filePath ) ) {
104 throw new Exception( "$filePath doesn't exist!" );
107 if ( !copy( $filePath, $tmpName ) ) {
108 throw new Exception( "couldn't copy $filePath to $tmpName" );
111 clearstatcache();
112 $size = filesize( $tmpName );
113 if ( $size === false ) {
114 throw new Exception( "couldn't stat $tmpName" );
117 $_FILES[$fieldName] = array(
118 'name' => $fileName,
119 'type' => $type,
120 'tmp_name' => $tmpName,
121 'size' => $size,
122 'error' => null
125 return true;
128 function fakeUploadChunk( $fieldName, $fileName, $type, & $chunkData ) {
129 $tmpName = $this->getNewTempFile();
130 // copy the chunk data to temp location:
131 if ( !file_put_contents( $tmpName, $chunkData ) ) {
132 throw new Exception( "couldn't copy chunk data to $tmpName" );
135 clearstatcache();
136 $size = filesize( $tmpName );
137 if ( $size === false ) {
138 throw new Exception( "couldn't stat $tmpName" );
141 $_FILES[$fieldName] = array(
142 'name' => $fileName,
143 'type' => $type,
144 'tmp_name' => $tmpName,
145 'size' => $size,
146 'error' => null
151 * Remove traces of previous fake uploads
153 function clearFakeUploads() {
154 $_FILES = array();