sonata-project /
SonataMediaBundle
This project does not seem to handle request data directly as such no vulnerable execution paths were found.
include, or for example
via PHP's auto-loading mechanism.
These results are based on our legacy PHP analysis, consider migrating to our new PHP analysis engine instead. Learn more
| 1 | <?php |
||
| 2 | |||
| 3 | declare(strict_types=1); |
||
| 4 | |||
| 5 | /* |
||
| 6 | * This file is part of the Sonata Project package. |
||
| 7 | * |
||
| 8 | * (c) Thomas Rabaix <[email protected]> |
||
| 9 | * |
||
| 10 | * For the full copyright and license information, please view the LICENSE |
||
| 11 | * file that was distributed with this source code. |
||
| 12 | */ |
||
| 13 | |||
| 14 | namespace Sonata\MediaBundle\Tests\Provider; |
||
| 15 | |||
| 16 | use Buzz\Browser; |
||
| 17 | use Buzz\Message\AbstractMessage; |
||
| 18 | use Buzz\Message\Response; |
||
| 19 | use Gaufrette\Adapter; |
||
| 20 | use Gaufrette\File; |
||
| 21 | use Gaufrette\Filesystem; |
||
| 22 | use Imagine\Image\Box; |
||
| 23 | use Sonata\AdminBundle\Admin\AdminInterface; |
||
| 24 | use Sonata\AdminBundle\Form\FormMapper; |
||
| 25 | use Sonata\MediaBundle\CDN\Server; |
||
| 26 | use Sonata\MediaBundle\Generator\IdGenerator; |
||
| 27 | use Sonata\MediaBundle\Metadata\MetadataBuilderInterface; |
||
| 28 | use Sonata\MediaBundle\Provider\MediaProviderInterface; |
||
| 29 | use Sonata\MediaBundle\Provider\VimeoProvider; |
||
| 30 | use Sonata\MediaBundle\Resizer\ResizerInterface; |
||
| 31 | use Sonata\MediaBundle\Tests\Entity\Media; |
||
| 32 | use Sonata\MediaBundle\Thumbnail\FormatThumbnail; |
||
| 33 | |||
| 34 | class VimeoProviderTest extends AbstractProviderTest |
||
| 35 | { |
||
| 36 | public function getProvider(?Browser $browser = null): MediaProviderInterface |
||
| 37 | { |
||
| 38 | if (!$browser) { |
||
| 39 | $browser = $this->createMock(Browser::class); |
||
| 40 | } |
||
| 41 | |||
| 42 | $resizer = $this->createMock(ResizerInterface::class); |
||
| 43 | $resizer->method('resize')->willReturn(true); |
||
| 44 | $resizer->method('getBox')->willReturn(new Box(100, 100)); |
||
| 45 | |||
| 46 | $adapter = $this->createMock(Adapter::class); |
||
| 47 | |||
| 48 | $filesystem = $this->getMockBuilder(Filesystem::class) |
||
| 49 | ->onlyMethods(['get']) |
||
| 50 | ->setConstructorArgs([$adapter]) |
||
| 51 | ->getMock(); |
||
| 52 | $file = $this->getMockBuilder(File::class) |
||
| 53 | ->setConstructorArgs(['foo', $filesystem]) |
||
| 54 | ->getMock(); |
||
| 55 | $filesystem->method('get')->willReturn($file); |
||
| 56 | |||
| 57 | $cdn = new Server('/uploads/media'); |
||
| 58 | |||
| 59 | $generator = new IdGenerator(); |
||
| 60 | |||
| 61 | $thumbnail = new FormatThumbnail('jpg'); |
||
| 62 | |||
| 63 | $metadata = $this->createMock(MetadataBuilderInterface::class); |
||
| 64 | |||
| 65 | $provider = new VimeoProvider('vimeo', $filesystem, $cdn, $generator, $thumbnail, $browser, $metadata); |
||
| 66 | $provider->setResizer($resizer); |
||
| 67 | |||
| 68 | return $provider; |
||
| 69 | } |
||
| 70 | |||
| 71 | public function testProvider(): void |
||
| 72 | { |
||
| 73 | $provider = $this->getProvider(); |
||
| 74 | |||
| 75 | $media = new Media(); |
||
| 76 | $media->setName('Blinkyâ„¢'); |
||
| 77 | $media->setProviderName('vimeo'); |
||
| 78 | $media->setProviderReference('21216091'); |
||
| 79 | $media->setContext('default'); |
||
| 80 | $media->setProviderMetadata(json_decode('{"type":"video","version":"1.0","provider_name":"Vimeo","provider_url":"http:\/\/vimeo.com\/","title":"Blinky\u2122","author_name":"Ruairi Robinson","author_url":"http:\/\/vimeo.com\/ruairirobinson","is_plus":"1","html":"<iframe src=\"http:\/\/player.vimeo.com\/video\/21216091\" width=\"1920\" height=\"1080\" frameborder=\"0\"><\/iframe>","width":"1920","height":"1080","duration":"771","description":"","thumbnail_url":"http:\/\/b.vimeocdn.com\/ts\/136\/375\/136375440_1280.jpg","thumbnail_width":1280,"thumbnail_height":720,"video_id":"21216091"}', true)); |
||
| 81 | |||
| 82 | $media->setId(1023457); |
||
| 83 | $this->assertSame('http://b.vimeocdn.com/ts/136/375/136375440_1280.jpg', $provider->getReferenceImage($media)); |
||
| 84 | |||
| 85 | $this->assertSame('default/0011/24', $provider->generatePath($media)); |
||
| 86 | $this->assertSame('/uploads/media/default/0011/24/thumb_1023457_big.jpg', $provider->generatePublicUrl($media, 'big')); |
||
| 87 | } |
||
| 88 | |||
| 89 | public function testThumbnail(): void |
||
| 90 | { |
||
| 91 | $response = $this->createMock(AbstractMessage::class); |
||
| 92 | $response->expects($this->once())->method('getContent')->willReturn('content'); |
||
| 93 | |||
| 94 | $browser = $this->createMock(Browser::class); |
||
| 95 | |||
| 96 | $browser->expects($this->once())->method('get')->willReturn($response); |
||
| 97 | |||
| 98 | $provider = $this->getProvider($browser); |
||
| 99 | |||
| 100 | $media = new Media(); |
||
| 101 | $media->setName('Blinkyâ„¢'); |
||
| 102 | $media->setProviderName('vimeo'); |
||
| 103 | $media->setProviderReference('21216091'); |
||
| 104 | $media->setContext('default'); |
||
| 105 | $media->setProviderMetadata(json_decode('{"type":"video","version":"1.0","provider_name":"Vimeo","provider_url":"http:\/\/vimeo.com\/","title":"Blinky\u2122","author_name":"Ruairi Robinson","author_url":"http:\/\/vimeo.com\/ruairirobinson","is_plus":"1","html":"<iframe src=\"http:\/\/player.vimeo.com\/video\/21216091\" width=\"1920\" height=\"1080\" frameborder=\"0\"><\/iframe>","width":"1920","height":"1080","duration":"771","description":"","thumbnail_url":"http:\/\/b.vimeocdn.com\/ts\/136\/375\/136375440_1280.jpg","thumbnail_width":1280,"thumbnail_height":720,"video_id":"21216091"}', true)); |
||
| 106 | |||
| 107 | $media->setId(1023457); |
||
| 108 | |||
| 109 | $this->assertTrue($provider->requireThumbnails()); |
||
| 110 | |||
| 111 | $provider->addFormat('big', ['width' => 200, 'height' => 100, 'constraint' => true]); |
||
| 112 | |||
| 113 | $this->assertNotEmpty($provider->getFormats(), '::getFormats() return an array'); |
||
| 114 | |||
| 115 | $provider->generateThumbnails($media); |
||
| 116 | |||
| 117 | $this->assertSame('default/0011/24/thumb_1023457_big.jpg', $provider->generatePrivateUrl($media, 'big')); |
||
| 118 | } |
||
| 119 | |||
| 120 | public function testTransformWithSig(): void |
||
| 121 | { |
||
| 122 | $response = new Response(); |
||
| 123 | $response->setContent(file_get_contents(__DIR__.'/../fixtures/valid_vimeo.txt')); |
||
| 124 | |||
| 125 | $browser = $this->createMock(Browser::class); |
||
| 126 | $browser->expects($this->once())->method('get')->willReturn($response); |
||
| 127 | |||
| 128 | $provider = $this->getProvider($browser); |
||
| 129 | |||
| 130 | $provider->addFormat('big', ['width' => 200, 'height' => 100, 'constraint' => true]); |
||
| 131 | |||
| 132 | $media = new Media(); |
||
| 133 | $media->setContext('default'); |
||
| 134 | $media->setBinaryContent('BDYAbAtaDzA'); |
||
| 135 | $media->setId(1023456); |
||
| 136 | |||
| 137 | // pre persist the media |
||
| 138 | $provider->transform($media); |
||
| 139 | $provider->prePersist($media); |
||
| 140 | |||
| 141 | $this->assertSame('Blinkyâ„¢', $media->getName(), '::getName() return the file name'); |
||
| 142 | $this->assertSame('BDYAbAtaDzA', $media->getProviderReference(), '::getProviderReference() is set'); |
||
| 143 | } |
||
| 144 | |||
| 145 | /** |
||
| 146 | * @dataProvider getTransformWithUrlMedia |
||
| 147 | */ |
||
| 148 | public function testTransformWithUrl(Media $media): void |
||
| 149 | { |
||
| 150 | $response = new Response(); |
||
| 151 | $response->setContent(file_get_contents(__DIR__.'/../fixtures/valid_vimeo.txt')); |
||
| 152 | |||
| 153 | $browser = $this->createMock(Browser::class); |
||
| 154 | $browser->expects($this->once())->method('get')->willReturn($response); |
||
| 155 | |||
| 156 | $provider = $this->getProvider($browser); |
||
| 157 | |||
| 158 | $provider->addFormat('big', ['width' => 200, 'height' => 100, 'constraint' => true]); |
||
| 159 | |||
| 160 | // pre persist the media |
||
| 161 | $provider->transform($media); |
||
| 162 | $provider->prePersist($media); |
||
| 163 | |||
| 164 | $this->assertSame('Blinkyâ„¢', $media->getName(), '::getName() return the file name'); |
||
| 165 | $this->assertSame('012341231', $media->getProviderReference(), '::getProviderReference() is set'); |
||
| 166 | } |
||
| 167 | |||
| 168 | public function getTransformWithUrlMedia(): array |
||
| 169 | { |
||
| 170 | $mediaWebsite = new Media(); |
||
| 171 | $mediaWebsite->setContext('default'); |
||
| 172 | $mediaWebsite->setBinaryContent('https://vimeo.com/012341231'); |
||
| 173 | $mediaWebsite->setId(1023456); |
||
| 174 | |||
| 175 | $mediaPlayer = new Media(); |
||
| 176 | $mediaPlayer->setContext('default'); |
||
| 177 | $mediaPlayer->setBinaryContent('https://player.vimeo.com/video/012341231'); |
||
| 178 | $mediaPlayer->setId(1023456); |
||
| 179 | |||
| 180 | return [ |
||
| 181 | 'transform with website url' => [$mediaWebsite], |
||
| 182 | 'transform with player url' => [$mediaPlayer], |
||
| 183 | ]; |
||
| 184 | } |
||
| 185 | |||
| 186 | public function testGetMetadataException(): void |
||
| 187 | { |
||
| 188 | $this->expectException(\RuntimeException::class); |
||
| 189 | $this->expectExceptionMessage('Unable to retrieve the video information for :012341231'); |
||
| 190 | $this->expectExceptionCode(12); |
||
| 191 | |||
| 192 | $response = new Response(); |
||
| 193 | $response->setContent(file_get_contents(__DIR__.'/../fixtures/valid_vimeo.txt')); |
||
| 194 | |||
| 195 | $browser = $this->createMock(Browser::class); |
||
| 196 | $browser->expects($this->once())->method('get')->will($this->throwException(new \RuntimeException('First error on get', 12))); |
||
| 197 | |||
| 198 | $provider = $this->getProvider($browser); |
||
| 199 | |||
| 200 | $provider->addFormat('big', ['width' => 200, 'height' => 100, 'constraint' => true]); |
||
| 201 | |||
| 202 | $media = new Media(); |
||
| 203 | $media->setBinaryContent('https://vimeo.com/012341231'); |
||
| 204 | $media->setId(1023456); |
||
| 205 | |||
| 206 | $method = new \ReflectionMethod($provider, 'getMetadata'); |
||
| 207 | $method->setAccessible(true); |
||
| 208 | |||
| 209 | $method->invokeArgs($provider, [$media, '012341231']); |
||
| 210 | } |
||
| 211 | |||
| 212 | public function testForm(): void |
||
| 213 | { |
||
| 214 | $provider = $this->getProvider(); |
||
| 215 | |||
| 216 | $admin = $this->createMock(AdminInterface::class); |
||
| 217 | $admin |
||
| 218 | ->method('trans') |
||
| 219 | ->willReturn('message'); |
||
| 220 | |||
| 221 | $formMapper = $this->createMock(FormMapper::class); |
||
| 222 | $formMapper->expects($this->exactly(8)) |
||
| 223 | ->method('add') |
||
| 224 | ->willReturn(null); |
||
| 225 | |||
| 226 | $provider->buildCreateForm($formMapper); |
||
| 227 | |||
| 228 | $provider->buildEditForm($formMapper); |
||
| 229 | } |
||
| 230 | |||
| 231 | public function testHelperProperies(): void |
||
| 232 | { |
||
| 233 | $provider = $this->getProvider(); |
||
| 234 | |||
| 235 | $provider->addFormat('admin', ['width' => 100]); |
||
| 236 | $media = new Media(); |
||
| 237 | $media->setName('Les tests'); |
||
| 238 | $media->setProviderReference('ASDASDAS.png'); |
||
| 239 | $media->setId(10); |
||
| 240 | $media->setHeight(100); |
||
| 241 | $media->setWidth(100); |
||
| 242 | |||
| 243 | $properties = $provider->getHelperProperties($media, 'admin'); |
||
| 244 | |||
| 245 | $this->assertIsArray($properties); |
||
| 246 | $this->assertSame(100, $properties['height']); |
||
| 247 | $this->assertSame(100, $properties['width']); |
||
| 248 | } |
||
| 249 | |||
| 250 | public function testGetReferenceUrl(): void |
||
| 251 | { |
||
| 252 | $media = new Media(); |
||
| 253 | $media->setProviderReference('123456'); |
||
| 254 | $this->assertSame('https://vimeo.com/123456', $this->getProvider()->getReferenceUrl($media)); |
||
|
0 ignored issues
–
show
|
|||
| 255 | } |
||
| 256 | |||
| 257 | public function testMetadata(): void |
||
| 258 | { |
||
| 259 | $provider = $this->getProvider(); |
||
| 260 | |||
| 261 | $this->assertSame('vimeo', $provider->getProviderMetadata()->getTitle()); |
||
| 262 | $this->assertSame('vimeo.description', $provider->getProviderMetadata()->getDescription()); |
||
| 263 | $this->assertNotNull($provider->getProviderMetadata()->getImage()); |
||
| 264 | $this->assertSame('fa fa-vimeo-square', $provider->getProviderMetadata()->getOption('class')); |
||
| 265 | $this->assertSame('SonataMediaBundle', $provider->getProviderMetadata()->getDomain()); |
||
| 266 | } |
||
| 267 | } |
||
| 268 |
Let’s take a look at an example:
In the above example, the authenticate() method works fine as long as you just pass instances of MyUser. However, if you now also want to pass a different implementation of User which does not have a getDisplayName() method, the code will break.
Available Fixes
Change the type-hint for the parameter:
Add an additional type-check:
Add the method to the interface: