Update to Drupal 8.0-dev-2015-11-17. Commits through da81cd220, Tue Nov 17 15:53:49 2015 +0000, Issue #2617224 by Wim Leers: Move around/fix some documentation.
This commit is contained in:
parent
4afb23bbd3
commit
7784f4c23d
929 changed files with 19798 additions and 5304 deletions
119
core/modules/file/tests/src/Kernel/FileItemValidationTest.php
Normal file
119
core/modules/file/tests/src/Kernel/FileItemValidationTest.php
Normal file
|
@ -0,0 +1,119 @@
|
|||
<?php
|
||||
|
||||
/**
|
||||
* @file
|
||||
* Contains \Drupal\Tests\file\Kernel\FileItemValidationTest.
|
||||
*/
|
||||
|
||||
namespace Drupal\Tests\file\Kernel;
|
||||
|
||||
use Drupal\entity_test\Entity\EntityTest;
|
||||
use Drupal\field\Entity\FieldConfig;
|
||||
use Drupal\field\Entity\FieldStorageConfig;
|
||||
use Drupal\file\Entity\File;
|
||||
use Drupal\KernelTests\KernelTestBase;
|
||||
use Drupal\user\Entity\User;
|
||||
use org\bovigo\vfs\vfsStream;
|
||||
|
||||
/**
|
||||
* Tests that files referenced in file and image fields are always validated.
|
||||
*
|
||||
* @group file
|
||||
*/
|
||||
class FileItemValidationTest extends KernelTestBase {
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
public static $modules = ['file', 'image', 'entity_test', 'field', 'user', 'system'];
|
||||
|
||||
/**
|
||||
* A user.
|
||||
*
|
||||
* @var \Drupal\user\UserInterface
|
||||
*/
|
||||
protected $user;
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
protected function setUp() {
|
||||
parent::setUp();
|
||||
|
||||
$this->installEntitySchema('user');
|
||||
$this->installEntitySchema('file');
|
||||
$this->installSchema('file', 'file_usage');
|
||||
$this->installSchema('system', 'sequences');
|
||||
|
||||
$this->user = User::create([
|
||||
'name' => 'username',
|
||||
'status' => 1,
|
||||
]);
|
||||
$this->user->save();
|
||||
}
|
||||
|
||||
/**
|
||||
* @covers \Drupal\file\Plugin\Validation\Constraint\FileValidationConstraint
|
||||
* @covers \Drupal\file\Plugin\Validation\Constraint\FileValidationConstraintValidator
|
||||
* @dataProvider getFileTypes
|
||||
*/
|
||||
public function testFileValidationConstraint($file_type) {
|
||||
$field_storage = FieldStorageConfig::create([
|
||||
'field_name' => 'field_test_file',
|
||||
'entity_type' => 'entity_test',
|
||||
'type' => $file_type,
|
||||
]);
|
||||
$field_storage->save();
|
||||
|
||||
$field = FieldConfig::create([
|
||||
'field_name' => 'field_test_file',
|
||||
'entity_type' => 'entity_test',
|
||||
'bundle' => 'entity_test',
|
||||
'settings' => [
|
||||
'max_filesize' => '2k',
|
||||
'file_extensions' => 'jpg|png',
|
||||
],
|
||||
]);
|
||||
$field->save();
|
||||
|
||||
vfsStream::setup('drupal_root');
|
||||
vfsStream::create([
|
||||
'sites' => [
|
||||
'default' => [
|
||||
'files' => [
|
||||
'test.txt' => str_repeat('a', 3000),
|
||||
]
|
||||
]
|
||||
]
|
||||
]);
|
||||
|
||||
// Test for max filesize.
|
||||
$file = File::create([
|
||||
'uri' => 'vfs://drupal_root/sites/default/files/test.txt',
|
||||
]);
|
||||
$file->setPermanent();
|
||||
$file->save();
|
||||
|
||||
$entity_test = EntityTest::create([
|
||||
'uid' => $this->user->id(),
|
||||
'field_test_file' => [
|
||||
'target_id' => $file->id(),
|
||||
]
|
||||
]);
|
||||
$result = $entity_test->validate();
|
||||
$this->assertCount(2, $result);
|
||||
|
||||
$this->assertEquals('field_test_file.0', $result->get(0)->getPropertyPath());
|
||||
$this->assertEquals('The file is <em class="placeholder">2.93 KB</em> exceeding the maximum file size of <em class="placeholder">2 KB</em>.', (string) $result->get(0)->getMessage());
|
||||
$this->assertEquals('field_test_file.0', $result->get(1)->getPropertyPath());
|
||||
$this->assertEquals('Only files with the following extensions are allowed: <em class="placeholder">jpg|png</em>.', (string) $result->get(1)->getMessage());
|
||||
}
|
||||
|
||||
/**
|
||||
* Provides a list of file types to test.
|
||||
*/
|
||||
public function getFileTypes() {
|
||||
return [['file'], ['image']];
|
||||
}
|
||||
|
||||
}
|
Reference in a new issue