From 3aaa12f114e5ae35ea052a2df5f5eb4c43edd34c Mon Sep 17 00:00:00 2001 From: Russell Michell Date: Fri, 13 Sep 2013 12:21:21 +1200 Subject: [PATCH] FIX: Fixes #2398 - hasAmount() failed to return true for values <= 0.99 and >= 0.01 - Added unit tests --- model/fieldtypes/Money.php | 3 ++- tests/model/MoneyTest.php | 30 ++++++++++++++++++++++++++++++ 2 files changed, 32 insertions(+), 1 deletion(-) diff --git a/model/fieldtypes/Money.php b/model/fieldtypes/Money.php index 298e97bb8..3fa0dabb9 100644 --- a/model/fieldtypes/Money.php +++ b/model/fieldtypes/Money.php @@ -212,7 +212,8 @@ class Money extends DBField implements CompositeDBField { * @return boolean */ public function hasAmount() { - return (int)$this->getAmount() != '0'; + $a = $this->getAmount(); + return (!empty($a) && is_numeric($a)); } public function isChanged() { diff --git a/tests/model/MoneyTest.php b/tests/model/MoneyTest.php index 7323bc87c..cc0397063 100644 --- a/tests/model/MoneyTest.php +++ b/tests/model/MoneyTest.php @@ -277,6 +277,36 @@ class MoneyTest extends SapphireTest { $this->assertEquals('£2.46', $obj->obj('MyOtherMoney')->Nice()); } + + public function testHasAmount() { + $obj = new MoneyTest_DataObject(); + $m = new Money(); + $obj->MyMoney = $m; + + $m->setValue(array('Amount' => 1)); + $this->assertEquals(true, $obj->MyMoney->hasAmount()); + + $m->setValue(array('Amount' => 1.00)); + $this->assertEquals(true, $obj->MyMoney->hasAmount()); + + $m->setValue(array('Amount' => 1.01)); + $this->assertEquals(true, $obj->MyMoney->hasAmount()); + + $m->setValue(array('Amount' => 0.99)); + $this->assertEquals(true, $obj->MyMoney->hasAmount()); + + $m->setValue(array('Amount' => 0.01)); + $this->assertEquals(true, $obj->MyMoney->hasAmount()); + + $m->setValue(array('Amount' => 0)); + $this->assertEquals(false, $obj->MyMoney->hasAmount()); + + $m->setValue(array('Amount' => 0.0)); + $this->assertEquals(false, $obj->MyMoney->hasAmount()); + + $m->setValue(array('Amount' => 0.00)); + $this->assertEquals(false, $obj->MyMoney->hasAmount()); + } }