From f3520187d62a28d5f9d1854f47467b563e6784a6 Mon Sep 17 00:00:00 2001 From: Kai Mindermann Date: Thu, 24 Jul 2014 12:45:43 +0200 Subject: [PATCH] formatter: adds unit tests for base 1000, fixes #4412 --- framework/CHANGELOG.md | 1 + framework/base/Formatter.php | 24 +++++++++++++++++++-- tests/unit/framework/base/FormatterTest.php | 22 +++++++++++++++++++ 3 files changed, 45 insertions(+), 2 deletions(-) diff --git a/framework/CHANGELOG.md b/framework/CHANGELOG.md index de4558346a..fb3137578b 100644 --- a/framework/CHANGELOG.md +++ b/framework/CHANGELOG.md @@ -70,6 +70,7 @@ Yii Framework 2 Change Log - Bug #4276: Added check for UPLOAD_ERR_NO_FILE in `yii\web\UploadedFile` and return null if no file was uploaded (OmgDef) - Bug #4342: mssql (dblib) driver does not support getting attributes (tof06) - Bug #4409: Upper case letters in subdirectory prefixes of controller IDs were not properly handled (qiangxue) +- Bug #4412: Formatter used SI Prefixes for base 1024, now uses binary prefixes (kmindi) - Bug: Fixed inconsistent return of `\yii\console\Application::runAction()` (samdark) - Bug: URL encoding for the route parameter added to `\yii\web\UrlManager` (klimov-paul) - Bug: Fixed the bug that requesting protected or private action methods would cause 500 error instead of 404 (qiangxue) diff --git a/framework/base/Formatter.php b/framework/base/Formatter.php index 980ce9907c..e2839dbfe9 100644 --- a/framework/base/Formatter.php +++ b/framework/base/Formatter.php @@ -444,10 +444,13 @@ class Formatter extends Component * @param integer $value value in bytes to be formatted * @param boolean $verbose if full names should be used (e.g. bytes, kilobytes, ...). * Defaults to false meaning that short names will be used (e.g. B, KB, ...). + * @param boolean $binaryPrefix if binary prefixes should be used for base 1024 + * Defaults to true meaning that binary prefixes are used (e.g. kibibyte/KiB, mebibyte/MiB, ...). + * @link http://en.wikipedia.org/wiki/Binary_prefix * @return string the formatted result * @see sizeFormat */ - public function asSize($value, $verbose = false) + public function asSize($value, $verbose = false, $binaryPrefix = true) { $position = 0; @@ -464,6 +467,23 @@ class Formatter extends Component $formattedValue = isset($this->sizeFormat['decimalSeparator']) ? str_replace('.', $this->sizeFormat['decimalSeparator'], $value) : $value; $params = ['n' => $formattedValue]; + if ($binaryPrefix && $this->sizeFormat['base'] === 1024) { + switch ($position) { + case 0: + return $verbose ? Yii::t('yii', '{n, plural, =1{# byte} other{# bytes}}', $params) : Yii::t('yii', '{n} B', $params); + case 1: + return $verbose ? Yii::t('yii', '{n, plural, =1{# kibibyte} other{# kibibytes}}', $params) : Yii::t('yii', '{n} KiB', $params); + case 2: + return $verbose ? Yii::t('yii', '{n, plural, =1{# mebibyte} other{# mebibytes}}', $params) : Yii::t('yii', '{n} MiB', $params); + case 3: + return $verbose ? Yii::t('yii', '{n, plural, =1{# gibibyte} other{# gibibytes}}', $params) : Yii::t('yii', '{n} GiB', $params); + case 4: + return $verbose ? Yii::t('yii', '{n, plural, =1{# tebibyte} other{# tebibytes}}', $params) : Yii::t('yii', '{n} TiB', $params); + default: + return $verbose ? Yii::t('yii', '{n, plural, =1{# pebibyte} other{# pebibytes}}', $params) : Yii::t('yii', '{n} PiB', $params); + } + } + switch ($position) { case 0: return $verbose ? Yii::t('yii', '{n, plural, =1{# byte} other{# bytes}}', $params) : Yii::t('yii', '{n} B', $params); @@ -479,7 +499,7 @@ class Formatter extends Component return $verbose ? Yii::t('yii', '{n, plural, =1{# petabyte} other{# petabytes}}', $params) : Yii::t('yii', '{n} PB', $params); } } - + /** * Formats the value as the time interval between a date and now in human readable form. * diff --git a/tests/unit/framework/base/FormatterTest.php b/tests/unit/framework/base/FormatterTest.php index 5afc2dbf98..ecc201fae7 100644 --- a/tests/unit/framework/base/FormatterTest.php +++ b/tests/unit/framework/base/FormatterTest.php @@ -190,6 +190,28 @@ class FormatterTest extends TestCase $this->assertSame($this->formatter->nullDisplay, $this->formatter->asNumber(null)); } + public function testAsSize() { + // tests for base 1000 + $this->formatter->sizeFormat['base'] = 1000; + $this->assertSame("1.05 MB", $this->formatter->asSize(1024 * 1024)); + $this->assertSame("1.05 MB", $this->formatter->asSize(1024 * 1024, false, false)); + $this->assertSame("1 KB", $this->formatter->asSize(1000)); + $this->assertSame("1.02 KB", $this->formatter->asSize(1023)); + $this->assertSame("3 gigabytes", $this->formatter->asSize(3 * 1000 * 1000 * 1000, true)); + + // tests for base 1024 + $this->formatter->sizeFormat['base'] = 1024; + $this->assertSame("1 KiB", $this->formatter->asSize(1024)); + $this->assertSame("1 MB", $this->formatter->asSize(1024 * 1024, false, false)); + $this->assertSame("1023 B", $this->formatter->asSize(1023)); + $this->assertSame("5 GiB", $this->formatter->asSize(5 * 1024 * 1024 * 1024)); + //$this->assertSame("1 YiB", $this->formatter->asSize(pow(2, 80))); + $this->assertSame("2 GiB", $this->formatter->asSize(2147483647)); // round 1.999 up to 2 + $this->formatter->sizeFormat['decimalSeparator'] = ','; + $this->formatter->sizeFormat['decimals'] = 3; + $this->assertSame("1,001 KiB", $this->formatter->asSize(1025)); + } + public function testFormat() { $value = time();