From c699b88bc58ae07fffb33611a6d7ed950bbb1e15 Mon Sep 17 00:00:00 2001 From: Andreas Fischer Date: Sun, 11 Nov 2012 14:35:31 +0100 Subject: [PATCH 1/8] [ticket/11192] Add Tebibyte to get_formatted_filesize(). PHPBB3-11192 --- phpBB/includes/functions.php | 6 ++++++ phpBB/language/en/common.php | 2 ++ 2 files changed, 8 insertions(+) diff --git a/phpBB/includes/functions.php b/phpBB/includes/functions.php index 65d8be32ad..33545ab845 100644 --- a/phpBB/includes/functions.php +++ b/phpBB/includes/functions.php @@ -301,6 +301,12 @@ function get_formatted_filesize($value, $string_only = true, $allowed_units = fa global $user; $available_units = array( + 'tb' => array( + 'min' => 1099511627776, // pow(2, 40) + 'index' => 4, + 'si_unit' => 'TB', + 'iec_unit' => 'TIB', + ), 'gb' => array( 'min' => 1073741824, // pow(2, 30) 'index' => 3, diff --git a/phpBB/language/en/common.php b/phpBB/language/en/common.php index 0184dd083b..844d5ef3ef 100644 --- a/phpBB/language/en/common.php +++ b/phpBB/language/en/common.php @@ -568,9 +568,11 @@ $lang = array_merge($lang, array( 'SUBJECT' => 'Subject', 'SUBMIT' => 'Submit', + 'TB' => 'TB', 'TERMS_USE' => 'Terms of use', 'TEST_CONNECTION' => 'Test connection', 'THE_TEAM' => 'The team', + 'TIB' => 'TiB', 'TIME' => 'Time', 'TOO_LARGE' => 'The value you entered is too large.', From c15b98999ef1ba26531e3d6e20695a8f2672f5a4 Mon Sep 17 00:00:00 2001 From: Oleg Pudeyev Date: Thu, 15 Nov 2012 23:57:55 -0500 Subject: [PATCH 2/8] [ticket/11192] Add tests. PHPBB3-11192 --- .../functions/get_formatted_filesize_test.php | 50 +++++++++++++++++++ 1 file changed, 50 insertions(+) create mode 100644 tests/functions/get_formatted_filesize_test.php diff --git a/tests/functions/get_formatted_filesize_test.php b/tests/functions/get_formatted_filesize_test.php new file mode 100644 index 0000000000..1cc7ddf9ec --- /dev/null +++ b/tests/functions/get_formatted_filesize_test.php @@ -0,0 +1,50 @@ +assertEquals($expected, $output); + } +} From b7ec639945a4667508e4a3d1f8ac73da94809b21 Mon Sep 17 00:00:00 2001 From: Andreas Fischer Date: Fri, 16 Nov 2012 07:43:24 +0100 Subject: [PATCH 3/8] [ticket/11192] Also test powers of 10 / 1000. PHPBB3-11192 --- tests/functions/get_formatted_filesize_test.php | 7 +++++++ 1 file changed, 7 insertions(+) diff --git a/tests/functions/get_formatted_filesize_test.php b/tests/functions/get_formatted_filesize_test.php index 1cc7ddf9ec..802f82d126 100644 --- a/tests/functions/get_formatted_filesize_test.php +++ b/tests/functions/get_formatted_filesize_test.php @@ -21,6 +21,13 @@ class phpbb_get_formatted_filesize_test extends phpbb_test_case array(1073741824, '1 GIB'), array(1099511627776, '1 TIB'), + // exact powers of 10 + array(1000, '1000 BYTES'), + array(1000000, '976.56 KIB'), + array(1000000000, '953.67 MIB'), + array(1000000000000, '931.32 GIB'), + array(100000000000000, '90.95 TIB'), + array(0, '0 BYTES'), array(2, '2 BYTES'), array(-2, '-2 BYTES'), From 09c8c58a5c9602a665519586b75ae3e9831367c8 Mon Sep 17 00:00:00 2001 From: Andreas Fischer Date: Fri, 16 Nov 2012 08:00:12 +0100 Subject: [PATCH 4/8] [ticket/11192] Also test strings, e.g. sums returned by the database. PHPBB3-11192 --- tests/functions/get_formatted_filesize_test.php | 10 ++++++++++ 1 file changed, 10 insertions(+) diff --git a/tests/functions/get_formatted_filesize_test.php b/tests/functions/get_formatted_filesize_test.php index 802f82d126..85b06b723d 100644 --- a/tests/functions/get_formatted_filesize_test.php +++ b/tests/functions/get_formatted_filesize_test.php @@ -54,4 +54,14 @@ class phpbb_get_formatted_filesize_test extends phpbb_test_case $this->assertEquals($expected, $output); } + + /** + * @dataProvider get_formatted_filesize_test_data + */ + public function test_get_formatted_filesize_string($input, $expected) + { + $output = get_formatted_filesize("$input"); + + $this->assertEquals($expected, $output); + } } From 4e3a42f59fa32fb1a3e14cff959b19576e5ba7cf Mon Sep 17 00:00:00 2001 From: Andreas Fischer Date: Fri, 16 Nov 2012 08:20:58 +0100 Subject: [PATCH 5/8] [ticket/11192] Test strings not converted to int/float before. PHPBB3-11192 --- .../functions/get_formatted_filesize_test.php | 38 ++++++++++++++++++- 1 file changed, 36 insertions(+), 2 deletions(-) diff --git a/tests/functions/get_formatted_filesize_test.php b/tests/functions/get_formatted_filesize_test.php index 85b06b723d..88866f90ac 100644 --- a/tests/functions/get_formatted_filesize_test.php +++ b/tests/functions/get_formatted_filesize_test.php @@ -45,6 +45,40 @@ class phpbb_get_formatted_filesize_test extends phpbb_test_case ); } + public function get_formatted_filesize_test_data_string() + { + return array( + // exact powers of 2 + array('1', '1 BYTES'), + array('1024', '1 KIB'), + array('1048576', '1 MIB'), + array('1073741824', '1 GIB'), + array('1099511627776', '1 TIB'), + + // exact powers of 10 + array('1000', '1000 BYTES'), + array('1000000', '976.56 KIB'), + array('1000000000', '953.67 MIB'), + array('1000000000000', '931.32 GIB'), + array('100000000000000', '90.95 TIB'), + + array('0', '0 BYTES'), + array('2', '2 BYTES'), + array('-2', '-2 BYTES'), + + array('1023', '1023 BYTES'), + array('1025', '1 KIB'), + array('-1023', '-1023 BYTES'), + array('-1025', '-1025 BYTES'), + + array('1048575', '1024 KIB'), + + // large negatives + array('-1073741824', '-1073741824 BYTES'), + array('-1099511627776', '-1099511627776 BYTES'), + ); + } + /** * @dataProvider get_formatted_filesize_test_data */ @@ -56,11 +90,11 @@ class phpbb_get_formatted_filesize_test extends phpbb_test_case } /** - * @dataProvider get_formatted_filesize_test_data + * @dataProvider get_formatted_filesize_test_data_string */ public function test_get_formatted_filesize_string($input, $expected) { - $output = get_formatted_filesize("$input"); + $output = get_formatted_filesize($input); $this->assertEquals($expected, $output); } From 7cbd440e7a69eb836eb9d58800f1c535d41b83ab Mon Sep 17 00:00:00 2001 From: Andreas Fischer Date: Fri, 16 Nov 2012 08:28:35 +0100 Subject: [PATCH 6/8] [ticket/11192] Mark negative byte numbers as unsupported. PHPBB3-11192 --- phpBB/includes/functions.php | 2 +- tests/functions/get_formatted_filesize_test.php | 16 ---------------- 2 files changed, 1 insertion(+), 17 deletions(-) diff --git a/phpBB/includes/functions.php b/phpBB/includes/functions.php index 33545ab845..8688ba3f7e 100644 --- a/phpBB/includes/functions.php +++ b/phpBB/includes/functions.php @@ -289,7 +289,7 @@ function phpbb_gmgetdate($time = false) /** * Return formatted string for filesizes * -* @param int $value filesize in bytes +* @param int $value filesize in bytes (non-negative number) * @param bool $string_only true if language string should be returned * @param array $allowed_units only allow these units (data array indexes) * diff --git a/tests/functions/get_formatted_filesize_test.php b/tests/functions/get_formatted_filesize_test.php index 88866f90ac..c4793e8073 100644 --- a/tests/functions/get_formatted_filesize_test.php +++ b/tests/functions/get_formatted_filesize_test.php @@ -30,18 +30,10 @@ class phpbb_get_formatted_filesize_test extends phpbb_test_case array(0, '0 BYTES'), array(2, '2 BYTES'), - array(-2, '-2 BYTES'), array(1023, '1023 BYTES'), array(1025, '1 KIB'), - array(-1023, '-1023 BYTES'), - array(-1025, '-1025 BYTES'), - array(1048575, '1024 KIB'), - - // large negatives - array(-1073741824, '-1073741824 BYTES'), - array(-1099511627776, '-1099511627776 BYTES'), ); } @@ -64,18 +56,10 @@ class phpbb_get_formatted_filesize_test extends phpbb_test_case array('0', '0 BYTES'), array('2', '2 BYTES'), - array('-2', '-2 BYTES'), array('1023', '1023 BYTES'), array('1025', '1 KIB'), - array('-1023', '-1023 BYTES'), - array('-1025', '-1025 BYTES'), - array('1048575', '1024 KIB'), - - // large negatives - array('-1073741824', '-1073741824 BYTES'), - array('-1099511627776', '-1099511627776 BYTES'), ); } From efd6f1df63c24ed2947bccb863bb92c2346bf697 Mon Sep 17 00:00:00 2001 From: Andreas Fischer Date: Fri, 16 Nov 2012 08:29:57 +0100 Subject: [PATCH 7/8] [ticket/11192] Update $value parameter description to support other types. PHPBB3-11192 --- phpBB/includes/functions.php | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/phpBB/includes/functions.php b/phpBB/includes/functions.php index 8688ba3f7e..6e661228b7 100644 --- a/phpBB/includes/functions.php +++ b/phpBB/includes/functions.php @@ -289,7 +289,8 @@ function phpbb_gmgetdate($time = false) /** * Return formatted string for filesizes * -* @param int $value filesize in bytes (non-negative number) +* @param mixed $value filesize in bytes +* (non-negative number; int, float or string) * @param bool $string_only true if language string should be returned * @param array $allowed_units only allow these units (data array indexes) * From 8851f9589a5295d569316e76b366f1b16f1a9a96 Mon Sep 17 00:00:00 2001 From: Andreas Fischer Date: Fri, 16 Nov 2012 16:20:55 +0100 Subject: [PATCH 8/8] [ticket/11192] Merge dataProvider arrays because the test is the same now. PHPBB3-11192 --- tests/functions/get_formatted_filesize_test.php | 16 +--------------- 1 file changed, 1 insertion(+), 15 deletions(-) diff --git a/tests/functions/get_formatted_filesize_test.php b/tests/functions/get_formatted_filesize_test.php index c4793e8073..96ea2be132 100644 --- a/tests/functions/get_formatted_filesize_test.php +++ b/tests/functions/get_formatted_filesize_test.php @@ -34,12 +34,8 @@ class phpbb_get_formatted_filesize_test extends phpbb_test_case array(1023, '1023 BYTES'), array(1025, '1 KIB'), array(1048575, '1024 KIB'), - ); - } - public function get_formatted_filesize_test_data_string() - { - return array( + // String values // exact powers of 2 array('1', '1 BYTES'), array('1024', '1 KIB'), @@ -72,14 +68,4 @@ class phpbb_get_formatted_filesize_test extends phpbb_test_case $this->assertEquals($expected, $output); } - - /** - * @dataProvider get_formatted_filesize_test_data_string - */ - public function test_get_formatted_filesize_string($input, $expected) - { - $output = get_formatted_filesize($input); - - $this->assertEquals($expected, $output); - } }