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