Follow up r85566. Add the helper classes to test autoloader.
[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 /**
23 * Fixture -- run after every test
24 * Clean up temporary files etc.
25 */
26 function tearDown() {
27 }
28
29
30 /**
31 * Helper function -- remove files and associated articles by Title
32 * @param $title Title: title to be removed
33 */
34 public function deleteFileByTitle( $title ) {
35 if ( $title->exists() ) {
36 $file = wfFindFile( $title, array( 'ignoreRedirect' => true ) );
37 $noOldArchive = ""; // yes this really needs to be set this way
38 $comment = "removing for test";
39 $restrictDeletedVersions = false;
40 $status = FileDeleteForm::doDelete( $title, $file, $noOldArchive, $comment, $restrictDeletedVersions );
41 if ( !$status->isGood() ) {
42 return false;
43 }
44 $article = new Article( $title );
45 $article->doDeleteArticle( "removing for test" );
46
47 // see if it now doesn't exist; reload
48 $title = Title::newFromText( $title->getText(), NS_FILE );
49 }
50 return ! ( $title && $title instanceof Title && $title->exists() );
51 }
52
53 /**
54 * Helper function -- remove files and associated articles with a particular filename
55 * @param $fileName String: filename to be removed
56 */
57 public function deleteFileByFileName( $fileName ) {
58 return $this->deleteFileByTitle( Title::newFromText( $fileName, NS_FILE ) );
59 }
60
61
62 /**
63 * Helper function -- given a file on the filesystem, find matching content in the db (and associated articles) and remove them.
64 * @param $filePath String: path to file on the filesystem
65 */
66 public function deleteFileByContent( $filePath ) {
67 $hash = File::sha1Base36( $filePath );
68 $dupes = RepoGroup::singleton()->findBySha1( $hash );
69 $success = true;
70 foreach ( $dupes as $dupe ) {
71 $success &= $this->deleteFileByTitle( $dupe->getTitle() );
72 }
73 return $success;
74 }
75
76 /**
77 * Fake an upload by dumping the file into temp space, and adding info to $_FILES.
78 * (This is what PHP would normally do).
79 * @param $fieldName String: name this would have in the upload form
80 * @param $fileName String: name to title this
81 * @param $type String: mime type
82 * @param $filePath String: path where to find file contents
83 */
84 function fakeUploadFile( $fieldName, $fileName, $type, $filePath ) {
85 $tmpName = tempnam( wfTempDir(), "" );
86 if ( !file_exists( $filePath ) ) {
87 throw new Exception( "$filePath doesn't exist!" );
88 };
89
90 if ( !copy( $filePath, $tmpName ) ) {
91 throw new Exception( "couldn't copy $filePath to $tmpName" );
92 }
93
94 clearstatcache();
95 $size = filesize( $tmpName );
96 if ( $size === false ) {
97 throw new Exception( "couldn't stat $tmpName" );
98 }
99
100 $_FILES[ $fieldName ] = array(
101 'name' => $fileName,
102 'type' => $type,
103 'tmp_name' => $tmpName,
104 'size' => $size,
105 'error' => null
106 );
107
108 return true;
109
110 }
111
112 /**
113 * Remove traces of previous fake uploads
114 */
115 function clearFakeUploads() {
116 $_FILES = array();
117 }
118
119
120
121
122 }