Skip to content

Commit

Permalink
Add CakeNumber::fromReadableSize() and Validation::filesize()
Browse files Browse the repository at this point in the history
  • Loading branch information
ceeram committed Jun 26, 2012
1 parent ad53458 commit 6ec0afc
Show file tree
Hide file tree
Showing 5 changed files with 105 additions and 3 deletions.
4 changes: 2 additions & 2 deletions lib/Cake/Test/Case/Console/Command/Task/ModelTaskTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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');
Expand All @@ -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'));
Expand Down
39 changes: 39 additions & 0 deletions lib/Cake/Test/Case/Utility/CakeNumberTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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),
);
}

}
21 changes: 20 additions & 1 deletion lib/Cake/Test/Case/Utility/ValidationTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -2203,7 +2203,7 @@ public function testMimeTypeFalse() {
}

/**
* testMimeType method
* testUploadError method
*
* @return void
*/
Expand All @@ -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'));
}

}
22 changes: 22 additions & 0 deletions lib/Cake/Utility/CakeNumber.php
Original file line number Diff line number Diff line change
Expand Up @@ -101,6 +101,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.
*
Expand Down
22 changes: 22 additions & 0 deletions lib/Cake/Utility/Validation.php
Original file line number Diff line number Diff line change
Expand Up @@ -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');
Expand Down Expand Up @@ -881,6 +882,27 @@ public static function mimeType($check, $mimeTypes = array()) {
return in_array($mime, $mimeTypes);
}

/**
* 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
*
Expand Down

0 comments on commit 6ec0afc

Please sign in to comment.