Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

Add CakeNumber::fromReadableSize() and Validation::filesize()

  • Loading branch information...
commit 6ec0afcf5d813382964798944893ee5db7f0574c 1 parent ad53458
@ceeram ceeram authored
View
4 lib/Cake/Test/Case/Console/Command/Task/ModelTaskTest.php
@@ -315,7 +315,7 @@ public function testInteractiveFieldValidation() {
$this->Task->initValidations();
$this->Task->interactive = true;
$this->Task->expects($this->any())->method('in')
- ->will($this->onConsecutiveCalls('23', 'y', '17', 'n'));
+ ->will($this->onConsecutiveCalls('24', 'y', '18', 'n'));
$result = $this->Task->fieldValidation('text', array('type' => 'string', 'length' => 10, 'null' => false));
$expected = array('notempty' => 'notempty', 'maxlength' => 'maxlength');
@@ -333,7 +333,7 @@ public function testInteractiveFieldValidationWithBogusResponse() {
$this->Task->interactive = true;
$this->Task->expects($this->any())->method('in')
- ->will($this->onConsecutiveCalls('999999', '23', 'n'));
+ ->will($this->onConsecutiveCalls('999999', '24', 'n'));
$this->Task->expects($this->at(10))->method('out')
->with($this->stringContains('make a valid'));
View
39 lib/Cake/Test/Case/Utility/CakeNumberTest.php
@@ -523,4 +523,43 @@ public function testToPercentage() {
$this->assertEquals($expected, $result);
}
+/**
+ * testFromReadableSize
+ *
+ * @dataProvider filesizes
+ * @return void
+ */
+ public function testFromReadableSize($size, $expected) {
+ $result = $this->Number->fromReadableSize($size);
+ $this->assertEquals($expected, $result);
+ }
+
+/**
+ * testFromReadableSize
+ *
+ * @expectedException CakeException
+ * @return void
+ */
+ public function testFromReadableSizeException() {
+ $result = $this->Number->fromReadableSize('bogus');
+ }
+
+/**
+ * filesizes dataprovider
+ *
+ * @return array
+ */
+ public function filesizes() {
+ return array(
+ array('512B', 512),
+ array('1KB', 1024),
+ array('1.5KB', 1536),
+ array('1MB', 1048576),
+ array('1.5MB', 1572864),
+ array('1GB', 1073741824),
+ array('1.5GB', 1610612736),
+ array('512', 512),
+ );
+ }
+
}
View
21 lib/Cake/Test/Case/Utility/ValidationTest.php
@@ -2203,7 +2203,7 @@ public function testMimeTypeFalse() {
}
/**
- * testMimeType method
+ * testUploadError method
*
* @return void
*/
@@ -2214,4 +2214,23 @@ public function testUploadError() {
$this->assertFalse(Validation::uploadError(2));
$this->assertFalse(Validation::uploadError(array('error' => 2)));
}
+
+/**
+ * testFileSize method
+ *
+ * @return void
+ */
+ public function testFileSize() {
+ $image = CORE_PATH . 'Cake' . DS . 'Test' . DS . 'test_app' . DS . 'webroot' . DS . 'img' . DS . 'cake.power.gif';
+ $this->assertTrue(Validation::fileSize($image, '<', 1024));
+ $this->assertTrue(Validation::fileSize(array('tmp_name' => $image), 'isless', 1024));
+ $this->assertTrue(Validation::fileSize($image, '<', '1KB'));
+ $this->assertTrue(Validation::fileSize($image, '>=', 200));
+ $this->assertTrue(Validation::fileSize($image, '==', 201));
+ $this->assertTrue(Validation::fileSize($image, '==', '201B'));
+
+ $this->assertFalse(Validation::fileSize($image, 'isgreater', 1024));
+ $this->assertFalse(Validation::fileSize(array('tmp_name' => $image), '>', '1KB'));
+ }
+
}
View
22 lib/Cake/Utility/CakeNumber.php
@@ -102,6 +102,28 @@ public static function toReadableSize($size) {
}
/**
+ * Converts filesize from human readable string to bytes
+ *
+ * @param string $size Size in human readable string like '5MB'
+ * @return integer Bytes
+ */
+ public static function fromReadableSize($size) {
+ if (ctype_digit($size)) {
+ return $size * 1;
+ }
+ $units = array('KB', 'MB', 'GB', 'TB', 'PB');
+ foreach ($units as $i => $unit) {
+ if ($unit == substr($size, -2)) {
+ return $size * pow(1024, $i + 1);
+ }
+ }
+ if (substr($size, -1) == 'B' && ctype_digit(substr($size, 0, strlen($size) - 1))) {
+ return $size * 1;
+ }
+ throw new CakeException(__d('cake_dev', 'No unit type.'));
+ }
+
+/**
* Formats a number into a percentage string.
*
* @param float $number A floating point number
View
22 lib/Cake/Utility/Validation.php
@@ -19,6 +19,7 @@
App::uses('Multibyte', 'I18n');
App::uses('File', 'Utility');
+App::uses('CakeNumber', 'Utility');
// Load multibyte if the extension is missing.
if (!function_exists('mb_strlen')) {
class_exists('Multibyte');
@@ -882,6 +883,27 @@ public static function mimeType($check, $mimeTypes = array()) {
}
/**
+ * Checks the filesize
+ *
+ * @param string|array $check
+ * @param integer|string $size Size in bytes or human readable string like '5MB'
+ * @param string $operator See `Validation::comparison()`
+ * @return boolean Success
+ */
+ public static function fileSize($check, $operator = null, $size = null) {
+ if (is_array($check) && isset($check['tmp_name'])) {
+ $check = $check['tmp_name'];
+ }
+
+ if (is_string($size)) {
+ $size = CakeNumber::fromReadableSize($size);
+ }
+ $filesize = filesize($check);
+
+ return self::comparison($filesize, $operator, $size);
+ }
+
+/**
* Checking for upload errors
*
* @param string|array $check

0 comments on commit 6ec0afc

Please sign in to comment.
Something went wrong with that request. Please try again.