Per wikitech-l discussion: Move tests from maintenance/tests/ to tests/. They're...
[lhc/web/wiklou.git] / tests / phpunit / includes / UploadTest.php
1 <?php
2 /**
3 * @group Upload
4 */
5 class UploadTest extends PHPUnit_Framework_TestCase {
6 protected $upload;
7
8
9 function setUp() {
10 global $wgContLang;
11 parent::setUp();
12 $wgContLang = Language::factory( 'en' );
13 $this->upload = new UploadTestHandler;
14 }
15
16 /**
17 * Test various forms of valid and invalid titles that can be supplied.
18 */
19 public function testTitleValidation() {
20
21
22 /* Test a valid title */
23 $this->assertUploadTitleAndCode( 'ValidTitle.jpg',
24 'ValidTitle.jpg', UploadTestHandler::OK,
25 'upload valid title' );
26
27 /* A title with a slash */
28 $this->assertUploadTitleAndCode( 'A/B.jpg',
29 'B.jpg', UploadTestHandler::OK,
30 'upload title with slash' );
31
32 /* A title with illegal char */
33 $this->assertUploadTitleAndCode( 'A:B.jpg',
34 'A-B.jpg', UploadTestHandler::OK,
35 'upload title with colon' );
36
37 /* A title without extension */
38 $this->assertUploadTitleAndCode( 'A',
39 null, UploadTestHandler::FILETYPE_MISSING,
40 'upload title without extension' );
41
42 /* A title with no basename */
43 $this->assertUploadTitleAndCode( '.jpg',
44 null, UploadTestHandler::MIN_LENGTH_PARTNAME,
45 'upload title without basename' );
46
47 }
48 /**
49 * Helper function for testTitleValidation. First checks the return code
50 * of UploadBase::getTitle() and then the actual returned titl
51 */
52 private function assertUploadTitleAndCode( $srcFilename, $dstFilename, $code, $msg ) {
53 /* Check the result code */
54 $this->assertEquals( $code,
55 $this->upload->testTitleValidation( $srcFilename ),
56 "$msg code" );
57
58 /* If we expect a valid title, check the title itself. */
59 if ( $code == UploadTestHandler::OK ) {
60 $this->assertEquals( $dstFilename,
61 $this->upload->getTitle()->getText(),
62 "$msg text" );
63 }
64 }
65
66 /**
67 * Test the upload verification functions
68 */
69 public function testVerifyUpload() {
70 /* Setup with zero file size */
71 $this->upload->initializePathInfo( '', '', 0 );
72 $result = $this->upload->verifyUpload();
73 $this->assertEquals( UploadTestHandler::EMPTY_FILE,
74 $result['status'],
75 'upload empty file' );
76 }
77
78 }
79
80 class UploadTestHandler extends UploadBase {
81 public function initializeFromRequest( &$request ) { }
82 public function testTitleValidation( $name ) {
83 $this->mTitle = false;
84 $this->mDesiredDestName = $name;
85 $this->getTitle();
86 return $this->mTitleError;
87 }
88
89
90 }