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