You are here

protected function MediaFileFieldTestCase::createFileEntity in D7 Media 7.2

Same name and namespace in other branches
  1. 7.4 tests/media.test \MediaFileFieldTestCase::createFileEntity()
  2. 7.3 tests/media.test \MediaFileFieldTestCase::createFileEntity()

Creates a new file entity.

Parameters

$settings: A list of settings that will be added to the entity defaults.

6 calls to MediaFileFieldTestCase::createFileEntity()
MediaAdminTestCase::testFilesAdminPages in tests/media.test
Tests files overview with different user permissions.
MediaAdminTestCase::testFilesAdminSort in tests/media.test
Tests that the table sorting works on the files admin pages.
MediaBrowserLibraryTestCase::testFilesBrowserLibrary in tests/media.test
Tests media browser 'Library' tab with different user permissions.
MediaBrowserLibraryTestCase::testFilesBrowserSort in tests/media.test
Tests that the views sorting works on the media browser 'Library' tab.
MediaBrowserMyFilesTestCase::testFilesBrowserMyFiles in tests/media.test
Tests media browser 'My files' tab with different user permissions.

... See full list

File

tests/media.test, line 48
Tests for media.module.

Class

MediaFileFieldTestCase
Provides methods specifically for testing Media module's field handling.

Code

protected function createFileEntity($settings = array()) {
  $file = new stdClass();

  // Populate defaults array.
  $settings += array(
    'filepath' => 'Файл для тестирования ' . $this
      ->randomName(),
    // Prefix with non-latin characters to ensure that all file-related tests work with international filenames.
    'filemime' => 'text/plain',
    'uid' => 1,
    'timestamp' => REQUEST_TIME,
    'status' => FILE_STATUS_PERMANENT,
    'contents' => "file_put_contents() doesn't seem to appreciate empty strings so let's put in some data.",
    'scheme' => file_default_scheme(),
    'type' => NULL,
  );
  $filepath = $settings['scheme'] . '://' . $settings['filepath'];
  file_put_contents($filepath, $settings['contents']);
  $this
    ->assertTrue(is_file($filepath), t('The test file exists on the disk.'), 'Create test file');
  $file = new stdClass();
  $file->uri = $filepath;
  $file->filename = drupal_basename($file->uri);
  $file->filemime = $settings['filemime'];
  $file->uid = $settings['uid'];
  $file->timestamp = $settings['timestamp'];
  $file->filesize = filesize($file->uri);
  $file->status = $settings['status'];
  $file->type = $settings['type'];

  // The file type is used as a bundle key, and therefore, must not be NULL.
  if (!isset($file->type)) {
    $file->type = FILE_TYPE_NONE;
  }

  // If the file isn't already assigned a real type, determine what type should
  // be assigned to it.
  if ($file->type === FILE_TYPE_NONE) {
    $type = file_get_type($file);
    if (isset($type)) {
      $file->type = $type;
    }
  }

  // Write the record directly rather than calling file_save() so we don't
  // invoke the hooks.
  $this
    ->assertNotIdentical(drupal_write_record('file_managed', $file), FALSE, t('The file was added to the database.'), 'Create test file');
  return $file;
}