Skip to content

Commit 4ed3e21

Browse files
committed
ACP2E-615: Elasticsearch Query breaks when we configure "int" backend type attribute and make it searchable
- Fixed the issue and test coverage.
1 parent 53a3899 commit 4ed3e21

File tree

2 files changed

+65
-3
lines changed

2 files changed

+65
-3
lines changed

app/code/Magento/Elasticsearch/SearchAdapter/Query/ValueTransformer/IntegerTransformer.php

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -20,14 +20,16 @@ class IntegerTransformer implements ValueTransformerInterface
2020
public function transform(string $value): ?int
2121
{
2222
return (\is_numeric($value) &&
23-
$this->validateIntegerTypesWithInRange($value)) ? (int) $value : null;
23+
$this->validateIntegerTypesWithInRange((int) $value)) ? (int) $value : null;
2424
}
2525

2626
/**
27-
* @param $value
27+
* Validate integer value is within the range of 32 bytes as per elasticsearch.
28+
*
29+
* @param int $value
2830
* @return bool
2931
*/
30-
public function validateIntegerTypesWithInRange($value): bool
32+
public function validateIntegerTypesWithInRange(int $value): bool
3133
{
3234
return (abs($value) & 0x7FFFFFFF) === abs($value);
3335
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
<?php
2+
/**
3+
* Copyright © Magento, Inc. All rights reserved.
4+
* See COPYING.txt for license details.
5+
*/
6+
declare(strict_types=1);
7+
8+
namespace Magento\Elasticsearch\Test\Unit\SearchAdapter\Query\ValueTransformer;
9+
10+
use Magento\Elasticsearch\SearchAdapter\Query\ValueTransformer\IntegerTransformer;
11+
use Magento\Framework\TestFramework\Unit\Helper\ObjectManager as ObjectManagerHelper;
12+
use PHPUnit\Framework\TestCase;
13+
14+
/**
15+
* Test value transformer
16+
*/
17+
class IntegerTransformerTest extends TestCase
18+
{
19+
/**
20+
* @var IntegerTransformer
21+
*/
22+
protected $model;
23+
24+
/**
25+
* Setup method
26+
* @return void
27+
*/
28+
public function setUp(): void
29+
{
30+
$objectManagerHelper = new ObjectManagerHelper($this);
31+
$this->model = $objectManagerHelper->getObject(
32+
IntegerTransformer::class
33+
);
34+
}
35+
36+
/**
37+
* Test integer transform value
38+
* @param string $value
39+
* @param int|null $expected
40+
* @return void
41+
* @dataProvider valuesDataProvider
42+
*/
43+
public function testIntegerTransform(string $value, ?int $expected): void
44+
{
45+
$this->assertEquals($expected, $this->model->transform($value));
46+
}
47+
48+
/**
49+
* Values data provider
50+
*
51+
* @return array
52+
*/
53+
public function valuesDataProvider(): array
54+
{
55+
return [
56+
['12345', 12345],
57+
['3310042623',null]
58+
];
59+
}
60+
}

0 commit comments

Comments
 (0)