summaryrefslogtreecommitdiff
path: root/extensions/TimedMediaHandler/tests/phpunit/ApiTestCaseVideoUpload.php
blob: 9dd4cb52d51770b543df3e2396d6772e1f52f93d (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
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
<?php
/**
 * Abstract test class to support Video Tests with video uploads
 * @author dale
 */

if ( !defined( 'MEDIAWIKI' ) ) {
	exit;
}

// Include core class ApiTestCaseUpload ( not part of base autoLoader )
global $IP;
require_once( "$IP/tests/phpunit/includes/api/ApiTestCaseUpload.php" );

abstract class ApiTestCaseVideoUpload extends ApiTestCaseUpload {
	/**
	 * @return Array set of test files with associated metadata
	 */
	static function mediaFilesProvider(){
		return array(
			array(
				// Double wrap the file array to match phpunit data provider conventions
				array(
					'mime' => 'application/ogg',
					'filePath' => dirname( __FILE__ ) . '/media/test5seconds.electricsheep.300x400.ogv',
					"size" => 301477,
					"width"  => 400,
					"height" => 300,
					"mediatype" => "VIDEO",
					"bandwidth" => 452216,
					"framerate" => 30
				)
			),
			array(
				array(
					'mime' => 'video/webm',
					'filePath' => dirname( __FILE__ ) . '/media/shuttle10seconds.1080x608.webm',
					"size" => 699018,
					"width" => 1080,
					"height" => 608,
					"mediatype" => "VIDEO",
					"bandwidth" => 522142,
					"framerate" => 29.97
				)
			)
		);
	}
	/**
	 * Fixture -- run after every test
	 * Clean up temporary files etc.
	 *
	*/
	protected function tearDown() {
		parent::tearDown();

		$testMediaFiles = $this->mediaFilesProvider();
		foreach( $testMediaFiles as $file ){
			$file = $file[0];
			// Clean up and delete all files
			$this->deleteFileByFilename( $file['filePath'] );
		}
	}

	/**
	 * Do login
	 */
	protected function doLogin( $user = 'sysop' ) {
		$user = self::$users['uploader'];

		$params = array(
			'action' => 'login',
			'lgname' => $user->username,
			'lgpassword' => $user->password
		);
		list( $result, , $session ) = $this->doApiRequest( $params );
		$token = $result['login']['token'];

		$params = array(
			'action' => 'login',
			'lgtoken' => $token,
			'lgname' => $user->username,
			'lgpassword' => $user->password
		);
		list( $result, , $session ) = $this->doApiRequest( $params, $session );
		return $session;
	}

	/**
	 * uploads a file:
	 */
	public function uploadFile( $file ){
		global $wgUser;
		// get a session object
		$session = $this->doLogin();
		// Update the global user:
		$wgUser = self::$users['uploader']->getUser();

		// Upload the media file:
		$fileName = basename( $file['filePath'] );

		// remove if already in thd db:
		$this->deleteFileByFileName( $fileName );
		$this->deleteFileByContent( $file['filePath'] );

		if ( !$this->fakeUploadFile( 'file', $fileName, $file['mime'], $file['filePath'] ) ) {
			$this->markTestIncomplete( "Couldn't upload file!\n" );
		}

		$params = array(
			'action' => 'upload',
			'filename' => $fileName,
			'file' => 'dummy content',
			'comment' => 'dummy comment',
			'text'	=> "This is the page text for $fileName",
			// This uploadFile function supports video tests not a test upload warnings
			'ignorewarnings' => true
		);

		try{
			list( $result, , ) = $this->doApiRequestWithToken( $params, $session );
		} catch( Exception $e ) {
			// Could not upload mark test that called uploadFile as incomplete
			$this->markTestIncomplete( $e->getMessage() );
		}

		return $result;

	}

}