Skip to content

Commit

Permalink
EN-4581 Bugfix: back office needs to be refreshed for button to show (#…
Browse files Browse the repository at this point in the history
…1266)

* Bugfix: back office needs to be refreshed for button to show

* fix tests

* fix tests

* fix tests
  • Loading branch information
dumega authored and njfaries committed May 10, 2021
1 parent 0101907 commit 8254aac
Show file tree
Hide file tree
Showing 11 changed files with 71 additions and 43 deletions.
2 changes: 1 addition & 1 deletion Block/BlockTrait.php
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,7 @@ public function getStoreId()
public function isEnabled()
{
$storeId = $this->getStoreId();
return $this->configHelper->isActive($storeId);
return ($this->_appState->getAreaCode() == \Magento\Framework\App\Area::AREA_ADMINHTML) || $this->configHelper->isActive($storeId);
}

/**
Expand Down
4 changes: 2 additions & 2 deletions Block/Form.php
Original file line number Diff line number Diff line change
Expand Up @@ -180,15 +180,15 @@ public function isAdminReorderForLoggedInCustomerFeatureEnabled()
*/
public function getPublishableKeyBackOffice()
{
return $this->configHelper->getPublishableKeyBackOffice();
return $this->configHelper->getPublishableKeyBackOffice($this->getQuoteData()->getStoreId());
}

/**
* @return string
*/
public function getPublishableKeyPaymentOnly()
{
return $this->configHelper->getPublishableKeyPayment();
return $this->configHelper->getPublishableKeyPayment($this->getQuoteData()->getStoreId());
}

/**
Expand Down
9 changes: 6 additions & 3 deletions Controller/Adminhtml/Cart/Data.php
Original file line number Diff line number Diff line change
Expand Up @@ -142,9 +142,10 @@ public function execute()
$quote = $this->_getOrderCreateModel()->getQuote();
$this->storeManager->setCurrentStore($storeId);

$backOfficeKey = $this->configHelper->getPublishableKeyBackOffice();
$paymentOnlyKey = $this->configHelper->getPublishableKeyPayment();
$isPreAuth = $this->configHelper->getIsPreAuth();
$backOfficeKey = $this->configHelper->getPublishableKeyBackOffice($storeId);
$paymentOnlyKey = $this->configHelper->getPublishableKeyPayment($storeId);
$isPreAuth = $this->configHelper->getIsPreAuth($storeId);
$connectUrl = $this->configHelper->getCdnUrl($storeId) . '/connect.js';

$customerEmail = $quote->getCustomerEmail();
if (!$quote->getCustomerId() && $this->cartHelper->getCustomerByEmail($customerEmail)) {
Expand Down Expand Up @@ -185,6 +186,7 @@ public function execute()
'paymentOnlyKey' => $paymentOnlyKey,
'storeId' => $storeId,
'isPreAuth' => $isPreAuth,
'connectUrl' => $connectUrl
]
);
} catch (LocalizedException $e) {
Expand All @@ -196,6 +198,7 @@ public function execute()
'paymentOnlyKey' => $paymentOnlyKey ?? '',
'storeId' => $storeId ?? '',
'isPreAuth' => $isPreAuth ?? '',
'connectUrl' => $connectUrl ?? '',
]
);
} catch (Exception $e) {
Expand Down
4 changes: 2 additions & 2 deletions Helper/Api.php
Original file line number Diff line number Diff line change
Expand Up @@ -267,10 +267,10 @@ public function sendRequest($request)
*
* @return Request
*/
public function buildRequest($requestData)
public function buildRequest($requestData, $storeId = null)
{
$apiData = $requestData->getApiData();
$apiUrl = $this->configHelper->getApiUrl() . self::API_CURRENT_VERSION . $requestData->getDynamicApiUrl();
$apiUrl = $this->configHelper->getApiUrl($storeId) . self::API_CURRENT_VERSION . $requestData->getDynamicApiUrl();
$apiKey = $requestData->getApiKey();
$requestMethod = $requestData->getRequestMethod() ?: 'POST';
$contentType = $requestData->getContentType() ?: 'application/json';
Expand Down
4 changes: 2 additions & 2 deletions Helper/Order.php
Original file line number Diff line number Diff line change
Expand Up @@ -415,7 +415,7 @@ public function fetchTransactionInfo($reference, $storeId = null)
$requestData->setApiKey($this->configHelper->getApiKey($storeId));
$requestData->setRequestMethod('GET');
//Build Request
$request = $this->apiHelper->buildRequest($requestData);
$request = $this->apiHelper->buildRequest($requestData, $storeId);

$result = $this->apiHelper->sendRequest($request);
$response = $result->getResponse();
Expand Down Expand Up @@ -996,7 +996,7 @@ public function voidTransactionOnBolt($transactionId, $storeId)
$requestData->setDynamicApiUrl(ApiHelper::API_VOID_TRANSACTION);
$requestData->setApiKey($apiKey);
//Build Request
$request = $this->apiHelper->buildRequest($requestData);
$request = $this->apiHelper->buildRequest($requestData, $storeId);
$result = $this->apiHelper->sendRequest($request);
$response = $result->getResponse();

Expand Down
22 changes: 0 additions & 22 deletions Test/Unit/Block/BlockTraitTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -152,28 +152,6 @@ public function dataProvider_isKeyMissing()
];
}


