You are here

protected function MediaBrowserPlusTest::createTestFile in Media Browser Plus 7.2

Creates a test file.

Parameters

string $mime: Allowed values text/plain, image/jpg.

null|term|integer $folder: NULL to use the root folder, term object or term id of a folder.

Return value

file_entity|FALSE The file entity of the newly created file or FALSE on error.

2 calls to MediaBrowserPlusTest::createTestFile()
MediaBrowserPlusTest::testFolderMovemet in tests/media_browser_plus.test
Test the ability to move folders.
MediaBrowserPlusTest::testMovingRootFolder in tests/media_browser_plus.test
Test the ability to move the root folder for media files.

File

tests/media_browser_plus.test, line 112
Media Browser Plus tests.

Class

MediaBrowserPlusTest
Defines media entity creation and management test cases.

Code

protected function createTestFile($mime = 'text/plain', $folder = NULL) {
  if ($tmp_file = drupal_tempnam('temporary://', 'mbp_test_')) {
    if (empty($folder)) {
      $folder = media_browser_plus_get_media_root_folder();
    }
    elseif (is_numeric($folder)) {
      $folder = taxonomy_term_load($folder);
    }
    switch ($mime) {
      case 'image/jpg':
        $type = 'image';

        // Make an image split into 4 sections with random colors.
        $im = imagecreate(800, 600);
        for ($n = 0; $n < 4; $n++) {
          $color = imagecolorallocate($im, rand(0, 255), rand(0, 255), rand(0, 255));
          $x = 800 / 2 * ($n % 2);
          $y = 600 / 2 * (int) ($n >= 2);
          imagefilledrectangle($im, $x, $y, $x + 800 / 2, $y + 600 / 2, $color);
        }

        // Make a perfect circle in the image middle.
        $color = imagecolorallocate($im, rand(0, 255), rand(0, 255), rand(0, 255));
        $smaller_dimension = min(800, 600);
        $smaller_dimension = $smaller_dimension % 2 ? $smaller_dimension : $smaller_dimension;
        imageellipse($im, 800 / 2, 600 / 2, $smaller_dimension, $smaller_dimension, $color);
        $destination = $tmp_file . '.jpg';
        imagejpeg($im, drupal_realpath($tmp_file));
        break;
      case 'text/plain':
      default:
        $type = FILE_TYPE_NONE;
        $destination = $tmp_file . '.txt';
        $fp = fopen($destination, 'w');
        fwrite($fp, str_repeat('01', 512));
        fclose($fp);
        break;
    }
    file_unmanaged_move($tmp_file, $destination);
    $source = new stdClass();
    $source->uri = $destination;
    $source->uid = 1;
    $source->filemime = $mime;
    $source->type = $type;
    $source->filename = basename($tmp_file);
    $source->field_folder[LANGUAGE_NONE][0]['tid'] = $folder->tid;

    // Move the file to the right directory.
    $destination_dir = media_browser_plus_construct_dir_path($folder);
    $file = file_move($source, $destination_dir, FILE_CREATE_DIRECTORY);
    return $file;
  }
  return FALSE;
}