Skip to content

Commit

Permalink
Implement the tracking of Headless requests to the FrontEndType header
Browse files Browse the repository at this point in the history
  • Loading branch information
khushboo-singhvi committed Feb 26, 2025
1 parent f37a2d8 commit 129767b
Show file tree
Hide file tree
Showing 2 changed files with 66 additions and 7 deletions.
25 changes: 21 additions & 4 deletions Helper/Data.php
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,7 @@
use Magento\Store\Model\StoreManagerInterface;
use Magento\Tax\Model\Calculation;
use Magento\Tax\Model\Config;
use Magento\Framework\App\Request\Http;

/**
* @SuppressWarnings(PHPMD.LongVariable)
Expand Down Expand Up @@ -199,6 +200,11 @@ class Data extends AbstractHelper
*/
private $backendHelper;

/**
* @var Http
*/
private Http $request;

public function __construct(
Context $context,
EncryptorInterface $encryptor,
Expand All @@ -221,7 +227,8 @@ public function __construct(
Locale $localeHelper,
OrderManagementInterface $orderManagement,
HistoryFactory $orderStatusHistoryFactory,
ConfigHelper $configHelper
ConfigHelper $configHelper,
HTTP $request
) {
parent::__construct($context);
$this->_encryptor = $encryptor;
Expand All @@ -245,6 +252,7 @@ public function __construct(
$this->orderManagement = $orderManagement;
$this->orderStatusHistoryFactory = $orderStatusHistoryFactory;
$this->configHelper = $configHelper;
$this->request = $request;
}

/**
Expand Down Expand Up @@ -1184,9 +1192,18 @@ public function buildRequestHeaders($payment = null)
HeaderDataBuilderInterface::MERCHANT_APPLICATION_VERSION => $this->getModuleVersion()
];

if(isset($payment) && !is_null($payment->getAdditionalInformation(HeaderDataBuilderInterface::ADDITIONAL_DATA_FRONTEND_TYPE_KEY))) {
$headers[HeaderDataBuilderInterface::EXTERNAL_PLATFORM_FRONTEND_TYPE] =
$payment->getAdditionalInformation(HeaderDataBuilderInterface::ADDITIONAL_DATA_FRONTEND_TYPE_KEY);
if (isset($payment)) {
$frontendType = $payment->getAdditionalInformation(HeaderDataBuilderInterface::ADDITIONAL_DATA_FRONTEND_TYPE_KEY);
if (is_null($frontendType)) {
// Check the request URI
$requestUri = $this->request->getPathInfo();
if (str_contains($requestUri, '/graphql')) {
$frontendType = 'headless-graphql';
} else {
$frontendType = 'headless-rest';
}
}
$headers[HeaderDataBuilderInterface::EXTERNAL_PLATFORM_FRONTEND_TYPE] = $frontendType;
}

return $headers;
Expand Down
48 changes: 45 additions & 3 deletions Test/Unit/Helper/DataTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@
use Magento\Framework\UrlInterface;
use Magento\Framework\View\Asset\Repository;
use Magento\Framework\View\Asset\Source;
use Magento\Payment\Model\InfoInterface;
use Magento\Quote\Model\Quote\Address\RateRequest;
use Magento\Sales\Api\OrderManagementInterface;
use Magento\Sales\Model\Order\Payment;
Expand All @@ -57,6 +58,7 @@
use Magento\Sales\Model\Order;
use ReflectionClass;
use Magento\Framework\TestFramework\Unit\Helper\ObjectManager;
use Magento\Framework\App\Request\Http;

class DataTest extends AbstractAdyenTestCase
{
Expand Down Expand Up @@ -86,6 +88,7 @@ class DataTest extends AbstractAdyenTestCase
private $localeHelper;
private $orderManagement;
private $orderStatusHistoryFactory;
private $request;

public function setUp(): void
{
Expand Down Expand Up @@ -148,6 +151,13 @@ public function setUp(): void
->with('adyen_credit_cards')
->willReturn($this->ccTypesAltData);

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

$this->paymentMock = $this->getMockBuilder(InfoInterface::class)
->disableOriginalConstructor()
->setMethods(null)
->getMock();

// Partial mock builder is being used for mocking the methods in the class being tested.
$this->dataHelper = $this->getMockBuilder(Data::class)
->setMethods(['getModuleVersion'])
Expand All @@ -173,7 +183,8 @@ public function setUp(): void
$this->localeHelper,
$this->orderManagement,
$this->orderStatusHistoryFactory,
$this->configHelper
$this->configHelper,
$this->request
])
->getMock();

Expand Down Expand Up @@ -1055,6 +1066,37 @@ public function testBuildRequestHeaders()
$this->assertEquals($expectedHeaders, $headers);
}

public function testBuildRequestHeadersWithFrontendTypeSet(): void
{
// Mock dependencies as needed
$payment = $this->createMock(Payment::class);

// Set up expectations for the getAdditionalInformation method
$payment->method('getAdditionalInformation')
->with(HeaderDataBuilderInterface::ADDITIONAL_DATA_FRONTEND_TYPE_KEY)
->willReturn(null);

$headers = $this->dataHelper->buildRequestHeaders($this->paymentMock);

$this->assertArrayHasKey(HeaderDataBuilderInterface::EXTERNAL_PLATFORM_FRONTEND_TYPE, $headers);
$this->assertEquals('headless-rest', $headers[HeaderDataBuilderInterface::EXTERNAL_PLATFORM_FRONTEND_TYPE]);
}

public function testBuildRequestHeadersWithNullFrontendTypeGraphQL(): void
{
$this->paymentMock->method('getAdditionalInformation')
->with(HeaderDataBuilderInterface::ADDITIONAL_DATA_FRONTEND_TYPE_KEY)
->willReturn(null);

$this->request->method('getPathInfo')
->willReturn('/graphql');

$headers = $this->dataHelper->buildRequestHeaders($this->paymentMock);

$this->assertArrayHasKey(HeaderDataBuilderInterface::EXTERNAL_PLATFORM_FRONTEND_TYPE, $headers);
$this->assertEquals('headless-graphql', $headers[HeaderDataBuilderInterface::EXTERNAL_PLATFORM_FRONTEND_TYPE]);
}

public function testBuildApplicationInfo()
{
$expectedApplicationInfo = new ApplicationInfo();
Expand Down Expand Up @@ -1093,14 +1135,14 @@ public function testBuildRequestHeadersWithNonNullFrontendType()
// Set up expectations for the getAdditionalInformation method
$payment->method('getAdditionalInformation')
->with(HeaderDataBuilderInterface::ADDITIONAL_DATA_FRONTEND_TYPE_KEY)
->willReturn('some_frontend_type');
->willReturn('default');

// Call the method under test
$result = $this->dataHelper->buildRequestHeaders($payment);

// Assert that the 'frontend-type' header is correctly set
$this->assertArrayHasKey(HeaderDataBuilderInterface::EXTERNAL_PLATFORM_FRONTEND_TYPE, $result);
$this->assertEquals('some_frontend_type', $result[HeaderDataBuilderInterface::EXTERNAL_PLATFORM_FRONTEND_TYPE]);
$this->assertEquals('default', $result[HeaderDataBuilderInterface::EXTERNAL_PLATFORM_FRONTEND_TYPE]);

// Assert other headers as needed
}
Expand Down

0 comments on commit 129767b

Please sign in to comment.