/**
* @test
* @dataProvider dataProvider_isEnabled
* @param $apiKey
* @param $expected
*/
public function isEnabled($apiKey, $expected)
{
$this->currentMock->expects(self::any())->method('getStoreId')->willReturn(self::STORE_ID);
$this->configHelper->expects(self::any())->method('isActive')->with(self::STORE_ID)->willReturn($apiKey);
$this->assertEquals($expected, $this->currentMock->isEnabled());
}

public function dataProvider_isEnabled()
{
return [
[true, true],
[false, false]
];
}

/**
* @test
*/
Expand Down
20 changes: 19 additions & 1 deletion Test/Unit/Block/FormTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,7 @@ private function initRequiredMocks()

$this->quoteMock = $this->getMockBuilder(BackendQuote::class)
->disableOriginalConstructor()
->setMethods(['getBillingAddress'])
->setMethods(['getBillingAddress','getStoreId'])
->getMock();

$this->addressMock = $this->getMockBuilder(Address::class)
Expand Down Expand Up @@ -188,12 +188,30 @@ public function isAdminReorderForLoggedInCustomerFeatureEnabled()
*/
public function getPublishableKeyBackOfficeShouldReturnConfigValue()
{
$this->quoteMock->expects(self::once())->method('getStoreId')->willReturn(1);
$this->block->expects(self::once())->method('getQuoteData')->willReturn($this->quoteMock);
$this->configHelperMock
->method('getPublishableKeyBackOffice')
->with(1)
->willReturn("backoffice-key");

$this->assertEquals("backoffice-key", $this->block->getPublishableKeyBackOffice());
}

/**
* @test
*/
public function getPublishableKeyPaymentOnlyShouldReturnConfigValue()
{
$this->quoteMock->expects(self::once())->method('getStoreId')->willReturn(1);
$this->block->expects(self::once())->method('getQuoteData')->willReturn($this->quoteMock);
$this->configHelperMock
->method('getPublishableKeyPayment')
->with(1)
->willReturn("payment-key");

$this->assertEquals("payment-key", $this->block->getPublishableKeyPaymentOnly());
}

/**
* @test
Expand Down
20 changes: 17 additions & 3 deletions Test/Unit/Block/JsTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,8 @@
use Magento\Store\Model\StoreManagerInterface;
use PHPUnit\Framework\MockObject\MockObject;
use ReflectionException;
use Magento\Framework\App\State;
use Magento\Framework\App\Area;

/**
* Class JsTest
Expand Down Expand Up @@ -145,6 +147,9 @@ class JsTest extends BoltTestCase
* @var MockObject|EventsForThirdPartyModules
*/
private $eventsForThirdPartyModules;

/** @var State */
private $_appState;

