40407dc6f18bac094d37c530518b6398f2c4d6d7
[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 protected function setUp() {
12 parent::setUp();
13
14 $this->setMwGlobals( array(
15 'wgEnableUploads' => true,
16 'wgEnableAPI' => true,
17 ) );
18
19 wfSetupSession();
20
21 $this->clearFakeUploads();
22 }
23
24 protected function tearDown() {
25 $this->clearTempUpload();
26
27 parent::tearDown();
28 }
29
30 /**
31 * Helper function -- remove files and associated articles by Title
32 *
33 * @param Title $title title to be removed
34 *
35 * @return bool
36 */
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
49 );
50
51 if ( !$status->isGood() ) {
52 return false;
53 }
54
55 $page = WikiPage::factory( $title );
56 $page->doDeleteArticle( "removing for test" );
57
58 // see if it now doesn't exist; reload
59 $title = Title::newFromText( $title->getText(), NS_FILE );
60 }
61
62 return !( $title && $title instanceof Title && $title->exists() );
63 }
64
65 /**
66 * Helper function -- remove files and associated articles with a particular filename
67 *
68 * @param string $fileName filename to be removed
69 *
70 * @return bool
71 */
72 public function deleteFileByFileName( $fileName ) {
73 return $this->deleteFileByTitle( Title::newFromText( $fileName, NS_FILE ) );
74 }
75
76 /**
77 * Helper function -- given a file on the filesystem, find matching
78 * content in the db (and associated articles) and remove them.
79 *
80 * @param string $filePath path to file on the filesystem
81 *
82 * @return bool
83 */
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() );
90 }
91
92 return $success;
93 }
94
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).
98 *
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
103 *
104 * @throws Exception
105 * @return bool
106 */
107 function fakeUploadFile( $fieldName, $fileName, $type, $filePath ) {
108 $tmpName = tempnam( wfTempDir(), "" );
109 if ( !file_exists( $filePath ) ) {
110 throw new Exception( "$filePath doesn't exist!" );
111 }
112
113 if ( !copy( $filePath, $tmpName ) ) {
114 throw new Exception( "couldn't copy $filePath to $tmpName" );
115 }
116
117 clearstatcache();
118 $size = filesize( $tmpName );
119 if ( $size === false ) {
120 throw new Exception( "couldn't stat $tmpName" );
121 }
122
123 $_FILES[$fieldName] = array(
124 'name' => $fileName,
125 'type' => $type,
126 'tmp_name' => $tmpName,
127 'size' => $size,
128 'error' => null
129 );
130
131 return true;
132 }
133
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" );
139 }
140
141 clearstatcache();
142 $size = filesize( $tmpName );
143 if ( $size === false ) {
144 throw new Exception( "couldn't stat $tmpName" );
145 }
146
147 $_FILES[$fieldName] = array(
148 'name' => $fileName,
149 'type' => $type,
150 'tmp_name' => $tmpName,
151 'size' => $size,
152 'error' => null
153 );
154 }
155
156 function clearTempUpload() {
157 if ( isset( $_FILES['file']['tmp_name'] ) ) {
158 $tmp = $_FILES['file']['tmp_name'];
159 if ( file_exists( $tmp ) ) {
160 unlink( $tmp );
161 }
162 }
163 }
164
165 /**
166 * Remove traces of previous fake uploads
167 */
168 function clearFakeUploads() {
169 $_FILES = array();
170 }
171 }