Skip to content

Commit

Permalink
Merge 4.x into 5.x
Browse files Browse the repository at this point in the history
  • Loading branch information
SonataCI committed Nov 17, 2021
2 parents dc9e7e2 + b9573c3 commit 530530d
Show file tree
Hide file tree
Showing 8 changed files with 161 additions and 57 deletions.
4 changes: 2 additions & 2 deletions .github/workflows/qa.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ jobs:
php-version: '8.0'
coverage: none
tools: composer:v2
extensions: mongodb-1.9.0, soap, gd
extensions: mongodb-1.9.0, gd

- name: Install Composer dependencies (highest)
uses: ramsey/composer-install@v1
Expand All @@ -53,7 +53,7 @@ jobs:
php-version: '8.0'
coverage: none
tools: composer:v2
extensions: mongodb-1.9.0, soap, gd
extensions: mongodb-1.9.0, gd

- name: Install Composer dependencies (highest)
uses: ramsey/composer-install@v1
Expand Down
12 changes: 2 additions & 10 deletions .github/workflows/test.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -43,14 +43,6 @@ jobs:
dependencies: lowest
allowed-to-fail: false
variant: normal
- php-version: '8.0'
dependencies: highest
allowed-to-fail: false
variant: sonata-project/admin-bundle:"4.*"
- php-version: '8.0'
dependencies: highest
allowed-to-fail: false
variant: imagine/imagine:"1.*"
- php-version: '8.0'
dependencies: highest
allowed-to-fail: false
Expand All @@ -77,7 +69,7 @@ jobs:
php-version: ${{ matrix.php-version }}
coverage: pcov
tools: composer:v2
extensions: mongodb-1.9.0, soap, gd
extensions: mongodb-1.9.0, gd

- name: Add PHPUnit matcher
run: echo "::add-matcher::${{ runner.tool_cache }}/phpunit.json"
Expand All @@ -95,7 +87,7 @@ jobs:

- name: Remove dev dependencies not compatible with Symfony 6
if: matrix.symfony-require == '6.0.*'
run: composer remove vimeo/psalm psalm/plugin-phpunit psalm/plugin-symfony weirdan/doctrine-psalm-plugin symfony/maker-bundle --dev --no-update --no-interaction
run: composer remove psalm/plugin-symfony symfony/maker-bundle symfony/panther --dev --no-update --no-interaction

