PHRAS-1819: add quarantine rule based on media type

This commit is contained in:
Sandeep
2018-05-07 14:31:12 +04:00
parent 0b6892ac35
commit 7f8251d4e2
3 changed files with 66 additions and 4 deletions

View File

@@ -102,6 +102,7 @@ border-manager:
enabled: false
options:
colorspaces: [cmyk, grayscale, rgb]
media_types: [Image]
-
type: Checker\Dimension
enabled: false

View File

@@ -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())

View File

@@ -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
*/