Page Menu
Home
In-Portal Phabricator
Search
Configure Global Search
Log In
Files
F1168603
D509.id1400.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
Thu, Sep 25, 12:51 AM
Size
7 KB
Mime Type
text/x-diff
Expires
Fri, Sep 26, 12:51 AM (6 h, 48 m)
Engine
blob
Format
Raw Data
Handle
757041
Attached To
D509: INP-1900 Ensure numeric parameter values inside "kUtil::Pounds2Kg" method
D509.id1400.diff
View Options
Index: branches/5.2.x/core/kernel/globals.php
===================================================================
--- branches/5.2.x/core/kernel/globals.php
+++ branches/5.2.x/core/kernel/globals.php
@@ -364,33 +364,44 @@
/**
* Converts KG to Pounds
*
- * @param float $kg
- * @param bool $pounds_only
- * @return float
- * @access public
+ * @param float $kg Kilograms.
+ * @param boolean $pounds_only Calculate as pounds only.
+ *
+ * @return array
+ * @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: branches/5.2.x/core/kernel/utility/formatters/unit_formatter.php
===================================================================
--- branches/5.2.x/core/kernel/utility/formatters/unit_formatter.php
+++ branches/5.2.x/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: branches/5.2.x/core/tests/Unit/kernel/globalsTest.php
===================================================================
--- branches/5.2.x/core/tests/Unit/kernel/globalsTest.php
+++ branches/5.2.x/core/tests/Unit/kernel/globalsTest.php
@@ -60,4 +60,66 @@
);
}
+ /**
+ * @dataProvider validKg2PoundsDataProvider
+ */
+ public function testValidKg2Pounds($kg, $pounds_only, $expected_pounds, $expected_ounces)
+ {
+ list($pounds, $ounces) = kUtil::Kg2Pounds($kg, $pounds_only);
+ $this->assertSame($expected_pounds, $pounds);
+ $this->assertSame($expected_ounces, $ounces);
+ }
+
+ public static function validKg2PoundsDataProvider()
+ {
+ return array(
+ 'pounds_only=false' => array(0.68039, false, 1.0, 8.0),
+ '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->assertSame($expected, kUtil::Pounds2Kg($pounds, $ounces));
+ }
+
+ public static function validPounds2KgDataProvider()
+ {
+ return array(
+ 'pounds <> 0, ounces <> 0' => array(1.0, 8.0, 0.68039),
+ 'pounds <> 0, ounces = 0' => array(1.5, 0.0, 0.68039),
+ 'pounds = 0, ounces <> 0' => array(0.0, 8.0, 0.2268),
+ 'pounds = 0, ounces = 0' => array(0.0, 0.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: branches/5.2.x/core/tests/Unit/kernel/utility/formatters/unit_formatterTest.php
===================================================================
--- branches/5.2.x/core/tests/Unit/kernel/utility/formatters/unit_formatterTest.php
+++ branches/5.2.x/core/tests/Unit/kernel/utility/formatters/unit_formatterTest.php
@@ -0,0 +1,67 @@
+<?php
+
+/**
+ * The class name must match the file name for Phabricator-invoked PHPUnit to run this test.
+ *
+ * TODO: Once "unit_formatter.php" file is renamed we can rename this class/filename as well.
+ */
+final class unit_formatterTest extends AbstractTestCase // phpcs:ignore Squiz.Classes.ValidClassName.NotCamelCaps
+{
+
+ /**
+ * 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->assertSame(0.0, $this->dbItemFixture->GetDBField('SampleField_a'));
+ $this->assertSame(0.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->assertSame($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.0),
+ );
+ }
+
+}
Event Timeline
Log In to Comment