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