Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
Merge remote-tracking branch 'main-ce/develop' into MAGETWO-36095
  • Loading branch information
Yuri Kovsher committed May 21, 2015
2 parents dc9dd17 + 77e1bfd commit 342f476
Show file tree
Hide file tree
Showing 15 changed files with 619 additions and 149 deletions.
10 changes: 10 additions & 0 deletions app/code/Magento/Checkout/Block/Onepage.php
Expand Up @@ -126,4 +126,14 @@ public function getCheckoutConfig()
{
return $this->configProvider->getConfig();
}

/**
* Get base url for block.
*
* @return string
*/
public function getBaseUrl()
{
return $this->_storeManager->getStore()->getBaseUrl();
}
}
138 changes: 138 additions & 0 deletions app/code/Magento/Checkout/Test/Unit/Block/OnepageTest.php
@@ -0,0 +1,138 @@
<?php
/**
* Copyright © 2015 Magento. All rights reserved.
* See COPYING.txt for license details.
*/
namespace Magento\Checkout\Test\Unit\Block;

class OnepageTest extends \PHPUnit_Framework_TestCase
{
/**
* @var \Magento\Checkout\Block\Onepage
*/
protected $model;

/**
* @var \PHPUnit_Framework_MockObject_MockObject
*/
protected $configProviderMock;

/**
* @var \PHPUnit_Framework_MockObject_MockObject
*/
protected $storeManagerMock;

/**
* @var \PHPUnit_Framework_MockObject_MockObject
*/
protected $formKeyMock;

/**
* @var \PHPUnit_Framework_MockObject_MockObject
*/
protected $layoutProcessorMock;

protected function setUp()
{
$contextMock = $this->getMock('\Magento\Framework\View\Element\Template\Context', [], [], '', false);
$directoryHelperMock = $this->getMock('\Magento\Directory\Helper\Data', [], [], '', false);
$configCacheTypeMock = $this->getMock('\Magento\Framework\App\Cache\Type\Config', [], [], '', false);
$customerSessionMock = $this->getMock('\Magento\Customer\Model\Session', [], [], '', false);
$resourceSessionMock = $this->getMock('\Magento\Checkout\Model\Session', [], [], '', false);
$addressConfigMock = $this->getMock('\Magento\Customer\Model\Address\Config', [], [], '', false);
$httpContextMock = $this->getMock('\Magento\Framework\App\Http\Context', [], [], '', false);
$addressMapperMock = $this->getMock('\Magento\Customer\Model\Address\Mapper', [], [], '', false);
$this->formKeyMock = $this->getMock('\Magento\Framework\Data\Form\FormKey', [], [], '', false);
$this->configProviderMock = $this->getMock(
'\Magento\Checkout\Model\CompositeConfigProvider',
[],
[],
'',
false
);
$countryCollectionFactoryMock = $this->getMock(
'\Magento\Directory\Model\Resource\Country\CollectionFactory',
[],
[],
'',
false
);
$regionCollectionFactoryMock = $this->getMock(
'\Magento\Directory\Model\Resource\Region\CollectionFactory',
[],
[],
'',
false
);
$customerRepositoryMock = $this->getMock(
'\Magento\Customer\Api\CustomerRepositoryInterface',
[],
[],
'',
false
);

$this->storeManagerMock = $this->getMock('\Magento\Store\Model\StoreManagerInterface', [], [], '', false);
$contextMock->expects($this->once())->method('getStoreManager')->willReturn($this->storeManagerMock);
$this->layoutProcessorMock = $this->getMock(
'\Magento\Checkout\Block\Checkout\LayoutProcessorInterface',
[],
[],
'',
false
);

$this->model = new \Magento\Checkout\Block\Onepage(
$contextMock,
$directoryHelperMock,
$configCacheTypeMock,
$customerSessionMock,
$resourceSessionMock,
$countryCollectionFactoryMock,
$regionCollectionFactoryMock,
$customerRepositoryMock,
$addressConfigMock,
$httpContextMock,
$addressMapperMock,
$this->formKeyMock,
$this->configProviderMock,
[$this->layoutProcessorMock]
);
}

public function testGetBaseUrl()
{
$baseUrl = 'http://magento.com';
$storeMock = $this->getMock('\Magento\Store\Model\Store', [], [], '', false);

$storeMock->expects($this->once())->method('getBaseUrl')->willReturn($baseUrl);
$this->storeManagerMock->expects($this->once())->method('getStore')->willReturn($storeMock);

$this->assertEquals($baseUrl, $this->model->getBaseUrl());
}

public function testGetCheckoutConfig()
{
$checkoutConfig = ['checkout', 'config'];
$this->configProviderMock->expects($this->once())->method('getConfig')->willReturn($checkoutConfig);

$this->assertEquals($checkoutConfig, $this->model->getCheckoutConfig());
}

public function testGetFormKey()
{
$formKey = 'form_key';
$this->formKeyMock->expects($this->once())->method('getFormKey')->willReturn($formKey);

$this->assertEquals($formKey, $this->model->getFormKey());
}

public function testGetJsLayout()
{
$processedLayout = ['layout' => ['processed' => true]];
$jsonLayout = '{"layout":{"processed":true}}';
$this->layoutProcessorMock->expects($this->once())->method('process')->with([])->willReturn($processedLayout);

$this->assertEquals($jsonLayout, $this->model->getJsLayout());
}
}
Expand Up @@ -13,27 +13,31 @@ define(
'uiRegistry',
'../model/url-builder',
'mage/storage',
'../model/payment-service'

'../model/payment-service',
'underscore'
],
function (quote, addressList, navigator, selectShippingAddress, registry, urlBuilder, storage, paymentService) {
function (quote, addressList, navigator, selectShippingAddress, registry, urlBuilder, storage, paymentService, _) {
"use strict";
var actionCallback;
var result = function (billingAddress, useForShipping, additionalData) {
var copyBillingToShipping = function() {
var shippingAddressSource = registry.get('checkoutProvider'),
shippingAddress = shippingAddressSource.get('shippingAddress');
for (var property in billingAddress) {
if (billingAddress.hasOwnProperty(property) && shippingAddress.hasOwnProperty(property)) {
if (typeof billingAddress[property] === 'string') {
shippingAddressSource.set('shippingAddress.' + property, billingAddress[property]);
} else {
shippingAddressSource.set('shippingAddress.' + property, _.clone(billingAddress[property]));
}
}
}
};
additionalData = additionalData || {};
quote.setBillingAddress(billingAddress);
if (useForShipping() === '1' && !quote.isVirtual()) {
if (!billingAddress.customerAddressId) {
// update shipping address data in corresponding provider
var shippingAddressSource = registry.get('checkoutProvider');
var shippingAddress = shippingAddressSource.get('shippingAddress');
for (var property in billingAddress) {
if (billingAddress.hasOwnProperty(property)
&& shippingAddress.hasOwnProperty(property)
) {
shippingAddressSource.set('shippingAddress.' + property, billingAddress[property]);
}
}
copyBillingToShipping();
}
selectShippingAddress(billingAddress, useForShipping, additionalData);
} else if (quote.isVirtual()) {
Expand Down Expand Up @@ -72,6 +76,9 @@ define(
);
} else {
navigator.setCurrent('billingAddress').goNext();
if (addressList.isBillingSameAsShipping) {
copyBillingToShipping();
}
}
};
result.setActionCallback = function (value) {
Expand Down
Expand Up @@ -22,16 +22,8 @@ define(['jquery'], function($) {
return address;
},
getAddresses: function() {
if (addresses.indexOf(this.newAddress) == -1) {
this.add(this.newAddress);
}
return addresses;
return addresses.slice(0);
},
newAddress: {
getAddressInline: function() {
return 'New Address';
},
customerAddressId: null
}
isBillingSameAsShipping: false
};
});
Expand Up @@ -3,21 +3,19 @@
* See COPYING.txt for license details.
*/
/*jshint browser:true*/
/*global define,alert*/
/*global define*/
define(
[
"jquery",
'Magento_Ui/js/form/form',
'ko',
'Magento_Customer/js/model/customer',
'../action/select-billing-address',
'Magento_Checkout/js/model/step-navigator',
'../model/step-navigator',
'../model/quote',
'../model/addresslist',
'../action/check-email-availability',
'mage/validation'
'../model/addresslist'
],
function ($, Component, ko, customer, selectBillingAddress, navigator, quote, addressList, checkEmailAvailability) {
function ($, Component, ko, customer, selectBillingAddress, navigator, quote, addressList) {
"use strict";
var stepName = 'billingAddress';
var newAddressSelected = ko.observable(false);
Expand All @@ -35,12 +33,23 @@ define(
return navigator.getStepClassAttributes(stepName);
},
stepNumber: navigator.getStepNumber(stepName),
billingAddresses: customer.getBillingAddressList(),
selectedBillingAddressId: addressList.getAddresses()[0].customerAddressId,
billingAddresses: function() {
var newAddress = {
getAddressInline: function() {
return $.mage.__('New address');
},
customerAddressId: null
},
addresses = addressList.getAddresses();
addresses.push(newAddress);
return addresses;
},
selectedBillingAddressId: ko.observable(
addressList.getAddresses().length ? addressList.getAddresses()[0].customerAddressId : null
),
isVisible: navigator.isStepVisible(stepName),
useForShipping: "1",
quoteIsVirtual: quote.isVirtual(),
isEmailCheckComplete: $.Deferred(),
billingAddressesOptionsText: function(item) {
return item.getAddressInline();
},
Expand All @@ -53,42 +62,32 @@ define(
return additionalData;
},
submitBillingAddress: function() {
if (this.selectedBillingAddressId) {
var additionalData = this.checkUseForShipping(this.useForShipping);
var additionalData = this.checkUseForShipping(this.useForShipping);
if (this.selectedBillingAddressId()) {
selectBillingAddress(
addressList.getAddressById(this.selectedBillingAddressId),
addressList.getAddressById(this.selectedBillingAddressId()),
this.useForShipping,
additionalData
);
} else {
var that = this;
this.validate();
$.when(this.isEmailCheckComplete).done( function() {
if (!that.source.get('params.invalid')) {
var addressData = that.source.get('billingAddress');
var additionalData = that.checkUseForShipping(that.useForShipping);
/**
* All the the input fields that are not a part of the address but need to be submitted
* in the same request must have data-scope attribute set
*/
var additionalFields = $('input[data-scope="additionalAddressData"]').serializeArray();
additionalFields.forEach(function (field) {
additionalData[field.name] = field.value;
});
if (quote.getCheckoutMethod()() && !customer.isLoggedIn()()) {
addressData.email = that.source.get('customerDetails.email');
}
if($(billingFormSelector).validation() && $(billingFormSelector).validation('isValid')) {
selectBillingAddress(addressData, that.useForShipping, additionalData);
}
if (!this.source.get('params.invalid')) {
var addressData = this.source.get('billingAddress');
/**
* All the the input fields that are not a part of the address (e. g. CAPTCHA) but need to be
* submitted in the same request must have data-scope attribute set
*/
var additionalFields = $('input[data-scope="additionalAddressData"]').serializeArray();
additionalFields.forEach(function (field) {
additionalData[field.name] = field.value;
});
if (quote.getCheckoutMethod()() && !customer.isLoggedIn()()) {
addressData.email = this.source.get('customerDetails.email');
}
if($(billingFormSelector).validation() && $(billingFormSelector).validation('isValid')) {
selectBillingAddress(addressData, this.useForShipping, additionalData);
}
}).fail( function() {
alert(
"There is already a registered customer using this email address. " +
"Please log in using this email address or enter a different email address " +
"to register your account."
);
});
}
}
},
navigateToCurrentStep: function() {
Expand All @@ -103,11 +102,7 @@ define(
return newAddressSelected();
},
onAddressChange: function (value) {
if (value === null) {
newAddressSelected(true);
} else {
newAddressSelected(false);
}
value() === null ? newAddressSelected(true) : newAddressSelected(false);
},
validate: function() {
var fields = $(billingFormSelector).find('input, select');
Expand All @@ -116,7 +111,6 @@ define(
fields.trigger('change');
this.source.trigger('billingAddress.data.validate');
this.validateAdditionalAddressFields();
this.isEmailCheckComplete.resolve();
},
validateAdditionalAddressFields: function() {
$(billingFormSelector).validation();
Expand Down
Expand Up @@ -83,11 +83,11 @@ define(
return info;
},
getPaymentMethodCallbacks: function() {
var callbacks = [this.getActiveMethodView().afterSave];
var callbacks = [this.getActiveMethodView().afterSave.bind(this.getActiveMethodView())];

_.each(this.getAdditionalMethods(), function(elem) {
if (elem.isActive()) {
callbacks = _.union(callbacks, [elem.afterSave]);
callbacks = _.union(callbacks, [elem.afterSave.bind(elem)]);
}
});

Expand Down

0 comments on commit 342f476

Please sign in to comment.