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