/**
* @test
Expand Down Expand Up @@ -1372,10 +1377,17 @@ public function isBoltProductPage_withVariousProductCheckoutStatesAndVariousPage
*/
public function isEnabled_withVariousConfigActiveStates_returnsBoltPaymentModuleIsActive(
$isActive,
$areaCode,
$expectedResult
) {
$this->_appState = $this->createPartialMock(
State::class,
['getAreaCode']
);
TestHelper::setProperty($this->currentMock, '_appState', $this->_appState);
$this->_appState->expects(self::once())->method('getAreaCode')->willReturn($areaCode);
$this->currentMock->expects(static::once())->method('getStoreId')->willReturn(static::STORE_ID);
$this->configHelper->expects(static::once())
$this->configHelper->expects($areaCode == Area::AREA_ADMINHTML ? static::never() : static::once())
->method('isActive')
->with(static::STORE_ID)
->willReturn($isActive);
Expand All @@ -1391,8 +1403,10 @@ public function isEnabled_withVariousConfigActiveStates_returnsBoltPaymentModule
public function isEnabled_withVariousConfigActiveStatesProvider()
{
return [
['isActive' => false, 'expectedResult' => false],
['isActive' => true, 'expectedResult' => true],
['isActive' => false, 'areaCode' => Area::AREA_WEBAPI_SOAP, 'expectedResult' => false],
['isActive' => true, 'areaCode' => Area::AREA_WEBAPI_SOAP, 'expectedResult' => true],
['isActive' => false, 'areaCode' => Area::AREA_ADMINHTML, 'expectedResult' => true],
['isActive' => true, 'areaCode' => Area::AREA_ADMINHTML, 'expectedResult' => true],
];
}

Expand Down
13 changes: 12 additions & 1 deletion Test/Unit/Controller/Adminhtml/Cart/DataTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,8 @@ class DataTest extends BoltTestCase
const HINT = 'hint!';
const RESPONSE_TOKEN = 'response_token';
const CUSTOMER_EMAIL = 'test@bolt.com';
const CDN_URL_SANDBOX = 'https://connect-sandbox.bolt.com';
const CDN_URL_SANDBOX_JS = 'https://connect-sandbox.bolt.com/connect.js';

/**
* @var Context
Expand Down Expand Up @@ -171,7 +173,8 @@ private function initData()
'storeId' => self::STORE_ID,
'isPreAuth' => self::IS_PREAUTH,
'backOfficeKey' => self::PUBLISHABLE_KEY,
'paymentOnlyKey' => null
'paymentOnlyKey' => null,
'connectUrl' => self::CDN_URL_SANDBOX_JS,
];

$this->noTokenCartArray = ['orderToken' => ''];
Expand All @@ -184,6 +187,7 @@ private function initData()
'isPreAuth' => self::IS_PREAUTH,
'backOfficeKey' => self::PUBLISHABLE_KEY,
'paymentOnlyKey' => '',
'connectUrl' => self::CDN_URL_SANDBOX_JS,
];

$this->map = [
Expand All @@ -210,9 +214,14 @@ private function initRequiredMocks()

$this->configHelperMock = $this->createMock(Config::class);
$this->configHelperMock->method('getPublishableKeyBackOffice')
->with(self::STORE_ID)
->willReturn(self::PUBLISHABLE_KEY);
$this->configHelperMock->method('getIsPreAuth')
->with(self::STORE_ID)
->willReturn(self::IS_PREAUTH);
$this->configHelperMock->method('getCdnUrl')
->with(self::STORE_ID)
->willReturn(self::CDN_URL_SANDBOX);

$this->quoteMock = $this->createPartialMock(
\Magento\Quote\Model\Quote::class,
Expand Down Expand Up @@ -440,6 +449,7 @@ public function execute_withSessionStoreIdNotSet_returnsErrorResponse()
'paymentOnlyKey' => '',
'storeId' => '',
'isPreAuth' => '',
'connectUrl' => '',
]
);

Expand Down Expand Up @@ -477,6 +487,7 @@ public function execute_withAlreadyUsedCustomerEmail_returnsErrorResponse()
'paymentOnlyKey' => '',
'storeId' => self::STORE_ID,
'isPreAuth' => self::IS_PREAUTH,
'connectUrl' => self::CDN_URL_SANDBOX_JS,
]
);

Expand Down
4 changes: 2 additions & 2 deletions Test/Unit/Helper/OrderTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -645,7 +645,7 @@ public function fetchTransactionInfo()

$request = $this->createMock(Request::class);

$this->apiHelper->expects(static::once())->method('buildRequest')->with($requestObject)
$this->apiHelper->expects(static::once())->method('buildRequest')->with($requestObject, self::STORE_ID)
->willReturn($request);

/** @var MockObject|Response $result */
Expand Down Expand Up @@ -4988,7 +4988,7 @@ public function voidTransactionOnBolt($data)
$this->dataObjectFactory->expects(self::once())->method('setApiKey')->willReturnSelf();

$this->apiHelper->expects(self::once())->method('buildRequest')
->with($this->dataObjectFactory)->willReturn($this->boltRequest);
->with($this->dataObjectFactory, self::STORE_ID)->willReturn($this->boltRequest);
$this->apiHelper->expects(self::once())->method('sendRequest')
->withAnyParameters()->willReturn($this->responseFactory);

Expand Down
12 changes: 8 additions & 4 deletions view/adminhtml/templates/boltpay/js/replacejs.phtml
Original file line number Diff line number Diff line change
Expand Up @@ -221,7 +221,7 @@ require([
* Inject connect.js
* return void
*/
var insertConnectScript = function(publishableKey) {
var insertConnectScript = function(publishableKey, connectUrl) {
var scriptTag = document.getElementById('bolt-connect');
if (scriptTag) {
BoltCheckout.configure(cart, hints, callbacks);
Expand All @@ -231,9 +231,13 @@ require([
console.error('No publishable key set');
return;
}
if (!connectUrl) {
console.error('No connect url set');
return;
}
scriptTag = document.createElement('script');
scriptTag.setAttribute('type', 'text/javascript');
scriptTag.setAttribute('src', settings.connect_url);
scriptTag.setAttribute('src', connectUrl);
scriptTag.setAttribute('id', 'bolt-connect');
scriptTag.setAttribute('data-publishable-key', publishableKey);
scriptTag.onload = function() {BoltCheckout.configure(cart, hints, callbacks);}
Expand Down Expand Up @@ -394,8 +398,8 @@ require([
settings.storeId = data.storeId;
settings.isPreAuth = data.isPreAuth;

if (data.backOfficeKey) {
insertConnectScript(data.backOfficeKey);
if (data.backOfficeKey && data.connectUrl) {
insertConnectScript(data.backOfficeKey, data.connectUrl);
}
if (data.paymentOnlyKey && settings.pay_by_link_url && cart.orderToken) {
$(".bolt-checkout-options-separator").toggle(!!data.backOfficeKey);
Expand Down

0 comments on commit 8254aac

Please sign in to comment.