diff --git a/config/configuration.sample.yml b/config/configuration.sample.yml index 2467514a84..f220a4c867 100644 --- a/config/configuration.sample.yml +++ b/config/configuration.sample.yml @@ -102,6 +102,7 @@ border-manager: enabled: false options: colorspaces: [cmyk, grayscale, rgb] + media_types: [Image] - type: Checker\Dimension enabled: false diff --git a/lib/Alchemy/Phrasea/Border/Checker/Colorspace.php b/lib/Alchemy/Phrasea/Border/Checker/Colorspace.php index a54481be60..be5f179854 100644 --- a/lib/Alchemy/Phrasea/Border/Checker/Colorspace.php +++ b/lib/Alchemy/Phrasea/Border/Checker/Colorspace.php @@ -20,6 +20,7 @@ use Symfony\Component\Translation\TranslatorInterface; class Colorspace extends AbstractChecker { protected $colorspaces; + protected $mediatypes; const COLORSPACE_RGB = 'rgb'; const COLORSPACE_CMYK = 'cmyk'; @@ -32,7 +33,12 @@ class Colorspace extends AbstractChecker throw new \InvalidArgumentException('Missing "colorspaces" options'); } + if (!isset($options['media_types'])) { + throw new \InvalidArgumentException('Missing "media_types" options'); + } + $this->colorspaces = array_map('strtolower', (array) $options['colorspaces']); + $this->mediatypes = $options['media_types']; parent::__construct($app); } @@ -42,8 +48,8 @@ class Colorspace extends AbstractChecker if (0 === count($this->colorspaces)) { $boolean = true; //bypass color if empty array - } elseif ($file->getMedia()->getType() === Document::TYPE_DOCUMENT) { - $boolean = true; //bypass color checker if file is of type document + } elseif (0 !== count($this->mediatypes) && $file->getMedia()->getType() !== NULL && !in_array($file->getMedia()->getType(), $this->mediatypes)) { + $boolean = true; //bypass color checker if media type is not in the config } elseif (method_exists($file->getMedia(), 'getColorSpace')) { $colorspace = null; switch ($file->getMedia()->getColorSpace()) diff --git a/tests/Alchemy/Tests/Phrasea/Border/Checker/ColorspaceTest.php b/tests/Alchemy/Tests/Phrasea/Border/Checker/ColorspaceTest.php index 5e3148286f..6785a405f1 100644 --- a/tests/Alchemy/Tests/Phrasea/Border/Checker/ColorspaceTest.php +++ b/tests/Alchemy/Tests/Phrasea/Border/Checker/ColorspaceTest.php @@ -23,18 +23,21 @@ class ColorspaceTest extends \PhraseanetTestCase public function setUp() { parent::setUp(); - $this->object = new Colorspace(self::$DI['app'], ['colorspaces' => ['RGB', 'cmyk']]); + $this->object = new Colorspace(self::$DI['app'], ['colorspaces' => ['RGB', 'cmyk'], 'media_types' => ['Image', 'Document']]); } /** * @covers Alchemy\Phrasea\Border\Checker\Colorspace::check */ - public function testCheck() + public function testCheckImage() { $media = $this ->getMockBuilder('\\MediaVorus\\Media\\Image') ->disableOriginalConstructor() ->getMock(); + $media->expects($this->any()) + ->method('getType') + ->will($this->returnValue('Image')); $media->expects($this->once()) ->method('getColorSpace') ->will($this->returnValue('RGB')); @@ -50,6 +53,58 @@ class ColorspaceTest extends \PhraseanetTestCase $this->assertTrue($response->isOk()); } + /** + * @covers Alchemy\Phrasea\Border\Checker\Colorspace::check + */ + public function testCheckVideo() + { + $media = $this + ->getMockBuilder('\\MediaVorus\\Media\\Video') + ->disableOriginalConstructor() + ->getMock(); + $media->expects($this->any()) + ->method('getType') + ->will($this->returnValue('Video')); + $media->expects($this->any()) + ->method('getFile') + ->will($this->returnValue(new \SplFileInfo(__FILE__))); + + $File = new File(self::$DI['app'], $media, self::$DI['collection']); + + $response = $this->object->check(self::$DI['app']['orm.em'], $File); + + $this->assertInstanceOf('\\Alchemy\\Phrasea\\Border\\Checker\\Response', $response); + $this->assertTrue($response->isOk()); + } + + /** + * @covers Alchemy\Phrasea\Border\Checker\Colorspace::check + */ + public function testCheckFailDocument() + { + $media = $this + ->getMockBuilder('\\MediaVorus\\Media\\Document') + ->disableOriginalConstructor() + ->getMock(); + $media->expects($this->once()) + ->method('getColorSpace') + ->will($this->returnValue('')); + $media->expects($this->any()) + ->method('getType') + ->will($this->returnValue('Document')); + $media->expects($this->any()) + ->method('getFile') + ->will($this->returnValue(new \SplFileInfo(__FILE__))); + + $File = new File(self::$DI['app'], $media, self::$DI['collection']); + + $response = $this->object->check(self::$DI['app']['orm.em'], $File); + + $this->assertInstanceOf('\\Alchemy\\Phrasea\\Border\\Checker\\Response', $response); + + $this->assertEquals($this->createTranslatorMock()->trans('The file does not match available color'), $response->getMessage($this->createTranslatorMock())); + } + /** * @covers Alchemy\Phrasea\Border\Checker\Colorspace::getMessage */