Skip to content

Commit c37f621

Browse files
authored
ENGCOM-8326: Fix #30296 - Wrong ip value in sendfriend_log table #30355
2 parents 442caf2 + e316fd9 commit c37f621

File tree

4 files changed

+44
-37
lines changed

4 files changed

+44
-37
lines changed

app/code/Magento/SendFriend/Model/ResourceModel/SendFriend.php

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -6,10 +6,6 @@
66
namespace Magento\SendFriend\Model\ResourceModel;
77

88
/**
9-
* SendFriend Log Resource Model
10-
*
11-
* @author Magento Core Team <[email protected]>
12-
*
139
* @api
1410
* @since 100.0.2
1511
*/
@@ -32,6 +28,7 @@ protected function _construct()
3228
* @param int $ip
3329
* @param int $startTime
3430
* @param int $websiteId
31+
*
3532
* @return int
3633
* @SuppressWarnings(PHPMD.UnusedFormalParameter)
3734
*/
@@ -46,7 +43,7 @@ public function getSendCount($object, $ip, $startTime, $websiteId = null)
4643
AND time>=:time
4744
AND website_id=:website_id'
4845
);
49-
$bind = ['ip' => ip2long($ip), 'time' => $startTime, 'website_id' => (int)$websiteId];
46+
$bind = ['ip' => $ip, 'time' => $startTime, 'website_id' => (int)$websiteId];
5047

5148
$row = $connection->fetchRow($select, $bind);
5249
return $row['count'];
@@ -58,21 +55,24 @@ public function getSendCount($object, $ip, $startTime, $websiteId = null)
5855
* @param int $ip
5956
* @param int $startTime
6057
* @param int $websiteId
58+
*
6159
* @return $this
6260
*/
6361
public function addSendItem($ip, $startTime, $websiteId)
6462
{
6563
$this->getConnection()->insert(
6664
$this->getMainTable(),
67-
['ip' => ip2long($ip), 'time' => $startTime, 'website_id' => $websiteId]
65+
['ip' => $ip, 'time' => $startTime, 'website_id' => $websiteId]
6866
);
67+
6968
return $this;
7069
}
7170

7271
/**
7372
* Delete Old logs
7473
*
7574
* @param int $time
75+
*
7676
* @return $this
7777
*/
7878
public function deleteLogsBefore($time)

dev/tests/integration/testsuite/Magento/SendFriend/Model/SendFriendTest.php

Lines changed: 22 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@
1313
use Magento\SendFriend\Helper\Data as SendFriendHelper;
1414
use Magento\TestFramework\Helper\Bootstrap;
1515
use PHPUnit\Framework\TestCase;
16-
use Zend\Stdlib\Parameters;
16+
use Laminas\Stdlib\Parameters;
1717

1818
/**
1919
* Class checks send friend model behavior
@@ -28,6 +28,9 @@ class SendFriendTest extends TestCase
2828
/** @var SendFriend */
2929
private $sendFriend;
3030

31+
/** @var ResourceModel\SendFriend */
32+
private $sendFriendResource;
33+
3134
/** @var CookieManagerInterface */
3235
private $cookieManager;
3336

@@ -43,6 +46,7 @@ protected function setUp(): void
4346

4447
$this->objectManager = Bootstrap::getObjectManager();
4548
$this->sendFriend = $this->objectManager->get(SendFriendFactory::class)->create();
49+
$this->sendFriendResource = $this->objectManager->get(ResourceModel\SendFriend::class);
4650
$this->cookieManager = $this->objectManager->get(CookieManagerInterface::class);
4751
$this->request = $this->objectManager->get(RequestInterface::class);
4852
}
@@ -55,6 +59,7 @@ protected function setUp(): void
5559
* @param array $sender
5660
* @param array $recipients
5761
* @param string|bool $expectedResult
62+
*
5863
* @return void
5964
*/
6065
public function testValidate(array $sender, array $recipients, $expectedResult): void
@@ -185,22 +190,34 @@ public function testisExceedLimitByCookies(): void
185190
* @magentoDataFixture Magento/SendFriend/_files/sendfriend_log_record_half_hour_before.php
186191
*
187192
* @magentoDbIsolation disabled
193+
*
188194
* @return void
189195
*/
190196
public function testisExceedLimitByIp(): void
191197
{
192-
$this->markTestSkipped('Blocked by MC-31968');
198+
$remoteAddr = '127.0.0.1';
193199
$parameters = $this->objectManager->create(Parameters::class);
194-
$parameters->set('REMOTE_ADDR', '127.0.0.1');
200+
$parameters->set('REMOTE_ADDR', $remoteAddr);
195201
$this->request->setServer($parameters);
196202
$this->assertTrue($this->sendFriend->isExceedLimit());
203+
// Verify that ip is saved correctly as integer value
204+
$this->assertEquals(
205+
1,
206+
(int)$this->sendFriendResource->getSendCount(
207+
null,
208+
ip2long($remoteAddr),
209+
time() - (60 * 60 * 24 * 365),
210+
1
211+
)
212+
);
197213
}
198214

