Skip to content
This repository
Browse code

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

  • Loading branch information...
commit 6ec0afcf5d813382964798944893ee5db7f0574c 1 parent ad53458
ceeram authored June 25, 2012
4  lib/Cake/Test/Case/Console/Command/Task/ModelTaskTest.php
@@ -315,7 +315,7 @@ public function testInteractiveFieldValidation() {
315 315
 		$this->Task->initValidations();
316 316
 		$this->Task->interactive = true;
317 317
 		$this->Task->expects($this->any())->method('in')
318  
-			->will($this->onConsecutiveCalls('23', 'y', '17', 'n'));
  318
+			->will($this->onConsecutiveCalls('24', 'y', '18', 'n'));
319 319
 
320 320
 		$result = $this->Task->fieldValidation('text', array('type' => 'string', 'length' => 10, 'null' => false));
321 321
 		$expected = array('notempty' => 'notempty', 'maxlength' => 'maxlength');
@@ -333,7 +333,7 @@ public function testInteractiveFieldValidationWithBogusResponse() {
333 333
 		$this->Task->interactive = true;
334 334
 
335 335
 		$this->Task->expects($this->any())->method('in')
336  
-			->will($this->onConsecutiveCalls('999999', '23', 'n'));
  336
+			->will($this->onConsecutiveCalls('999999', '24', 'n'));
337 337
 
338 338
 		$this->Task->expects($this->at(10))->method('out')
339 339
 			->with($this->stringContains('make a valid'));
39  lib/Cake/Test/Case/Utility/CakeNumberTest.php
@@ -523,4 +523,43 @@ public function testToPercentage() {
523 523
 		$this->assertEquals($expected, $result);
524 524
 	}
525 525
 
  526
+/**
  527
+ * testFromReadableSize
  528
+ *
  529
+ * @dataProvider filesizes
  530
+ * @return void
  531
+ */
  532
+	public function testFromReadableSize($size, $expected) {
  533
+		$result = $this->Number->fromReadableSize($size);
  534
+		$this->assertEquals($expected, $result);
  535
+	}
  536
+
  537
+/**
  538
+ * testFromReadableSize
  539
+ *
  540
+ * @expectedException CakeException
  541
+ * @return void
  542
+ */
  543
+	public function testFromReadableSizeException() {
  544
+		$result = $this->Number->fromReadableSize('bogus');
  545
+	}
  546
+
  547
+/**
  548
+ * filesizes dataprovider
  549
+ *
  550
+ * @return array
  551
+ */
  552
+	public function filesizes() {
  553
+		return array(
  554
+			array('512B', 512),
  555
+			array('1KB', 1024),
  556
+			array('1.5KB', 1536),
  557
+			array('1MB', 1048576),
  558
+			array('1.5MB', 1572864),
  559
+			array('1GB', 1073741824),
  560
+			array('1.5GB', 1610612736),
  561
+			array('512', 512),
  562
+		);
  563
+	}
  564
+
526 565
 }
21  lib/Cake/Test/Case/Utility/ValidationTest.php
@@ -2203,7 +2203,7 @@ public function testMimeTypeFalse() {
2203 2203
 	}
2204 2204
 
2205 2205
 /**
2206  
- * testMimeType method
  2206
+ * testUploadError method
2207 2207
  *
2208 2208
  * @return void
2209 2209
  */
@@ -2214,4 +2214,23 @@ public function testUploadError() {
2214 2214
 		$this->assertFalse(Validation::uploadError(2));
2215 2215
 		$this->assertFalse(Validation::uploadError(array('error' => 2)));
2216 2216
 	}
  2217
+
  2218
+/**
  2219
+ * testFileSize method
  2220
+ *
  2221
+ * @return void
  2222
+ */
  2223
+	public function testFileSize() {
  2224
+		$image = CORE_PATH . 'Cake' . DS . 'Test' . DS . 'test_app' . DS . 'webroot' . DS . 'img' . DS . 'cake.power.gif';
  2225
+		$this->assertTrue(Validation::fileSize($image, '<', 1024));
  2226
+		$this->assertTrue(Validation::fileSize(array('tmp_name' => $image), 'isless', 1024));
  2227
+		$this->assertTrue(Validation::fileSize($image, '<', '1KB'));
  2228
+		$this->assertTrue(Validation::fileSize($image, '>=', 200));
  2229
+		$this->assertTrue(Validation::fileSize($image, '==', 201));
  2230
+		$this->assertTrue(Validation::fileSize($image, '==', '201B'));
  2231
+
  2232
+		$this->assertFalse(Validation::fileSize($image, 'isgreater', 1024));
  2233
+		$this->assertFalse(Validation::fileSize(array('tmp_name' => $image), '>', '1KB'));
  2234
+	}
  2235
+
2217 2236
 }
22  lib/Cake/Utility/CakeNumber.php
@@ -102,6 +102,28 @@ public static function toReadableSize($size) {
102 102
 	}
103 103
 
104 104
 /**
  105
+ * Converts filesize from human readable string to bytes
  106
+ *
  107
+ * @param string $size Size in human readable string like '5MB'
  108
+ * @return integer Bytes
  109
+ */
  110
+	public static function fromReadableSize($size) {
  111
+		if (ctype_digit($size)) {
  112
+			return $size * 1;
  113
+		}
  114
+		$units = array('KB', 'MB', 'GB', 'TB', 'PB');
  115
+		foreach ($units as $i => $unit) {
  116
+			if ($unit == substr($size, -2)) {
  117
+				return $size * pow(1024, $i + 1);
  118
+			}
  119
+		}
  120
+		if (substr($size, -1) == 'B' && ctype_digit(substr($size, 0, strlen($size) - 1))) {
  121
+			return $size * 1;
  122
+		}
  123
+		throw new CakeException(__d('cake_dev', 'No unit type.'));
  124
+	}
  125
+
  126
+/**
105 127
  * Formats a number into a percentage string.
106 128
  *
107 129
  * @param float $number A floating point number
22  lib/Cake/Utility/Validation.php
@@ -19,6 +19,7 @@
19 19
 
20 20
 App::uses('Multibyte', 'I18n');
21 21
 App::uses('File', 'Utility');
  22
+App::uses('CakeNumber', 'Utility');
22 23
 // Load multibyte if the extension is missing.
23 24
 if (!function_exists('mb_strlen')) {
24 25
 	class_exists('Multibyte');
@@ -882,6 +883,27 @@ public static function mimeType($check, $mimeTypes = array()) {
882 883
 	}
883 884
 
884 885
 /**
  886
+ * Checks the filesize
  887
+ *
  888
+ * @param string|array $check
  889
+ * @param integer|string $size Size in bytes or human readable string like '5MB'
  890
+ * @param string $operator See `Validation::comparison()`
  891
+ * @return boolean Success
  892
+ */
  893
+	public static function fileSize($check, $operator = null, $size = null) {
  894
+		if (is_array($check) && isset($check['tmp_name'])) {
  895
+			$check = $check['tmp_name'];
  896
+		}
  897
+
  898
+		if (is_string($size)) {
  899
+			$size = CakeNumber::fromReadableSize($size);
  900
+		}
  901
+		$filesize = filesize($check);
  902
+
  903
+		return self::comparison($filesize, $operator, $size);
  904
+	}
  905
+
  906
+/**
885 907
  * Checking for upload errors
886 908
  *
887 909
  * @param string|array $check

0 notes on commit 6ec0afc

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