Commit ecb032a2 authored by Dries's avatar Dries

- Patch #151902 by MadHarold et al: a better format_size() (and removed some...

- Patch #151902 by MadHarold et al: a better format_size() (and removed some excessive white space).
parent 5a9b7c5b
......@@ -1079,17 +1079,21 @@ function parse_size($size) {
* A translated string representation of the size.
*/
function format_size($size, $langcode = NULL) {
if ($size < 1024) {
if ($size < 1000) {
return format_plural($size, '1 byte', '@count bytes', array(), $langcode);
}
else {
$size = round($size / 1024, 2);
$suffix = t('KB', array(), $langcode);
if ($size >= 1024) {
$size = round($size / 1024, 2);
$suffix = t('MB', array(), $langcode);
$size = $size / 1000; // convert bytes to kilobytes (1000 bytes)
$units = array('KB', 'MB', 'GB', 'TB', 'PB', 'EB', 'ZB', 'YB');
foreach ($units as $suffix) {
if (round($size, 2) >= 1000) {
$size = $size / 1000;
}
else {
break;
}
}
return t('@size @suffix', array('@size' => $size, '@suffix' => $suffix), $langcode);
return t('@size @suffix', array('@size' => round($size, 2), '@suffix' => $suffix), $langcode);
}
}
......
<?php
class CommonFormatSizeTestCase extends DrupalWebTestCase {
/**
* Implementation of getInfo().
*/
function getInfo() {
return array(
'name' => t('Format size test'),
'description' => t('Parse a predefined amount of bytes and compare the output with the expected value.'),
'group' => t('System')
);
}
/**
* Implementation of setUp().
*/
function setUp() {
$this->exact_test_cases = array(
'1 byte' => 1, // byte
'1 KB' => 1000, // kilobyte
'1 MB' => 1000000, // megabyte
'1 GB' => 1000000000, // gigabyte
'1 TB' => 1000000000000, // terabyte
'1 PB' => 1000000000000000, // petabyte
'1 EB' => 1000000000000000000, // exabyte
'1 ZB' => 1000000000000000000000, // zettabyte
'1 YB' => 1000000000000000000000000, // yottabyte
);
$this->rounded_test_cases = array(
'2 bytes' => 2, // bytes
'1 MB' => 999999, // 1 MB (not 1000 kilobyte!)
'3.62 MB' => 3623651, // megabytes
'67.23 PB' => 67234178751368124, // petabytes
'235.35 YB' => 235346823821125814962843827, // yottabytes
);
parent::setUp();
}
/**
* testCommonFormatSize
*/
function testCommonFormatSize() {
foreach (array($this->exact_test_cases, $this->rounded_test_cases) as $test_cases) {
foreach ($test_cases as $expected => $size) {
$this->assertTrue(
($result = format_size($size, NULL)) == $expected,
$expected . " == " . $result . " (" . $size . " bytes) %s"
);
}
}
}
}
......@@ -12,7 +12,7 @@ class XMLRPCValidator1Test extends DrupalWebTestCase {
'group' => t('XML-RPC')
);
}
function setUp() {
parent::setUp('simpletest_xmlrpc');
}
......
......@@ -323,13 +323,13 @@ function drupalLogout() {
}
/**
* Generates a random database prefix, runs the install scripts on the
* prefixed database and enable the specified modules. After installation
* many caches are flushed and the internal browser is setup so that the
* page requests will run on the new prefix. A temporary files directory
* Generates a random database prefix, runs the install scripts on the
* prefixed database and enable the specified modules. After installation
* many caches are flushed and the internal browser is setup so that the
* page requests will run on the new prefix. A temporary files directory
* is created with the same name as the database prefix.
*
* @param ...
* @param ...
* List of modules to enable for the duration of the test.
*/
function setUp() {
......@@ -586,7 +586,7 @@ function drupalPost($path, $edit, $submit, $tamper = FALSE) {
}
$out = $this->curlExec(array(CURLOPT_URL => $action, CURLOPT_POST => TRUE, CURLOPT_POSTFIELDS => $post));
// Ensure that any changes to variables in the other thread are picked up.
$this->refreshVariables();
$this->refreshVariables();
return $out;
}
}
......@@ -604,15 +604,15 @@ function drupalPost($path, $edit, $submit, $tamper = FALSE) {
* exist and attempt to create POST data in the correct manner for the particular
* field type.
*
* @param array $post
* @param array $post
* Reference to array of post values.
* @param array $edit
* @param array $edit
* Reference to array of edit values to be checked against the form.
* @param string $submit
* @param string $submit
* Form submit button value.
* @param array $form
* @param array $form
* Array of form elements.
* @return boolean
* @return boolean
* Submit value matches a valid submit input in the form.
*/
protected function handleForm(&$post, &$edit, &$upload, $submit, $form) {
......
......@@ -208,7 +208,7 @@ class UserDeleteTestCase extends DrupalWebTestCase {
class UserPictureTestCase extends DrupalWebTestCase {
protected $user;
protected $_directory_test;
function getInfo() {
return array(
'name' => t('Upload user picture'),
......@@ -216,18 +216,18 @@ class UserPictureTestCase extends DrupalWebTestCase {
'group' => t('User')
);
}
function setUp() {
parent::setUp();
// Enable user pictures.
variable_set('user_pictures', 1);
$this->user = $this->drupalCreateUser();
// Test if directories specified in settings exist in filesystem.
$file_dir = file_directory_path();
$file_check = file_check_directory($file_dir, FILE_CREATE_DIRECTORY, 'file_directory_path');
$picture_dir = variable_get('user_picture_path', 'pictures');
$picture_path = $file_dir .'/'.$picture_dir;
......@@ -267,7 +267,7 @@ class UserPictureTestCase extends DrupalWebTestCase {
variable_set('user_picture_file_size', $test_size);
$pic_path = $this->saveUserPicture($image);
// check if image is displayed in user's profile page
$this->assertRaw(file_create_url($pic_path), "Image is displayed in user's profile page");
......@@ -291,7 +291,7 @@ class UserPictureTestCase extends DrupalWebTestCase {
$image = current($this->drupalGetTestFiles('image'));
$info = image_get_info($image->filename);
// Set new variables.
$test_dim = ($info['width'] + 10) . 'x' . ($info['height'] + 10);
$test_size = filesize($image->filename);
......@@ -324,7 +324,7 @@ class UserPictureTestCase extends DrupalWebTestCase {
$image = current($this->drupalGetTestFiles('image'));
$info = image_get_info($image->filename);
// Set new variables.
$test_size = floor(filesize($image->filename) / 1000) + 1;
$test_dim = ($info['width'] - 10) . 'x' . ($info['height'] - 10);
......@@ -380,7 +380,7 @@ class UserPictureTestCase extends DrupalWebTestCase {
function testPictureIsValid() {
if ($this->_directory_test) {
$this->drupalLogin($this->user);
$image = current($this->drupalGetTestFiles('image'));
$info = image_get_info($image->filename);
......@@ -400,7 +400,7 @@ class UserPictureTestCase extends DrupalWebTestCase {
$this->assertTrue(is_file($pic_path), t('File is located in proper directory'));
}
}
function saveUserPicture($image) {
$edit = array('files[picture_upload]' => realpath($image->filename));
$this->drupalPost('user/' . $this->user->uid.'/edit', $edit, t('Save'));
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment