| 1 |
<?php |
| 2 |
|
| 3 |
namespace MediaWiki\Tests\Rest\Handler; |
| 4 |
|
| 5 |
use LocalFile; |
| 6 |
use LocalRepo; |
| 7 |
use MediaWiki\Page\PageReference; |
| 8 |
use MediaWiki\User\UserIdentityValue; |
| 9 |
use MockTitleTrait; |
| 10 |
use PHPUnit\Framework\MockObject\MockObject; |
| 11 |
use RepoGroup; |
| 12 |
use ThumbnailImage; |
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
trait MediaTestTrait { |
| 19 |
|
| 20 |
use HandlerTestTrait; |
| 21 |
use MockTitleTrait; |
| 22 |
|
| 23 |
|
| 24 |
|
| 25 |
|
| 26 |
|
| 27 |
|
| 28 |
private function makeMissingMockFile( $title ) { |
| 29 |
$title = $title instanceof PageReference |
| 30 |
? $this->makeMockTitle( $title->getDBkey(), [ 'namespace' => $title->getNamespace() ] ) |
| 31 |
: $this->makeMockTitle( 'File:' . $title, [ 'namespace' => NS_FILE ] ); |
| 32 |
|
| 33 |
|
| 34 |
$file = $this->createNoOpMock( |
| 35 |
LocalFile::class, |
| 36 |
[ 'getTitle', 'exists', 'getDescriptionUrl', 'load' ] |
| 37 |
); |
| 38 |
$file->method( 'getTitle' )->willReturn( $title ); |
| 39 |
$file->method( 'exists' )->willReturn( false ); |
| 40 |
$file->method( 'getDescriptionUrl' )->willReturn( |
| 41 |
'https://example.com/wiki/' . $title->getPrefixedDBkey() |
| 42 |
); |
| 43 |
|
| 44 |
return $file; |
| 45 |
} |
| 46 |
|
| 47 |
|
| 48 |
|
| 49 |
|
| 50 |
|
| 51 |
|
| 52 |
private function makeMockFile( $title ) { |
| 53 |
$title = $title instanceof PageReference |
| 54 |
? $this->makeMockTitle( $title->getDBkey(), [ 'namespace' => $title->getNamespace() ] ) |
| 55 |
: $this->makeMockTitle( 'File:' . $title, [ 'namespace' => NS_FILE ] ); |
| 56 |
|
| 57 |
|
| 58 |
$file = $this->createNoOpMock( |
| 59 |
LocalFile::class, |
| 60 |
[ 'getName', 'getTitle', 'getDescriptionUrl', 'exists', 'userCan', 'getUploader', 'getTimestamp', |
| 61 |
'getMediaType', 'getSize', 'getHeight', 'getWidth', 'getDisplayWidthHeight', |
| 62 |
'getLength', 'getUrl', 'allowInlineDisplay', 'transform', 'getSha1', 'load', 'getMimeType' ] |
| 63 |
); |
| 64 |
$file->method( 'getName' )->willReturn( ucfirst( $title->getDBkey() ) ); |
| 65 |
$file->method( 'getTitle' )->willReturn( $title ); |
| 66 |
$file->method( 'exists' )->willReturn( true ); |
| 67 |
$file->method( 'userCan' )->willReturn( true ); |
| 68 |
$file->method( 'getUploader' ) |
| 69 |
->willReturn( UserIdentityValue::newRegistered( 7, 'Alice' ) ); |
| 70 |
$file->method( 'getTimestamp' )->willReturn( '20200102030405' ); |
| 71 |
$file->method( 'getMediaType' )->willReturn( 'test' ); |
| 72 |
$file->method( 'getSize' )->willReturn( 12345 ); |
| 73 |
$file->method( 'getHeight' )->willReturn( 400 ); |
| 74 |
$file->method( 'getWidth' )->willReturn( 600 ); |
| 75 |
$file->method( 'getLength' )->willReturn( 678 ); |
| 76 |
$file->method( 'getSha1' )->willReturn( 'DEADBEEF' ); |
| 77 |
$file->method( 'allowInlineDisplay' )->willReturn( true ); |
| 78 |
$file->method( 'getUrl' )->willReturn( |
| 79 |
'https://media.example.com/static/' . $title->getDBkey() |
| 80 |
); |
| 81 |
$file->method( 'getDescriptionUrl' )->willReturn( |
| 82 |
'https://example.com/wiki/' . $title->getPrefixedDBkey() |
| 83 |
); |
| 84 |
$file->method( 'getMimeType' )->willReturn( 'image/jpeg' ); |
| 85 |
|
| 86 |
$getDisplayWidthHeight = static function ( $maxWidth, $maxHeight, $page = 1 ) use ( $file ) { |
| 87 |
$width = $file->getWidth( $page ); |
| 88 |
$height = $file->getHeight( $page ); |
| 89 |
$ratio = $width / $height; |
| 90 |
if ( $width / $maxWidth > $height / $maxHeight ) { |
| 91 |
return [ $maxWidth, round( $maxWidth / $ratio ) ]; |
| 92 |
} |
| 93 |
return [ round( $maxHeight * $ratio ), $maxHeight ]; |
| 94 |
}; |
| 95 |
$file->method( 'getDisplayWidthHeight' )->willReturnCallback( $getDisplayWidthHeight ); |
| 96 |
|
| 97 |
$transform = static function ( $params ) use ( $file, $title ) { |
| 98 |
return new ThumbnailImage( |
| 99 |
$file, |
| 100 |
'https://media.example.com/static/thumb/' . $title->getDBkey(), |
| 101 |
false, |
| 102 |
[ |
| 103 |
'width' => $params['width'] ?? 64, |
| 104 |
'height' => $params['height'] ?? 64, |
| 105 |
'page' => $params['page'] ?? false |
| 106 |
] |
| 107 |
); |
| 108 |
}; |
| 109 |
$file->method( 'transform' )->willReturnCallback( $transform ); |
| 110 |
|
| 111 |
return $file; |
| 112 |
} |
| 113 |
|
| 114 |
|
| 115 |
|
| 116 |
|
| 117 |
|
| 118 |
private function makeMockRepoGroup( array $existingFileDBKeys ) { |
| 119 |
$findFile = function ( $title ) use ( $existingFileDBKeys ) { |
| 120 |
$title = $title instanceof PageReference |
| 121 |
? $this->makeMockTitle( $title->getDBkey(), [ 'namespace' => $title->getNamespace() ] ) |
| 122 |
: $this->makeMockTitle( 'File:' . $title, [ 'namespace' => NS_FILE ] ); |
| 123 |
|
| 124 |
if ( !in_array( $title->getDBkey(), $existingFileDBKeys ) || !$title->inNamespace( NS_FILE ) ) { |
| 125 |
return $this->makeMissingMockFile( $title ); |
| 126 |
} else { |
| 127 |
return $this->makeMockFile( $title ); |
| 128 |
} |
| 129 |
}; |
| 130 |
|
| 131 |
$findFiles = static function ( array $inputItems ) use ( $findFile ) { |
| 132 |
$files = []; |
| 133 |
foreach ( $inputItems as $item ) { |
| 134 |
$files[] = ( $findFile )( $item['title'] ); |
| 135 |
} |
| 136 |
|
| 137 |
return $files; |
| 138 |
}; |
| 139 |
|
| 140 |
$mockLocalRepo = $this->createMock( LocalRepo::class ); |
| 141 |
$mockLocalRepo->method( 'newFile' )->willReturnCallback( $findFile ); |
| 142 |
|
| 143 |
|
| 144 |
$repoGroup = $this->createNoOpMock( RepoGroup::class, [ 'findFiles', 'findFile', 'getLocalRepo' ] ); |
| 145 |
$repoGroup->method( 'findFile' )->willReturnCallback( $findFile ); |
| 146 |
$repoGroup->method( 'findFiles' )->willReturnCallback( $findFiles ); |
| 147 |
$repoGroup->method( 'getLocalRepo' )->willReturn( $mockLocalRepo ); |
| 148 |
|
| 149 |
return $repoGroup; |
| 150 |
} |
| 151 |
|
| 152 |
} |