File: UploadFromChunksTest.php

package info (click to toggle)
mediawiki 1%3A1.43.3%2Bdfsg-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 417,464 kB
  • sloc: php: 1,062,949; javascript: 664,290; sql: 9,714; python: 5,458; xml: 3,489; sh: 1,131; makefile: 64
file content (108 lines) | stat: -rw-r--r-- 2,769 bytes parent folder | download
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
<?php

use MediaWiki\Request\FauxRequest;

/**
 * @group Database
 *
 * @covers \UploadFromChunks
 */
class UploadFromChunksTest extends MediaWikiIntegrationTestCase {

	public function testUploadWithTwoChunks() {
		$user = $this->getTestUser()->getUser();
		$filepath = __DIR__ . '/../../data/media/1bit-png.png';
		$filename = 'TestChunked.png';
		$filecontent = file_get_contents( $filepath );
		$offset = 0;

		// copy the chunk data to temp location
		$chunkPath = $this->getNewTempFile();
		$chunkSize = 100;
		file_put_contents( $chunkPath, substr( $filecontent, $offset, $chunkSize ) );

		$request = new FauxRequest( [], true );
		$request->setUpload( 'chunk', [
			'name' => $filename,
			'type' => 'image/png',
			'tmp_name' => $chunkPath,
			'size' => $chunkSize,
			'error' => '',
		] );

		$upload = new UploadFromChunks( $user );
		// handle first chunk
		$upload->initialize(
			$filename,
			$request->getUpload( 'chunk' )
		);
		$status = $upload->tryStashFile( $user, true );
		$this->assertStatusGood( $status );
		$stashFile = $status->getValue();
		$filekey = $stashFile->getFileKey();

		// handle new chunk
		$offset += $chunkSize;
		$chunkPath = $this->getNewTempFile();
		$chunkSize = strlen( $filecontent ) - $offset;
		file_put_contents( $chunkPath, substr( $filecontent, $offset, $chunkSize ) );

		$request->setUpload( 'chunk', [
			'name' => $filename,
			'type' => 'image/png',
			'tmp_name' => $chunkPath,
			'size' => $chunkSize,
			'error' => '',
		] );

		$upload->continueChunks(
			$filename,
			$filekey,
			$request->getUpload( 'chunk' )
		);

		$status = $upload->addChunk(
			$chunkPath,
			$chunkSize,
			$offset
		);
		$this->assertStatusGood( $status );

		$status = $upload->concatenateChunks();
		$this->assertStatusGood( $status );

		$newFilekey = $upload->getStashFile()->getFileKey();
		$this->assertNotSame( $filekey, $newFilekey );
	}

	public function testUploadWholeFileInOneChunk() {
		$user = $this->getTestUser()->getUser();
		$filepath = __DIR__ . '/../../data/media/1bit-png.png';
		$filename = 'TestOnlyOneChunk.png';

		$request = new FauxRequest( [], true );
		$request->setUpload( 'chunk', [
			'name' => $filename,
			'type' => 'image/png',
			'tmp_name' => $filepath,
			'size' => filesize( $filepath ),
			'error' => '',
		] );

		$upload = new UploadFromChunks( $user );
		$upload->initialize(
			$filename,
			$request->getUpload( 'chunk' )
		);
		$status = $upload->tryStashFile( $user, true );
		$this->assertStatusGood( $status );
		$stashFile = $status->getValue();
		$filekey = $stashFile->getFileKey();

		$status = $upload->concatenateChunks();
		$this->assertStatusGood( $status );

		$newFilekey = $upload->getStashFile()->getFileKey();
		$this->assertNotSame( $filekey, $newFilekey );
	}
}