test: coverage recording now needs to be explicit
[mediawiki.git] / tests / phpunit / includes / upload / UploadBaseTest.php
blob298420bc90f09cd396c7e3829142d18229f5bf2e
1 <?php
2 /**
3 * @group Upload
4 */
5 class UploadBaseTest extends MediaWikiTestCase {
6 protected $upload;
9 protected function setUp() {
10 global $wgHooks;
11 parent::setUp();
13 $this->upload = new UploadTestHandler;
14 $this->hooks = $wgHooks;
15 $wgHooks['InterwikiLoadPrefix'][] = function ( $prefix, &$data ) {
16 return false;
20 protected function tearDown() {
21 global $wgHooks;
22 $wgHooks = $this->hooks;
24 parent::tearDown();
28 /**
29 * First checks the return code
30 * of UploadBase::getTitle() and then the actual returned title
32 * @dataProvider provideTestTitleValidation
34 public function testTitleValidation( $srcFilename, $dstFilename, $code, $msg ) {
35 /* Check the result code */
36 $this->assertEquals( $code,
37 $this->upload->testTitleValidation( $srcFilename ),
38 "$msg code" );
40 /* If we expect a valid title, check the title itself. */
41 if ( $code == UploadBase::OK ) {
42 $this->assertEquals( $dstFilename,
43 $this->upload->getTitle()->getText(),
44 "$msg text" );
48 /**
49 * Test various forms of valid and invalid titles that can be supplied.
51 public static function provideTestTitleValidation() {
52 return array(
53 /* Test a valid title */
54 array( 'ValidTitle.jpg', 'ValidTitle.jpg', UploadBase::OK,
55 'upload valid title' ),
56 /* A title with a slash */
57 array( 'A/B.jpg', 'B.jpg', UploadBase::OK,
58 'upload title with slash' ),
59 /* A title with illegal char */
60 array( 'A:B.jpg', 'A-B.jpg', UploadBase::OK,
61 'upload title with colon' ),
62 /* Stripping leading File: prefix */
63 array( 'File:C.jpg', 'C.jpg', UploadBase::OK,
64 'upload title with File prefix' ),
65 /* Test illegal suggested title (r94601) */
66 array( '%281%29.JPG', null, UploadBase::ILLEGAL_FILENAME,
67 'illegal title for upload' ),
68 /* A title without extension */
69 array( 'A', null, UploadBase::FILETYPE_MISSING,
70 'upload title without extension' ),
71 /* A title with no basename */
72 array( '.jpg', null, UploadBase::MIN_LENGTH_PARTNAME,
73 'upload title without basename' ),
74 /* A title that is longer than 255 bytes */
75 array( str_repeat( 'a', 255 ) . '.jpg', null, UploadBase::FILENAME_TOO_LONG,
76 'upload title longer than 255 bytes' ),
77 /* A title that is longer than 240 bytes */
78 array( str_repeat( 'a', 240 ) . '.jpg', null, UploadBase::FILENAME_TOO_LONG,
79 'upload title longer than 240 bytes' ),
83 /**
84 * Test the upload verification functions
86 public function testVerifyUpload() {
87 /* Setup with zero file size */
88 $this->upload->initializePathInfo( '', '', 0 );
89 $result = $this->upload->verifyUpload();
90 $this->assertEquals( UploadBase::EMPTY_FILE,
91 $result['status'],
92 'upload empty file' );
95 // Helper used to create an empty file of size $size.
96 private function createFileOfSize( $size ) {
97 $filename = tempnam( wfTempDir(), "mwuploadtest" );
99 $fh = fopen( $filename, 'w' );
100 ftruncate( $fh, $size );
101 fclose( $fh );
103 return $filename;
107 * test uploading a 100 bytes file with $wgMaxUploadSize = 100
109 * This method should be abstracted so we can test different settings.
112 public function testMaxUploadSize() {
113 global $wgMaxUploadSize;
114 $savedGlobal = $wgMaxUploadSize; // save global
115 global $wgFileExtensions;
116 $wgFileExtensions[] = 'txt';
118 $wgMaxUploadSize = 100;
120 $filename = $this->createFileOfSize( $wgMaxUploadSize );
121 $this->upload->initializePathInfo( basename( $filename ) . '.txt', $filename, 100 );
122 $result = $this->upload->verifyUpload();
123 unlink( $filename );
125 $this->assertEquals(
126 array( 'status' => UploadBase::OK ), $result );
128 $wgMaxUploadSize = $savedGlobal; // restore global
132 class UploadTestHandler extends UploadBase {
133 public function initializeFromRequest( &$request ) {
136 public function testTitleValidation( $name ) {
137 $this->mTitle = false;
138 $this->mDesiredDestName = $name;
139 $this->mTitleError = UploadBase::OK;
140 $this->getTitle();
142 return $this->mTitleError;