You are here

public function FileUploadResourceTestBase::setUp in Drupal 8

Same name and namespace in other branches
  1. 9 core/modules/rest/tests/src/Functional/FileUploadResourceTestBase.php \Drupal\Tests\rest\Functional\FileUploadResourceTestBase::setUp()
  2. 10 core/modules/rest/tests/src/Functional/FileUploadResourceTestBase.php \Drupal\Tests\rest\Functional\FileUploadResourceTestBase::setUp()

Overrides ResourceTestBase::setUp

File

core/modules/rest/tests/src/Functional/FileUploadResourceTestBase.php, line 94

Class

FileUploadResourceTestBase
Tests binary data file upload route.

Namespace

Drupal\Tests\rest\Functional

Code

public function setUp() {
  parent::setUp();
  $this->fileStorage = $this->container
    ->get('entity_type.manager')
    ->getStorage('file');

  // Add a file field.
  $this->fieldStorage = FieldStorageConfig::create([
    'entity_type' => 'entity_test',
    'field_name' => 'field_rest_file_test',
    'type' => 'file',
    'settings' => [
      'uri_scheme' => 'public',
    ],
  ])
    ->setCardinality(FieldStorageDefinitionInterface::CARDINALITY_UNLIMITED);
  $this->fieldStorage
    ->save();
  $this->field = FieldConfig::create([
    'entity_type' => 'entity_test',
    'field_name' => 'field_rest_file_test',
    'bundle' => 'entity_test',
    'settings' => [
      'file_directory' => 'foobar',
      'file_extensions' => 'txt',
      'max_filesize' => '',
    ],
  ])
    ->setLabel('Test file field')
    ->setTranslatable(FALSE);
  $this->field
    ->save();

  // Create an entity that a file can be attached to.
  $this->entity = EntityTest::create([
    'name' => 'Llama',
    'type' => 'entity_test',
  ]);
  $this->entity
    ->setOwnerId(isset($this->account) ? $this->account
    ->id() : 0);
  $this->entity
    ->save();

  // Provision entity_test resource.
  $this->resourceConfigStorage
    ->create([
    'id' => 'entity.entity_test',
    'granularity' => RestResourceConfigInterface::RESOURCE_GRANULARITY,
    'configuration' => [
      'methods' => [
        'POST',
      ],
      'formats' => [
        static::$format,
      ],
      'authentication' => [
        static::$auth,
      ],
    ],
    'status' => TRUE,
  ])
    ->save();

  // Provisioning the file upload REST resource without the File REST resource
  // does not make sense.
  $this->resourceConfigStorage
    ->create([
    'id' => 'entity.file',
    'granularity' => RestResourceConfigInterface::RESOURCE_GRANULARITY,
    'configuration' => [
      'methods' => [
        'GET',
      ],
      'formats' => [
        static::$format,
      ],
      'authentication' => isset(static::$auth) ? [
        static::$auth,
      ] : [],
    ],
    'status' => TRUE,
  ])
    ->save();
  $this
    ->refreshTestStateAfterRestConfigChange();
}