235 lines
9.9 KiB
Plaintext
235 lines
9.9 KiB
Plaintext
<?php
|
|
// $Id$
|
|
|
|
/**
|
|
* @file
|
|
* This file contains tests for the upload module.
|
|
*/
|
|
|
|
|
|
class UploadTestCase extends DrupalWebTestCase {
|
|
public static function getInfo() {
|
|
return array(
|
|
'name' => 'Upload functionality',
|
|
'description' => 'Check content uploaded to nodes.',
|
|
'group' => 'Upload',
|
|
);
|
|
}
|
|
|
|
function setUp() {
|
|
parent::setUp('upload');
|
|
}
|
|
|
|
/**
|
|
* Create node; upload files to node; and edit, and delete uploads.
|
|
*/
|
|
function testNodeUpload() {
|
|
global $base_url;
|
|
$admin_user = $this->drupalCreateUser(array('administer site configuration'));
|
|
$web_user = $this->drupalCreateUser(array('access content', 'edit own page content', 'upload files', 'view uploaded files'));
|
|
|
|
$this->drupalLogin($admin_user);
|
|
|
|
// Setup upload settings.
|
|
$edit = array();
|
|
$edit['upload_list_default'] = '1'; // Yes.
|
|
$edit['upload_extensions_default'] = 'jpg jpeg gif png txt doc xls pdf ppt pps odt ods odp';
|
|
$edit['upload_uploadsize_default'] = '1.5';
|
|
$edit['upload_usersize_default'] = '1.5';
|
|
$this->drupalPost('admin/config/media/uploads', $edit, t('Save configuration'));
|
|
$this->assertText('The configuration options have been saved.', 'Upload setting saved.');
|
|
|
|
$this->drupalLogout();
|
|
$this->drupalLogin($web_user);
|
|
|
|
// Create a node and attempt to attach files.
|
|
$node = $this->drupalCreateNode();
|
|
$text_files = $this->drupalGetTestFiles('text');
|
|
$files = array(current($text_files)->uri, next($text_files)->uri);
|
|
|
|
$this->uploadFile($node, $files[0]);
|
|
$this->uploadFile($node, $files[1]);
|
|
|
|
// Check to see that uploaded file is listed in detail page and actually accessible.
|
|
$this->assertText(basename($files[0]), basename($files[0]) . ' found on node.');
|
|
$this->assertText(basename($files[1]), basename($files[1]) . ' found on node.');
|
|
|
|
$this->checkUploadedFile(basename($files[0]));
|
|
$this->checkUploadedFile(basename($files[1]));
|
|
|
|
// Check that files are also accessible when using private files.
|
|
variable_set('file_default_scheme', 'private');
|
|
$this->checkUploadedFile(basename($files[0]));
|
|
$this->checkUploadedFile(basename($files[1]));
|
|
|
|
// Assure that the attachment link appears on teaser view and has correct count.
|
|
$node = node_load($node->nid);
|
|
$teaser = drupal_render(node_build($node, 'teaser'));
|
|
$this->assertTrue(strpos($teaser, format_plural(2, '1 attachment', '@count attachments')), 'Attachments link found on node teaser.');
|
|
|
|
// Fetch db record and use fid to rename and delete file.
|
|
$upload = db_query('SELECT fid, description FROM {upload} WHERE nid = :nid', array(':nid' => $node->nid))->fetchObject();
|
|
if ($upload) {
|
|
// Rename file.
|
|
$edit = array();
|
|
$edit['files[' . $upload->fid . '][description]'] = $new_name = substr($upload->description, 1);
|
|
$this->drupalPost('node/' . $node->nid . '/edit', $edit, t('Save'));
|
|
$this->assertRaw(t('Page %title has been updated.', array('%title' => $node->title)), 'File renamed successfully.');
|
|
|
|
$this->assertText($new_name, $new_name . ' found on node.');
|
|
$this->assertNoText($upload->description, $upload->description . ' not found on node.');
|
|
|
|
// Delete a file.
|
|
$edit = array();
|
|
$edit['files[' . $upload->fid . '][remove]'] = TRUE;
|
|
$this->drupalPost('node/' . $node->nid . '/edit', $edit, t('Save'));
|
|
$this->assertRaw(t('Page %title has been updated.', array('%title' => $node->title)), 'File deleted successfully.');
|
|
|
|
$this->assertNoText($new_name, $new_name . ' not found on node.');
|
|
$uri = 'public://' . $upload->description;
|
|
$external_uri = file_stream_wrapper_get_instance_by_uri($uri)->getExternalUrl();
|
|
$this->drupalGet($external_uri, array('external' => TRUE));
|
|
$this->assertResponse(array(404), 'Uploaded ' . $upload->description . ' is not accessible.');
|
|
}
|
|
else {
|
|
$this->fail('File upload record not found in database.');
|
|
}
|
|
}
|
|
|
|
/**
|
|
* Ensure the the file filter works correctly by attempting to upload a non-allowed file extension.
|
|
*/
|
|
function testFilesFilter() {
|
|
$admin_user = $this->drupalCreateUser(array('administer site configuration'));
|
|
$web_user = $this->drupalCreateUser(array('access content', 'edit own page content', 'upload files', 'view uploaded files'));
|
|
|
|
$this->drupalLogin($admin_user);
|
|
|
|
// Setup upload settings.
|
|
$settings = array();
|
|
$settings['upload_list'] = '1'; // Yes.
|
|
$settings['upload_extensions'] = 'html';
|
|
$settings['upload_uploadsize'] = '1';
|
|
$settings['upload_usersize'] = '1';
|
|
$this->setUploadSettings($settings, $this->getSimpletestRoleId($web_user));
|
|
|
|
$this->drupalLogin($web_user);
|
|
|
|
$node = $this->drupalCreateNode();
|
|
|
|
// Attempt to upload .txt file when .html is only extension allowed.
|
|
$text_file = current($this->drupalGetTestFiles('text'));
|
|
// Select a file that's less than the 1MB upload limit so we only test one
|
|
// limit at a time.
|
|
$this->uploadFile($node, $text_file->uri, FALSE);
|
|
// Test the error message in two steps in case there are additional errors
|
|
// that change the error message's format.
|
|
$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)));
|
|
$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'));
|
|
|
|
// Attempt to upload .html file when .html is only extension allowed.
|
|
$html_files = array_values($this->drupalGetTestFiles('html'));
|
|
// Use the HTML file with the .html extension, $html_files[0] has a .txt
|
|
// extension.
|
|
$html_file = $html_files[1]->uri;
|
|
$this->uploadFile($node, $html_file);
|
|
$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'));
|
|
}
|
|
|
|
/**
|
|
* Attempt to upload a file that is larger than the maxsize and see that it fails.
|
|
*/
|
|
function testLimit() {
|
|
$files = $this->drupalGetTestFiles('text', 1310720); // 1 MB.
|
|
$file = current($files)->uri;
|
|
|
|
$admin_user = $this->drupalCreateUser(array('administer site configuration'));
|
|
$web_user = $this->drupalCreateUser(array('access content', 'edit own page content', 'upload files', 'view uploaded files'));
|
|
|
|
$this->drupalLogin($admin_user);
|
|
|
|
// Setup upload settings.
|
|
$settings = array();
|
|
$settings['upload_list'] = '1'; // Yes.
|
|
$settings['upload_extensions'] = 'jpg jpeg gif png txt doc xls pdf ppt pps odt ods odp';
|
|
$settings['upload_uploadsize'] = '0.5';
|
|
$settings['upload_usersize'] = '1.5';
|
|
$this->setUploadSettings($settings, $this->getSimpletestRoleId($web_user));
|
|
|
|
$this->drupalLogin($web_user);
|
|
|
|
$node = $this->drupalCreateNode();
|
|
|
|
// Attempt to upload file which is bigger than the maximum size of 0.5 MB.
|
|
$this->uploadFile($node, $file, FALSE);
|
|
|
|
$info = stat($file);
|
|
$filename = basename($file);
|
|
$filesize = format_size($info['size']);
|
|
$maxsize = format_size(parse_size(($settings['upload_uploadsize'] * 1024) . 'KB')); // Won't parse decimals.
|
|
// Test the error message in two steps in case there are additional errors
|
|
// that change the error message's format.
|
|
$this->assertRaw(t('The specified file %name could not be uploaded.', array('%name' => $filename)), t('File upload was blocked'));
|
|
$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'));
|
|
}
|
|
|
|
function setUploadSettings($settings, $rid = NULL) {
|
|
$edit = array();
|
|
foreach ($settings as $key => $value) {
|
|
$edit[$key . '_default'] = $value;
|
|
if ($rid !== NULL && $key != 'upload_list' && $key != 'upload_max_resolution_x' && $key != 'upload_max_resolution_y') {
|
|
$edit[$key . '_' . $rid] = $value;
|
|
}
|
|
}
|
|
$this->drupalPost('admin/config/media/uploads', $edit, 'Save configuration');
|
|
$this->assertText('The configuration options have been saved.', 'Upload setting saved.');
|
|
}
|
|
|
|
/**
|
|
* Upload file to specified node.
|
|
*
|
|
* @param object $node Node object.
|
|
* @param string $filename Name of file to upload.
|
|
* @param boolean $assert Assert that the node was successfully updated.
|
|
*/
|
|
function uploadFile($node, $filename, $assert = TRUE) {
|
|
$edit = array();
|
|
$edit['files[upload]'] = $filename; //edit-upload
|
|
$this->drupalPost('node/' . $node->nid . '/edit', $edit, t('Save'));
|
|
if ($assert) {
|
|
$this->assertRaw(t('Page %title has been updated.', array('%title' => $node->title)), 'File attached successfully.');
|
|
}
|
|
}
|
|
|
|
/**
|
|
* Check that uploaded file is accessible and verify the contents against the original.
|
|
*
|
|
* @param string $filename Name of file to verify.
|
|
*/
|
|
function checkUploadedFile($filename) {
|
|
$file = 'public://' . $filename;
|
|
$this->drupalGet(file_create_url($file), array('external' => TRUE));
|
|
$this->assertResponse(array(200), 'Uploaded ' . $filename . ' is accessible.');
|
|
$this->assertTrue(strpos($this->drupalGetHeader('Content-Type'), 'text/plain') === 0, t('MIME type is text/plain.'));
|
|
$this->assertEqual(file_get_contents($file), $this->drupalGetContent(), 'Uploaded contents of ' . $filename . ' verified.');
|
|
// Verify file actually is readable and writeable by PHP.
|
|
$this->assertTrue(is_readable($file), t('Uploaded file is readable.'));
|
|
$this->assertTrue(is_writeable($file), t('Uploaded file is writeable.'));
|
|
}
|
|
|
|
/**
|
|
* Get the role id of the 'simpletest' role associated with a SimpleTest test user.
|
|
*
|
|
* @param object $user User object.
|
|
* @return integer SimpleTest role id.
|
|
*/
|
|
function getSimpletestRoleId($user) {
|
|
foreach ($user->roles as $rid => $role) {
|
|
if (preg_match('/s\d+/', $role)) {
|
|
return $rid;
|
|
}
|
|
}
|
|
return NULL;
|
|
}
|
|
}
|