1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327 |
- <?php
- class FileFieldTestCase extends DrupalWebTestCase {
- protected $admin_user;
- function setUp() {
-
-
-
- $modules = func_get_args();
- if (isset($modules[0]) && is_array($modules[0])) {
- $modules = $modules[0];
- }
- $modules[] = 'file';
- $modules[] = 'file_module_test';
- parent::setUp($modules);
- $this->admin_user = $this->drupalCreateUser(array('access content', 'access administration pages', 'administer site configuration', 'administer users', 'administer permissions', 'administer content types', 'administer nodes', 'bypass node access'));
- $this->drupalLogin($this->admin_user);
- }
-
- function getTestFile($type_name, $size = NULL) {
-
- $file = current($this->drupalGetTestFiles($type_name, $size));
-
- $file->filesize = filesize($file->uri);
- return $file;
- }
-
- function getLastFileId() {
- return (int) db_query('SELECT MAX(fid) FROM {file_managed}')->fetchField();
- }
-
- function createFileField($name, $type_name, $field_settings = array(), $instance_settings = array(), $widget_settings = array()) {
- $field = array(
- 'field_name' => $name,
- 'type' => 'file',
- 'settings' => array(),
- 'cardinality' => !empty($field_settings['cardinality']) ? $field_settings['cardinality'] : 1,
- );
- $field['settings'] = array_merge($field['settings'], $field_settings);
- field_create_field($field);
- $this->attachFileField($name, 'node', $type_name, $instance_settings, $widget_settings);
- }
-
- function attachFileField($name, $entity_type, $bundle, $instance_settings = array(), $widget_settings = array()) {
- $instance = array(
- 'field_name' => $name,
- 'label' => $name,
- 'entity_type' => $entity_type,
- 'bundle' => $bundle,
- 'required' => !empty($instance_settings['required']),
- 'settings' => array(),
- 'widget' => array(
- 'type' => 'file_generic',
- 'settings' => array(),
- ),
- );
- $instance['settings'] = array_merge($instance['settings'], $instance_settings);
- $instance['widget']['settings'] = array_merge($instance['widget']['settings'], $widget_settings);
- field_create_instance($instance);
- }
-
- function updateFileField($name, $type_name, $instance_settings = array(), $widget_settings = array()) {
- $instance = field_info_instance('node', $name, $type_name);
- $instance['settings'] = array_merge($instance['settings'], $instance_settings);
- $instance['widget']['settings'] = array_merge($instance['widget']['settings'], $widget_settings);
- field_update_instance($instance);
- }
-
- function uploadNodeFile($file, $field_name, $nid_or_type, $new_revision = TRUE, $extras = array()) {
- $langcode = LANGUAGE_NONE;
- $edit = array(
- "title" => $this->randomName(),
- 'revision' => (string) (int) $new_revision,
- );
- if (is_numeric($nid_or_type)) {
- $nid = $nid_or_type;
- }
- else {
-
- $extras['type'] = $nid_or_type;
- $node = $this->drupalCreateNode($extras);
- $nid = $node->nid;
-
- $this->drupalCreateNode(get_object_vars($node));
- $node = node_load($nid, NULL, TRUE);
- $this->assertNotEqual($nid, $node->vid, 'Node revision exists.');
- }
-
- $edit['files[' . $field_name . '_' . $langcode . '_0]'] = drupal_realpath($file->uri);
- $this->drupalPost("node/$nid/edit", $edit, t('Save'));
- return $nid;
- }
-
- function removeNodeFile($nid, $new_revision = TRUE) {
- $edit = array(
- 'revision' => (string) (int) $new_revision,
- );
- $this->drupalPost('node/' . $nid . '/edit', array(), t('Remove'));
- $this->drupalPost(NULL, $edit, t('Save'));
- }
-
- function replaceNodeFile($file, $field_name, $nid, $new_revision = TRUE) {
- $edit = array(
- 'files[' . $field_name . '_' . LANGUAGE_NONE . '_0]' => drupal_realpath($file->uri),
- 'revision' => (string) (int) $new_revision,
- );
- $this->drupalPost('node/' . $nid . '/edit', array(), t('Remove'));
- $this->drupalPost(NULL, $edit, t('Save'));
- }
-
- function assertFileExists($file, $message = NULL) {
- $message = isset($message) ? $message : format_string('File %file exists on the disk.', array('%file' => $file->uri));
- $this->assertTrue(is_file($file->uri), $message);
- }
-
- function assertFileEntryExists($file, $message = NULL) {
- entity_get_controller('file')->resetCache();
- $db_file = file_load($file->fid);
- $message = isset($message) ? $message : format_string('File %file exists in database at the correct path.', array('%file' => $file->uri));
- $this->assertEqual($db_file->uri, $file->uri, $message);
- }
-
- function assertFileNotExists($file, $message = NULL) {
- $message = isset($message) ? $message : format_string('File %file exists on the disk.', array('%file' => $file->uri));
- $this->assertFalse(is_file($file->uri), $message);
- }
-
- function assertFileEntryNotExists($file, $message) {
- entity_get_controller('file')->resetCache();
- $message = isset($message) ? $message : format_string('File %file exists in database at the correct path.', array('%file' => $file->uri));
- $this->assertFalse(file_load($file->fid), $message);
- }
-
- function assertFileIsPermanent($file, $message = NULL) {
- $message = isset($message) ? $message : format_string('File %file is permanent.', array('%file' => $file->uri));
- $this->assertTrue($file->status == FILE_STATUS_PERMANENT, $message);
- }
- }
- class FileTaxonomyTermTestCase extends DrupalWebTestCase {
- protected $admin_user;
- public static function getInfo() {
- return array(
- 'name' => 'Taxonomy term file test',
- 'description' => 'Tests adding a file to a non-node entity.',
- 'group' => 'File',
- );
- }
- public function setUp() {
- $modules[] = 'file';
- $modules[] = 'taxonomy';
- parent::setUp($modules);
- $this->admin_user = $this->drupalCreateUser(array('access content', 'access administration pages', 'administer site configuration', 'administer taxonomy'));
- $this->drupalLogin($this->admin_user);
- }
-
- protected function createAttachFileField($name, $uri_scheme) {
- $field = array(
- 'field_name' => $name,
- 'type' => 'file',
- 'settings' => array(
- 'uri_scheme' => $uri_scheme,
- ),
- 'cardinality' => 1,
- );
- field_create_field($field);
-
- $instance = array(
- 'field_name' => $name,
- 'label' => 'File',
- 'entity_type' => 'taxonomy_term',
- 'bundle' => 'tags',
- 'required' => FALSE,
- 'settings' => array(),
- 'widget' => array(
- 'type' => 'file_generic',
- 'settings' => array(),
- ),
- );
- field_create_instance($instance);
- }
-
- public function testTermFilePublic() {
- $this->_testTermFile('public');
- }
-
- public function testTermFilePrivate() {
- $this->_testTermFile('private');
- }
-
- protected function _testTermFile($uri_scheme) {
- $field_name = strtolower($this->randomName());
- $this->createAttachFileField($field_name, $uri_scheme);
-
- $file = current($this->drupalGetTestFiles('text'));
-
- $file->filesize = filesize($file->uri);
- $langcode = LANGUAGE_NONE;
- $edit = array(
- "name" => $this->randomName(),
- );
-
- $edit['files[' . $field_name . '_' . $langcode . '_0]'] = drupal_realpath($file->uri);
- $this->drupalPost("admin/structure/taxonomy/tags/add", $edit, t('Save'));
-
- $tid = db_query_range('SELECT tid FROM {taxonomy_term_data} ORDER BY tid DESC', 0, 1)->fetchField();
- $terms = entity_load('taxonomy_term', array($tid));
- $term = $terms[$tid];
- $fid = $term->{$field_name}[LANGUAGE_NONE][0]['fid'];
-
- $this->drupalGet("taxonomy/term/$tid/edit");
- $file_input_name = $field_name . '[' . LANGUAGE_NONE . '][0][fid]';
- $this->assertFieldByXpath('//input[@type="hidden" and @name="' . $file_input_name . '"]', $fid, 'File is attached on edit form.');
-
- $edit = array(
- "name" => $this->randomName(),
- );
- $this->drupalPost("taxonomy/term/$tid/edit", $edit, t('Save'));
-
- $this->drupalGet("taxonomy/term/$tid/edit");
- $file_input_name = $field_name . '[' . LANGUAGE_NONE . '][0][fid]';
- $this->assertFieldByXpath('//input[@type="hidden" and @name="' . $file_input_name . '"]', $fid, 'File is attached on edit form.');
-
- $terms = entity_load('taxonomy_term', array($tid), array(), TRUE);
- $term = $terms[$tid];
- $this->assertTrue(!empty($term->{$field_name}[LANGUAGE_NONE]), 'Term has attached files.');
- $this->assertEqual($term->{$field_name}[LANGUAGE_NONE][0]['fid'], $fid, 'Same File ID is attached to the term.');
- }
- }
- class FileManagedFileElementTestCase extends FileFieldTestCase {
- public static function getInfo() {
- return array(
- 'name' => 'Managed file element test',
- 'description' => 'Tests the managed_file element type.',
- 'group' => 'File',
- );
- }
-
- function testManagedFile() {
-
- $this->drupalGet('file/test');
- $this->assertFieldByXpath('//input[@name="files[nested_file]" and @size="13"]', NULL, 'The custom #size attribute is passed to the child upload element.');
-
-
- foreach (array(0, 1) as $tree) {
- foreach (array(0, 1) as $extended) {
- $test_file = $this->getTestFile('text');
- $path = 'file/test/' . $tree . '/' . $extended;
- $input_base_name = $tree ? 'nested_file' : 'file';
-
- $this->drupalPost($path, array(), t('Save'));
- $this->assertRaw(t('The file id is %fid.', array('%fid' => 0)), 'Submitted without a file.');
-
- $last_fid_prior = $this->getLastFileId();
- $edit = array('files[' . $input_base_name . ']' => drupal_realpath($test_file->uri));
- $this->drupalPost($path, $edit, t('Save'));
- $last_fid = $this->getLastFileId();
- $this->assertTrue($last_fid > $last_fid_prior, 'New file got saved.');
- $this->assertRaw(t('The file id is %fid.', array('%fid' => $last_fid)), 'Submit handler has correct file info.');
-
- $this->drupalPost($path . '/' . $last_fid, array(), t('Save'));
- $this->assertRaw(t('The file id is %fid.', array('%fid' => $last_fid)), 'Empty submission did not change an existing file.');
-
- foreach (array(FALSE, TRUE) as $ajax) {
-
- $last_fid_prior = $this->getLastFileId();
- $this->drupalGet($path);
- $edit = array('files[' . $input_base_name . ']' => drupal_realpath($test_file->uri));
- if ($ajax) {
- $this->drupalPostAJAX(NULL, $edit, $input_base_name . '_upload_button');
- }
- else {
- $this->drupalPost(NULL, $edit, t('Upload'));
- }
- $last_fid = $this->getLastFileId();
- $this->assertTrue($last_fid > $last_fid_prior, 'New file got uploaded.');
- $this->drupalPost(NULL, array(), t('Save'));
- $this->assertRaw(t('The file id is %fid.', array('%fid' => $last_fid)), 'Submit handler has correct file info.');
-
- $this->drupalGet($path . '/' . $last_fid);
- if ($ajax) {
- $this->drupalPostAJAX(NULL, array(), $input_base_name . '_remove_button');
- }
- else {
- $this->drupalPost(NULL, array(), t('Remove'));
- }
- $this->drupalPost(NULL, array(), t('Save'));
- $this->assertRaw(t('The file id is %fid.', array('%fid' => 0)), 'Submission after file removal was successful.');
-
- $this->drupalGet($path);
- $edit = array('files[' . $input_base_name . ']' => drupal_realpath($test_file->uri));
- if ($ajax) {
- $this->drupalPostAJAX(NULL, $edit, $input_base_name . '_upload_button');
- $this->drupalPostAJAX(NULL, array(), $input_base_name . '_remove_button');
- }
- else {
- $this->drupalPost(NULL, $edit, t('Upload'));
- $this->drupalPost(NULL, array(), t('Remove'));
- }
- $this->drupalPost(NULL, array(), t('Save'));
- $this->assertRaw(t('The file id is %fid.', array('%fid' => 0)), 'Submission after file upload and removal was successful.');
- }
- }
- }
- }
- }
- class FileFieldWidgetTestCase extends FileFieldTestCase {
- public static function getInfo() {
- return array(
- 'name' => 'File field widget test',
- 'description' => 'Tests the file field widget, single and multi-valued, with and without AJAX, with public and private files.',
- 'group' => 'File',
- );
- }
-
- function testSingleValuedWidget() {
-
-
-
-
- $type_name = 'page';
- $field_name = strtolower($this->randomName());
- $this->createFileField($field_name, $type_name);
- $field = field_info_field($field_name);
- $instance = field_info_instance('node', $field_name, $type_name);
- $test_file = $this->getTestFile('text');
- foreach (array('nojs', 'js') as $type) {
-
-
-
-
- $nid = $this->uploadNodeFile($test_file, $field_name, $type_name);
- $node = node_load($nid, NULL, TRUE);
- $node_file = (object) $node->{$field_name}[LANGUAGE_NONE][0];
- $this->assertFileExists($node_file, 'New file saved to disk on node creation.');
-
- $field = new stdClass();
- $field->type = $type_name;
- $field->nid = $nid;
- field_attach_update('node', $field);
- $node = node_load($nid);
- $node_file = (object) $node->{$field_name}[LANGUAGE_NONE][0];
- $this->assertFileExists($node_file, 'New file still saved to disk on field update.');
-
- $this->drupalGet(file_create_url($node_file->uri));
- $this->assertResponse(200, 'Confirmed that the generated URL is correct by downloading the shipped file.');
-
- $this->drupalGet("node/$nid/edit");
- $this->assertNoFieldByXPath('//input[@type="submit"]', t('Upload'), 'Node with file does not display the "Upload" button.');
- $this->assertFieldByXpath('//input[@type="submit"]', t('Remove'), 'Node with file displays the "Remove" button.');
-
- switch ($type) {
- case 'nojs':
- $this->drupalPost(NULL, array(), t('Remove'));
- break;
- case 'js':
- $button = $this->xpath('//input[@type="submit" and @value="' . t('Remove') . '"]');
- $this->drupalPostAJAX(NULL, array(), array((string) $button[0]['name'] => (string) $button[0]['value']));
- break;
- }
-
- $this->assertNoFieldByXPath('//input[@type="submit"]', t('Remove'), 'After clicking the "Remove" button, it is no longer displayed.');
- $this->assertFieldByXpath('//input[@type="submit"]', t('Upload'), 'After clicking the "Remove" button, the "Upload" button is displayed.');
-
- $this->drupalPost(NULL, array(), t('Save'));
- $node = node_load($nid, NULL, TRUE);
- $this->assertTrue(empty($node->{$field_name}[LANGUAGE_NONE][0]['fid']), 'File was successfully removed from the node.');
- }
- }
-
- function testMultiValuedWidget() {
-
-
-
-
- $type_name = 'page';
- $field_name = strtolower($this->randomName());
- $field_name2 = strtolower($this->randomName());
- $this->createFileField($field_name, $type_name, array('cardinality' => 3));
- $this->createFileField($field_name2, $type_name, array('cardinality' => 3));
- $field = field_info_field($field_name);
- $instance = field_info_instance('node', $field_name, $type_name);
- $field2 = field_info_field($field_name2);
- $instance2 = field_info_instance('node', $field_name2, $type_name);
- $test_file = $this->getTestFile('text');
- foreach (array('nojs', 'js') as $type) {
-
-
-
-
-
-
-
-
- $this->drupalGet("node/add/$type_name");
- foreach (array($field_name2, $field_name) as $each_field_name) {
- for ($delta = 0; $delta < 3; $delta++) {
- $edit = array('files[' . $each_field_name . '_' . LANGUAGE_NONE . '_' . $delta . ']' => drupal_realpath($test_file->uri));
-
-
- $this->drupalPost(NULL, $edit, t('Upload'));
- }
- }
- $this->assertNoFieldByXpath('//input[@type="submit"]', t('Upload'), 'After uploading 3 files for each field, the "Upload" button is no longer displayed.');
- $num_expected_remove_buttons = 6;
- foreach (array($field_name, $field_name2) as $current_field_name) {
-
- $remaining = 3;
-
-
-
-
-
-
- foreach (array(1,1,0) as $delta) {
-
-
- $buttons = $this->xpath('//input[@type="submit" and @value="Remove"]');
- $this->assertTrue(is_array($buttons) && count($buttons) === $num_expected_remove_buttons, format_string('There are %n "Remove" buttons displayed (JSMode=%type).', array('%n' => $num_expected_remove_buttons, '%type' => $type)));
- foreach ($buttons as $i => $button) {
- $key = $i >= $remaining ? $i - $remaining : $i;
- $check_field_name = $field_name2;
- if ($current_field_name == $field_name && $i < $remaining) {
- $check_field_name = $field_name;
- }
- $this->assertIdentical((string) $button['name'], $check_field_name . '_' . LANGUAGE_NONE . '_' . $key. '_remove_button');
- }
-
- $button_name = $current_field_name . '_' . LANGUAGE_NONE . '_' . $delta . '_remove_button';
- switch ($type) {
- case 'nojs':
-
-
-
-
-
-
-
- foreach ($buttons as $button) {
- if ($button['name'] != $button_name) {
- $button['value'] = 'DUMMY';
- }
- }
- $this->drupalPost(NULL, array(), t('Remove'));
- break;
- case 'js':
-
-
- $this->drupalPostAJAX(NULL, array(), array($button_name => t('Remove')));
- break;
- }
- $num_expected_remove_buttons--;
- $remaining--;
-
-
- $upload_button_name = $current_field_name . '_' . LANGUAGE_NONE . '_' . $remaining . '_upload_button';
- $buttons = $this->xpath('//input[@type="submit" and @value="Upload" and @name=:name]', array(':name' => $upload_button_name));
- $this->assertTrue(is_array($buttons) && count($buttons) == 1, format_string('The upload button is displayed with the correct name (JSMode=%type).', array('%type' => $type)));
-
- $buttons = $this->xpath('//input[@type="submit" and @value="Upload"]');
- $expected = $current_field_name == $field_name ? 1 : 2;
- $this->assertTrue(is_array($buttons) && count($buttons) == $expected, format_string('After removing a file, only one "Upload" button for each possible field is displayed (JSMode=%type).', array('%type' => $type)));
- }
- }
-
- $this->assertNoFieldByXPath('//input[@type="submit"]', t('Remove'), format_string('After removing all files, there is no "Remove" button displayed (JSMode=%type).', array('%type' => $type)));
-
- $this->drupalPost(NULL, array('title' => $this->randomName()), t('Save'));
- $matches = array();
- preg_match('/node\/([0-9]+)/', $this->getUrl(), $matches);
- $nid = $matches[1];
- $node = node_load($nid, NULL, TRUE);
- $this->assertTrue(empty($node->{$field_name}[LANGUAGE_NONE][0]['fid']), 'Node was successfully saved without any files.');
- }
- }
-
- function testPrivateFileSetting() {
-
-
-
-
- $type_name = 'page';
- $field_name = strtolower($this->randomName());
- $this->createFileField($field_name, $type_name);
- $field = field_info_field($field_name);
- $instance = field_info_instance('node', $field_name, $type_name);
- $test_file = $this->getTestFile('text');
-
- $edit = array('field[settings][uri_scheme]' => 'private');
- $this->drupalPost("admin/structure/types/manage/$type_name/fields/$field_name", $edit, t('Save settings'));
- $nid = $this->uploadNodeFile($test_file, $field_name, $type_name);
- $node = node_load($nid, NULL, TRUE);
- $node_file = (object) $node->{$field_name}[LANGUAGE_NONE][0];
- $this->assertFileExists($node_file, 'New file saved to disk on node creation.');
-
- $this->drupalGet(file_create_url($node_file->uri));
- $this->assertResponse(200, 'Confirmed that the generated URL is correct by downloading the shipped file.');
-
-
- $this->drupalGet("admin/structure/types/manage/$type_name/fields/$field_name");
- $this->assertFieldByXpath('//input[@id="edit-field-settings-uri-scheme-public" and @disabled="disabled"]', 'public', 'Upload destination setting disabled.');
-
- node_delete($nid);
- $this->drupalGet("admin/structure/types/manage/$type_name/fields/$field_name");
- $this->assertFieldByXpath('//input[@id="edit-field-settings-uri-scheme-public" and not(@disabled)]', 'public', 'Upload destination setting enabled.');
- }
-
- function testPrivateFileComment() {
- $user = $this->drupalCreateUser(array('access comments'));
-
- $edit = array(
- DRUPAL_ANONYMOUS_RID . '[access comments]' => FALSE,
- );
- $this->drupalPost('admin/people/permissions', $edit, t('Save permissions'));
-
- $edit = array(
- 'fields[_add_new_field][label]' => $label = $this->randomName(),
- 'fields[_add_new_field][field_name]' => $name = strtolower($this->randomName()),
- 'fields[_add_new_field][type]' => 'file',
- 'fields[_add_new_field][widget_type]' => 'file_generic',
- );
- $this->drupalPost('admin/structure/types/manage/article/comment/fields', $edit, t('Save'));
- $edit = array('field[settings][uri_scheme]' => 'private');
- $this->drupalPost(NULL, $edit, t('Save field settings'));
- $this->drupalPost(NULL, array(), t('Save settings'));
-
- $text_file = $this->getTestFile('text');
- $edit = array(
- 'title' => $this->randomName(),
- );
- $this->drupalPost('node/add/article', $edit, t('Save'));
- $node = $this->drupalGetNodeByTitle($edit['title']);
-
- $text_file = $this->getTestFile('text');
- $edit = array(
- 'files[field_' . $name . '_' . LANGUAGE_NONE . '_' . 0 . ']' => drupal_realpath($text_file->uri),
- 'comment_body[' . LANGUAGE_NONE . '][0][value]' => $comment_body = $this->randomName(),
- );
- $this->drupalPost(NULL, $edit, t('Save'));
-
- preg_match('/comment-([0-9]+)/', $this->getUrl(), $matches);
- $cid = $matches[1];
-
- $this->drupalLogin($user);
- $comment = comment_load($cid);
- $comment_file = (object) $comment->{'field_' . $name}[LANGUAGE_NONE][0];
- $this->assertFileExists($comment_file, 'New file saved to disk on node creation.');
-
- $url = file_create_url($comment_file->uri);
- $this->assertNotEqual($url, NULL, 'Confirmed that the URL is valid');
- $this->drupalGet(file_create_url($comment_file->uri));
- $this->assertResponse(200, 'Confirmed that the generated URL is correct by downloading the shipped file.');
-
- $this->drupalLogout();
- $this->drupalGet(file_create_url($comment_file->uri));
- $this->assertResponse(403, 'Confirmed that access is denied for the file without the needed permission.');
-
- $this->drupalLogin($this->admin_user);
- $edit = array(
- 'status' => FALSE,
- );
- $this->drupalPost('node/' . $node->nid . '/edit', $edit, t('Save'));
-
- $this->drupalLogin($user);
- $this->drupalGet(file_create_url($comment_file->uri));
- $this->assertResponse(403, 'Confirmed that access is denied for the file without the needed permission.');
- }
- }
- class FileFieldRevisionTestCase extends FileFieldTestCase {
- public static function getInfo() {
- return array(
- 'name' => 'File field revision test',
- 'description' => 'Test creating and deleting revisions with files attached.',
- 'group' => 'File',
- );
- }
-
- function testRevisions() {
- $type_name = 'article';
- $field_name = strtolower($this->randomName());
- $this->createFileField($field_name, $type_name);
- $field = field_info_field($field_name);
- $instance = field_info_instance('node', $field_name, $type_name);
-
- $this->attachFileField($field_name, 'user', 'user');
- $test_file = $this->getTestFile('text');
-
- $nid = $this->uploadNodeFile($test_file, $field_name, $type_name);
-
- $node = node_load($nid, NULL, TRUE);
- $node_file_r1 = (object) $node->{$field_name}[LANGUAGE_NONE][0];
- $node_vid_r1 = $node->vid;
- $this->assertFileExists($node_file_r1, 'New file saved to disk on node creation.');
- $this->assertFileEntryExists($node_file_r1, 'File entry exists in database on node creation.');
- $this->assertFileIsPermanent($node_file_r1, 'File is permanent.');
-
- $this->replaceNodeFile($test_file, $field_name, $nid);
- $node = node_load($nid, NULL, TRUE);
- $node_file_r2 = (object) $node->{$field_name}[LANGUAGE_NONE][0];
- $node_vid_r2 = $node->vid;
- $this->assertFileExists($node_file_r2, 'Replacement file exists on disk after creating new revision.');
- $this->assertFileEntryExists($node_file_r2, 'Replacement file entry exists in database after creating new revision.');
- $this->assertFileIsPermanent($node_file_r2, 'Replacement file is permanent.');
-
- $node = node_load($nid, $node_vid_r1, TRUE);
- $this->assertEqual($node_file_r1, (object) $node->{$field_name}[LANGUAGE_NONE][0], 'Original file still in place after replacing file in new revision.');
- $this->assertFileExists($node_file_r1, 'Original file still in place after replacing file in new revision.');
- $this->assertFileEntryExists($node_file_r1, 'Original file entry still in place after replacing file in new revision');
- $this->assertFileIsPermanent($node_file_r1, 'Original file is still permanent.');
-
-
- $this->drupalPost('node/' . $nid . '/edit', array('revision' => '1'), t('Save'));
- $node = node_load($nid, NULL, TRUE);
- $node_file_r3 = (object) $node->{$field_name}[LANGUAGE_NONE][0];
- $node_vid_r3 = $node->vid;
- $this->assertEqual($node_file_r2, $node_file_r3, 'Previous revision file still in place after creating a new revision without a new file.');
- $this->assertFileIsPermanent($node_file_r3, 'New revision file is permanent.');
-
- $this->drupalPost('node/' . $nid . '/revisions/' . $node_vid_r1 . '/revert', array(), t('Revert'));
- $node = node_load($nid, NULL, TRUE);
- $node_file_r4 = (object) $node->{$field_name}[LANGUAGE_NONE][0];
- $node_vid_r4 = $node->vid;
- $this->assertEqual($node_file_r1, $node_file_r4, 'Original revision file still in place after reverting to the original revision.');
- $this->assertFileIsPermanent($node_file_r4, 'Original revision file still permanent after reverting to the original revision.');
-
-
- $this->drupalPost('node/' . $nid . '/revisions/' . $node_vid_r2 . '/delete', array(), t('Delete'));
- $this->assertFileExists($node_file_r3, 'Second file is still available after deleting second revision, since it is being used by the third revision.');
- $this->assertFileEntryExists($node_file_r3, 'Second file entry is still available after deleting second revision, since it is being used by the third revision.');
- $this->assertFileIsPermanent($node_file_r3, 'Second file entry is still permanent after deleting second revision, since it is being used by the third revision.');
-
- $user = $this->drupalCreateUser();
- $edit = (array) $user;
- $edit[$field_name][LANGUAGE_NONE][0] = (array) $node_file_r3;
- user_save($user, $edit);
- $this->drupalGet('user/' . $user->uid . '/edit');
-
- $this->drupalPost('node/' . $nid . '/revisions/' . $node_vid_r3 . '/delete', array(), t('Delete'));
- $this->assertFileExists($node_file_r3, 'Second file is still available after deleting third revision, since it is being used by the user.');
- $this->assertFileEntryExists($node_file_r3, 'Second file entry is still available after deleting third revision, since it is being used by the user.');
- $this->assertFileIsPermanent($node_file_r3, 'Second file entry is still permanent after deleting third revision, since it is being used by the user.');
-
- user_delete($user->uid);
-
-
-
- clearstatcache();
- $this->assertFileNotExists($node_file_r3, 'Second file is now deleted after deleting third revision, since it is no longer being used by any other nodes.');
- $this->assertFileEntryNotExists($node_file_r3, 'Second file entry is now deleted after deleting third revision, since it is no longer being used by any other nodes.');
-
- $this->drupalPost('node/' . $nid . '/delete', array(), t('Delete'));
- $this->assertFileNotExists($node_file_r1, 'Original file is deleted after deleting the entire node with two revisions remaining.');
- $this->assertFileEntryNotExists($node_file_r1, 'Original file entry is deleted after deleting the entire node with two revisions remaining.');
- }
- }
- class FileFieldDisplayTestCase extends FileFieldTestCase {
- public static function getInfo() {
- return array(
- 'name' => 'File field display tests',
- 'description' => 'Test the display of file fields in node and views.',
- 'group' => 'File',
- );
- }
-
- function testNodeDisplay() {
- $field_name = strtolower($this->randomName());
- $type_name = 'article';
- $field_settings = array(
- 'display_field' => '1',
- 'display_default' => '1',
- 'cardinality' => FIELD_CARDINALITY_UNLIMITED,
- );
- $instance_settings = array(
- 'description_field' => '1',
- );
- $widget_settings = array();
- $this->createFileField($field_name, $type_name, $field_settings, $instance_settings, $widget_settings);
- $field = field_info_field($field_name);
- $instance = field_info_instance('node', $field_name, $type_name);
-
-
- $node = $this->drupalCreateNode(array('type' => $type_name));
- $file_formatters = array('file_default', 'file_table', 'file_url_plain', 'hidden');
- foreach ($file_formatters as $formatter) {
- $edit = array(
- "fields[$field_name][type]" => $formatter,
- );
- $this->drupalPost("admin/structure/types/manage/$type_name/display", $edit, t('Save'));
- $this->drupalGet('node/' . $node->nid);
- $this->assertNoText($field_name, format_string('Field label is hidden when no file attached for formatter %formatter', array('%formatter' => $formatter)));
- }
- $test_file = $this->getTestFile('text');
-
- $nid = $this->uploadNodeFile($test_file, $field_name, $type_name);
- $this->drupalGet('node/' . $nid . '/edit');
-
- $node = node_load($nid, NULL, TRUE);
- $node_file = (object) $node->{$field_name}[LANGUAGE_NONE][0];
- $default_output = theme('file_link', array('file' => $node_file));
- $this->assertRaw($default_output, 'Default formatter displaying correctly on full node view.');
-
- $edit = array($field_name . '[' . LANGUAGE_NONE . '][0][display]' => FALSE);
- $this->drupalPost('node/' . $nid . '/edit', $edit, t('Save'));
- $this->assertNoRaw($default_output, 'Field is hidden when "display" option is unchecked.');
-
-
- $name = 'files[' . $field_name . '_' . LANGUAGE_NONE . '_1]';
- $edit[$name] = drupal_realpath($test_file->uri);
-
- $edit[$field_name . '[' . LANGUAGE_NONE . '][0][display]'] = FALSE;
- $edit[$field_name . '[' . LANGUAGE_NONE . '][1][display]'] = FALSE;
- $this->drupalPost('node/' . $nid . '/edit', $edit, t('Preview'));
- $this->assertRaw($field_name . '[' . LANGUAGE_NONE . '][0][display]', 'First file appears as expected.');
- $this->assertRaw($field_name . '[' . LANGUAGE_NONE . '][1][display]', 'Second file appears as expected.');
- }
- }
- class FileFieldValidateTestCase extends FileFieldTestCase {
- protected $field;
- protected $node_type;
- public static function getInfo() {
- return array(
- 'name' => 'File field validation tests',
- 'description' => 'Tests validation functions such as file type, max file size, max size per node, and required.',
- 'group' => 'File',
- );
- }
-
- function testRequired() {
- $type_name = 'article';
- $field_name = strtolower($this->randomName());
- $this->createFileField($field_name, $type_name, array(), array('required' => '1'));
- $field = field_info_field($field_name);
- $instance = field_info_instance('node', $field_name, $type_name);
- $test_file = $this->getTestFile('text');
-
- $langcode = LANGUAGE_NONE;
- $edit = array("title" => $this->randomName());
- $this->drupalPost('node/add/' . $type_name, $edit, t('Save'));
- $this->assertRaw(t('!title field is required.', array('!title' => $instance['label'])), 'Node save failed when required file field was empty.');
-
- $nid = $this->uploadNodeFile($test_file, $field_name, $type_name);
- $this->assertTrue($nid !== FALSE, format_string('uploadNodeFile(@test_file, @field_name, @type_name) succeeded', array('@test_file' => $test_file->uri, '@field_name' => $field_name, '@type_name' => $type_name)));
- $node = node_load($nid, NULL, TRUE);
- $node_file = (object) $node->{$field_name}[LANGUAGE_NONE][0];
- $this->assertFileExists($node_file, 'File exists after uploading to the required field.');
- $this->assertFileEntryExists($node_file, 'File entry exists after uploading to the required field.');
-
- field_delete_field($field_name);
- $this->createFileField($field_name, $type_name, array('cardinality' => FIELD_CARDINALITY_UNLIMITED), array('required' => '1'));
-
- $edit = array('title' => $this->randomName());
- $this->drupalPost('node/add/' . $type_name, $edit, t('Save'));
- $this->assertRaw(t('!title field is required.', array('!title' => $instance['label'])), 'Node save failed when required multiple value file field was empty.');
-
- $nid = $this->uploadNodeFile($test_file, $field_name, $type_name);
- $node = node_load($nid, NULL, TRUE);
- $node_file = (object) $node->{$field_name}[LANGUAGE_NONE][0];
- $this->assertFileExists($node_file, 'File exists after uploading to the required multiple value field.');
- $this->assertFileEntryExists($node_file, 'File entry exists after uploading to the required multipel value field.');
-
- field_delete_field($field_name);
- }
-
- function testFileMaxSize() {
- $type_name = 'article';
- $field_name = strtolower($this->randomName());
- $this->createFileField($field_name, $type_name, array(), array('required' => '1'));
- $field = field_info_field($field_name);
- $instance = field_info_instance('node', $field_name, $type_name);
- $small_file = $this->getTestFile('text', 131072);
- $large_file = $this->getTestFile('text', 1310720);
-
- $sizes = array(
- '1M' => 1048576,
- '1024K' => 1048576,
- '1048576' => 1048576,
- );
- foreach ($sizes as $max_filesize => $file_limit) {
-
- $this->updateFileField($field_name, $type_name, array('max_filesize' => $max_filesize));
- $instance = field_info_instance('node', $field_name, $type_name);
-
- $nid = $this->uploadNodeFile($small_file, $field_name, $type_name);
- $node = node_load($nid, NULL, TRUE);
- $node_file = (object) $node->{$field_name}[LANGUAGE_NONE][0];
- $this->assertFileExists($node_file, format_string('File exists after uploading a file (%filesize) under the max limit (%maxsize).', array('%filesize' => format_size($small_file->filesize), '%maxsize' => $max_filesize)));
- $this->assertFileEntryExists($node_file, format_string('File entry exists after uploading a file (%filesize) under the max limit (%maxsize).', array('%filesize' => format_size($small_file->filesize), '%maxsize' => $max_filesize)));
-
- $nid = $this->uploadNodeFile($large_file, $field_name, $type_name);
- $error_message = t('The file is %filesize exceeding the maximum file size of %maxsize.', array('%filesize' => format_size($large_file->filesize), '%maxsize' => format_size($file_limit)));
- $this->assertRaw($error_message, format_string('Node save failed when file (%filesize) exceeded the max upload size (%maxsize).', array('%filesize' => format_size($large_file->filesize), '%maxsize' => $max_filesize)));
- }
-
- $this->updateFileField($field_name, $type_name, array('max_filesize' => ''));
-
- $nid = $this->uploadNodeFile($large_file, $field_name, $type_name);
- $node = node_load($nid, NULL, TRUE);
- $node_file = (object) $node->{$field_name}[LANGUAGE_NONE][0];
- $this->assertFileExists($node_file, format_string('File exists after uploading a file (%filesize) with no max limit.', array('%filesize' => format_size($large_file->filesize))));
- $this->assertFileEntryExists($node_file, format_string('File entry exists after uploading a file (%filesize) with no max limit.', array('%filesize' => format_size($large_file->filesize))));
-
- field_delete_field($field_name);
- }
-
- function testFileExtension() {
- $type_name = 'article';
- $field_name = strtolower($this->randomName());
- $this->createFileField($field_name, $type_name);
- $field = field_info_field($field_name);
- $instance = field_info_instance('node', $field_name, $type_name);
- $test_file = $this->getTestFile('image');
- list(, $test_file_extension) = explode('.', $test_file->filename);
-
- $this->updateFileField($field_name, $type_name, array('file_extensions' => ''));
-
- $nid = $this->uploadNodeFile($test_file, $field_name, $type_name);
- $node = node_load($nid, NULL, TRUE);
- $node_file = (object) $node->{$field_name}[LANGUAGE_NONE][0];
- $this->assertFileExists($node_file, 'File exists after uploading a file with no extension checking.');
- $this->assertFileEntryExists($node_file, 'File entry exists after uploading a file with no extension checking.');
-
- $this->updateFileField($field_name, $type_name, array('file_extensions' => 'txt'));
-
- $nid = $this->uploadNodeFile($test_file, $field_name, $type_name);
- $error_message = t('Only files with the following extensions are allowed: %files-allowed.', array('%files-allowed' => 'txt'));
- $this->assertRaw($error_message, 'Node save failed when file uploaded with the wrong extension.');
-
- $this->updateFileField($field_name, $type_name, array('file_extensions' => "txt $test_file_extension"));
-
- $nid = $this->uploadNodeFile($test_file, $field_name, $type_name);
- $node = node_load($nid, NULL, TRUE);
- $node_file = (object) $node->{$field_name}[LANGUAGE_NONE][0];
- $this->assertFileExists($node_file, 'File exists after uploading a file with extension checking.');
- $this->assertFileEntryExists($node_file, 'File entry exists after uploading a file with extension checking.');
-
- field_delete_field($field_name);
- }
- }
- class FileFieldPathTestCase extends FileFieldTestCase {
- public static function getInfo() {
- return array(
- 'name' => 'File field file path tests',
- 'description' => 'Test that files are uploaded to the proper location with token support.',
- 'group' => 'File',
- );
- }
-
- function testUploadPath() {
- $field_name = strtolower($this->randomName());
- $type_name = 'article';
- $field = $this->createFileField($field_name, $type_name);
- $test_file = $this->getTestFile('text');
-
- $nid = $this->uploadNodeFile($test_file, $field_name, $type_name);
-
- $node = node_load($nid, NULL, TRUE);
- $node_file = (object) $node->{$field_name}[LANGUAGE_NONE][0];
- $this->assertPathMatch('public://' . $test_file->filename, $node_file->uri, format_string('The file %file was uploaded to the correct path.', array('%file' => $node_file->uri)));
-
- $field = $this->updateFileField($field_name, $type_name, array('file_directory' => 'foo/bar/baz'));
-
- $nid = $this->uploadNodeFile($test_file, $field_name, $type_name);
-
- $node = node_load($nid, NULL, TRUE);
- $node_file = (object) $node->{$field_name}[LANGUAGE_NONE][0];
- $this->assertPathMatch('public://foo/bar/baz/' . $test_file->filename, $node_file->uri, format_string('The file %file was uploaded to the correct path.', array('%file' => $node_file->uri)));
-
-
- $field = $this->updateFileField($field_name, $type_name, array('file_directory' => '[current-user:uid]/[current-user:name]'));
-
- $nid = $this->uploadNodeFile($test_file, $field_name, $type_name);
-
- $node = node_load($nid, NULL, TRUE);
- $node_file = (object) $node->{$field_name}[LANGUAGE_NONE][0];
-
-
- $data = array('user' => $this->admin_user);
- $subdirectory = token_replace('[user:uid]/[user:name]', $data);
- $this->assertPathMatch('public://' . $subdirectory . '/' . $test_file->filename, $node_file->uri, format_string('The file %file was uploaded to the correct path with token replacements.', array('%file' => $node_file->uri)));
- }
-
- function assertPathMatch($expected_path, $actual_path, $message) {
-
-
- $pos = strrpos($expected_path, '.');
- $base_path = substr($expected_path, 0, $pos);
- $extension = substr($expected_path, $pos + 1);
- $result = preg_match('/' . preg_quote($base_path, '/') . '(_[0-9]+)?\.' . preg_quote($extension, '/') . '/', $actual_path);
- $this->assertTrue($result, $message);
- }
- }
- class FileTokenReplaceTestCase extends FileFieldTestCase {
- public static function getInfo() {
- return array(
- 'name' => 'File token replacement',
- 'description' => 'Generates text using placeholders for dummy content to check file token replacement.',
- 'group' => 'File',
- );
- }
-
- function testFileTokenReplacement() {
- global $language;
- $url_options = array(
- 'absolute' => TRUE,
- 'language' => $language,
- );
-
- $type_name = 'article';
- $field_name = 'field_' . strtolower($this->randomName());
- $this->createFileField($field_name, $type_name);
- $field = field_info_field($field_name);
- $instance = field_info_instance('node', $field_name, $type_name);
- $test_file = $this->getTestFile('text');
-
- $filename = drupal_dirname($test_file->uri) . '/текстовый файл.txt';
- $test_file = file_copy($test_file, $filename);
-
- $nid = $this->uploadNodeFile($test_file, $field_name, $type_name);
-
- $node = node_load($nid, NULL, TRUE);
- $file = file_load($node->{$field_name}[LANGUAGE_NONE][0]['fid']);
-
- $tests = array();
- $tests['[file:fid]'] = $file->fid;
- $tests['[file:name]'] = check_plain($file->filename);
- $tests['[file:path]'] = check_plain($file->uri);
- $tests['[file:mime]'] = check_plain($file->filemime);
- $tests['[file:size]'] = format_size($file->filesize);
- $tests['[file:url]'] = check_plain(file_create_url($file->uri));
- $tests['[file:timestamp]'] = format_date($file->timestamp, 'medium', '', NULL, $language->language);
- $tests['[file:timestamp:short]'] = format_date($file->timestamp, 'short', '', NULL, $language->language);
- $tests['[file:owner]'] = check_plain(format_username($this->admin_user));
- $tests['[file:owner:uid]'] = $file->uid;
-
- $this->assertFalse(in_array(0, array_map('strlen', $tests)), 'No empty tokens generated.');
- foreach ($tests as $input => $expected) {
- $output = token_replace($input, array('file' => $file), array('language' => $language));
- $this->assertEqual($output, $expected, format_string('Sanitized file token %token replaced.', array('%token' => $input)));
- }
-
- $tests['[file:name]'] = $file->filename;
- $tests['[file:path]'] = $file->uri;
- $tests['[file:mime]'] = $file->filemime;
- $tests['[file:size]'] = format_size($file->filesize);
- foreach ($tests as $input => $expected) {
- $output = token_replace($input, array('file' => $file), array('language' => $language, 'sanitize' => FALSE));
- $this->assertEqual($output, $expected, format_string('Unsanitized file token %token replaced.', array('%token' => $input)));
- }
- }
- }
- class FilePrivateTestCase extends FileFieldTestCase {
- public static function getInfo() {
- return array(
- 'name' => 'Private file test',
- 'description' => 'Uploads a test to a private node and checks access.',
- 'group' => 'File',
- );
- }
- function setUp() {
- parent::setUp(array('node_access_test', 'field_test'));
- node_access_rebuild();
- variable_set('node_access_test_private', TRUE);
- }
-
- function testPrivateFile() {
-
-
-
-
- $type_name = 'page';
- $field_name = strtolower($this->randomName());
- $this->createFileField($field_name, $type_name, array('uri_scheme' => 'private'));
-
- $no_access_field_name = 'field_no_view_access';
- $this->createFileField($no_access_field_name, $type_name, array('uri_scheme' => 'private'));
- $test_file = $this->getTestFile('text');
- $nid = $this->uploadNodeFile($test_file, $field_name, $type_name, TRUE, array('private' => TRUE));
- $node = node_load($nid, NULL, TRUE);
- $node_file = (object) $node->{$field_name}[LANGUAGE_NONE][0];
-
- $this->drupalGet(file_create_url($node_file->uri));
- $this->assertResponse(200, 'Confirmed that the generated URL is correct by downloading the shipped file.');
- $this->drupalLogOut();
- $this->drupalGet(file_create_url($node_file->uri));
- $this->assertResponse(403, 'Confirmed that access is denied for the file without the needed permission.');
-
- $this->drupalLogin($this->admin_user);
- $nid = $this->uploadNodeFile($test_file, $no_access_field_name, $type_name, TRUE, array('private' => TRUE));
- $node = node_load($nid, NULL, TRUE);
- $node_file = (object) $node->{$no_access_field_name}[LANGUAGE_NONE][0];
-
- $this->drupalGet(file_create_url($node_file->uri));
- $this->assertResponse(403, 'Confirmed that access is denied for the file without view field access permission.');
-
-
- $edit = array();
- $edit['title'] = $this->randomName(8);
- $edit[$field_name . '[' . LANGUAGE_NONE . '][0][fid]'] = $node_file->fid;
- $this->drupalPost('node/add/page', $edit, t('Save'));
- $new_node = $this->drupalGetNodeByTitle($edit['title']);
- $this->assertTrue(!empty($new_node), 'Node was created.');
- $this->assertUrl('node/' . $new_node->nid);
- $this->assertNoRaw($node_file->filename, 'File without view field access permission does not appear after attempting to attach it to a new node.');
- $this->drupalGet(file_create_url($node_file->uri));
- $this->assertResponse(403, 'Confirmed that access is denied for the file without view field access permission after attempting to attach it to a new node.');
- }
- }
|