Skip to content

Commit 08eebe4

Browse files
committed
Merge pull request #623 from magento-api/pull-request
[API] Bug Fixes
2 parents b5900bc + ceb7f46 commit 08eebe4

38 files changed

+499
-12
lines changed

app/code/Magento/Backend/Block/Widget/Grid/Export.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -257,6 +257,7 @@ public function _exportIterateCollection($callback, array $args)
257257
$break = false;
258258

259259
while ($break !== true) {
260+
$originalCollection->clear();
260261
$originalCollection->setPageSize($this->getExportPageSize());
261262
$originalCollection->setCurPage($page);
262263
$originalCollection->load();

app/code/Magento/Backend/Test/Unit/Model/Session/AdminConfigTest.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -112,7 +112,7 @@ public function testSetCookiePathNonDefault()
112112
public function testSetSessionSettingsByConstructor($secureRequest)
113113
{
114114
$sessionName = 'admin';
115-
$this->requestMock->expects($this->once())->method('isSecure')->willReturn($secureRequest);
115+
$this->requestMock->expects($this->exactly(2))->method('isSecure')->willReturn($secureRequest);
116116

117117
$validatorMock = $this->getMockBuilder('Magento\Framework\Validator\ValidatorInterface')
118118
->disableOriginalConstructor()

app/code/Magento/Customer/Api/CustomerRepositoryInterface.php

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@
1313
interface CustomerRepositoryInterface
1414
{
1515
/**
16-
* Create customer.
16+
* Create or update a customer.
1717
*
1818
* @api
1919
* @param \Magento\Customer\Api\Data\CustomerInterface $customer
@@ -38,7 +38,7 @@ public function save(\Magento\Customer\Api\Data\CustomerInterface $customer, $pa
3838
public function get($email, $websiteId = null);
3939

4040
/**
41-
* Retrieve customer.
41+
* Get customer by customer ID.
4242
*
4343
* @api
4444
* @param int $customerId
@@ -51,6 +51,10 @@ public function getById($customerId);
5151
/**
5252
* Retrieve customers which match a specified criteria.
5353
*
54+
* This call returns an array of objects, but detailed information about each object’s attributes might not be
55+
* included. See http://devdocs.magento.com/codelinks/attributes.html#CustomerRepositoryInterface to determine
56+
* which call to use to get detailed information about all attributes for an object.
57+
*
5458
* @api
5559
* @param \Magento\Framework\Api\SearchCriteriaInterface $searchCriteria
5660
* @return \Magento\Customer\Api\Data\CustomerSearchResultsInterface

app/code/Magento/Customer/Api/GroupRepositoryInterface.php

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,10 @@ public function getById($id);
4141
* The list of groups can be filtered to exclude the NOT_LOGGED_IN group using the first parameter and/or it can
4242
* be filtered by tax class.
4343
*
44+
* This call returns an array of objects, but detailed information about each object’s attributes might not be
45+
* included. See http://devdocs.magento.com/codelinks/attributes.html#GroupRepositoryInterface to determine
46+
* which call to use to get detailed information about all attributes for an object.
47+
*
4448
* @api
4549
* @param \Magento\Framework\Api\SearchCriteriaInterface $searchCriteria
4650
* @return \Magento\Customer\Api\Data\GroupSearchResultsInterface

app/code/Magento/Directory/Model/Currency/Import/FixerIo.php

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -80,8 +80,11 @@ private function convertBatch($data, $currencyFrom, $currenciesTo)
8080
$url = str_replace('{{CURRENCY_TO}}', $currenciesStr, $url);
8181

8282
set_time_limit(0);
83-
$response = $this->getServiceResponse($url);
84-
ini_restore('max_execution_time');
83+
try {
84+
$response = $this->getServiceResponse($url);
85+
} finally {
86+
ini_restore('max_execution_time');
87+
}
8588

8689
foreach ($currenciesTo as $currencyTo) {
8790
if ($currencyFrom == $currencyTo) {

app/code/Magento/Directory/Model/Currency/Import/Webservicex.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -72,7 +72,7 @@ protected function _convert($currencyFrom, $currencyTo, $retry = 0)
7272
)->getBody();
7373

7474
$xml = simplexml_load_string($response, null, LIBXML_NOERROR);
75-
if (!$xml) {
75+
if (!$xml || (isset($xml[0]) && $xml[0] == -1)) {
7676
$this->_messages[] = __('We can\'t retrieve a rate from %1.', $url);
7777
return null;
7878
}
Lines changed: 183 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,183 @@
1+
<?php
2+
/**
3+
* Copyright © 2016 Magento. All rights reserved.
4+
* See COPYING.txt for license details.
5+
*/
6+
namespace Magento\Directory\Model\Currency\Import;
7+
8+
/**
9+
* Currency rate import model (From http://query.yahooapis.com/)
10+
*/
11+
class YahooFinance extends \Magento\Directory\Model\Currency\Import\AbstractImport
12+
{
13+
/**
14+
* Currency converter url string
15+
*
16+
* @var string
17+
*/
18+
private $currencyConverterUrl = 'http://query.yahooapis.com/v1/public/yql?format=json&q={{YQL_STRING}}'
19+
.'&env=store://datatables.org/alltableswithkeys';
20+
21+
/**
22+
* Config path for service timeout
23+
*
24+
* @var string
25+
*/
26+
private $timeoutConfigPath = 'currency/yahoofinance/timeout';
27+
28+
/**
29+
* Http Client Factory
30+
*
31+
* @var \Magento\Framework\HTTP\ZendClientFactory
32+
*/
33+
protected $httpClientFactory;
34+
35+
/**
36+
* Core scope config
37+
*
38+
* @var \Magento\Framework\App\Config\ScopeConfigInterface
39+
*/
40+
private $scopeConfig;
41+
42+
/**
43+
* Initialize dependencies
44+
*
45+
* @param \Magento\Directory\Model\CurrencyFactory $currencyFactory
46+
* @param \Magento\Framework\App\Config\ScopeConfigInterface $scopeConfig
47+
* @param \Magento\Framework\HTTP\ZendClientFactory $httpClientFactory
48+
*/
49+
public function __construct(
50+
\Magento\Directory\Model\CurrencyFactory $currencyFactory,
51+
\Magento\Framework\App\Config\ScopeConfigInterface $scopeConfig,
52+
\Magento\Framework\HTTP\ZendClientFactory $httpClientFactory
53+
) {
54+
parent::__construct($currencyFactory);
55+
$this->scopeConfig = $scopeConfig;
56+
$this->httpClientFactory = $httpClientFactory;
57+
}
58+
59+
/**
60+
* {@inheritdoc}
61+
*/
62+
public function fetchRates()
63+
{
64+
$data = [];
65+
$currencies = $this->_getCurrencyCodes();
66+
$defaultCurrencies = $this->_getDefaultCurrencyCodes();
67+
68+
foreach ($defaultCurrencies as $currencyFrom) {
69+
if (!isset($data[$currencyFrom])) {
70+
$data[$currencyFrom] = [];
71+
}
72+
$data = $this->convertBatch($data, $currencyFrom, $currencies);
73+
ksort($data[$currencyFrom]);
74+
}
75+
return $data;
76+
}
77+
78+
/**
79+
* Return currencies convert rates in batch mode
80+
*
81+
* @param array $data
82+
* @param string $currencyFrom
83+
* @param array $currenciesTo
84+
* @return array
85+
*/
86+
private function convertBatch($data, $currencyFrom, $currenciesTo)
87+
{
88+
$url = $this->buildUrl($currencyFrom, $currenciesTo);
89+
set_time_limit(0);
90+
try {
91+
$response = $this->getServiceResponse($url);
92+
} finally {
93+
ini_restore('max_execution_time');
94+
}
95+
96+
foreach ($currenciesTo as $currencyTo) {
97+
if ($currencyFrom == $currencyTo) {
98+
$data[$currencyFrom][$currencyTo] = $this->_numberFormat(1);
99+
} else {
100+
if (empty($response[$currencyFrom . $currencyTo])) {
101+
$this->_messages[] = __('We can\'t retrieve a rate from %1 for %2.', $url, $currencyTo);
102+
$data[$currencyFrom][$currencyTo] = null;
103+
} else {
104+
$data[$currencyFrom][$currencyTo] = $this->_numberFormat(
105+
(double)$response[$currencyFrom . $currencyTo]
106+
);
107+
}
108+
}
109+
}
110+
return $data;
111+
}
112+
113+
/**
114+
* Get Fixer.io service response
115+
*
116+
* @param string $url
117+
* @param int $retry
118+
* @return array
119+
*/
120+
private function getServiceResponse($url, $retry = 0)
121+
{
122+
/** @var \Magento\Framework\HTTP\ZendClient $httpClient */
123+
$httpClient = $this->httpClientFactory->create();
124+
$response = [];
125+
try {
126+
$jsonResponse = $httpClient->setUri(
127+
$url
128+
)->setConfig(
129+
[
130+
'timeout' => $this->scopeConfig->getValue(
131+
$this->timeoutConfigPath,
132+
\Magento\Store\Model\ScopeInterface::SCOPE_STORE
133+
),
134+
]
135+
)->request(
136+
'GET'
137+
)->getBody();
138+
139+
$jsonResponse = json_decode($jsonResponse, true);
140+
if (!empty($jsonResponse['query']['results']['rate'])) {
141+
$response = array_column($jsonResponse['query']['results']['rate'], 'Rate', 'id');
142+
}
143+
} catch (\Exception $e) {
144+
if ($retry == 0) {
145+
$response = $this->getServiceResponse($url, 1);
146+
}
147+
}
148+
return $response;
149+
}
150+
151+
/**
152+
* {@inheritdoc}
153+
*/
154+
protected function _convert($currencyFrom, $currencyTo)
155+
{
156+
}
157+
158+
/**
159+
* Build url for Currency Service
160+
*
161+
* @param string $currencyFrom
162+
* @param string[] $currenciesTo
163+
* @return string
164+
*/
165+
private function buildUrl($currencyFrom, $currenciesTo)
166+
{
167+
$query = urlencode('select ') . '*' . urlencode(' from yahoo.finance.xchange where pair in (');
168+
$query .=
169+
urlencode(
170+
implode(
171+
',',
172+
array_map(
173+
function ($currencyTo) use ($currencyFrom) {
174+
return '"' . $currencyFrom . $currencyTo . '"';
175+
},
176+
$currenciesTo
177+
)
178+
)
179+
);
180+
$query .= ')';
181+
return str_replace('{{YQL_STRING}}', $query, $this->currencyConverterUrl);
182+
}
183+
}
Lines changed: 94 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,94 @@
1+
<?php
2+
/**
3+
* Copyright © 2016 Magento. All rights reserved.
4+
* See COPYING.txt for license details.
5+
*/
6+
7+
namespace Magento\Directory\Test\Unit\Model\Currency\Import;
8+
9+
class YahooFinanceTest extends \PHPUnit_Framework_TestCase
10+
{
11+
/**
12+
* @var \Magento\Directory\Model\Currency\Import\YahooFinance
13+
*/
14+
private $model;
15+
16+
/**
17+
* @var \Magento\Directory\Model\CurrencyFactory|\PHPUnit_Framework_MockObject_MockObject
18+
*/
19+
private $currencyFactoryMock;
20+
21+
/**
22+
* @var \Magento\Framework\HTTP\ZendClientFactory|\PHPUnit_Framework_MockObject_MockObject
23+
*/
24+
private $httpClientFactoryMock;
25+
26+
protected function setUp()
27+
{
28+
$objectManagerHelper = new \Magento\Framework\TestFramework\Unit\Helper\ObjectManager($this);
29+
30+
$this->currencyFactoryMock = $this->getMockBuilder('Magento\Directory\Model\CurrencyFactory')
31+
->disableOriginalConstructor()
32+
->setMethods(['create'])
33+
->getMock();
34+
$this->httpClientFactoryMock = $this->getMockBuilder('Magento\Framework\HTTP\ZendClientFactory')
35+
->disableOriginalConstructor()
36+
->setMethods(['create'])
37+
->getMock();
38+
$scopeMock = $this->getMockBuilder('Magento\Framework\App\Config\ScopeConfigInterface')
39+
->disableOriginalConstructor()
40+
->setMethods([])
41+
->getMock();
42+
43+
$this->model = $objectManagerHelper->getObject(
44+
'Magento\Directory\Model\Currency\Import\YahooFinance',
45+
[
46+
'currencyFactory' => $this->currencyFactoryMock,
47+
'scopeConfig' => $scopeMock,
48+
'httpClientFactory' => $this->httpClientFactoryMock
49+
]
50+
);
51+
}
52+
53+
public function testFetchRates()
54+
{
55+
$currencyFromList = ['USD'];
56+
$currencyToList = ['EUR', 'UAH'];
57+
$responseBody = '{"query":{"count":7,"created":"2016-04-05T16:46:55Z","lang":"en-US","results":{"rate":'
58+
. '[{"id":"USDEUR","Name":"USD/EUR","Rate":"0.9022","Date":"4/5/2016"}]}}}';
59+
$expectedCurrencyRateList = ['USD' => ['EUR' => 0.9022, 'UAH' => null]];
60+
$message = "We can't retrieve a rate from http://query.yahooapis.com/v1/public/yql?format=json"
61+
. "&q=select+*+from+yahoo.finance.xchange+where+pair+in+%28%22USDEUR%22%2C%22USDUAH%22)"
62+
. "&env=store://datatables.org/alltableswithkeys for UAH.";
63+
64+
/** @var \Magento\Directory\Model\Currency|\PHPUnit_Framework_MockObject_MockObject $currencyMock */
65+
$currencyMock = $this->getMockBuilder('Magento\Directory\Model\Currency')
66+
->disableOriginalConstructor()
67+
->setMethods([])
68+
->getMock();
69+
/** @var \Magento\Framework\HTTP\ZendClient|\PHPUnit_Framework_MockObject_MockObject $currencyMock */
70+
$httpClientMock = $this->getMockBuilder('Magento\Framework\HTTP\ZendClient')
71+
->disableOriginalConstructor()
72+
->setMethods([])
73+
->getMock();
74+
/** @var \Zend_Http_Response|\PHPUnit_Framework_MockObject_MockObject $currencyMock */
75+
$httpResponseMock = $this->getMockBuilder('Zend_Http_Response')
76+
->disableOriginalConstructor()
77+
->setMethods([])
78+
->getMock();
79+
$this->currencyFactoryMock->expects($this->any())->method('create')->willReturn($currencyMock);
80+
$currencyMock->expects($this->once())->method('getConfigBaseCurrencies')->willReturn($currencyFromList);
81+
$currencyMock->expects($this->once())->method('getConfigAllowCurrencies')->willReturn($currencyToList);
82+
$this->httpClientFactoryMock->expects($this->any())->method('create')->willReturn($httpClientMock);
83+
$httpClientMock->expects($this->atLeastOnce())->method('setUri')->willReturnSelf();
84+
$httpClientMock->expects($this->atLeastOnce())->method('setConfig')->willReturnSelf();
85+
$httpClientMock->expects($this->atLeastOnce())->method('request')->willReturn($httpResponseMock);
86+
$httpResponseMock->expects($this->any())->method('getBody')->willReturn($responseBody);
87+
88+
$this->assertEquals($expectedCurrencyRateList, $this->model->fetchRates());
89+
$messages = $this->model->getMessages();
90+
$this->assertNotEmpty($messages);
91+
$this->assertTrue(is_array($messages));
92+
$this->assertEquals($message, (string)$messages[0]);
93+
}
94+
}

app/code/Magento/Directory/etc/adminhtml/system.xml

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,18 @@
3434
<can_be_empty>1</can_be_empty>
3535
</field>
3636
</group>
37+
<group id="yahoofinance" translate="label" sortOrder="33" showInDefault="1" showInWebsite="0" showInStore="0">
38+
<label>Yahoo Finance Exchange</label>
39+
<field id="timeout" translate="label" type="text" sortOrder="0" showInDefault="1" showInWebsite="0" showInStore="0">
40+
<label>Connection Timeout in Seconds</label>
41+
</field>
42+
</group>
43+
<group id="fixerio" translate="label" sortOrder="35" showInDefault="1" showInWebsite="0" showInStore="0">
44+
<label>Fixer.io</label>
45+
<field id="timeout" translate="label" type="text" sortOrder="0" showInDefault="1" showInWebsite="0" showInStore="0">
46+
<label>Connection Timeout in Seconds</label>
47+
</field>
48+
</group>
3749
<group id="webservicex" translate="label" sortOrder="40" showInDefault="1" showInWebsite="0" showInStore="0">
3850
<label>Webservicex</label>
3951
<field id="timeout" translate="label" type="text" sortOrder="0" showInDefault="1" showInWebsite="0" showInStore="0" canRestore="1">

app/code/Magento/Directory/etc/config.xml

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,12 @@
1818
<base>USD</base>
1919
<default>USD</default>
2020
</options>
21+
<yahoofinance>
22+
<timeout>100</timeout>
23+
</yahoofinance>
24+
<fixerio>
25+
<timeout>100</timeout>
26+
</fixerio>
2127
<webservicex>
2228
<timeout>100</timeout>
2329
</webservicex>

0 commit comments

Comments
 (0)