You are here

protected function FileDownloadLinkMediaApplicableTest::createTestNode in File Download Link 8

Helper function to create node that can be used for testing.

This node has several media fields to test with, though no media items are actually created.

Return value

Drupal\node\Entity\Node An node to be used for testing.

1 call to FileDownloadLinkMediaApplicableTest::createTestNode()
FileDownloadLinkMediaApplicableTest::setUp in tests/src/Kernel/FileDownloadLinkMediaApplicableTest.php

File

tests/src/Kernel/FileDownloadLinkMediaApplicableTest.php, line 119

Class

FileDownloadLinkMediaApplicableTest
Class for testing file_download_link_media formatter.

Namespace

Drupal\Tests\file_download_link\Kernel

Code

protected function createTestNode() {
  $node_type = NodeType::create([
    'type' => 'test_node',
    'name' => 'Test Node',
  ]);
  $node_type
    ->save();

  // Just image media.
  $field_storage = FieldStorageConfig::create([
    'field_name' => 'field_image',
    'entity_type' => 'node',
    'type' => 'entity_reference',
    'settings' => [
      'target_type' => 'media',
    ],
  ]);
  $field_storage
    ->save();
  $instance = FieldConfig::create([
    'field_storage' => $field_storage,
    'bundle' => 'test_node',
    'label' => 'Media',
    'settings' => [
      'handler_settings' => [
        'target_bundles' => [
          'image_media',
        ],
      ],
    ],
  ]);
  $instance
    ->save();

  // Just file media.
  $field_storage = FieldStorageConfig::create([
    'field_name' => 'field_file',
    'entity_type' => 'node',
    'type' => 'entity_reference',
    'settings' => [
      'target_type' => 'media',
    ],
  ]);
  $field_storage
    ->save();
  $instance = FieldConfig::create([
    'field_storage' => $field_storage,
    'bundle' => 'test_node',
    'label' => 'Media',
    'settings' => [
      'handler_settings' => [
        'target_bundles' => [
          'file_media',
        ],
      ],
    ],
  ]);
  $instance
    ->save();

  // Just oembed media.
  $field_storage = FieldStorageConfig::create([
    'field_name' => 'field_oembed',
    'entity_type' => 'node',
    'type' => 'entity_reference',
    'settings' => [
      'target_type' => 'media',
    ],
  ]);
  $field_storage
    ->save();
  $instance = FieldConfig::create([
    'field_storage' => $field_storage,
    'bundle' => 'test_node',
    'label' => 'Media',
    'settings' => [
      'handler_settings' => [
        'target_bundles' => [
          'oembed_media',
        ],
      ],
    ],
  ]);
  $instance
    ->save();

  // Image and file.
  $field_storage = FieldStorageConfig::create([
    'field_name' => 'field_both',
    'entity_type' => 'node',
    'type' => 'entity_reference',
    'settings' => [
      'target_type' => 'media',
    ],
  ]);
  $field_storage
    ->save();
  $instance = FieldConfig::create([
    'field_storage' => $field_storage,
    'bundle' => 'test_node',
    'label' => 'Media',
    'settings' => [
      'handler_settings' => [
        'target_bundles' => [
          'file_media',
          'image_media',
        ],
      ],
    ],
  ]);
  $instance
    ->save();

  // All media.
  $field_storage = FieldStorageConfig::create([
    'field_name' => 'field_all',
    'entity_type' => 'node',
    'type' => 'entity_reference',
    'settings' => [
      'target_type' => 'media',
    ],
  ]);
  $field_storage
    ->save();
  $instance = FieldConfig::create([
    'field_storage' => $field_storage,
    'bundle' => 'test_node',
    'label' => 'Media',
  ]);
  $instance
    ->save();
  $node = Node::create([
    'type' => 'test_node',
    'title' => 'Test Entity',
  ]);
  $node
    ->set('status', 1);
  $node
    ->save();
  return $node;
}