t('Upload functionality'), 'desc' => t('Check content uploaded to nodes.'), 'group' => t('Upload Tests'), ); } function setUp() { parent::setUp(); $this->drupalModuleEnable('upload'); } /** * Create node; upload files to node; and edit, and delete uploads. */ function test_node_upload() { $admin_user = $this->drupalCreateUserRolePerm(array('administer site configuration')); $web_user = $this->drupalCreateUserRolePerm(array('access content', 'edit any page content', 'upload files', 'view uploaded files')); $this->drupalLoginUser($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'; $edit['upload_usersize_default'] = '1'; $this->drupalPost('admin/settings/uploads', $edit, 'Save configuration'); $this->assertText('The configuration options have been saved.', 'Upload setting saved.'); $this->drupalGet('logout'); $this->drupalLoginUser($web_user); // Create a node and attempt to attach files. $node = $this->drupalCreateNode(); $files = array('README.txt', 'INSTALL.txt'); $this->upload_file($node, $files[0]); $this->upload_file($node, $files[1]); // Check to see that uploaded file is listed and actually accessible. $this->assertText($files[0], $files[0] .' found on node.'); $this->assertText($files[1], $files[1] .' found on node.'); $this->check_uploaded_file($files[0]); $this->check_uploaded_file($files[1]); // Fetch db record and use fid to rename and delete file. $upload = db_fetch_object(db_query('SELECT fid, description FROM {upload} WHERE nid = %d', array($node->nid))); if ($upload) { // Rename file. $edit = array(); $edit['files['. $upload->fid .'][description]'] = $new_name = substr($upload->description, 1); $this->drupalPost('node/'. $node->nid .'/edit', $edit, 'Save'); $this->assertWantedRaw(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, 'Save'); $this->assertWantedRaw(t('Page %title has been updated.', array('%title' => $node->title)), 'File deleted successfully.'); $this->assertNoText($new_name, $new_name .' not found on node.'); } else { $this->fail('File upload record not found in database.'); } } /** * Upload file to specified node. * * @param object $node Node object. * @param string $filename Name of file to upload. */ function upload_file($node, $filename) { $edit = array(); $edit['files[upload]'] = $this->get_file_path($filename); $this->drupalPost('node/'. $node->nid .'/edit', $edit, 'Save'); $this->assertWantedRaw(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 verifiy. */ function check_uploaded_file($filename) { $file = $this->get_file_path($filename); $this->drupalGet(file_directory_path() .'/'. $filename); $this->assertResponse(array(200), 'Uploaded '. $filename .' is accessible.'); $this->assertEqual(file_get_contents($file), $this->drupalGetContent(), 'Uploaded contents of '. $filename .' verified.'); } /** * Get canonicalized absolute path to file located in the SimpleTest module. * * @param string $filename Name of file to get path for. * @return string Absolute path. */ function get_file_path($filename) { return realpath(drupal_get_path('module', 'simpletest') .'/'. $filename); } }