Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
BUGFIX: Flat fee shipping fixed to support Countries as DataObjects.
Tidy up of unit tests.

Flat fee shipping test for changed countries as DOs.
  • Loading branch information
frankmullenger committed May 19, 2012
1 parent 61c79a8 commit 94666d0
Show file tree
Hide file tree
Showing 12 changed files with 162 additions and 275 deletions.
5 changes: 2 additions & 3 deletions code/customer/CheckoutPage.php
Expand Up @@ -139,6 +139,7 @@ function index() {
* @return CheckoutForm The checkout/order form
*/
function OrderForm() {

$fields = array();
$validator = new OrderFormValidator();

Expand Down Expand Up @@ -249,9 +250,7 @@ private function addShippingAddressFields(&$fields, &$validator) {
if (!Member::currentUserID() && Geoip::$default_country_code) $countryField->setValue(Geoip::$default_country_code);

$regions = Region::shipping_regions();

SS_Log::log(new Exception(print_r($regions, true)), SS_Log::NOTICE);


$regionField = null;
if (!empty($regions)) {
$regionField = new RegionField('Shipping[Region]', _t('CheckoutPage.REGION',"Region"));
Expand Down
Expand Up @@ -43,7 +43,7 @@ function updateCMSFields(FieldSet &$fields) {
array(
'Title' => 'Label',
'Description' => 'Description',
'SummaryOfCountryCode' => 'Country',
'Country.Title' => 'Country',
'SummaryOfAmount'=> 'Amount'
),
'getCMSFields_forPopup'
Expand Down
38 changes: 19 additions & 19 deletions code/modifiers/shipping/FlatFee/FlatFeeShippingField.php
Expand Up @@ -33,9 +33,9 @@ function updateValue($order) {

//Update the field source based on the shipping address in the current order
$shippingAddress = $order->ShippingAddress();
$shippingCountry = $shippingAddress->Country;
$shippingCountryID = $shippingAddress->CountryID;

$shippingOptions = DataObject::get('FlatFeeShippingRate', "CountryCode = '$shippingCountry'");
$shippingOptions = DataObject::get('FlatFeeShippingRate', "CountryID = '$shippingCountryID'");

$optionsMap = array();
if ($shippingOptions && $shippingOptions->exists()) {
Expand Down Expand Up @@ -82,11 +82,11 @@ function validate($validator){
}

$validator->validationError(
$this->Name(),
$errorMessage,
$this->Name(),
$errorMessage,
"error"
);
$valid = false;
);
$valid = false;
}

if (!$shippingAddressCountry) {
Expand All @@ -97,11 +97,11 @@ function validate($validator){
}

$validator->validationError(
$this->Name(),
$errorMessage,
$this->Name(),
$errorMessage,
"error"
);
$valid = false;
);
$valid = false;
}

$shippingOption = $flatFeeShippingRates->find('ID', $value);
Expand All @@ -113,27 +113,27 @@ function validate($validator){
}

$validator->validationError(
$this->Name(),
$errorMessage,
$this->Name(),
$errorMessage,
"error"
);
$valid = false;
);
$valid = false;
}
else if ($shippingOption) {

if ($shippingAddressCountry != $shippingOption->CountryCode) {
if ($shippingAddressCountry != $shippingOption->CountryID) {

$errorMessage = _t('Form.FLAT_FEE_SHIPPING_COUNTRY_NOT_MATCH', 'This shipping option is no longer available for the shipping country you have selected sorry');
if ($msg = $this->getCustomValidationMessage()) {
$errorMessage = $msg;
}

$validator->validationError(
$this->Name(),
$errorMessage,
$this->Name(),
$errorMessage,
"error"
);
$valid = false;
);
$valid = false;
}
}

Expand Down
11 changes: 6 additions & 5 deletions code/modifiers/shipping/FlatFee/FlatFeeShippingRate.php
Expand Up @@ -19,8 +19,7 @@ class FlatFeeShippingRate extends DataObject {
public static $db = array(
'Title' => 'Varchar',
'Description' => 'Varchar',
'Amount' => 'Money',
'CountryCode' => 'Varchar(2)' //Two letter country codes for ISO 3166-1 alpha-2
'Amount' => 'Money'
);

/**
Expand All @@ -31,7 +30,8 @@ class FlatFeeShippingRate extends DataObject {
* @var unknown_type
*/
static $has_one = array (
'SiteConfig' => 'SiteConfig'
'SiteConfig' => 'SiteConfig',
'Country' => 'Country_Shipping'
);

/**
Expand All @@ -50,7 +50,7 @@ public function getCMSFields_forPopup() {
$amountField->setAllowedCurrencies(Product::$allowed_currency);
$fields->push($amountField);

$countryField = new DropdownField('CountryCode', _t('FlatFeeShippingRate.COUNTRY', 'Country'), Country::shipping_countries());
$countryField = new DropdownField('CountryID', _t('FlatFeeShippingRate.COUNTRY', 'Country'), Country::shipping_countries());
$fields->push($countryField);

return $fields;
Expand Down Expand Up @@ -79,13 +79,14 @@ public function SummaryOfAmount() {
* Country name for a given country code
*
* @return String Name of country
*/
*
public function SummaryOfCountryCode() {
$supportedCountries = Country::shipping_countries();
if (in_array($this->CountryCode, array_keys($supportedCountries))) {
return $supportedCountries[$this->CountryCode];
}
return _t('FlatFeeShippingRate.NO_COUNTRY_SET', 'No Country Set');
}
*/

}
40 changes: 1 addition & 39 deletions tests/CartTest.php
Expand Up @@ -34,11 +34,7 @@
* @subpackage tests
* @version 1.0
*/
class CartTest extends FunctionalTest {

static $fixture_file = 'swipestripe/tests/Shop.yml';
static $disable_themes = true;
static $use_draft_site = false;
class CartTest extends SwipeStripeTest {

function setUp() {
parent::setUp();
Expand All @@ -48,40 +44,6 @@ function setUp() {
//Check that payment module is installed
$this->assertTrue(class_exists('Payment'), 'Payment module is installed.');
}

/**
* Log current member out by clearing session
*/
function logOut() {
$this->session()->clear('loggedInAs');
}

/**
* Helper to get data from a form.
*
* @param String $formID
* @return Array
*/
function getFormData($formID) {
$page = $this->mainSession->lastPage();
$data = array();

if ($page) {
$form = $page->getFormById($formID);
if (!$form) user_error("Function getFormData() failed to find the form {$formID}", E_USER_ERROR);

foreach ($form->_widgets as $widget) {

$fieldName = $widget->getName();
$fieldValue = $widget->getValue();

$data[$fieldName] = $fieldValue;
}
}
else user_error("Function getFormData() called when there is no form loaded. Visit the page with the form first", E_USER_ERROR);

return $data;
}

/**
* Create product and check basic attributes
Expand Down
43 changes: 1 addition & 42 deletions tests/CheckoutTest.php
Expand Up @@ -28,11 +28,7 @@
* @subpackage tests
* @version 1.0
*/
class CheckoutTest extends FunctionalTest {

static $fixture_file = 'swipestripe/tests/Shop.yml';
static $disable_themes = true;
static $use_draft_site = false;
class CheckoutTest extends SwipeStripeTest {

function setUp() {
parent::setUp();
Expand All @@ -56,43 +52,6 @@ function setUp() {
Payment::set_supported_methods(array(
'ChequePayment' => 'Cheque Or Pay On Site'
));

//Enable shipping countries
Address::$shipping_countries = array('NZ' => 'New Zealand', 'AU' => 'Australia');
}

/**
* Log current member out by clearing session
*/
function logOut() {
$this->session()->clear('loggedInAs');
}

/**
* Helper to get data from a form.
*
* @param String $formID
* @return Array
*/
function getFormData($formID) {
$page = $this->mainSession->lastPage();
$data = array();

if ($page) {
$form = $page->getFormById($formID);
if (!$form) user_error("Function getFormData() failed to find the form {$formID}", E_USER_ERROR);

foreach ($form->_widgets as $widget) {

$fieldName = $widget->getName();
$fieldValue = $widget->getValue();

$data[$fieldName] = $fieldValue;
}
}
else user_error("Function getFormData() called when there is no form loaded. Visit the page with the form first", E_USER_ERROR);

return $data;
}

/**
Expand Down
51 changes: 6 additions & 45 deletions tests/OrderModifierTest.php → tests/FlatFeeShippingTest.php
Expand Up @@ -14,11 +14,7 @@
* @subpackage tests
* @version 1.0
*/
class OrderModifierTest extends FunctionalTest {

static $fixture_file = 'swipestripe/tests/Shop.yml';
static $disable_themes = true;
static $use_draft_site = false;
class FlatFeeShippingTest extends SwipeStripeTest {

function setUp() {
parent::setUp();
Expand All @@ -32,54 +28,19 @@ function setUp() {
Payment::set_supported_methods(array(
'ChequePayment' => 'Cheque Or Pay On Site'
));

//Flat fee shipping enabled for NZ and AU
Address::$shipping_countries = array('NZ' => 'New Zealand', 'AU' => 'Australia');

FlatFeeShipping::enable();
}

/**
* Log current member out by clearing session
*/
function logOut() {
$this->session()->clear('loggedInAs');
}

/**
* Helper to get data from a form.
*
* @param String $formID
* @return Array
*/
function getFormData($formID) {
$page = $this->mainSession->lastPage();
$data = array();

if ($page) {
$form = $page->getFormById($formID);
if (!$form) user_error("Function getFormData() failed to find the form {$formID}", E_USER_ERROR);

foreach ($form->_widgets as $widget) {

$fieldName = $widget->getName();
$fieldValue = $widget->getValue();

$data[$fieldName] = $fieldValue;
}
}
else user_error("Function getFormData() called when there is no form loaded. Visit the page with the form first", E_USER_ERROR);

return $data;
}

/**
* Try to checkout with valid flat fee shipping option
*/
function testCheckoutFlatFeeShipping() {

$productA = $this->objFromFixture('Product', 'productA');
$shippingMainCentreNZ = $this->objFromFixture('FlatFeeShippingRate', 'MainCentreNewZealand');
$shippingAmount = $shippingMainCentreNZ->Amount->getAmount();
$shippingNZ = $this->objFromFixture('Country_Shipping', 'newZealand');

$checkoutPage = DataObject::get_one('CheckoutPage');
$accountPage = DataObject::get_one('AccountPage');
Expand Down Expand Up @@ -111,10 +72,10 @@ function testCheckoutFlatFeeShipping() {

$this->get(Director::makeRelative($checkoutPage->Link()));
$this->submitForm('CheckoutForm_OrderForm', null, array(
'Shipping[Country]' => 'NZ',
'Shipping[Country]' => 1,
'Modifiers[FlatFeeShipping]' => $shippingMainCentreNZ->ID
));

$orders = $buyer->Orders();
$orders->sort('ID', "ASC");
$this->assertEquals(2, $orders->Count());
Expand Down

0 comments on commit 94666d0

Please sign in to comment.