FileUrlFieldItemListTest.php in File URL 2.0.x
File
tests/src/Kernel/FileUrlFieldItemListTest.php
View source
<?php
namespace Drupal\Tests\file_url\Kernel;
use Drupal\Core\Entity\EntityInterface;
use Drupal\Core\Field\FieldStorageDefinitionInterface;
use Drupal\entity_test\Entity\EntityTest;
use Drupal\field\Entity\FieldConfig;
use Drupal\field\Entity\FieldStorageConfig;
use Drupal\file\Entity\File;
use Drupal\file_url\Entity\RemoteFile;
use Drupal\file_url\Plugin\Field\FieldType\FileUrlFieldItemList;
use Drupal\Tests\field\Kernel\FieldKernelTestBase;
class FileUrlFieldItemListTest extends FieldKernelTestBase {
public static $modules = [
'file',
'file_url',
];
protected $entityTypeManager;
protected $localFile;
protected $remoteFile;
protected $directory;
public function setUp() {
parent::setUp();
$this->entityTypeManager = $this->container
->get('entity_type.manager');
$this
->installEntitySchema('file');
$this
->installSchema('file', [
'file_usage',
]);
FieldStorageConfig::create([
'field_name' => 'file_url_test',
'entity_type' => 'entity_test',
'type' => 'file_url',
'cardinality' => FieldStorageDefinitionInterface::CARDINALITY_UNLIMITED,
])
->save();
$this->directory = $this
->getRandomGenerator()
->name(8);
FieldConfig::create([
'entity_type' => 'entity_test',
'field_name' => 'file_url_test',
'bundle' => 'entity_test',
'settings' => [
'file_directory' => $this->directory,
],
])
->save();
file_put_contents('public://example.txt', $this
->randomMachineName());
$this->localFile = File::create([
'uri' => 'public://example.txt',
]);
$this->localFile
->save();
$this->remoteFile = RemoteFile::create([
'uri' => $this
->getRandomUrl(),
]);
$this->remoteFile
->save();
}
public function testReferencedEntities() {
$entity = EntityTest::create();
if (!$entity->file_url_test instanceof FileUrlFieldItemList) {
$this
->fail('A freshly created File Url field has the correct item list class.');
}
$entity->file_url_test
->setValue([
0 => [
'target_id' => $this->localFile
->id(),
],
1 => [
'target_id' => $this->remoteFile
->id(),
],
]);
$entity->name->value = $this
->randomMachineName();
$entity
->save();
$reloaded_entity = $this
->reloadEntity($entity);
if (!$entity->file_url_test instanceof FileUrlFieldItemList) {
$this
->fail('A freshly created File Url field has the correct item list class.');
}
$referenced_entities = $reloaded_entity->file_url_test
->referencedEntities();
$this
->assertTrue(!empty($referenced_entities[0]));
$this
->assertEquals('Drupal\\file\\Entity\\File', get_class($referenced_entities[0]));
$this
->assertEquals($this->localFile
->id(), $referenced_entities[0]
->id());
$this
->assertTrue(!empty($referenced_entities[1]));
$this
->assertEquals('Drupal\\file_url\\Entity\\RemoteFile', get_class($referenced_entities[1]));
$this
->assertEquals($this->remoteFile
->id(), $referenced_entities[1]
->id());
}
protected function getRandomUrl() {
return 'http://example.com/' . $this
->randomMachineName();
}
protected function reloadEntity(EntityInterface $entity) {
$controller = $this->entityTypeManager
->getStorage($entity
->getEntityTypeId());
$controller
->resetCache([
$entity
->id(),
]);
return $controller
->load($entity
->id());
}
}