Merged FileBackend branch. Manually avoiding merging the many prop-only changes SVN...
[lhc/web/wiklou.git] / tests / phpunit / includes / api / ApiTestCaseUpload.php
1 <?php
2
3 /**
4 * * Abstract class to support upload tests
5 */
6
7 abstract class ApiTestCaseUpload extends ApiTestCase {
8 /**
9 * Fixture -- run before every test
10 */
11 public function setUp() {
12 global $wgEnableUploads, $wgEnableAPI;
13 parent::setUp();
14
15 $wgEnableUploads = true;
16 $wgEnableAPI = true;
17 wfSetupSession();
18
19 $this->clearFakeUploads();
20 }
21
22 public function tearDown() {
23 $this->clearTempUpload();
24 }
25
26 /**
27 * Helper function -- remove files and associated articles by Title
28 * @param $title Title: title to be removed
29 */
30 public function deleteFileByTitle( $title ) {
31 if ( $title->exists() ) {
32 $file = wfFindFile( $title, array( 'ignoreRedirect' => true ) );
33 $noOldArchive = ""; // yes this really needs to be set this way
34 $comment = "removing for test";
35 $restrictDeletedVersions = false;
36 $status = FileDeleteForm::doDelete( $title, $file, $noOldArchive, $comment, $restrictDeletedVersions );
37 if ( !$status->isGood() ) {
38 return false;
39 }
40 $article = new Article( $title );
41 $article->doDeleteArticle( "removing for test" );
42
43 // see if it now doesn't exist; reload
44 $title = Title::newFromText( $title->getText(), NS_FILE );
45 }
46 return ! ( $title && $title instanceof Title && $title->exists() );
47 }
48
49 /**
50 * Helper function -- remove files and associated articles with a particular filename
51 * @param $fileName String: filename to be removed
52 */
53 public function deleteFileByFileName( $fileName ) {
54 return $this->deleteFileByTitle( Title::newFromText( $fileName, NS_FILE ) );
55 }
56
57
58 /**
59 * Helper function -- given a file on the filesystem, find matching content in the db (and associated articles) and remove them.
60 * @param $filePath String: path to file on the filesystem
61 */
62 public function deleteFileByContent( $filePath ) {
63 $hash = FSFile::getSha1Base36FromPath( $filePath );
64 $dupes = RepoGroup::singleton()->findBySha1( $hash );
65 $success = true;
66 foreach ( $dupes as $dupe ) {
67 $success &= $this->deleteFileByTitle( $dupe->getTitle() );
68 }
69 return $success;
70 }
71
72 /**
73 * Fake an upload by dumping the file into temp space, and adding info to $_FILES.
74 * (This is what PHP would normally do).
75 * @param $fieldName String: name this would have in the upload form
76 * @param $fileName String: name to title this
77 * @param $type String: mime type
78 * @param $filePath String: path where to find file contents
79 */
80 function fakeUploadFile( $fieldName, $fileName, $type, $filePath ) {
81 $tmpName = tempnam( wfTempDir(), "" );
82 if ( !file_exists( $filePath ) ) {
83 throw new Exception( "$filePath doesn't exist!" );
84 };
85
86 if ( !copy( $filePath, $tmpName ) ) {
87 throw new Exception( "couldn't copy $filePath to $tmpName" );
88 }
89
90 clearstatcache();
91 $size = filesize( $tmpName );
92 if ( $size === false ) {
93 throw new Exception( "couldn't stat $tmpName" );
94 }
95
96 $_FILES[ $fieldName ] = array(
97 'name' => $fileName,
98 'type' => $type,
99 'tmp_name' => $tmpName,
100 'size' => $size,
101 'error' => null
102 );
103
104 return true;
105
106 }
107
108 function clearTempUpload() {
109 if( isset( $_FILES['file']['tmp_name'] ) ) {
110 $tmp = $_FILES['file']['tmp_name'];
111 if( file_exists( $tmp ) ) {
112 unlink( $tmp );
113 }
114 }
115 }
116
117 /**
118 * Remove traces of previous fake uploads
119 */
120 function clearFakeUploads() {
121 $_FILES = array();
122 }
123
124
125
126
127 }