summaryrefslogtreecommitdiff
path: root/bin/reevotech/vendor/addwiki/mediawiki-api/tests/integration/FileUploaderTest.php
blob: e805ee1db9874e318b15093b55324bb3d1fb888f (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
<?php

namespace Mediawiki\Api\Test;

use Mediawiki\Api\ApiUser;
use Mediawiki\Api\MediawikiApi;
use Mediawiki\Api\MediawikiFactory;
use Mediawiki\Api\Service\FileUploader;
use Mediawiki\Api\SimpleRequest;
use Mediawiki\DataModel\Page;
use Mediawiki\DataModel\PageIdentifier;
use Mediawiki\DataModel\Title;
use PHPUnit_Framework_TestCase;

/**
 * Test the \Mediawiki\Api\Service\FileUploader class.
 */
class FileUploaderTest extends PHPUnit_Framework_TestCase {

	/** @var MediawikiFactory */
	protected $factory;

	/** @var FileUploader */
	protected $fileUploader;

	/**
	 * Create a FileUploader to use in all these tests.
	 */
	public function setup() {
		parent::setup();
		$testEnvironment = TestEnvironment::newDefault();
		$this->factory = $testEnvironment->getFactory();
		$this->fileUploader = $this->factory->newFileUploader();

		// Log in as the sysop user. These credentials are referenced in docs/contributing.rst.
		$localApiUser = new ApiUser( 'admin', 'admin123' );
		$api = $testEnvironment->getApi();
		$api->login( $localApiUser );
	}

	public function testUpload() {
		$testPagename = uniqid( 'file-uploader-test-' ) . '.png';
		$testTitle = new Title( 'File:'.$testPagename );

		// Check that the file doesn't exist yet.
		$testFile = $this->factory->newPageGetter()->getFromTitle( $testTitle );
		$this->assertEquals( 0, $testFile->getPageIdentifier()->getId() );

		// Upload a file.
		$testFilename = dirname( __DIR__ ) . '/fixtures/blue ℳ𝒲β™₯π“Šπ“ƒπ’Ύπ’Έβ„΄π’Ήβ„―.png';
		$uploaded = $this->fileUploader->upload( $testPagename, $testFilename, 'Testing',
			null, null, true );
		$this->assertTrue( $uploaded );

		// Get the file again, and check that it exists this time.
		$testFile2 = $this->factory->newPageGetter()->getFromTitle( $testTitle );
		$this->assertGreaterThan( 0, $testFile2->getPageIdentifier()->getId() );
	}

	public function testUploadByChunks() {
		$testPagename = uniqid( 'file-uploader-test-' ) . '.png';
		$testTitle = new Title( 'File:'.$testPagename );

		// Upload a 83725 byte file in 10k chunks.
		$testFilename = dirname( __DIR__ ) . '/fixtures/blue ℳ𝒲β™₯π“Šπ“ƒπ’Ύπ’Έβ„΄π’Ήβ„―.png';
		$this->fileUploader->setChunkSize( 1024 * 10 );
		$uploaded = $this->fileUploader->upload( $testPagename, $testFilename, 'Testing',
			null, null, true );
		$this->assertTrue( $uploaded );

		// Get the file again, and check that it exists this time.
		$testFile2 = $this->factory->newPageGetter()->getFromTitle( $testTitle );
		$this->assertGreaterThan( 0, $testFile2->getPageIdentifier()->getId() );
	}
}