|
| 1 | +<?php |
| 2 | + |
| 3 | +namespace Netgen\Bundle\EnhancedBinaryFileBundle\Tests\Core\FieldType\EnhancedBinaryFile; |
| 4 | + |
| 5 | +use eZ\Publish\Core\FieldType\ValidationError; |
| 6 | +use eZ\Publish\Core\MVC\ConfigResolverInterface; |
| 7 | +use eZ\Publish\SPI\IO\MimeTypeDetector; |
| 8 | +use eZ\Publish\Core\Repository\Values\ContentType\FieldDefinition; |
| 9 | +use Netgen\Bundle\EnhancedBinaryFileBundle\Core\FieldType\EnhancedBinaryFile\Type; |
| 10 | +use Netgen\Bundle\EnhancedBinaryFileBundle\Core\FieldType\EnhancedBinaryFile\Value; |
| 11 | +use PHPUnit\Framework\TestCase; |
| 12 | + |
| 13 | +class TypeTest extends TestCase |
| 14 | +{ |
| 15 | + /** |
| 16 | + * @var \PHPUnit_Framework_MockObject_MockObject |
| 17 | + */ |
| 18 | + protected $mimeTypeDetector; |
| 19 | + |
| 20 | + /** |
| 21 | + * @var \PHPUnit_Framework_MockObject_MockObject |
| 22 | + */ |
| 23 | + protected $configResolver; |
| 24 | + |
| 25 | + /** |
| 26 | + * @var Type |
| 27 | + */ |
| 28 | + protected $type; |
| 29 | + |
| 30 | + /** |
| 31 | + * @var string |
| 32 | + */ |
| 33 | + protected $file; |
| 34 | + |
| 35 | + public function setUp() |
| 36 | + { |
| 37 | + $this->file = __DIR__ . '/test.txt'; |
| 38 | + $this->mimeTypeDetector = $this->createMock(MimeTypeDetector::class); |
| 39 | + $this->configResolver = $this->createMock(ConfigResolverInterface::class); |
| 40 | + $this->type = new Type($this->mimeTypeDetector, $this->configResolver); |
| 41 | + } |
| 42 | + |
| 43 | + public function testGetEmptyValue() |
| 44 | + { |
| 45 | + $this->assertEquals(new Value(), $this->type->getEmptyValue()); |
| 46 | + } |
| 47 | + |
| 48 | + public function testGetFieldTypeIdentifier() |
| 49 | + { |
| 50 | + $this->assertEquals("enhancedezbinaryfile", $this->type->getFieldTypeIdentifier()); |
| 51 | + } |
| 52 | + |
| 53 | + public function testValidate() |
| 54 | + { |
| 55 | + $fieldDefinition = new FieldDefinition([ |
| 56 | + 'fieldSettings' => [ |
| 57 | + 'allowedTypes' => 'jpg|pdf|txt' |
| 58 | + ], |
| 59 | + ]); |
| 60 | + |
| 61 | + $value = new Value([ |
| 62 | + 'path' => $this->file, |
| 63 | + ]); |
| 64 | + |
| 65 | + $this->mimeTypeDetector->expects($this->once()) |
| 66 | + ->method('getFromPath') |
| 67 | + ->with($this->file) |
| 68 | + ->willReturn('text/plain'); |
| 69 | + |
| 70 | + $expected = [ |
| 71 | + new ValidationError( |
| 72 | + "This mimeType is not allowed %mimeType%.", |
| 73 | + "These mimeTypes are not allowed %mimeType%.", |
| 74 | + [ "mimeType" => 'text/plain' ] |
| 75 | + ), |
| 76 | + ]; |
| 77 | + |
| 78 | + $this->assertEquals($expected, $this->type->validate($fieldDefinition, $value)); |
| 79 | + } |
| 80 | + |
| 81 | + public function testValidateWithEmptyValue() |
| 82 | + { |
| 83 | + $fieldDefinition = new FieldDefinition([ |
| 84 | + 'fieldSettings' => [ |
| 85 | + 'allowedTypes' => 'jpg|pdf|txt' |
| 86 | + ], |
| 87 | + ]); |
| 88 | + |
| 89 | + $value = new Value(); |
| 90 | + |
| 91 | + $this->type->validate($fieldDefinition, $value); |
| 92 | + } |
| 93 | + |
| 94 | + public function testValidateWithMineTypesFromConfig() |
| 95 | + { |
| 96 | + $fieldDefinition = new FieldDefinition([ |
| 97 | + 'fieldSettings' => [ |
| 98 | + 'allowedTypes' => 'jpg|pdf|txt' |
| 99 | + ], |
| 100 | + ]); |
| 101 | + |
| 102 | + $value = new Value([ |
| 103 | + 'path' => $this->file, |
| 104 | + ]); |
| 105 | + |
| 106 | + $this->mimeTypeDetector->expects($this->once()) |
| 107 | + ->method('getFromPath') |
| 108 | + ->with($this->file) |
| 109 | + ->willReturn('text/plain'); |
| 110 | + |
| 111 | + $this->configResolver->expects($this->any()) |
| 112 | + ->method('hasParameter') |
| 113 | + ->will( |
| 114 | + $this->returnCallback(function($arg) { |
| 115 | + if ($arg === 'txt.Types') { |
| 116 | + return true; |
| 117 | + } |
| 118 | + |
| 119 | + return false; |
| 120 | + }) |
| 121 | + ); |
| 122 | + |
| 123 | + $this->configResolver->expects($this->once()) |
| 124 | + ->method('getParameter') |
| 125 | + ->with('txt.Types', 'mime') |
| 126 | + ->willReturn(['text/plain']); |
| 127 | + |
| 128 | + $this->type->validate($fieldDefinition, $value); |
| 129 | + } |
| 130 | +} |
0 commit comments