media_internet.test in D7 Media 7.2
Same filename and directory in other branches
Tests for media_internet.module.
File
modules/media_internet/tests/media_internet.testView source
<?php
/**
* @file
* Tests for media_internet.module.
*/
/**
* Provides methods specifically for testing Media Internet module's remote media handling.
*/
class MediaInternetTestHelper extends DrupalWebTestCase {
function setUp() {
// Since this is a base class for many test cases, support the same
// flexibility that DrupalWebTestCase::setUp() has for the modules to be
// passed in as either an array or a variable number of string arguments.
$modules = func_get_args();
if (isset($modules[0]) && is_array($modules[0])) {
$modules = $modules[0];
}
$modules[] = 'media_internet';
parent::setUp($modules);
}
/**
* Retrieves a sample file of the specified type.
*/
function getTestFile($type_name, $size = NULL) {
// Get a file to upload.
$file = current($this
->drupalGetTestFiles($type_name, $size));
// Add a filesize property to files as would be read by file_load().
$file->filesize = filesize($file->uri);
return $file;
}
/**
* Retrieves the fid of the last inserted file.
*/
function getLastFileId() {
return (int) db_query('SELECT MAX(fid) FROM {file_managed}')
->fetchField();
}
/**
* Get a file from the database based on its filename.
*
* @param $filename
* A file filename, usually generated by $this->randomName().
* @param $reset
* (optional) Whether to reset the internal file_load() cache.
*
* @return
* A file object matching $filename.
*/
function getFileByFilename($filename, $reset = FALSE) {
$files = file_load_multiple(array(), array(
'filename' => $filename,
), $reset);
// Load the first file returned from the database.
$returned_file = reset($files);
return $returned_file;
}
protected function createFileType($overrides = array()) {
$type = new stdClass();
$type->type = 'test';
$type->label = "Test";
$type->description = '';
$type->mimetypes = array(
'image/jpeg',
'image/gif',
'image/png',
'image/tiff',
);
foreach ($overrides as $k => $v) {
$type->{$k} = $v;
}
file_type_save($type);
return $type;
}
}
/**
* Tests the media browser 'Web' tab.
*/
class MediaInternetBrowserWebTabTestCase extends MediaInternetTestHelper {
public static function getInfo() {
return array(
'name' => 'Media browser web tab test',
'description' => 'Tests the media browser web tab.',
'group' => 'Media Internet',
);
}
function setUp() {
parent::setUp();
$web_user = $this
->drupalCreateUser(array(
'access media browser',
'add media from remote sources',
));
$this
->drupalLogin($web_user);
}
/**
* Tests that the views sorting works on the media browser 'Library' tab.
*/
function testMediaBrowserWebTab() {
// Load only the 'Library' tab of the media browser.
$options = array(
'query' => array(
'enabledPlugins' => array(
'media_internet' => 'media_internet',
),
),
);
$this
->drupalGet('media/browser', $options);
$this
->assertResponse(200);
// Check that the web tab is available and has an 'embed code' field.
$this
->assertRaw(t('Web'), t('The web tab was found.'));
$this
->assertFieldByName('embed_code', '', t('Embed code form field found.'));
}
}
/**
* Test the default MediaInternetFileHandler provider.
*/
class MediaInternetRemoteFileTestCase extends MediaInternetTestHelper {
public static function getInfo() {
return array(
'name' => 'Remote media file handler provider',
'description' => 'Test the default remote file handler provider.',
'group' => 'Media Internet',
);
}
function setUp() {
parent::setUp();
// Disable the private file system which is automatically enabled by
// DrupalTestCase so we can test the upload wizard correctly.
variable_del('file_private_path');
$web_user = $this
->drupalCreateUser(array(
'create files',
'add media from remote sources',
));
$this
->drupalLogin($web_user);
}
/**
* Tests the default remote file handler.
*/
function testRemoteFileHandling() {
// Step 1: Add a basic document file by providing a URL to the file.
$edit = array();
$edit['embed_code'] = file_create_url('README.txt');
$this
->drupalPost('file/add/web', $edit, t('Next'));
// Check that the file exists in the database.
$fid = $this
->getLastFileId();
$file = file_load($fid);
$this
->assertTrue($file, t('File found in database.'));
// Check that the video file has been uploaded.
$this
->assertRaw(t('!type %name was uploaded.', array(
'!type' => 'Document',
'%name' => $file->filename,
)), t('Document file uploaded.'));
}
}
/**
* Tests custom media provider APIs.
*/
class MediaInternetProviderTestCase extends MediaInternetTestHelper {
public static function getInfo() {
return array(
'name' => 'Custom media provider test',
'description' => 'Tests the custom media provider APIs.',
'group' => 'Media Internet',
);
}
function setUp() {
parent::setUp('media_internet_test');
// Disable the private file system which is automatically enabled by
// DrupalTestCase so we can test the upload wizard correctly.
variable_del('file_private_path');
// Enable media_internet_test.module's hook_media_internet_providers()
// implementation.
variable_set('media_internet_test_media_internet_providers', TRUE);
$web_user = $this
->drupalCreateUser(array(
'create files',
'view own private files',
'add media from remote sources',
));
$this
->drupalLogin($web_user);
}
/**
* Test the basic file upload wizard functionality.
*/
function testMediaInternetCustomProviderWizardBasic() {
$this
->drupalGet('file/add/web');
$this
->assertResponse(200);
// Check that the provider is listed as supported.
$this
->assertRaw(t('Supported internet media providers: !providers.', array(
'!providers' => '<strong>' . 'Media Internet Test' . '</strong>',
)), t('The example media provider is enabled.'));
// Enable media_internet_test.module's
// hook_media_browser_plugin_info_alter_alter() implementation and ensure it
// is working as designed.
variable_set('media_internet_test_media_internet_providers_alter', TRUE);
$this
->drupalGet('file/add/web');
$this
->assertRaw(t('Supported internet media providers: !providers.', array(
'!providers' => '<strong>' . 'Altered provider title' . '</strong>',
)), t('The example media provider was successfully altered.'));
// Step 1: Upload a basic video file.
$edit = array();
$edit['embed_code'] = 'http://www.example.com/video/123';
$this
->drupalPost('file/add/web', $edit, t('Next'));
// Check that the file exists in the database.
$fid = $this
->getLastFileId();
$file = file_load($fid);
$this
->assertTrue($file, t('File found in database.'));
// Check that the video file has been uploaded.
$this
->assertRaw(t('!type %name was uploaded.', array(
'!type' => 'Video',
'%name' => $file->filename,
)), t('Video file uploaded.'));
}
/**
* Test the file upload wizard type step.
*/
function testMediaInternetCustomProviderWizardTypes() {
// Create multiple file types with the same mime types.
$this
->createFileType(array(
'type' => 'video1',
'label' => 'Video 1',
'mimetypes' => array(
'video/mediainternettest',
),
));
$this
->createFileType(array(
'type' => 'video2',
'label' => 'Video 2',
'mimetypes' => array(
'video/mediainternettest',
),
));
// Step 1: Upload a basic video file.
$edit = array();
$edit['embed_code'] = 'http://www.example.com/video/123';
$this
->drupalPost('file/add/web', $edit, t('Next'));
// Step 2: File type selection.
$edit = array();
$edit['type'] = 'video2';
$this
->drupalPost(NULL, $edit, t('Next'));
// Check that the file exists in the database.
$fid = $this
->getLastFileId();
$file = file_load($fid);
$this
->assertTrue($file, t('File found in database.'));
// Check that the video file has been uploaded.
$this
->assertRaw(t('!type %name was uploaded.', array(
'!type' => 'Video 2',
'%name' => $file->filename,
)), t('Video 2 file uploaded.'));
}
/**
* Test the file upload wizard scheme step.
*/
function testMediaInternetCustomProviderWizardSchemes() {
// Enable the private file system.
variable_set('file_private_path', $this->private_files_directory);
// Step 1: Upload a basic video file.
$edit = array();
$edit['embed_code'] = 'http://www.example.com/video/123';
$this
->drupalPost('file/add/web', $edit, t('Next'));
// Step 3: Users should not be able to select a scheme for files with
// read-only stream wrappers.
$this
->assertNoFieldByName('scheme');
// Check that the file exists in the database.
$fid = $this
->getLastFileId();
$file = file_load($fid);
$this
->assertTrue($file, t('File found in database.'));
// Check that the video file has been uploaded.
$this
->assertRaw(t('!type %name was uploaded.', array(
'!type' => 'Video',
'%name' => $file->filename,
)), t('Video file uploaded.'));
}
/**
* Test the file upload wizard field step.
*/
function testMediaInternetCustomProviderWizardFields() {
$filename = $this
->randomName();
// Add a text field to the video file type.
$field_name = drupal_strtolower($this
->randomName() . '_field_name');
$field = array(
'field_name' => $field_name,
'type' => 'text',
);
field_create_field($field);
$instance = array(
'field_name' => $field_name,
'entity_type' => 'file',
'bundle' => 'video',
'label' => $this
->randomName() . '_label',
);
field_create_instance($instance);
// Step 1: Upload a basic video file.
$edit = array();
$edit['embed_code'] = 'http://www.example.com/video/123';
$this
->drupalPost('file/add/web', $edit, t('Next'));
// Step 4: Attached fields.
$edit = array();
$edit['filename'] = $filename;
$edit[$field_name . '[' . LANGUAGE_NONE . '][0][value]'] = $this
->randomName();
$this
->drupalPost(NULL, $edit, t('Save'));
// Check that the file exists in the database.
$fid = $this
->getLastFileId();
$file = file_load($fid);
$this
->assertTrue($file, t('File found in database.'));
// Check that the video file has been uploaded.
$this
->assertRaw(t('!type %name was uploaded.', array(
'!type' => 'Video',
'%name' => $filename,
)), t('Video file uploaded.'));
}
/**
* Test skipping each of the file upload wizard steps.
*/
function testMediaInternetCustomProviderWizardStepSkipping() {
$filename = $this
->randomName();
// Ensure that the file is affected by every step.
variable_set('file_private_path', $this->private_files_directory);
$this
->createFileType(array(
'type' => 'video1',
'label' => 'Video 1',
'mimetypes' => array(
'video/mediainternettest',
),
));
$this
->createFileType(array(
'type' => 'video2',
'label' => 'Video 2',
'mimetypes' => array(
'video/mediainternettest',
),
));
$field_name = drupal_strtolower($this
->randomName() . '_field_name');
$field = array(
'field_name' => $field_name,
'type' => 'text',
);
field_create_field($field);
$instance = array(
'field_name' => $field_name,
'entity_type' => 'file',
'bundle' => 'video2',
'label' => $this
->randomName() . '_label',
);
field_create_instance($instance);
// Test skipping each upload wizard step.
foreach (array(
'types',
'schemes',
'fields',
) as $step) {
// Step to skip.
switch ($step) {
case 'types':
variable_set('file_entity_file_upload_wizard_skip_file_type', TRUE);
break;
case 'schemes':
variable_set('file_entity_file_upload_wizard_skip_scheme', TRUE);
break;
case 'fields':
variable_set('file_entity_file_upload_wizard_skip_fields', TRUE);
break;
}
// Step 1: Upload a basic video file.
$edit = array();
$edit['embed_code'] = 'http://www.example.com/video/123';
$this
->drupalPost('file/add/web', $edit, t('Next'));
// Step 2: File type selection.
if ($step != 'types') {
$edit = array();
$edit['type'] = 'video2';
$this
->drupalPost(NULL, $edit, t('Next'));
}
// Step 3: Users should not be able to select a scheme for files with
// read-only stream wrappers.
$this
->assertNoFieldByName('scheme');
// Step 4: Attached fields.
if ($step != 'fields') {
// Skipping file type selection essentially skips this step as well
// because the file will not be assigned a type so no fields will be
// available.
if ($step != 'types') {
$edit = array();
$edit['filename'] = $filename;
$edit[$field_name . '[' . LANGUAGE_NONE . '][0][value]'] = $this
->randomName();
$this
->drupalPost(NULL, $edit, t('Save'));
}
}
// Check that the file exists in the database.
$fid = $this
->getLastFileId();
$file = file_load($fid);
$this
->assertTrue($file, t('File found in database.'));
// Determine the file's file type.
$type = file_type_load($file->type);
// Check that the video file has been uploaded.
$this
->assertRaw(t('!type %name was uploaded.', array(
'!type' => $type->label,
'%name' => $file->filename,
)), t('Video file uploaded.'));
// Reset 'skip' variables.
variable_del('file_entity_file_upload_wizard_skip_file_type');
variable_del('file_entity_file_upload_wizard_skip_scheme');
variable_del('file_entity_file_upload_wizard_skip_fields');
}
}
}
Classes
Name | Description |
---|---|
MediaInternetBrowserWebTabTestCase | Tests the media browser 'Web' tab. |
MediaInternetProviderTestCase | Tests custom media provider APIs. |
MediaInternetRemoteFileTestCase | Test the default MediaInternetFileHandler provider. |
MediaInternetTestHelper | Provides methods specifically for testing Media Internet module's remote media handling. |