| 1 |
<?php
|
| 2 |
// $Id: upload.test,v 1.27 2009/10/11 03:07:21 webchick Exp $
|
| 3 |
|
| 4 |
/**
|
| 5 |
* @file
|
| 6 |
* This file contains tests for the upload module.
|
| 7 |
*/
|
| 8 |
|
| 9 |
|
| 10 |
class UploadTestCase extends DrupalWebTestCase {
|
| 11 |
public static function getInfo() {
|
| 12 |
return array(
|
| 13 |
'name' => 'Upload functionality',
|
| 14 |
'description' => 'Check content uploaded to nodes.',
|
| 15 |
'group' => 'Upload',
|
| 16 |
);
|
| 17 |
}
|
| 18 |
|
| 19 |
function setUp() {
|
| 20 |
parent::setUp('upload');
|
| 21 |
}
|
| 22 |
|
| 23 |
/**
|
| 24 |
* Create node; upload files to node; and edit, and delete uploads.
|
| 25 |
*/
|
| 26 |
function testNodeUpload() {
|
| 27 |
global $base_url;
|
| 28 |
$admin_user = $this->drupalCreateUser(array('administer site configuration'));
|
| 29 |
$web_user = $this->drupalCreateUser(array('access content', 'edit own page content', 'upload files', 'view uploaded files'));
|
| 30 |
|
| 31 |
$this->drupalLogin($admin_user);
|
| 32 |
|
| 33 |
// Setup upload settings.
|
| 34 |
$edit = array();
|
| 35 |
$edit['upload_list_default'] = '1'; // Yes.
|
| 36 |
$edit['upload_extensions_default'] = 'jpg jpeg gif png txt doc xls pdf ppt pps odt ods odp';
|
| 37 |
$edit['upload_uploadsize_default'] = '1.5';
|
| 38 |
$edit['upload_usersize_default'] = '1.5';
|
| 39 |
$this->drupalPost('admin/config/media/uploads', $edit, t('Save configuration'));
|
| 40 |
$this->assertText('The configuration options have been saved.', 'Upload setting saved.');
|
| 41 |
|
| 42 |
$this->drupalLogout();
|
| 43 |
$this->drupalLogin($web_user);
|
| 44 |
|
| 45 |
// Create a node and attempt to attach files.
|
| 46 |
$node = $this->drupalCreateNode();
|
| 47 |
$text_files = $this->drupalGetTestFiles('text');
|
| 48 |
$files = array(current($text_files)->uri, next($text_files)->uri);
|
| 49 |
|
| 50 |
$this->uploadFile($node, $files[0]);
|
| 51 |
$this->uploadFile($node, $files[1]);
|
| 52 |
|
| 53 |
// Check to see that uploaded file is listed in detail page and actually accessible.
|
| 54 |
$this->assertText(basename($files[0]), basename($files[0]) . ' found on node.');
|
| 55 |
$this->assertText(basename($files[1]), basename($files[1]) . ' found on node.');
|
| 56 |
|
| 57 |
$this->checkUploadedFile(basename($files[0]));
|
| 58 |
$this->checkUploadedFile(basename($files[1]));
|
| 59 |
|
| 60 |
// Check that files are also accessible when using private files.
|
| 61 |
variable_set('file_default_scheme', 'private');
|
| 62 |
$this->checkUploadedFile(basename($files[0]));
|
| 63 |
$this->checkUploadedFile(basename($files[1]));
|
| 64 |
|
| 65 |
// Assure that the attachment link appears on teaser view and has correct count.
|
| 66 |
$node = node_load($node->nid);
|
| 67 |
$teaser = drupal_render(node_build($node, 'teaser'));
|
| 68 |
$this->assertTrue(strpos($teaser, format_plural(2, '1 attachment', '@count attachments')), 'Attachments link found on node teaser.');
|
| 69 |
|
| 70 |
// Fetch db record and use fid to rename and delete file.
|
| 71 |
$upload = db_query('SELECT fid, description FROM {upload} WHERE nid = :nid', array(':nid' => $node->nid))->fetchObject();
|
| 72 |
if ($upload) {
|
| 73 |
// Rename file.
|
| 74 |
$edit = array();
|
| 75 |
$edit['files[' . $upload->fid . '][description]'] = $new_name = substr($upload->description, 1);
|
| 76 |
$this->drupalPost('node/' . $node->nid . '/edit', $edit, t('Save'));
|
| 77 |
$this->assertRaw(t('Page %title has been updated.', array('%title' => $node->title[FIELD_LANGUAGE_NONE][0]['value'])), 'File renamed successfully.');
|
| 78 |
|
| 79 |
$this->assertText($new_name, $new_name . ' found on node.');
|
| 80 |
$this->assertNoText($upload->description, $upload->description . ' not found on node.');
|
| 81 |
|
| 82 |
// Delete a file.
|
| 83 |
$edit = array();
|
| 84 |
$edit['files[' . $upload->fid . '][remove]'] = TRUE;
|
| 85 |
$this->drupalPost('node/' . $node->nid . '/edit', $edit, t('Save'));
|
| 86 |
$this->assertRaw(t('Page %title has been updated.', array('%title' => $node->title[FIELD_LANGUAGE_NONE][0]['value'])), 'File deleted successfully.');
|
| 87 |
|
| 88 |
$this->assertNoText($new_name, $new_name . ' not found on node.');
|
| 89 |
$uri = 'public://' . $upload->description;
|
| 90 |
$external_uri = file_stream_wrapper_get_instance_by_uri($uri)->getExternalUrl();
|
| 91 |
$this->drupalGet($external_uri, array('external' => TRUE));
|
| 92 |
$this->assertResponse(array(404), 'Uploaded ' . $upload->description . ' is not accessible.');
|
| 93 |
}
|
| 94 |
else {
|
| 95 |
$this->fail('File upload record not found in database.');
|
| 96 |
}
|
| 97 |
}
|
| 98 |
|
| 99 |
/**
|
| 100 |
* Ensure the the file filter works correctly by attempting to upload a non-allowed file extension.
|
| 101 |
*/
|
| 102 |
function testFilesFilter() {
|
| 103 |
$admin_user = $this->drupalCreateUser(array('administer site configuration'));
|
| 104 |
$web_user = $this->drupalCreateUser(array('access content', 'edit own page content', 'upload files', 'view uploaded files'));
|
| 105 |
|
| 106 |
$this->drupalLogin($admin_user);
|
| 107 |
|
| 108 |
// Setup upload settings.
|
| 109 |
$settings = array();
|
| 110 |
$settings['upload_list'] = '1'; // Yes.
|
| 111 |
$settings['upload_extensions'] = 'html';
|
| 112 |
$settings['upload_uploadsize'] = '1';
|
| 113 |
$settings['upload_usersize'] = '1';
|
| 114 |
$this->setUploadSettings($settings, $this->getSimpletestRoleId($web_user));
|
| 115 |
|
| 116 |
$this->drupalLogin($web_user);
|
| 117 |
|
| 118 |
$node = $this->drupalCreateNode();
|
| 119 |
|
| 120 |
// Attempt to upload .txt file when .html is only extension allowed.
|
| 121 |
$text_file = current($this->drupalGetTestFiles('text'));
|
| 122 |
// Select a file that's less than the 1MB upload limit so we only test one
|
| 123 |
// limit at a time.
|
| 124 |
$this->uploadFile($node, $text_file->uri, FALSE);
|
| 125 |
// Test the error message in two steps in case there are additional errors
|
| 126 |
// that change the error message's format.
|
| 127 |
$this->assertRaw(t('The specified file %name could not be uploaded.', array('%name' => $text_file->filename)), t('File %filepath was not allowed to be uploaded', array('%filepath' => $text_file->uri)));
|
| 128 |
$this->assertRaw(t('Only files with the following extensions are allowed: %files-allowed.', array('%files-allowed' => $settings['upload_extensions'])), t('File extension cited as reason for failure'));
|
| 129 |
|
| 130 |
// Attempt to upload .html file when .html is only extension allowed.
|
| 131 |
$html_files = array_values($this->drupalGetTestFiles('html'));
|
| 132 |
// Use the HTML file with the .html extension, $html_files[0] has a .txt
|
| 133 |
// extension.
|
| 134 |
$html_file = $html_files[1]->uri;
|
| 135 |
$this->uploadFile($node, $html_file);
|
| 136 |
$this->assertNoRaw(t('The specified file %name could not be uploaded.', array('%name' => basename($html_file))), t('File ' . $html_file . ' was allowed to be uploaded'));
|
| 137 |
}
|
| 138 |
|
| 139 |
/**
|
| 140 |
* Attempt to upload a file that is larger than the maxsize and see that it fails.
|
| 141 |
*/
|
| 142 |
function testLimit() {
|
| 143 |
$files = $this->drupalGetTestFiles('text', 1310720); // 1 MB.
|
| 144 |
$file = current($files)->uri;
|
| 145 |
|
| 146 |
$admin_user = $this->drupalCreateUser(array('administer site configuration'));
|
| 147 |
$web_user = $this->drupalCreateUser(array('access content', 'edit own page content', 'upload files', 'view uploaded files'));
|
| 148 |
|
| 149 |
$this->drupalLogin($admin_user);
|
| 150 |
|
| 151 |
// Setup upload settings.
|
| 152 |
$settings = array();
|
| 153 |
$settings['upload_list'] = '1'; // Yes.
|
| 154 |
$settings['upload_extensions'] = 'jpg jpeg gif png txt doc xls pdf ppt pps odt ods odp';
|
| 155 |
$settings['upload_uploadsize'] = '0.5';
|
| 156 |
$settings['upload_usersize'] = '1.5';
|
| 157 |
$this->setUploadSettings($settings, $this->getSimpletestRoleId($web_user));
|
| 158 |
|
| 159 |
$this->drupalLogin($web_user);
|
| 160 |
|
| 161 |
$node = $this->drupalCreateNode();
|
| 162 |
|
| 163 |
// Attempt to upload file which is bigger than the maximum size of 0.5 MB.
|
| 164 |
$this->uploadFile($node, $file, FALSE);
|
| 165 |
|
| 166 |
$info = stat($file);
|
| 167 |
$filename = basename($file);
|
| 168 |
$filesize = format_size($info['size']);
|
| 169 |
$maxsize = format_size(parse_size(($settings['upload_uploadsize'] * 1024) . 'KB')); // Won't parse decimals.
|
| 170 |
// Test the error message in two steps in case there are additional errors
|
| 171 |
// that change the error message's format.
|
| 172 |
$this->assertRaw(t('The specified file %name could not be uploaded.', array('%name' => $filename)), t('File upload was blocked'));
|
| 173 |
$this->assertRaw(t('The file is %filesize exceeding the maximum file size of %maxsize.', array('%filesize' => $filesize, '%maxsize' => $maxsize)), t('File size cited as problem with upload'));
|
| 174 |
}
|
| 175 |
|
| 176 |
function setUploadSettings($settings, $rid = NULL) {
|
| 177 |
$edit = array();
|
| 178 |
foreach ($settings as $key => $value) {
|
| 179 |
$edit[$key . '_default'] = $value;
|
| 180 |
if ($rid !== NULL && $key != 'upload_list' && $key != 'upload_max_resolution_x' && $key != 'upload_max_resolution_y') {
|
| 181 |
$edit[$key . '_' . $rid] = $value;
|
| 182 |
}
|
| 183 |
}
|
| 184 |
$this->drupalPost('admin/config/media/uploads', $edit, 'Save configuration');
|
| 185 |
$this->assertText('The configuration options have been saved.', 'Upload setting saved.');
|
| 186 |
}
|
| 187 |
|
| 188 |
/**
|
| 189 |
* Upload file to specified node.
|
| 190 |
*
|
| 191 |
* @param object $node Node object.
|
| 192 |
* @param string $filename Name of file to upload.
|
| 193 |
* @param boolean $assert Assert that the node was successfully updated.
|
| 194 |
*/
|
| 195 |
function uploadFile(stdClass $node, $filename, $assert = TRUE) {
|
| 196 |
$edit = array();
|
| 197 |
$edit['files[upload]'] = $filename; //edit-upload
|
| 198 |
$this->drupalPost('node/' . $node->nid . '/edit', $edit, t('Save'));
|
| 199 |
if ($assert) {
|
| 200 |
$this->assertRaw(t('Page %title has been updated.', array('%title' => $node->title[FIELD_LANGUAGE_NONE][0]['value'])), 'File attached successfully.');
|
| 201 |
}
|
| 202 |
}
|
| 203 |
|
| 204 |
/**
|
| 205 |
* Check that uploaded file is accessible and verify the contents against the original.
|
| 206 |
*
|
| 207 |
* @param string $filename Name of file to verify.
|
| 208 |
*/
|
| 209 |
function checkUploadedFile($filename) {
|
| 210 |
$file = 'public://' . $filename;
|
| 211 |
$this->drupalGet(file_create_url($file), array('external' => TRUE));
|
| 212 |
$this->assertResponse(array(200), 'Uploaded ' . $filename . ' is accessible.');
|
| 213 |
$this->assertTrue(strpos($this->drupalGetHeader('Content-Type'), 'text/plain') === 0, t('MIME type is text/plain.'));
|
| 214 |
$this->assertEqual(file_get_contents($file), $this->drupalGetContent(), 'Uploaded contents of ' . $filename . ' verified.');
|
| 215 |
// Verify file actually is readable and writeable by PHP.
|
| 216 |
$this->assertTrue(is_readable($file), t('Uploaded file is readable.'));
|
| 217 |
$this->assertTrue(is_writeable($file), t('Uploaded file is writeable.'));
|
| 218 |
}
|
| 219 |
|
| 220 |
/**
|
| 221 |
* Get the role id of the 'simpletest' role associated with a SimpleTest test user.
|
| 222 |
*
|
| 223 |
* @param object $user User object.
|
| 224 |
* @return integer SimpleTest role id.
|
| 225 |
*/
|
| 226 |
function getSimpletestRoleId($user) {
|
| 227 |
foreach ($user->roles as $rid => $role) {
|
| 228 |
if (preg_match('/s\d+/', $role)) {
|
| 229 |
return $rid;
|
| 230 |
}
|
| 231 |
}
|
| 232 |
return NULL;
|
| 233 |
}
|
| 234 |
}
|