| 1 |
<?php |
| 2 |
|
| 3 |
use Wikimedia\FileBackend\FileBackend; |
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
class ThumbnailImageTest extends MediaWikiMediaTestCase { |
| 13 |
|
| 14 |
private function newFile( $name = 'Test.jpg' ) { |
| 15 |
return $this->dataFile( $name ); |
| 16 |
} |
| 17 |
|
| 18 |
private function newThumbnail( $file = null, $url = null, $path = false, $parameters = [] ) { |
| 19 |
$file ??= $this->newFile(); |
| 20 |
$path ??= 'thumb/a/ab/Test.jpg/123px-Test.jpg'; |
| 21 |
$url ??= "https://example.com/w/images/$path"; |
| 22 |
|
| 23 |
$parameters += [ |
| 24 |
'width' => 200, |
| 25 |
'height' => 100, |
| 26 |
]; |
| 27 |
|
| 28 |
return new ThumbnailImage( $file, $url, $path, $parameters ); |
| 29 |
} |
| 30 |
|
| 31 |
public function testConstructor() { |
| 32 |
$file = $this->newFile(); |
| 33 |
$path = 'thumb/a/ab/Test.jpg/123px-Test.jpg'; |
| 34 |
$url = "https://example.com/w/images/$path"; |
| 35 |
|
| 36 |
$parameters = [ |
| 37 |
'width' => 300, |
| 38 |
'height' => 200, |
| 39 |
]; |
| 40 |
|
| 41 |
$thumbnail = $this->newThumbnail( |
| 42 |
$file, |
| 43 |
$url, |
| 44 |
$path, |
| 45 |
$parameters |
| 46 |
); |
| 47 |
|
| 48 |
$this->assertSame( $file, $thumbnail->getFile() ); |
| 49 |
$this->assertSame( $url, $thumbnail->getUrl() ); |
| 50 |
$this->assertSame( $parameters['width'], $thumbnail->getWidth() ); |
| 51 |
$this->assertSame( $parameters['height'], $thumbnail->getHeight() ); |
| 52 |
$this->assertFalse( $thumbnail->isError() ); |
| 53 |
} |
| 54 |
|
| 55 |
|
| 56 |
|
| 57 |
|
| 58 |
public function testStreamFileWithStatus_fsPath() { |
| 59 |
$fsPath = $this->getFilePath() . 'test.jpg'; |
| 60 |
$data = file_get_contents( $fsPath ); |
| 61 |
$file = $this->newFile(); |
| 62 |
|
| 63 |
|
| 64 |
|
| 65 |
$thumbnail = $this->newThumbnail( $file, null, $fsPath ); |
| 66 |
|
| 67 |
ob_start(); |
| 68 |
$thumbnail->streamFileWithStatus(); |
| 69 |
$output = ob_get_clean(); |
| 70 |
|
| 71 |
$this->assertSame( $data, $output ); |
| 72 |
} |
| 73 |
|
| 74 |
|
| 75 |
|
| 76 |
|
| 77 |
public function testStreamFileWithStatus_thumbStoragePath() { |
| 78 |
$this->backend = $this->createNoOpMock( FileBackend::class, [ 'streamFile' ] ); |
| 79 |
|
| 80 |
$this->backend->expects( $this->once() ) |
| 81 |
->method( 'streamFile' ) |
| 82 |
->wilLreturn( StatusValue::newGood() ); |
| 83 |
|
| 84 |
$this->repo = new FileRepo( $this->getRepoOptions() ); |
| 85 |
|
| 86 |
$file = $this->newFile( 'test.jpg' ); |
| 87 |
$thumbnail = $this->newThumbnail( |
| 88 |
$file, |
| 89 |
$file->getThumbUrl(), |
| 90 |
$file->getThumbPath() |
| 91 |
); |
| 92 |
|
| 93 |
$thumbnail->streamFileWithStatus(); |
| 94 |
|
| 95 |
|
| 96 |
} |
| 97 |
|
| 98 |
|
| 99 |
|
| 100 |
|
| 101 |
public function testStreamFileWithStatus_UnregisteredLocalFile() { |
| 102 |
|
| 103 |
|
| 104 |
|
| 105 |
|
| 106 |
$fsPath = $this->getFilePath() . 'this does not exist'; |
| 107 |
|
| 108 |
|
| 109 |
$file = new UnregisteredLocalFile( false, false, $fsPath ); |
| 110 |
$thumbnail = $this->newThumbnail( $file ); |
| 111 |
|
| 112 |
|
| 113 |
$status = $thumbnail->streamFileWithStatus(); |
| 114 |
$this->assertStatusError( 'backend-fail-stream', $status ); |
| 115 |
} |
| 116 |
|
| 117 |
} |