protected function FileDownloadLinkTestTrait::createTestEntity in File Download Link 8
Helper function to create entity that can be used for testing.
Return value
Drupal\node\Entity\Node An entity to be used for testing.
2 calls to FileDownloadLinkTestTrait::createTestEntity()
- FileDownloadLinkTest::setUp in tests/
src/ Kernel/ FileDownloadLinkTest.php - FileDownloadLinkTokenTest::setUp in tests/
src/ Kernel/ FileDownloadLinkTokenTest.php
File
- tests/
src/ Kernel/ FileDownloadLinkTestTrait.php, line 31
Class
- FileDownloadLinkTestTrait
- Trait for testing file_download_link formatter.
Namespace
Drupal\Tests\file_download_link\KernelCode
protected function createTestEntity() {
$node_type = NodeType::create([
'type' => 'test_node',
'name' => 'Test Node',
]);
$node_type
->save();
// Our entity will have an image field and a file field.
$field_storage = FieldStorageConfig::create([
'field_name' => 'field_image',
'entity_type' => 'node',
'type' => 'image',
'cardinality' => -1,
]);
$field_storage
->save();
$instance = FieldConfig::create([
'field_storage' => $field_storage,
'bundle' => 'test_node',
'label' => 'Image',
]);
$instance
->save();
$field_storage = FieldStorageConfig::create([
'field_name' => 'field_file',
'entity_type' => 'node',
'type' => 'file',
]);
$field_storage
->save();
$instance = FieldConfig::create([
'field_storage' => $field_storage,
'bundle' => 'test_node',
'label' => 'File',
]);
$instance
->save();
$image_path = drupal_get_path('theme', 'stable') . '/images/core/druplicon.png';
file_put_contents('public://file.png', file_get_contents($image_path));
$file_image = File::create([
'uri' => 'public://file.png',
'filename' => 'file.png',
]);
$file_image
->save();
file_put_contents('public://file.txt', str_repeat('t', 10));
$file_file = File::create([
'uri' => 'public://file.txt',
'filename' => 'file.txt',
]);
$file_file
->save();
$entity = Node::create([
'type' => 'test_node',
'title' => 'Test Entity',
]);
$field_image_value = [
[
'target_id' => $file_image
->id(),
'alt' => 'This alt text is for the first image.',
],
[
'target_id' => $file_image
->id(),
'alt' => "When delta is 1 we should see this alt text. Let's add special chars & test them!",
],
];
$entity
->set('field_image', $field_image_value);
$entity
->set('field_file', $file_file
->id());
$entity
->save();
return $entity;
}