protected function MediaBrowserPlusTestBase::createTestFile in Media Browser Plus 7.3
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.
string $scheme: The scheme where the file is created.
Return value
file_entity|FALSE The file entity of the newly created file or FALSE on error.
7 calls to MediaBrowserPlusTestBase::createTestFile()
- MediaBrowserPlusBypassTest::testBypassDueProperty in tests/
media_browser_plus.mbp_bypass.test - Test the ability to bypass folder processing setting mbp_bypass property.
- MediaBrowserPlusRootFolderTest::testMovingRootFolder in tests/
media_browser_plus.root_folder.test - Test the ability to move the root folder for media files.
- MediaBrowserPlusTest::testFileMovement in tests/
media_browser_plus.test - Test the ability to delete folders.
- MediaBrowserPlusTest::testFolderDeletion in tests/
media_browser_plus.test - Test the ability to delete folders.
- MediaBrowserPlusTest::testFolderDeletionWithUsedFiles in tests/
media_browser_plus.test - Test the ability to delete folders when files are used.
File
- tests/
media_browser_plus.base.test, line 117 - Media Browser Plus tests base.
Class
- MediaBrowserPlusTestBase
- Provides some basic functionality for all MBP tests.
Code
protected function createTestFile($mime = 'text/plain', $folder = NULL, $scheme = 'public') {
$tmp_file = 'temporary://' . uniqid('mbp_test_file');
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);
$tmp_file = $tmp_file . '.jpg';
imagejpeg($im, drupal_realpath($tmp_file));
break;
case 'text/plain':
default:
$type = FILE_TYPE_NONE;
$tmp_file = $tmp_file . '.txt';
$fp = fopen($tmp_file, 'w');
fwrite($fp, str_repeat('01', 512));
fclose($fp);
break;
}
$source = new stdClass();
$source->uri = $tmp_file;
$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_path = file_stream_wrapper_uri_normalize($scheme . '://' . media_browser_plus_construct_dir_path($folder));
$file = file_move($source, $destination_path, FILE_CREATE_DIRECTORY);
return $file;
}