first import
This commit is contained in:
113
sites/all/modules/file_entity/tests/file_entity.test
Normal file
113
sites/all/modules/file_entity/tests/file_entity.test
Normal file
@@ -0,0 +1,113 @@
|
||||
<?php
|
||||
|
||||
/**
|
||||
* @file
|
||||
* Test integration for the file_entity module.
|
||||
*/
|
||||
|
||||
class FileEntityTestHelper extends DrupalWebTestCase {
|
||||
protected $files = array();
|
||||
|
||||
function setUp($modules = array()) {
|
||||
$modules[] = 'file_entity';
|
||||
parent::setUp($modules);
|
||||
|
||||
$this->setUpFiles();
|
||||
}
|
||||
|
||||
function setUpFiles() {
|
||||
$types = array('text', 'image');
|
||||
foreach ($types as $type) {
|
||||
foreach ($this->drupalGetTestFiles($type) as $file) {
|
||||
$this->files[$type][] = file_save($file);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
class FileEntityUnitTestCase extends FileEntityTestHelper {
|
||||
public static function getInfo() {
|
||||
return array(
|
||||
'name' => 'File entity unit tests',
|
||||
'description' => 'Test basic file entity funcitonality.',
|
||||
'group' => 'File entity',
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* Regression tests for core issue http://drupal.org/node/1239376.
|
||||
*/
|
||||
function testMimeTypeMappings() {
|
||||
$tests = array(
|
||||
'public://test.ogg' => 'audio/ogg',
|
||||
'public://test.mkv' => 'video/x-m4v',
|
||||
'public://test.mka' => 'audio/x-matroska',
|
||||
'public://test.mkv' => 'video/x-matroska',
|
||||
'public://test.webp' => 'image/webp',
|
||||
);
|
||||
foreach ($tests as $input => $expected) {
|
||||
$this->assertEqual(file_get_mimetype($input), $expected);
|
||||
}
|
||||
}
|
||||
|
||||
function testFileEntity() {
|
||||
$file = reset($this->files['text']);
|
||||
|
||||
// Test entity ID, revision ID, and bundle.
|
||||
$ids = entity_extract_ids('file', $file);
|
||||
$this->assertIdentical($ids, array($file->fid, NULL, 'text'));
|
||||
|
||||
// Test the entity URI callback.
|
||||
$uri = entity_uri('file', $file);
|
||||
$this->assertEqual($uri['path'], "file/{$file->fid}");
|
||||
}
|
||||
}
|
||||
|
||||
class FileEntityTokenTestCase extends FileEntityTestHelper {
|
||||
public static function getInfo() {
|
||||
return array(
|
||||
'name' => 'File entity tokens',
|
||||
'description' => 'Test the file entity tokens.',
|
||||
'group' => 'File entity',
|
||||
);
|
||||
}
|
||||
|
||||
function testFileEntityTokens() {
|
||||
$tokens = array(
|
||||
'type' => 'Text',
|
||||
'type:name' => 'Text',
|
||||
'type:machine-name' => 'text',
|
||||
'type:count' => count($this->files['text']),
|
||||
);
|
||||
$this->assertTokens('file', array('file' => $this->files['text'][0]), $tokens);
|
||||
|
||||
$tokens = array(
|
||||
'type' => 'Image',
|
||||
'type:name' => 'Image',
|
||||
'type:machine-name' => 'image',
|
||||
'type:count' => count($this->files['image']),
|
||||
);
|
||||
$this->assertTokens('file', array('file' => $this->files['image'][0]), $tokens);
|
||||
}
|
||||
|
||||
function assertTokens($type, array $data, array $tokens, array $options = array()) {
|
||||
$token_input = drupal_map_assoc(array_keys($tokens));
|
||||
$values = token_generate($type, $token_input, $data, $options);
|
||||
foreach ($tokens as $token => $expected) {
|
||||
if (!isset($expected)) {
|
||||
$this->assertTrue(!isset($values[$token]), t("Token value for [@type:@token] was not generated.", array('@type' => $type, '@token' => $token)));
|
||||
}
|
||||
elseif (!isset($values[$token])) {
|
||||
$this->fail(t("Token value for [@type:@token] was not generated.", array('@type' => $type, '@token' => $token)));
|
||||
}
|
||||
elseif (!empty($options['regex'])) {
|
||||
$this->assertTrue(preg_match('/^' . $expected . '$/', $values[$token]), t("Token value for [@type:@token] was '@actual', matching regular expression pattern '@expected'.", array('@type' => $type, '@token' => $token, '@actual' => $values[$token], '@expected' => $expected)));
|
||||
}
|
||||
else {
|
||||
$this->assertIdentical($values[$token], $expected, t("Token value for [@type:@token] was '@actual', expected value '@expected'.", array('@type' => $type, '@token' => $token, '@actual' => $values[$token], '@expected' => $expected)));
|
||||
}
|
||||
}
|
||||
|
||||
return $values;
|
||||
}
|
||||
}
|
13
sites/all/modules/file_entity/tests/file_entity_test.info
Normal file
13
sites/all/modules/file_entity/tests/file_entity_test.info
Normal file
@@ -0,0 +1,13 @@
|
||||
name = "File Entity Test"
|
||||
description = "Support module for File Entity tests."
|
||||
package = Testing
|
||||
core = 7.x
|
||||
dependencies[] = file_entity
|
||||
hidden = TRUE
|
||||
|
||||
; Information added by drupal.org packaging script on 2012-01-12
|
||||
version = "7.x-2.0-unstable3"
|
||||
core = "7.x"
|
||||
project = "file_entity"
|
||||
datestamp = "1326409240"
|
||||
|
100
sites/all/modules/file_entity/tests/file_entity_test.module
Normal file
100
sites/all/modules/file_entity/tests/file_entity_test.module
Normal file
@@ -0,0 +1,100 @@
|
||||
<?php
|
||||
|
||||
/**
|
||||
* @file
|
||||
* File Entity Test
|
||||
*/
|
||||
|
||||
|
||||
/**
|
||||
* Implements hook_menu().
|
||||
*/
|
||||
function file_entity_test_menu() {
|
||||
$items = array();
|
||||
|
||||
$items['file-entity-test/file/add'] = array(
|
||||
'title' => 'Add file',
|
||||
'page callback' => 'drupal_get_form',
|
||||
'page arguments' => array('file_entity_test_add_form'),
|
||||
'access arguments' => array('administer site configuration'),
|
||||
'file' => 'file_entity_test.pages.inc',
|
||||
);
|
||||
$items['file-entity-test/file/%file'] = array(
|
||||
'title' => 'View file',
|
||||
'page callback' => 'file_entity_test_view_page',
|
||||
'page arguments' => array(2),
|
||||
'access arguments' => array('administer site configuration'),
|
||||
'file' => 'file_entity_test.pages.inc',
|
||||
);
|
||||
$items['file-entity-test/file/%file/view'] = array(
|
||||
'title' => 'View',
|
||||
'type' => MENU_DEFAULT_LOCAL_TASK,
|
||||
'weight' => -10,
|
||||
);
|
||||
$items['file-entity-test/file/%file/preview'] = array(
|
||||
'title' => 'Preview',
|
||||
'page callback' => 'file_entity_test_preview_page',
|
||||
'page arguments' => array(2),
|
||||
'access arguments' => array('administer site configuration'),
|
||||
'weight' => 0,
|
||||
'type' => MENU_LOCAL_TASK,
|
||||
'file' => 'file_entity_test.pages.inc',
|
||||
);
|
||||
$items['file-entity-test/file/%file/edit'] = array(
|
||||
'title' => 'Edit',
|
||||
'page callback' => 'drupal_get_form',
|
||||
'page arguments' => array('file_entity_test_edit_form', 2),
|
||||
'access arguments' => array('administer site configuration'),
|
||||
'weight' => 1,
|
||||
'type' => MENU_LOCAL_TASK,
|
||||
'file' => 'file_entity_test.pages.inc',
|
||||
);
|
||||
|
||||
return $items;
|
||||
}
|
||||
|
||||
/**
|
||||
* Implements hook_file_type_info().
|
||||
*/
|
||||
function file_entity_test_file_type_info() {
|
||||
return array(
|
||||
'file_entity_test' => array(
|
||||
'label' => t('Test'),
|
||||
'description' => t('A file type defined by the File Entity Test module. Used for testing only.'),
|
||||
'claim callback' => 'file_entity_test_file_type_file_entity_test_claim',
|
||||
'default view callback' => 'file_entity_test_file_type_file_entity_test_default_view',
|
||||
'weight' => 100,
|
||||
),
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* Implements hook_file_type_TYPE_claim().
|
||||
*
|
||||
* Returns TRUE if the passed in file should be assigned the 'file_entity_test'
|
||||
* file type.
|
||||
*/
|
||||
function file_entity_test_file_type_file_entity_test_claim($file) {
|
||||
return TRUE;
|
||||
}
|
||||
|
||||
/**
|
||||
* Implements hook_file_type_TYPE_default_view().
|
||||
*/
|
||||
function file_entity_test_file_type_file_entity_test_default_view($file, $view_mode, $langcode) {
|
||||
return array(
|
||||
'#type' => 'link',
|
||||
'#title' => $file->filename,
|
||||
'#href' => file_create_url($file->uri),
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* Implements hook_entity_info_alter().
|
||||
*/
|
||||
function file_entity_test_entity_info_alter(&$entity_info) {
|
||||
$entity_info['file']['view modes']['file_entity_test_preview'] = array(
|
||||
'label' => t('Test Preview'),
|
||||
'custom settings' => TRUE,
|
||||
);
|
||||
}
|
@@ -0,0 +1,93 @@
|
||||
<?php
|
||||
|
||||
/**
|
||||
* @file
|
||||
* Test pages for the File Entity Test module.
|
||||
*/
|
||||
|
||||
/**
|
||||
* Form callback; upload a file.
|
||||
*/
|
||||
function file_entity_test_add_form($form, &$form_state) {
|
||||
$form['file'] = array(
|
||||
'#type' => 'managed_file',
|
||||
'#required' => TRUE,
|
||||
'#title' => 'File',
|
||||
'#upload_location' => 'public://',
|
||||
);
|
||||
$form['actions'] = array('#type' => 'actions');
|
||||
$form['actions']['submit'] = array(
|
||||
'#type' => 'submit',
|
||||
'#value' => t('Save'),
|
||||
);
|
||||
|
||||
return $form;
|
||||
}
|
||||
|
||||
/**
|
||||
* Form submit callback; save the uploaded file.
|
||||
*/
|
||||
function file_entity_test_add_form_submit($form, &$form_state) {
|
||||
$file = file_load($form_state['values']['file']);
|
||||
if (!$file->status) {
|
||||
$file->status = FILE_STATUS_PERMANENT;
|
||||
file_save($file);
|
||||
}
|
||||
drupal_set_message(t('Your file has been saved.'));
|
||||
$form_state['redirect'] = 'file-entity-test/file/' . $file->fid;
|
||||
}
|
||||
|
||||
/**
|
||||
* Page callback; view a file.
|
||||
*/
|
||||
function file_entity_test_view_page($file) {
|
||||
return file_view($file, 'full');
|
||||
}
|
||||
|
||||
/**
|
||||
* Page callback; preview a file.
|
||||
*/
|
||||
function file_entity_test_preview_page($file) {
|
||||
return file_view($file, 'file_entity_test_preview');
|
||||
}
|
||||
|
||||
/**
|
||||
* Form callback; edit a file.
|
||||
*/
|
||||
function file_entity_test_edit_form($form, &$form_state, $file) {
|
||||
$form_state['file'] = $file;
|
||||
field_attach_form('file', $file, $form, $form_state);
|
||||
$form['file'] = file_view($file, 'file_entity_test_preview');
|
||||
|
||||
// Add internal file properties needed by
|
||||
// file_entity_test_edit_form_validate().
|
||||
foreach (array('fid', 'type') as $key) {
|
||||
$form[$key] = array('#type' => 'value', '#value' => $file->$key);
|
||||
}
|
||||
|
||||
$form['actions'] = array('#type' => 'actions');
|
||||
$form['actions']['submit'] = array(
|
||||
'#type' => 'submit',
|
||||
'#value' => t('Save'),
|
||||
);
|
||||
|
||||
return $form;
|
||||
}
|
||||
|
||||
/**
|
||||
* Form validation handler for the file edit form.
|
||||
*/
|
||||
function file_entity_test_edit_form_validate($form, &$form_state) {
|
||||
entity_form_field_validate('file', $form, $form_state);
|
||||
}
|
||||
|
||||
/**
|
||||
* Form submit handler for the file edit form
|
||||
*/
|
||||
function file_entity_test_edit_form_submit($form, &$form_state) {
|
||||
$file = $form_state['file'];
|
||||
entity_form_submit_build_entity('file', $file, $form, $form_state);
|
||||
file_save($file);
|
||||
drupal_set_message(t('Your changes to the file have been saved.'));
|
||||
$form_state['redirect'] = 'file-entity-test/file/' . $file->fid;
|
||||
}
|
Reference in New Issue
Block a user