- name: Install Composer dependencies (${{ matrix.dependencies }})
uses: ramsey/composer-install@v1
Expand Down
1 change: 1 addition & 0 deletions .php-cs-fixer.dist.php
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@
'php_unit_test_annotation' => false,
'php_unit_test_case_static_method_calls' => true,
'phpdoc_order' => true,
'phpdoc_to_comment' => ['ignored_tags' => ['psalm-suppress']],
'single_line_throw' => false,
'static_lambda' => true,
'strict_comparison' => true,
Expand Down
12 changes: 12 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,18 @@ This project adheres to [Semantic Versioning](http://semver.org/).
### Removed
- [[#2164](https://github.com/sonata-project/SonataMediaBundle/pull/2164)] Removed support for PHP 7.3 ([@jordisala1991](https://github.com/jordisala1991))

## [3.36.0](https://github.com/sonata-project/SonataMediaBundle/compare/3.35.1...3.36.0) - 2021-11-04
### Changed
- [[#2194](https://github.com/sonata-project/SonataMediaBundle/pull/2194)] Final annotation to methods that will be final on 4.x ([@jordisala1991](https://github.com/jordisala1991))

### Deprecated
- [[#2187](https://github.com/sonata-project/SonataMediaBundle/pull/2187)] PantherPortal CDN ([@jordisala1991](https://github.com/jordisala1991))
- [[#2186](https://github.com/sonata-project/SonataMediaBundle/pull/2186)] Using `master` and `slave` for filesystem and cdn configuration, use `primary` and `secondary` instead ([@jordisala1991](https://github.com/jordisala1991))

### Removed
- [[#2197](https://github.com/sonata-project/SonataMediaBundle/pull/2197)] Conflict against "nelmio/api-doc-bundle" >= 4.0 ([@phansys](https://github.com/phansys))
- [[#2197](https://github.com/sonata-project/SonataMediaBundle/pull/2197)] The ReST API endpoints and documentation if "nelmio/api-doc-bundle" >= 4.0 is installed ([@phansys](https://github.com/phansys))

## [3.35.1](https://github.com/sonata-project/SonataMediaBundle/compare/3.35.0...3.35.1) - 2021-10-18
### Fixed
- [[#2175](https://github.com/sonata-project/SonataMediaBundle/pull/2175)] Fixed calling `media_thumbnail` instead of `sonata_thumbnail` ([@jordisala1991](https://github.com/jordisala1991))
Expand Down
6 changes: 6 additions & 0 deletions UPGRADE-3.x.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,12 @@ UPGRADE 3.x
UPGRADE FROM 3.x to 3.x
=======================

### ReST API

Conflict against "nelmio/api-doc-bundle" >= 4.0 was removed.
If this package is installed, the deprecated ReST API will not be built.
This behavior allows to accept setups using "nelmio/api-doc-bundle" >= 4.0 dependency without conflicts.

### Mark final methods on abstract classes

The following abstract classes have some of their methods marked as final with annotations, on 4.0 they will be final, make sure to not reimplement them:
Expand Down
40 changes: 35 additions & 5 deletions src/Provider/ImageProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
use Sonata\MediaBundle\Metadata\MetadataBuilderInterface;
use Sonata\MediaBundle\Model\MediaInterface;
use Sonata\MediaBundle\Thumbnail\ThumbnailInterface;
use Symfony\Component\HttpFoundation\File\Exception\UploadException;
use Symfony\Component\HttpFoundation\File\File;
use Symfony\Component\HttpFoundation\File\UploadedFile;

Expand Down Expand Up @@ -232,6 +233,18 @@ protected function doTransform(MediaInterface $media): void
{
parent::doTransform($media);

if ([] === $this->allowedExtensions) {
$media->setProviderStatus(MediaInterface::STATUS_ERROR);

throw new UploadException('There are no allowed extensions for this image.');
}

if ([] === $this->allowedMimeTypes) {
$media->setProviderStatus(MediaInterface::STATUS_ERROR);

throw new UploadException('There are no allowed mime types for this image.');
}

if ($media->getBinaryContent() instanceof UploadedFile) {
$fileName = $media->getBinaryContent()->getClientOriginalName();
} elseif ($media->getBinaryContent() instanceof File) {
Expand All @@ -241,11 +254,28 @@ protected function doTransform(MediaInterface $media): void
return;
}

if (
!\in_array(strtolower(pathinfo($fileName, \PATHINFO_EXTENSION)), $this->allowedExtensions, true)
|| !\in_array($media->getBinaryContent()->getMimeType(), $this->allowedMimeTypes, true)
) {
return;
$extension = strtolower(pathinfo($fileName, \PATHINFO_EXTENSION));

if (!\in_array($extension, $this->allowedExtensions, true)) {
$media->setProviderStatus(MediaInterface::STATUS_ERROR);

throw new UploadException(sprintf(
'The image extension "%s" is not one of the allowed (%s).',
$extension,
'"'.implode('", "', $this->allowedExtensions).'"'
));
}

$mimeType = $media->getBinaryContent()->getMimeType();

if (!\in_array($mimeType, $this->allowedMimeTypes, true)) {
$media->setProviderStatus(MediaInterface::STATUS_ERROR);

throw new UploadException(sprintf(
'The image mime type "%s" is not one of the allowed (%s).',
$mimeType,
'"'.implode('", "', $this->allowedMimeTypes).'"'
));
}

try {
Expand Down
Binary file added tests/Fixtures/logo-png-without-ext
Binary file not shown.
143 changes: 103 additions & 40 deletions tests/Provider/ImageProviderTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,30 +13,33 @@

namespace Sonata\MediaBundle\Tests\Provider;

use Gaufrette\Adapter;
use Gaufrette\File;
use Gaufrette\Adapter\Local;
use Gaufrette\Filesystem;
use Imagine\Gd\Imagine;
use Imagine\Image\Box;
use Imagine\Image\BoxInterface;
use Imagine\Image\ImageInterface;
use Imagine\Image\ImagineInterface;
use PHPUnit\Framework\MockObject\MockObject;
use Sonata\MediaBundle\CDN\Server;
use Sonata\MediaBundle\Generator\IdGenerator;
use Sonata\MediaBundle\Metadata\MetadataBuilderInterface;
use Sonata\MediaBundle\Provider\ImageProvider;
use Sonata\MediaBundle\Provider\MediaProviderInterface;
use Sonata\MediaBundle\Resizer\ResizerInterface;
use Sonata\MediaBundle\Tests\Entity\Media;
use Sonata\MediaBundle\Thumbnail\FormatThumbnail;
use Symfony\Component\HttpFoundation\File\Exception\UploadException;
use Symfony\Component\HttpFoundation\File\File as SymfonyFile;

/**
* @phpstan-extends AbstractProviderTest<ImageProvider>
*/
class ImageProviderTest extends AbstractProviderTest
{
public function getProvider(): MediaProviderInterface
/**
* @param string[] $allowedExtensions
* @param string[] $allowedMimeTypes
*/
public function getProvider(array $allowedExtensions = [], array $allowedMimeTypes = []): ImageProvider
{
/** @var MockObject&ResizerInterface $resizer */
$resizer = $this->createMock(ResizerInterface::class);

$adminBox = new Box(100, 100);
Expand All @@ -59,37 +62,17 @@ public function getProvider(): MediaProviderInterface
$largeBox
));

$adapter = $this->createMock(Adapter::class);

$filesystem = $this->getMockBuilder(Filesystem::class)
->onlyMethods(['get'])
->setConstructorArgs([$adapter])
->getMock();
$file = $this->getMockBuilder(File::class)
->setConstructorArgs(['foo', $filesystem])
->getMock();
$file->method('getName')->willReturn('name');
$filesystem->method('get')->willReturn($file);

$filesystem = new Filesystem(new Local(sys_get_temp_dir().'/sonata-media-bundle/var/', true));
$cdn = new Server('/uploads/media');

$generator = new IdGenerator();

$thumbnail = new FormatThumbnail('jpg');
$adapter = new Imagine();

$size = $this->createMock(BoxInterface::class);
$size->method('getWidth')->willReturn(100);
$size->method('getHeight')->willReturn(100);

$image = $this->createMock(ImageInterface::class);
$image->method('getSize')->willReturn($size);

$adapter = $this->createMock(ImagineInterface::class);
$adapter->method('open')->willReturn($image);

/** @var MockObject&MetadataBuilderInterface $metadata */
$metadata = $this->createMock(MetadataBuilderInterface::class);
$metadata->method('get')->willReturn([]);

$provider = new ImageProvider('image', $filesystem, $cdn, $generator, $thumbnail, [], [], $adapter, $metadata);
$provider = new ImageProvider('image', $filesystem, $cdn, $generator, $thumbnail, $allowedExtensions, $allowedMimeTypes, $adapter, $metadata);
$provider->setResizer($resizer);

return $provider;
Expand Down Expand Up @@ -234,7 +217,9 @@ public function testThumbnail(): void

public function testEvent(): void
{
$this->provider->addFormat('big', [
$provider = $this->getProvider(['png'], ['image/png']);

$provider->addFormat('big', [
'width' => 200,
'height' => 100,
'quality' => 80,
Expand All @@ -256,18 +241,18 @@ public function testEvent(): void
$media->setId(1023456);

// pre persist the media
$this->provider->transform($media);
$this->provider->prePersist($media);
$provider->transform($media);
$provider->prePersist($media);

static::assertSame('logo.png', $media->getName(), '::getName() return the file name');
static::assertNotNull($media->getProviderReference(), '::getProviderReference() is set');

// post persist the media
$this->provider->postPersist($media);
$this->provider->postRemove($media);
$provider->postPersist($media);
$provider->postRemove($media);
}

public function testTransformFormatNotSupported(): void
public function testUpdateMetadata(): void
{
$realPath = realpath(__DIR__.'/../Fixtures/logo.png');

Expand All @@ -278,9 +263,87 @@ public function testTransformFormatNotSupported(): void
$media = new Media();
$media->setBinaryContent($file);

$this->provider->transform($media);
$this->provider->updateMetadata($media);

static::assertNotNull($media->getSize());
static::assertSame(132, $media->getHeight());
static::assertSame(535, $media->getWidth());
}

public function testTransformNoExtensions(): void
{
$realPath = realpath(__DIR__.'/../Fixtures/logo.png');

static::assertNotFalse($realPath);

$file = new SymfonyFile($realPath);

$media = new Media();
$media->setBinaryContent($file);

$provider = $this->getProvider([], ['image/png']);

$this->expectException(UploadException::class);
$this->expectExceptionMessage('There are no allowed extensions for this image.');

$provider->transform($media);
}

public function testTransformExtensionNotAllowed(): void
{
$realPath = realpath(__DIR__.'/../Fixtures/logo.png');

static::assertNotFalse($realPath);

$file = new SymfonyFile($realPath);

$media = new Media();
$media->setBinaryContent($file);

$provider = $this->getProvider(['jpg', 'jpeg'], ['image/jpg']);

$this->expectException(UploadException::class);
$this->expectExceptionMessage('The image extension "png" is not one of the allowed ("jpg", "jpeg")');

$provider->transform($media);
}

public function testTransformNoMimeTypes(): void
{
$realPath = realpath(__DIR__.'/../Fixtures/logo.png');

static::assertNotFalse($realPath);

$file = new SymfonyFile($realPath);

$media = new Media();
$media->setBinaryContent($file);

$provider = $this->getProvider(['png']);

$this->expectException(UploadException::class);
$this->expectExceptionMessage('There are no allowed mime types for this image.');

$provider->transform($media);
}

public function testTransformMimeTypeNotAllowed(): void
{
$realPath = realpath(__DIR__.'/../Fixtures/logo.png');

static::assertNotFalse($realPath);

$file = new SymfonyFile($realPath);

$media = new Media();
$media->setBinaryContent($file);

$provider = $this->getProvider(['png'], ['image/jpg', 'image/jpeg']);

$this->expectException(UploadException::class);
$this->expectExceptionMessage('The image mime type "image/png" is not one of the allowed ("image/jpg", "image/jpeg")');

static::assertNull($media->getWidth(), 'Width staid null');
$provider->transform($media);
}

public function testMetadata(): void
Expand Down

0 comments on commit 530530d

Please sign in to comment.