-
Notifications
You must be signed in to change notification settings - Fork 997
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Fixes crash in STPAddCardViewController with some prefilled billing addresses #1004
Changes from all commits
134a414
fe5b1aa
70e2b83
9e21431
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
// | ||
// NSLocale+STPSwizzling.h | ||
// StripeiOS Tests | ||
// | ||
// Created by Cameron Sabol on 7/17/18. | ||
// Copyright © 2018 Stripe, Inc. All rights reserved. | ||
// | ||
|
||
#import <Foundation/Foundation.h> | ||
|
||
@interface NSLocale (STPSwizzling) | ||
|
||
+ (void)stp_setCurrentLocale:(NSLocale *)locale; | ||
+ (void)stp_resetCurrentLocale; | ||
|
||
@end |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,59 @@ | ||
// | ||
// NSLocale+STPSwizzling.m | ||
// StripeiOS Tests | ||
// | ||
// Created by Cameron Sabol on 7/17/18. | ||
// Copyright © 2018 Stripe, Inc. All rights reserved. | ||
// | ||
|
||
#import "NSLocale+STPSwizzling.h" | ||
|
||
#import <objc/runtime.h> | ||
|
||
@interface NSObject (STPSwizzling) | ||
|
||
+ (void)stp_swizzleClassMethod:(SEL)original withReplacement:(SEL)replacement; | ||
|
||
@end | ||
|
||
@implementation NSObject (STPSwizzling) | ||
|
||
+ (void)stp_swizzleClassMethod:(SEL)original withReplacement:(SEL)replacement | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Did you consider using the This is a really simple usage, and since it's test code I don't think it needs to get fancy, mostly just curious. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. didn't even know that existed! I'll keep in mind for next time |
||
{ | ||
method_exchangeImplementations(class_getClassMethod(self, original), class_getClassMethod(self, replacement)); | ||
} | ||
|
||
@end | ||
|
||
@implementation NSLocale (STPSwizzling) | ||
|
||
static NSLocale *_stpLocaleOverride = nil; | ||
|
||
+ (void)stp_setCurrentLocale:(NSLocale *)locale | ||
{ | ||
if (_stpLocaleOverride == nil & locale != nil) { | ||
[self stp_swizzleClassMethod:@selector(currentLocale) withReplacement:@selector(stp_currentLocale)]; | ||
[self stp_swizzleClassMethod:@selector(autoupdatingCurrentLocale) withReplacement:@selector(stp_autoUpdatingCurrentLocale)]; | ||
[self stp_swizzleClassMethod:@selector(systemLocale) withReplacement:@selector(stp_systemLocale)]; | ||
} | ||
_stpLocaleOverride = locale; | ||
} | ||
|
||
+ (void)stp_resetCurrentLocale | ||
{ | ||
[self stp_setCurrentLocale:nil]; | ||
} | ||
|
||
+ (instancetype)stp_currentLocale { | ||
return _stpLocaleOverride ?: [self stp_currentLocale]; | ||
} | ||
|
||
+ (instancetype)stp_autoUpdatingCurrentLocale { | ||
return _stpLocaleOverride ?: [self stp_autoUpdatingCurrentLocale]; | ||
} | ||
|
||
+ (instancetype)stp_systemLocale { | ||
return _stpLocaleOverride ?: [self stp_systemLocale]; | ||
} | ||
|
||
@end |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -10,9 +10,11 @@ | |
#import <OCMock/OCMock.h> | ||
#import <Stripe/Stripe.h> | ||
#import "NSError+Stripe.h" | ||
#import "NSLocale+STPSwizzling.h" | ||
#import "STPCard.h" | ||
#import "STPFixtures.h" | ||
#import "STPPaymentCardTextFieldCell.h" | ||
#import "STPPostalCodeValidator.h" | ||
|
||
@interface STPAddCardViewController (Testing) | ||
@property (nonatomic) STPPaymentCardTextFieldCell *paymentCell; | ||
|
@@ -38,6 +40,60 @@ - (STPAddCardViewController *)buildAddCardViewController { | |
return vc; | ||
} | ||
|
||
- (void)testPrefilledBillingAddress_removeAddress { | ||
STPPaymentConfiguration *config = [STPFixtures paymentConfiguration]; | ||
config.requiredBillingAddressFields = STPBillingAddressFieldsZip; | ||
STPAddCardViewController *sut = [[STPAddCardViewController alloc] initWithConfiguration:config | ||
theme:[STPTheme defaultTheme]]; | ||
STPAddress *address = [STPAddress new]; | ||
address.name = @"John Smith Doe"; | ||
address.phone = @"8885551212"; | ||
address.email = @"[email protected]"; | ||
address.line1 = @"55 John St"; | ||
address.city = @"Harare"; | ||
address.postalCode = @"10002"; | ||
address.country = @"ZW"; // Zimbabwe does not require zip codes, while the default locale for tests (US) does | ||
// Sanity checks | ||
XCTAssertFalse([STPPostalCodeValidator postalCodeIsRequiredForCountryCode:@"ZW"]); | ||
XCTAssertTrue([STPPostalCodeValidator postalCodeIsRequiredForCountryCode:@"US"]); | ||
|
||
STPUserInformation *prefilledInfo = [[STPUserInformation alloc] init]; | ||
prefilledInfo.billingAddress = address; | ||
sut.prefilledInformation = prefilledInfo; | ||
|
||
XCTAssertNoThrow([sut loadView]); | ||
XCTAssertNoThrow([sut viewDidLoad]); | ||
} | ||
|
||
- (void)testPrefilledBillingAddress_addAddress { | ||
[NSLocale stp_setCurrentLocale:[NSLocale localeWithLocaleIdentifier:@"en_ZW"]]; // Zimbabwe does not require zip codes, while the default locale for tests (US) does | ||
// Sanity checks | ||
XCTAssertFalse([STPPostalCodeValidator postalCodeIsRequiredForCountryCode:@"ZW"]); | ||
XCTAssertTrue([STPPostalCodeValidator postalCodeIsRequiredForCountryCode:@"US"]); | ||
STPPaymentConfiguration *config = [STPFixtures paymentConfiguration]; | ||
config.requiredBillingAddressFields = STPBillingAddressFieldsZip; | ||
STPAddCardViewController *sut = [[STPAddCardViewController alloc] initWithConfiguration:config | ||
theme:[STPTheme defaultTheme]]; | ||
STPAddress *address = [STPAddress new]; | ||
address.name = @"John Smith Doe"; | ||
address.phone = @"8885551212"; | ||
address.email = @"[email protected]"; | ||
address.line1 = @"55 John St"; | ||
address.city = @"New York"; | ||
address.state = @"NY"; | ||
address.postalCode = @"10002"; | ||
address.country = @"US"; | ||
|
||
STPUserInformation *prefilledInfo = [[STPUserInformation alloc] init]; | ||
prefilledInfo.billingAddress = address; | ||
sut.prefilledInformation = prefilledInfo; | ||
|
||
XCTAssertNoThrow([sut loadView]); | ||
XCTAssertNoThrow([sut viewDidLoad]); | ||
[NSLocale stp_resetCurrentLocale]; | ||
} | ||
|
||
|
||
#pragma clang diagnostic push | ||
#pragma clang diagnostic ignored "-Warc-performSelector-leaks" | ||
|
||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,81 @@ | ||
// | ||
// STPShippingAddressViewControllerTest.m | ||
// StripeiOS Tests | ||
// | ||
// Created by Cameron Sabol on 8/7/18. | ||
// Copyright © 2018 Stripe, Inc. All rights reserved. | ||
// | ||
|
||
#import <XCTest/XCTest.h> | ||
|
||
#import <Stripe/Stripe.h> | ||
#import "NSLocale+STPSwizzling.h" | ||
#import "STPFixtures.h" | ||
#import "STPPostalCodeValidator.h" | ||
|
||
@interface STPShippingAddressViewControllerTest : XCTestCase | ||
|
||
@end | ||
|
||
@implementation STPShippingAddressViewControllerTest | ||
|
||
- (void)testPrefilledBillingAddress_removeAddress { | ||
STPPaymentConfiguration *config = [STPFixtures paymentConfiguration]; | ||
config.requiredShippingAddressFields = [NSSet setWithObject:STPContactFieldPostalAddress]; | ||
|
||
STPAddress *address = [STPAddress new]; | ||
address.name = @"John Smith Doe"; | ||
address.phone = @"8885551212"; | ||
address.email = @"[email protected]"; | ||
address.line1 = @"55 John St"; | ||
address.city = @"Harare"; | ||
address.postalCode = @"10002"; | ||
address.country = @"ZW"; // Zimbabwe does not require zip codes, while the default locale for tests (US) does | ||
// Sanity checks | ||
XCTAssertFalse([STPPostalCodeValidator postalCodeIsRequiredForCountryCode:@"ZW"]); | ||
XCTAssertTrue([STPPostalCodeValidator postalCodeIsRequiredForCountryCode:@"US"]); | ||
|
||
STPShippingAddressViewController *sut = [[STPShippingAddressViewController alloc] initWithConfiguration:config | ||
theme:[STPTheme defaultTheme] | ||
currency:nil | ||
shippingAddress:address | ||
selectedShippingMethod:nil | ||
prefilledInformation:nil]; | ||
|
||
XCTAssertNoThrow([sut loadView]); | ||
XCTAssertNoThrow([sut viewDidLoad]); | ||
} | ||
|
||
- (void)testPrefilledBillingAddress_addAddress { | ||
[NSLocale stp_setCurrentLocale:[NSLocale localeWithLocaleIdentifier:@"en_ZW"]]; | ||
// Zimbabwe does not require zip codes, while the default locale for tests (US) does | ||
// Sanity checks | ||
XCTAssertFalse([STPPostalCodeValidator postalCodeIsRequiredForCountryCode:@"ZW"]); | ||
XCTAssertTrue([STPPostalCodeValidator postalCodeIsRequiredForCountryCode:@"US"]); | ||
STPPaymentConfiguration *config = [STPFixtures paymentConfiguration]; | ||
config.requiredShippingAddressFields = [NSSet setWithObject:STPContactFieldPostalAddress]; | ||
|
||
STPAddress *address = [STPAddress new]; | ||
address.name = @"John Smith Doe"; | ||
address.phone = @"8885551212"; | ||
address.email = @"[email protected]"; | ||
address.line1 = @"55 John St"; | ||
address.city = @"New York"; | ||
address.state = @"NY"; | ||
address.postalCode = @"10002"; | ||
address.country = @"US"; | ||
|
||
STPShippingAddressViewController *sut = [[STPShippingAddressViewController alloc] initWithConfiguration:config | ||
theme:[STPTheme defaultTheme] | ||
currency:nil | ||
shippingAddress:address | ||
selectedShippingMethod:nil | ||
prefilledInformation:nil]; | ||
|
||
XCTAssertNoThrow([sut loadView]); | ||
XCTAssertNoThrow([sut viewDidLoad]); | ||
[NSLocale stp_resetCurrentLocale]; | ||
} | ||
|
||
|
||
@end |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Is it important that the call to
updateInputAccessoryVisibility
is inside theif
block for add, but outside it for remove?I don't think so, but wanted to see if there was something I'm missing.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
no, I'll move it out of the if in remove for consistency :)