Debian Codegraph @

tests/phpunit/includes/api/ApiUploadTestCase.php

raw symbols SCIP
1 <?php
2
3 namespace MediaWiki\Tests\Api;
4
5 use Exception;
6 use MediaWiki\MainConfigNames;
7 use MediaWiki\Page\File\FileDeleteForm;
8 use MediaWiki\Title\Title;
9 use Wikimedia\FileBackend\FSFile\FSFile;
10 use Wikimedia\Rdbms\IDBAccessObject;
11
12 /**
13 * Abstract class to support upload tests
14 */
15 abstract class ApiUploadTestCase extends ApiTestCase {
16
17 /**
18 * @since 1.37
19 * @var array Used to fake $_FILES in tests and given to MediaWiki\Request\FauxRequest
20 */
21 protected $requestDataFiles = [];
22
23 /**
24 * Fixture -- run before every test
25 */
26 protected function setUp(): void {
27 parent::setUp();
28
29 $this->overrideConfigValue( MainConfigNames::EnableUploads, true );
30 $this->clearFakeUploads();
31 }
32
33 /**
34 * Helper function -- remove files and associated articles by Title
35 *
36 * @param Title $title Title to be removed
37 *
38 * @return bool
39 */
40 public function deleteFileByTitle( $title ) {
41 if ( $title->exists() ) {
42 $file = $this->getServiceContainer()->getRepoGroup()
43 ->findFile( $title, [ 'ignoreRedirect' => true ] );
44 $noOldArchive = ""; // yes this really needs to be set this way
45 $comment = "removing for test";
46 $restrictDeletedVersions = false;
47 $user = $this->getTestSysop()->getUser();
48 $status = FileDeleteForm::doDelete(
49 $title,
50 $file,
51 $noOldArchive,
52 $comment,
53 $restrictDeletedVersions,
54 $user
55 );
56
57 if ( !$status->isGood() ) {
58 return false;
59 }
60
61 $page = $this->getServiceContainer()->getWikiPageFactory()->newFromTitle( $title );
62 $this->deletePage( $page, "removing for test" );
63 }
64
65 return !( $title && $title instanceof Title && $title->exists( IDBAccessObject::READ_LATEST ) );
66 }
67
68 /**
69 * Helper function -- remove files and associated articles with a particular filename
70 *
71 * @param string $fileName Filename to be removed
72 *
73 * @return bool
74 */
75 public function deleteFileByFileName( $fileName ) {
76 return $this->deleteFileByTitle( Title::newFromText( $fileName, NS_FILE ) );
77 }
78
79 /**
80 * Helper function -- given a file on the filesystem, find matching
81 * content in the db (and associated articles) and remove them.
82 *
83 * @param string $filePath Path to file on the filesystem
84 *
85 * @return bool
86 */
87 public function deleteFileByContent( $filePath ) {
88 $hash = FSFile::getSha1Base36FromPath( $filePath );
89 $dupes = $this->getServiceContainer()->getRepoGroup()->findBySha1( $hash );
90 $success = true;
91 foreach ( $dupes as $dupe ) {
92 $success &= $this->deleteFileByTitle( $dupe->getTitle() );
93 }
94
95 return $success;
96 }
97
98 /**
99 * Fake an upload by dumping the file into temp space, and adding info to $_FILES.
100 * (This is what PHP would normally do).
101 *
102 * @param string $fieldName Name this would have in the upload form
103 * @param string $fileName Name to title this
104 * @param string $type MIME type
105 * @param string $filePath Path where to find file contents
106 *
107 * @throws Exception
108 * @return bool
109 */
110 protected function fakeUploadFile( $fieldName, $fileName, $type, $filePath ) {
111 $tmpName = $this->getNewTempFile();
112 if ( !is_file( $filePath ) ) {
113 $this->fail( "$filePath doesn't exist!" );
114 }
115
116 if ( !copy( $filePath, $tmpName ) ) {
117 $this->fail( "couldn't copy $filePath to $tmpName" );
118 }
119
120 clearstatcache();
121 $size = filesize( $tmpName );
122 if ( $size === false ) {
123 $this->fail( "couldn't stat $tmpName" );
124 }
125
126 $this->requestDataFiles[$fieldName] = [
127 'name' => $fileName,
128 'type' => $type,
129 'tmp_name' => $tmpName,
130 'size' => $size,
131 'error' => UPLOAD_ERR_OK,
132 ];
133
134 return true;
135 }
136
137 public function fakeUploadChunk( $fieldName, $fileName, $type, &$chunkData ) {
138 $tmpName = $this->getNewTempFile();
139 // copy the chunk data to temp location:
140 if ( !file_put_contents( $tmpName, $chunkData ) ) {
141 $this->fail( "couldn't copy chunk data to $tmpName" );
142 }
143
144 clearstatcache();
145 $size = filesize( $tmpName );
146 if ( $size === false ) {
147 $this->fail( "couldn't stat $tmpName" );
148 }
149
150 $this->requestDataFiles[$fieldName] = [
151 'name' => $fileName,
152 'type' => $type,
153 'tmp_name' => $tmpName,
154 'size' => $size,
155 'error' => UPLOAD_ERR_OK,
156 ];
157 }
158
159 /** @inheritDoc */
160 protected function buildFauxRequest( $params, $session ) {
161 $request = parent::buildFauxRequest( $params, $session );
162 $request->setUploadData( $this->requestDataFiles );
163 return $request;
164 }
165
166 /**
167 * Remove traces of previous fake uploads
168 */
169 public function clearFakeUploads() {
170 $this->requestDataFiles = [];
171 }
172 }
173
174 /** @deprecated class alias since 1.42 */
175 class_alias( ApiUploadTestCase::class, 'ApiUploadTestCase' );