Page Menu
Home
In-Portal Phabricator
Search
Configure Global Search
Log In
Files
F1167494
D509.id1394.diff
No One
Temporary
Actions
View File
Edit File
Delete File
View Transforms
Subscribe
Mute Notifications
Award Token
Flag For Later
Subscribers
None
File Metadata
Details
File Info
Storage
Attached
Created
Tue, Sep 23, 1:07 PM
Size
6 KB
Mime Type
text/x-diff
Expires
Wed, Sep 24, 1:07 PM (18 h, 2 m)
Engine
blob
Format
Raw Data
Handle
756239
Attached To
D509: INP-1900 Ensure numeric parameter values inside "kUtil::Pounds2Kg" method
D509.id1394.diff
View Options
Index: core/kernel/globals.php
===================================================================
--- core/kernel/globals.php
+++ core/kernel/globals.php
@@ -364,33 +364,46 @@
/**
* Converts KG to Pounds
*
- * @param float $kg
- * @param bool $pounds_only
- * @return float
+ * @param float $kg Kilograms.
+ * @param boolean $pounds_only Calculate as pounds only.
+ *
+ * @return array
* @access public
+ * @throws InvalidArgumentException When the $kg argument isn't a number.
*/
public static function Kg2Pounds($kg, $pounds_only = false)
{
- $major = floor( round($kg / self::POUND_TO_KG, 3) );
+ if ( !is_numeric($kg) ) {
+ throw new InvalidArgumentException('The $kg argument isn\'t a number.');
+ }
+
+ $major = floor(round($kg / self::POUND_TO_KG, 3));
$minor = abs(round(($kg - $major * self::POUND_TO_KG) / self::POUND_TO_KG * 16, 2));
- if ($pounds_only) {
+ if ( $pounds_only ) {
$major += round($minor * 0.0625, 2);
$minor = 0;
}
+
return array($major, $minor);
}
/**
* Converts Pounds to KG
*
- * @param float $pounds
- * @param float $ounces
+ * @param float $pounds Pounds.
+ * @param float $ounces Ounces.
+ *
* @return float
* @access public
+ * @throws InvalidArgumentException When either the $pounds or $ounces argument isn't a number.
*/
public static function Pounds2Kg($pounds, $ounces = 0.00)
{
+ if ( !is_numeric($pounds) || !is_numeric($ounces) ) {
+ throw new InvalidArgumentException('Either the $pounds or $ounces argument isn\'t a number.');
+ }
+
return round(($pounds + ($ounces / 16)) * self::POUND_TO_KG, 5);
}
Index: core/kernel/utility/formatters/unit_formatter.php
===================================================================
--- core/kernel/utility/formatters/unit_formatter.php
+++ core/kernel/utility/formatters/unit_formatter.php
@@ -87,7 +87,7 @@
return;
}
else {
- $value = kUtil::Pounds2Kg($major, $minor);
+ $value = kUtil::Pounds2Kg((float)$major, (float)$minor);
}
}
@@ -118,7 +118,7 @@
$minor = null;
}
else {
- list($major, $minor) = kUtil::Kg2Pounds($value);
+ list($major, $minor) = kUtil::Kg2Pounds((float)$value);
// $major = floor( $value / 0.5 );
// $minor = ($value - $major * 0.5) * 32;
}
@@ -128,4 +128,4 @@
}
}
}
-}
\ No newline at end of file
+}
Index: core/tests/Unit/kernel/kUtilTest.php
===================================================================
--- core/tests/Unit/kernel/kUtilTest.php
+++ core/tests/Unit/kernel/kUtilTest.php
@@ -56,4 +56,66 @@
);
}
+ /**
+ * @dataProvider validKg2PoundsDataProvider
+ */
+ public function testValidKg2Pounds($kg, $pounds_only, $expected_pounds, $expected_ounces)
+ {
+ list($pounds, $ounces) = kUtil::Kg2Pounds($kg, $pounds_only);
+ $this->assertEquals($expected_pounds, $pounds);
+ $this->assertEquals($expected_ounces, $ounces);
+ }
+
+ public static function validKg2PoundsDataProvider()
+ {
+ return array(
+ 'pounds_only=false' => array(0.68039, false, 1, 8),
+ 'pounds_only=true' => array(0.68039, true, 1.5, 0),
+ );
+ }
+
+ public function testExceptionKg2Pounds()
+ {
+ $this->expectException(InvalidArgumentException::class);
+ $this->expectExceptionMessage('The $kg argument isn\'t a number.');
+ kUtil::Kg2Pounds(null);
+ }
+
+ /**
+ * @dataProvider validPounds2KgDataProvider
+ */
+ public function testValidPounds2Kg($pounds, $ounces, $expected)
+ {
+ $this->assertEquals($expected, kUtil::Pounds2Kg($pounds, $ounces));
+ }
+
+ public static function validPounds2KgDataProvider()
+ {
+ return array(
+ 'pounds <> 0, ounces <> 0' => array(1, 8, 0.68039),
+ 'pounds <> 0, ounces = 0' => array(1.5, 0, 0.68039),
+ 'pounds = 0, ounces <> 0' => array(0, 8, 0.2268),
+ 'pounds = 0, ounces = 0' => array(0, 0, 0),
+ );
+ }
+
+ /**
+ * @dataProvider exceptionPounds2KgDataProvider
+ */
+ public function testExceptionPounds2Kg($pounds, $ounces)
+ {
+ $this->expectException(InvalidArgumentException::class);
+ $this->expectExceptionMessage('Either the $pounds or $ounces argument isn\'t a number.');
+ kUtil::Pounds2Kg($pounds, $ounces);
+ }
+
+ public static function exceptionPounds2KgDataProvider()
+ {
+ return array(
+ 'pounds non-numeric, ounces numeric' => array(null, 8),
+ 'pounds numeric, ounces non-numeric' => array(1, null),
+ 'both pounds and ounces non-numeric' => array(null, null),
+ );
+ }
+
}
Index: core/tests/Unit/kernel/utility/formatters/kUnitFormatterTest.php
===================================================================
--- /dev/null
+++ core/tests/Unit/kernel/utility/formatters/kUnitFormatterTest.php
@@ -0,0 +1,63 @@
+<?php
+
+
+final class kUnitFormatterTest extends AbstractTestCase
+{
+
+ /**
+ * Fixture of the kDBItem class.
+ *
+ * @var kDBItem
+ */
+ protected $dbItemFixture;
+
+ /**
+ * @before
+ */
+ protected function setUpTest()
+ {
+ $db_item = new kDBItem();
+ $db_item->Init('stop-word', ''); // Existing unit used for the "ValidatorClass" unit config option to work.
+ $db_item->SetFieldOptions('SampleField', array());
+ $db_item->SetFieldOptions('SampleField_a', array());
+ $db_item->SetFieldOptions('SampleField_b', array());
+
+ $this->dbItemFixture = $db_item;
+
+ /** @var LanguagesItem $regional */
+ $regional = $this->Application->recallObject('lang.current');
+ $regional->SetDBField('UnitSystem', 2);
+ }
+
+ public function testNonNumericUpdateSubFields()
+ {
+ $unit_formatter = new kUnitFormatter();
+ $options = array();
+ $unit_formatter->UpdateSubFields('SampleField', 'non-numeric', $options, $this->dbItemFixture);
+ $this->assertEquals(0, $this->dbItemFixture->GetDBField('SampleField_a'));
+ $this->assertEquals(0, $this->dbItemFixture->GetDBField('SampleField_b'));
+ }
+
+ /**
+ * @dataProvider nonNumericUpdateMasterFieldsDataProvider
+ */
+ public function testNonNumericUpdateMasterFields($pounds, $ounces, $expected)
+ {
+ $unit_formatter = new kUnitFormatter();
+ $options = array();
+ $this->dbItemFixture->SetDBField('SampleField_a', $pounds);
+ $this->dbItemFixture->SetDBField('SampleField_b', $ounces);
+ $unit_formatter->UpdateMasterFields('SampleField', 'non-numeric', $options, $this->dbItemFixture);
+ $this->assertEquals($expected, $this->dbItemFixture->GetDBField('SampleField'));
+ }
+
+ public static function nonNumericUpdateMasterFieldsDataProvider()
+ {
+ return array(
+ 'pounds numeric, ounces non-numeric' => array(1.5, 'non-numeric', 0.68039),
+ 'pounds non-numeric, ounces numeric' => array('non-numeric', 24, 0.68039),
+ 'both pounds and ounces non-numeric' => array('non-numeric', 'non-numeric', 0),
+ );
+ }
+
+}
Event Timeline
Log In to Comment