Index: modules/blogapi/blogapi.test =================================================================== RCS file: /cvs/drupal/drupal/modules/blogapi/blogapi.test,v retrieving revision 1.9 diff -u -p -r1.9 blogapi.test --- modules/blogapi/blogapi.test 22 Feb 2009 17:55:29 -0000 1.9 +++ modules/blogapi/blogapi.test 4 Mar 2009 19:58:25 -0000 @@ -67,7 +67,7 @@ class BlogAPITestCase extends DrupalWebT $this->assertTrue($result, t('Post successfully modified.')); // Upload file. - $file = current($this->drupalGetTestFiles('text')); + $file = $this->drupalGetTestFile('text'); $file_contents = file_get_contents($file->filepath); $file = array(); $file['name'] = $this->randomName() . '.txt'; Index: modules/simpletest/drupal_web_test_case.php =================================================================== RCS file: /cvs/drupal/drupal/modules/simpletest/drupal_web_test_case.php,v retrieving revision 1.86 diff -u -p -r1.86 drupal_web_test_case.php --- modules/simpletest/drupal_web_test_case.php 22 Feb 2009 20:12:03 -0000 1.86 +++ modules/simpletest/drupal_web_test_case.php 4 Mar 2009 19:58:25 -0000 @@ -605,6 +605,49 @@ class DrupalWebTestCase { } /** + * Get a file that can be used in tests. + * + * @param $type + * File type, possible values: 'binary', 'html', 'image', 'javascript', 'php', 'sql', 'text'. + * @param $min_size + * Minimum file size in bytes. + * @param $max_size + * Maximum file size in bytes. + * @return + * List file that matches the given criteria. + */ + protected function drupalGetTestFile($type = NULL, $min_size = NULL, $max_size = NULL) { + + $matching_files = array(); + $valid_type = in_array($type, array('binary', 'html', 'image', 'javascript', 'php', 'sql', 'text')) ? $type : FALSE; + + foreach ($this->drupalGetTestFiles() as $file) { + // If a valid type is specified, get rid of all files that aren't of that type. + if ($valid_type && !preg_match('/' . $valid_type . '\-.*/')) { + continue; + } + + if ($min_size || $max_size) { + $stats = stat($file->filepath); + + // If a $min_size was passed in, get rid of any files below that size. + if ($min_size && $stats['size'] < $min_size) { + continue; + } + + // If a $max_size was passed in, get rid of any files above that size. + if ($max_size && $stats['size'] > $max_size) { + continue; + } + } + + $matching_files[] = $file; + } + + return count($matching_files) ? $matching_files[0] : FALSE; + } + + /** * Compare two files based on size and file name. */ protected function drupalCompareFiles($file1, $file2) { Index: modules/simpletest/tests/file.test =================================================================== RCS file: /cvs/drupal/drupal/modules/simpletest/tests/file.test,v retrieving revision 1.25 diff -u -p -r1.25 file.test --- modules/simpletest/tests/file.test 22 Feb 2009 17:55:30 -0000 1.25 +++ modules/simpletest/tests/file.test 4 Mar 2009 19:58:26 -0000 @@ -524,7 +524,7 @@ class FileSaveUploadTest extends FileHoo $account = $this->drupalCreateUser(array('access content')); $this->drupalLogin($account); - $this->image = current($this->drupalGetTestFiles('image')); + $this->image = $this->drupalGetTestFile('image'); $this->assertTrue(is_file($this->image->filepath), t("The file we're going to upload exists.")); $this->maxFidBefore = db_query('SELECT MAX(fid) AS fid FROM {files}')->fetchField(); @@ -558,7 +558,7 @@ class FileSaveUploadTest extends FileHoo // Upload a second file. $max_fid_before = db_query('SELECT MAX(fid) AS fid FROM {files}')->fetchField(); - $image2 = current($this->drupalGetTestFiles('image')); + $image2 = $this->drupalGetTestFile('image'); $edit = array('files[file_test_upload]' => realpath($image2->filepath)); $this->drupalPost('file-test/upload', $edit, t('Submit')); $this->assertResponse(200, t('Received a 200 response for posted test file.')); Index: modules/upload/upload.test =================================================================== RCS file: /cvs/drupal/drupal/modules/upload/upload.test,v retrieving revision 1.13 diff -u -p -r1.13 upload.test --- modules/upload/upload.test 22 Feb 2009 17:55:30 -0000 1.13 +++ modules/upload/upload.test 4 Mar 2009 19:58:27 -0000 @@ -105,7 +105,7 @@ class UploadTestCase extends DrupalWebTe $node = $this->drupalCreateNode(); // Attempt to upload .txt file when .html is only extension allowed. - $text_file = current($this->drupalGetTestFiles('text')); + $text_file = $this->drupalGetTestFile('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->filepath, FALSE); @@ -115,10 +115,7 @@ class UploadTestCase extends DrupalWebTe $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]->filepath; + $html_file = $this->drupalGetTestFile('html'); $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')); } @@ -127,8 +124,7 @@ class UploadTestCase extends DrupalWebTe * 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)->filepath; + $file = $this->drupalGetTestFile('text', 1310720); // 1 MB. $admin_user = $this->drupalCreateUser(array('administer site configuration')); $web_user = $this->drupalCreateUser(array('access content', 'edit any page content', 'upload files', 'view uploaded files')); Index: modules/user/user.test =================================================================== RCS file: /cvs/drupal/drupal/modules/user/user.test,v retrieving revision 1.29 diff -u -p -r1.29 user.test --- modules/user/user.test 22 Feb 2009 17:55:30 -0000 1.29 +++ modules/user/user.test 4 Mar 2009 19:58:27 -0000 @@ -517,7 +517,7 @@ class UserPictureTestCase extends Drupal $this->drupalLogin($this->user); // Try to upload a file that is not an image for the user picture. - $not_an_image = current($this->drupalGetTestFiles('html')); + $not_an_image = $this->drupalGetTestFile('html'); $this->saveUserPicture($not_an_image); $this->assertRaw(t('Only JPEG, PNG and GIF images are allowed.'), t('Non-image files are not accepted.')); } @@ -534,7 +534,7 @@ class UserPictureTestCase extends Drupal if (image_get_toolkit()) { $this->drupalLogin($this->user); - $image = current($this->drupalGetTestFiles('image')); + $image = $this->drupalGetTestFile('image'); $info = image_get_info($image->filepath); // Set new variables: invalid dimensions, valid filesize (0 = no limit). @@ -568,7 +568,7 @@ class UserPictureTestCase extends Drupal $this->drupalLogin($this->user); - $image = current($this->drupalGetTestFiles('image')); + $image = $this->drupalGetTestFile('image', 1024); $info = image_get_info($image->filepath); // Set new variables: valid dimensions, invalid filesize. @@ -603,7 +603,7 @@ class UserPictureTestCase extends Drupal $this->drupalLogin($this->user); - $image = current($this->drupalGetTestFiles('image')); + $image = $this->drupalGetTestFile('image'); $info = image_get_info($image->filepath); // Set new variables: invalid dimensions, valid filesize (0 = no limit). @@ -636,7 +636,7 @@ class UserPictureTestCase extends Drupal if (!image_get_toolkit()) { $this->drupalLogin($this->user); - $image = current($this->drupalGetTestFiles('image')); + $image = $this->drupalGetTestFile('image', 1024); $info = image_get_info($image->filepath); // Set new variables: valid dimensions, invalid filesize. @@ -668,7 +668,7 @@ class UserPictureTestCase extends Drupal if ($this->_directory_test) { $this->drupalLogin($this->user); - $image = current($this->drupalGetTestFiles('image')); + $image = $this->drupalGetTestFiles('image'); $info = image_get_info($image->filepath); // Set new variables: valid dimensions, valid filesize (0 = no limit).