Merge "Replace RLE/LRE/PDF with RLM/LRM."
[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 $page = WikiPage::factory( $title );
41 $page->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 function fakeUploadChunk( $fieldName, $fileName, $type, & $chunkData ){
108 $tmpName = tempnam( wfTempDir(), "" );
109 // copy the chunk data to temp location:
110 if ( !file_put_contents( $tmpName, $chunkData ) ) {
111 throw new Exception( "couldn't copy chunk data to $tmpName" );
112 }
113
114 clearstatcache();
115 $size = filesize( $tmpName );
116 if ( $size === false ) {
117 throw new Exception( "couldn't stat $tmpName" );
118 }
119
120 $_FILES[ $fieldName ] = array(
121 'name' => $fileName,
122 'type' => $type,
123 'tmp_name' => $tmpName,
124 'size' => $size,
125 'error' => null
126 );
127 }
128
129 function clearTempUpload() {
130 if( isset( $_FILES['file']['tmp_name'] ) ) {
131 $tmp = $_FILES['file']['tmp_name'];
132 if( file_exists( $tmp ) ) {
133 unlink( $tmp );
134 }
135 }
136 }
137
138 /**
139 * Remove traces of previous fake uploads
140 */
141 function clearFakeUploads() {
142 $_FILES = array();
143 }
144
145
146
147
148 }