199215
/**
200-
* Check result
216+
* Check test result
201217
*
202218
* @param array|bool $expectedResult
203219
* @param array|bool $result
220+
*
204221
* @return void
205222
*/
206223
private function checkResult($expectedResult, $result): void
@@ -217,6 +234,7 @@ private function checkResult($expectedResult, $result): void
217234
*
218235
* @param array $sender
219236
* @param array $recipients
237+
*
220238
* @return void
221239
*/
222240
private function prepareData(array $sender, array $recipients): void

lib/internal/Magento/Framework/HTTP/PhpEnvironment/RemoteAddress.php

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -120,7 +120,7 @@ function (string $ip) {
120120
public function getRemoteAddress(bool $ipToLong = false)
121121
{
122122
if ($this->remoteAddress !== null) {
123-
return $this->remoteAddress;
123+
return $ipToLong ? ip2long($this->remoteAddress) : $this->remoteAddress;
124124
}
125125

126126
$remoteAddress = $this->readAddress();
@@ -135,11 +135,11 @@ public function getRemoteAddress(bool $ipToLong = false)
135135
$this->remoteAddress = false;
136136

137137
return false;
138-
} else {
139-
$this->remoteAddress = $remoteAddress;
140-
141-
return $ipToLong ? ip2long($this->remoteAddress) : $this->remoteAddress;
142138
}
139+
140+
$this->remoteAddress = $remoteAddress;
141+
142+
return $ipToLong ? ip2long($this->remoteAddress) : $this->remoteAddress;
143143
}
144144

145145
/**

lib/internal/Magento/Framework/HTTP/Test/Unit/PhpEnvironment/RemoteAddressTest.php

Lines changed: 11 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -9,38 +9,28 @@
99

1010
use Magento\Framework\App\Request\Http as HttpRequest;
1111
use Magento\Framework\HTTP\PhpEnvironment\RemoteAddress;
12-
use Magento\Framework\TestFramework\Unit\Helper\ObjectManager;
1312
use PHPUnit\Framework\MockObject\MockObject;
1413
use PHPUnit\Framework\TestCase;
1514

1615
/**
17-
* Test for
18-
*
1916
* @see RemoteAddress
2017
*/
2118
class RemoteAddressTest extends TestCase
2219
{
2320
/**
2421
* @var MockObject|HttpRequest
2522
*/
26-
protected $_request;
27-
28-
/**
29-
* @var ObjectManager
30-
*/
31-
protected $_objectManager;
23+
private $requestMock;
3224

3325
/**
3426
* @inheritdoc
3527
*/
3628
protected function setUp(): void
3729
{
38-
$this->_request = $this->getMockBuilder(HttpRequest::class)
30+
$this->requestMock = $this->getMockBuilder(HttpRequest::class)
3931
->disableOriginalConstructor()
40-
->setMethods(['getServer'])
32+
->onlyMethods(['getServer'])
4133
->getMock();
42-
43-
$this->_objectManager = new ObjectManager($this);
4434
}
4535

4636
/**
@@ -49,6 +39,7 @@ protected function setUp(): void
4939
* @param string|bool $expected
5040
* @param bool $ipToLong
5141
* @param string[]|null $trustedProxies
42+
*
5243
* @return void
5344
* @dataProvider getRemoteAddressProvider
5445
*/
@@ -59,18 +50,16 @@ public function testGetRemoteAddress(
5950
bool $ipToLong,
6051
array $trustedProxies = null
6152
): void {
62-
$remoteAddress = $this->_objectManager->getObject(
63-
RemoteAddress::class,
64-
[
65-
'httpRequest' => $this->_request,
66-
'alternativeHeaders' => $alternativeHeaders,
67-
'trustedProxies' => $trustedProxies,
68-
]
53+
$remoteAddress = new RemoteAddress(
54+
$this->requestMock,
55+
$alternativeHeaders,
56+
$trustedProxies
6957
);
70-
$this->_request->expects($this->any())
71-
->method('getServer')
58+
$this->requestMock->method('getServer')
7259
->willReturnMap($serverValueMap);
7360

61+
// Check twice to verify if internal variable is cached correctly
62+
$this->assertEquals($expected, $remoteAddress->getRemoteAddress($ipToLong));
7463
$this->assertEquals($expected, $remoteAddress->getRemoteAddress($ipToLong));
7564
}
7665

0 commit comments

Comments
 (0)