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