Skip to content

Commit

Permalink
Merge pull request #29 from Kentico/MVCHF11-2-customer-contact-ralati…
Browse files Browse the repository at this point in the history
…onship

Fix YSOD in base license when ShoppingCart is saved
  • Loading branch information
MichalVanzura authored Feb 8, 2018
2 parents 9d680d3 + e99fadb commit 34377a5
Show file tree
Hide file tree
Showing 6 changed files with 44 additions and 9 deletions.
3 changes: 3 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -122,6 +122,9 @@


## Kentico.Ecommerce
### 2.0.1 (2018-02-07)
#### Fixed
* Fixed licensing errors that occurred when saving a shopping cart on sites with a lower license edition than EMS.

### 2.0.0 (2017-12-11)
#### Release notes
Expand Down
14 changes: 11 additions & 3 deletions src/Kentico.Ecommerce/Models/ShoppingCart.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
using System.Linq;

using CMS.ContactManagement;
using CMS.ContactManagement.Internal;
using CMS.Core;
using CMS.DataEngine;
using CMS.Ecommerce;
Expand All @@ -19,6 +20,7 @@ public class ShoppingCart
{
private readonly EcommerceActivityLogger mActivityLogger;
private readonly ICurrentContactProvider mCurrentContactProvider;
private readonly IContactProcessingChecker mContactProcessingChecker;
private IEnumerable<ShoppingCartItem> mItems;
private Customer mCustomer;
private CustomerAddress mBillingAddress;
Expand Down Expand Up @@ -217,12 +219,12 @@ public CustomerAddress ShippingAddress
/// </summary>
/// <param name="originalCart"><see cref="ShoppingCartInfo"/> object representing an original Kentico shopping cart info object from which the model is created.</param>
public ShoppingCart(ShoppingCartInfo originalCart)
:this(originalCart, new EcommerceActivityLogger(), Service.Resolve<ICurrentContactProvider>())
:this(originalCart, new EcommerceActivityLogger(), Service.Resolve<ICurrentContactProvider>(), Service.Resolve<IContactProcessingChecker>())
{
}


internal ShoppingCart(ShoppingCartInfo originalCart, EcommerceActivityLogger activityLogger, ICurrentContactProvider currentContactProvider)
internal ShoppingCart(ShoppingCartInfo originalCart, EcommerceActivityLogger activityLogger, ICurrentContactProvider currentContactProvider, IContactProcessingChecker contactProcessingChecker)
{
if (originalCart == null)
{
Expand All @@ -232,6 +234,7 @@ internal ShoppingCart(ShoppingCartInfo originalCart, EcommerceActivityLogger act
OriginalCart = originalCart;
mActivityLogger = activityLogger;
mCurrentContactProvider = currentContactProvider;
mContactProcessingChecker = contactProcessingChecker;
}


Expand Down Expand Up @@ -492,7 +495,12 @@ private CustomerInfo CreateCustomerFromUser()

private void SetCustomerRelationAndUpdateContact(CustomerInfo customerInfo)
{
if (mCurrentContactProvider == null)
if (mCurrentContactProvider == null || mContactProcessingChecker == null)
{
return;
}

if (!mContactProcessingChecker.CanProcessContactInCurrentContext())
{
return;
}
Expand Down
2 changes: 1 addition & 1 deletion src/Kentico.Ecommerce/Properties/AssemblyInfo.cs
Original file line number Diff line number Diff line change
Expand Up @@ -14,5 +14,5 @@
[assembly: Guid("5047a2a8-707c-49f0-995b-15bd5456ab2f")]
[assembly: AssemblyVersion("2.0.0.0")]
[assembly: AssemblyFileVersion("2.0.0.0")]
[assembly: AssemblyInformationalVersion("2.0.0")]
[assembly: AssemblyInformationalVersion("2.0.1")]
[assembly: InternalsVisibleTo("Kentico.Ecommerce.Tests")]
7 changes: 4 additions & 3 deletions test/Kentico.Ecommerce.Tests/EcommerceTestsBase.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using CMS.ContactManagement;
using CMS.ContactManagement.Internal;
using CMS.DataEngine;
using CMS.Ecommerce;
using CMS.SiteProvider;
Expand Down Expand Up @@ -132,7 +133,7 @@ public ShoppingCart CreateCartWithItem(int skuId, int units = 1)
}


public ShoppingCart CreateCartWithCustomerInfo(CustomerInfo customer, AddressInfo address = null, ICurrentContactProvider currentContactProvider = null)
public ShoppingCart CreateCartWithCustomerInfo(CustomerInfo customer, AddressInfo address = null, ICurrentContactProvider currentContactProvider = null, IContactProcessingChecker contactProcessingChecker = null)
{
var cartInfo = ShoppingCartFactory.CreateCart(SiteID);
cartInfo.ShoppingCartCurrencyID = Factory.MainCurrency.CurrencyID;
Expand All @@ -142,7 +143,7 @@ public ShoppingCart CreateCartWithCustomerInfo(CustomerInfo customer, AddressInf

ShoppingCartInfoProvider.SetShoppingCartInfo(cartInfo);

return new ShoppingCart(cartInfo, new EcommerceActivitiesLoggerFake(), currentContactProvider);
return new ShoppingCart(cartInfo, new EcommerceActivitiesLoggerFake(), currentContactProvider, contactProcessingChecker);
}


Expand All @@ -152,7 +153,7 @@ public ShoppingCart CreateEmptyShoppingCart()

ShoppingCartInfoProvider.SetShoppingCartInfo(originalCart);

return new ShoppingCart(originalCart, new EcommerceActivitiesLoggerFake(), null);
return new ShoppingCart(originalCart, new EcommerceActivitiesLoggerFake(), null, null);
}


Expand Down
2 changes: 1 addition & 1 deletion test/Kentico.Ecommerce.Tests/OrderTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,7 @@ private Order CreateOrderFromNewCart()
cartInfo.Evaluate();
cartInfo.Insert();

var cart = new ShoppingCart(cartInfo, mEcommerceActivitiesLogger, null);
var cart = new ShoppingCart(cartInfo, mEcommerceActivitiesLogger, null, null);

return mService.CreateOrder(cart);
}
Expand Down
25 changes: 24 additions & 1 deletion test/Kentico.Ecommerce.Tests/ShoppingCartTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
using CMS.Activities;
using CMS.Base;
using CMS.ContactManagement;
using CMS.ContactManagement.Internal;
using CMS.Core;
using CMS.Ecommerce;
using CMS.SiteProvider;
Expand Down Expand Up @@ -532,6 +533,25 @@ public void AddItem_LogsActivity()
}


[Test]
public void CartSave_ContactProcessingNotAllowed_ContactNotCreated()
{
SiteContext.CurrentSite = SiteInfo;
var customer = Factory.CustomerAnonymous;

var currentContactProvider = Substitute.For<ICurrentContactProvider>();

var contactProcessingChecker = Substitute.For<IContactProcessingChecker>();
contactProcessingChecker.CanProcessContactInCurrentContext().Returns(false);

var cart = CreateCartWithCustomerInfo(customer, null, currentContactProvider, contactProcessingChecker);

cart.Save();

currentContactProvider.DidNotReceive().GetCurrentContact(Arg.Any<IUserInfo>(), Arg.Any<bool>());
}


[Test]
public void CartSave_UpdateContactRelations_ContactIsUpdated()
{
Expand All @@ -542,7 +562,10 @@ public void CartSave_UpdateContactRelations_ContactIsUpdated()
var currentContactProvider = Substitute.For<ICurrentContactProvider>();
currentContactProvider.GetCurrentContact(Arg.Any<IUserInfo>(), Arg.Any<bool>()).Returns(contact);

var cart = CreateCartWithCustomerInfo(customer, null, currentContactProvider);
var contactProcessingChecker = Substitute.For<IContactProcessingChecker>();
contactProcessingChecker.CanProcessContactInCurrentContext().Returns(true);

var cart = CreateCartWithCustomerInfo(customer, null, currentContactProvider, contactProcessingChecker);

cart.Save();

Expand Down

0 comments on commit 34377a5

Please sign in to comment.