Debian Codegraph @

tests/phpunit/includes/upload/UploadFromChunksTest.php

raw symbols SCIP
1 <?php
2
3 use MediaWiki\Request\FauxRequest;
4
5 /**
6 * @group Database
7 *
8 * @covers \UploadFromChunks
9 */
10 class UploadFromChunksTest extends MediaWikiIntegrationTestCase {
11
12 public function testUploadWithTwoChunks() {
13 $user = $this->getTestUser()->getUser();
14 $filepath = __DIR__ . '/../../data/media/1bit-png.png';
15 $filename = 'TestChunked.png';
16 $filecontent = file_get_contents( $filepath );
17 $offset = 0;
18
19 // copy the chunk data to temp location
20 $chunkPath = $this->getNewTempFile();
21 $chunkSize = 100;
22 file_put_contents( $chunkPath, substr( $filecontent, $offset, $chunkSize ) );
23
24 $request = new FauxRequest( [], true );
25 $request->setUpload( 'chunk', [
26 'name' => $filename,
27 'type' => 'image/png',
28 'tmp_name' => $chunkPath,
29 'size' => $chunkSize,
30 'error' => '',
31 ] );
32
33 $upload = new UploadFromChunks( $user );
34 // handle first chunk
35 $upload->initialize(
36 $filename,
37 $request->getUpload( 'chunk' )
38 );
39 $status = $upload->tryStashFile( $user, true );
40 $this->assertStatusGood( $status );
41 $stashFile = $status->getValue();
42 $filekey = $stashFile->getFileKey();
43
44 // handle new chunk
45 $offset += $chunkSize;
46 $chunkPath = $this->getNewTempFile();
47 $chunkSize = strlen( $filecontent ) - $offset;
48 file_put_contents( $chunkPath, substr( $filecontent, $offset, $chunkSize ) );
49
50 $request->setUpload( 'chunk', [
51 'name' => $filename,
52 'type' => 'image/png',
53 'tmp_name' => $chunkPath,
54 'size' => $chunkSize,
55 'error' => '',
56 ] );
57
58 $upload->continueChunks(
59 $filename,
60 $filekey,
61 $request->getUpload( 'chunk' )
62 );
63
64 $status = $upload->addChunk(
65 $chunkPath,
66 $chunkSize,
67 $offset
68 );
69 $this->assertStatusGood( $status );
70
71 $status = $upload->concatenateChunks();
72 $this->assertStatusGood( $status );
73
74 $newFilekey = $upload->getStashFile()->getFileKey();
75 $this->assertNotSame( $filekey, $newFilekey );
76 }
77
78 public function testUploadWholeFileInOneChunk() {
79 $user = $this->getTestUser()->getUser();
80 $filepath = __DIR__ . '/../../data/media/1bit-png.png';
81 $filename = 'TestOnlyOneChunk.png';
82
83 $request = new FauxRequest( [], true );
84 $request->setUpload( 'chunk', [
85 'name' => $filename,
86 'type' => 'image/png',
87 'tmp_name' => $filepath,
88 'size' => filesize( $filepath ),
89 'error' => '',
90 ] );
91
92 $upload = new UploadFromChunks( $user );
93 $upload->initialize(
94 $filename,
95 $request->getUpload( 'chunk' )
96 );
97 $status = $upload->tryStashFile( $user, true );
98 $this->assertStatusGood( $status );
99 $stashFile = $status->getValue();
100 $filekey = $stashFile->getFileKey();
101
102 $status = $upload->concatenateChunks();
103 $this->assertStatusGood( $status );
104
105 $newFilekey = $upload->getStashFile()->getFileKey();
106 $this->assertNotSame( $filekey, $newFilekey );
107 }
108 }