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