Skip to content
Snippets Groups Projects
Commit 17cd8bd9 authored by Christel Loftus's avatar Christel Loftus
Browse files

remove old tracking classes and functions

parent 436e55f7
No related branches found
No related tags found
2 merge requests!201.0.41,!15Tracking page
......@@ -527,141 +527,6 @@ class BobGo extends AbstractCarrierOnline implements \Magento\Shipping\Model\Car
}
}
/**
* Get tracking
*
* @param string|string[] $trackings
* @return \Magento\Shipping\Model\Tracking\Result|null
*/
public function getTracking($trackings)
{
$this->setTrackingRequest(); // Ensure this method is correctly defined
if (!is_array($trackings)) {
$trackings = [$trackings];
}
foreach ($trackings as $tracking) {
$this->_getXMLTracking([$tracking]); // Ensure _getXMLTracking processes tracking correctly
}
return $this->_result; // Ensure _result is a \Magento\Shipping\Model\Tracking\Result
}
/**
* Set tracking request
*
* @return void
*/
protected function setTrackingRequest()
{
$r = new \Magento\Framework\DataObject();
$account = $this->getConfigData('account');
$r->setData('account', $account); // Using setData with the key 'account'
$this->_rawTrackingRequest = $r;
}
/**
* Get tracking request
*
* @return \Magento\Framework\DataObject|null
*/
protected function getTrackingRequest(): ?\Magento\Framework\DataObject
{
return $this->_rawTrackingRequest;
}
/**
* Send request for tracking
*
* @param string[] $tracking
* @return void
*/
protected function _getXMLTracking($tracking)
{
$this->_parseTrackingResponse($tracking);
}
/**
* Parse tracking response
*
* @param string|array<int,string> $trackingValue
* @return void
*/
protected function _parseTrackingResponse($trackingValue)
{
$result = $this->getResult();
$carrierTitle = $this->getConfigData('title');
$counter = 0;
if (!is_array($trackingValue)) {
$trackingValue = [$trackingValue];
}
foreach ($trackingValue as $trackingReference) {
$tracking = $this->_trackStatusFactory->create();
$tracking->setCarrier(self::CODE);
$tracking->setCarrierTitle($carrierTitle);
// Adjust as needed based on the environment
$tracking->setUrl(UData::TRACKING . $trackingReference);
$tracking->setTracking($trackingReference);
$tracking->addData($this->processTrackingDetails($trackingReference));
if ($result) {
$result->append($tracking);
$counter++;
}
}
// Tracking Details Not Available
if ($counter === 0) {
$this->appendTrackingError(
$trackingValue[0] ?? '',
(string)__('For some reason we can\'t retrieve tracking info right now.')
);
}
}
/**
* Get tracking response
*
* @return string
*/
public function getResponse(): string
{
$statuses = '';
// If $_result is of type \Magento\Shipping\Model\Tracking\Result, handle it
if ($this->_result instanceof \Magento\Shipping\Model\Tracking\Result) {
if ($trackings = $this->_result->getAllTrackings()) {
foreach ($trackings as $tracking) {
if ($data = $tracking->getAllData()) {
if (!empty($data['status'])) {
$statuses .= __($data['status']) . "\n<br/>";
} else {
$statuses .= __('Empty response') . "\n<br/>";
}
}
}
}
}
// // Handle \Magento\Shipping\Model\Rate\Result if needed
// if ($this->_result instanceof \Magento\Shipping\Model\Rate\Result) {
// // Implement the logic for Rate\Result if applicable
// }
if (trim($statuses) === '') {
$statuses = (string)__('Empty response');
}
return $statuses;
}
/**
* Get allowed shipping methods
*
......@@ -773,55 +638,6 @@ class BobGo extends AbstractCarrierOnline implements \Magento\Shipping\Model\Car
return $data;
}
/**
* Parse track details response from Bob Go.
*
* @param string $trackInfo
* @return array<string, array<int, array<string, string>>>
* @SuppressWarnings(PHPMD.CyclomaticComplexity)
* @SuppressWarnings(PHPMD.NPathComplexity)
*/
private function processTrackingDetails(string $trackInfo): array
{
$result = [
'shippeddate' => [], // Initializing as an array of arrays
'deliverydate' => [], // Initializing as an array of arrays
'deliverytime' => [], // Initializing as an array of arrays
'deliverylocation' => [], // Initializing as an array of arrays
'weight' => [], // Initializing as an array of arrays
'progressdetail' => [], // This will be populated with an array of arrays
];
$result = $this->_requestTracking($trackInfo, $result);
return $result;
}
/**
* Append error message to rate result instance.
*
* @param string $trackingValue
* @param string $errorMessage
* @return void
*/
private function appendTrackingError(string $trackingValue, string $errorMessage): void
{
$error = $this->_trackErrorFactory->create();
$error->setCarrier(self::CODE);
$error->setCarrierTitle($this->getConfigData('title'));
$error->setTracking($trackingValue);
$error->setErrorMessage($errorMessage);
$result = $this->getResult();
if ($result !== null) {
$result->append($error);
} else {
// Handle the case where $result is null, such as logging an error
$this->_logger->error('Failed to append tracking error: Result object is null.');
}
}
/**
* Format a date to 'd M Y'.
*
......@@ -883,26 +699,6 @@ class BobGo extends AbstractCarrierOnline implements \Magento\Shipping\Model\Car
}
}
/**
* Perform API Request for Shipment Tracking to Bob Go API and return response.
*
* @param string $trackInfo The tracking information or tracking ID.
* @param array<string,array<int,array<string,string>>> $result The result array to be
* populated with tracking details.
* @return array<string, array<int, array<string, string>>> The updated result array with tracking details.
*/
private function _requestTracking(string $trackInfo, array $result): array
{
$response = $this->trackBobGoShipment($trackInfo);
// Validate that the response is an array and contains at least one element
if (is_array($response) && isset($response[0]) && is_array($response[0])) {
$result = $this->prepareActivity($response[0], $result);
}
return $result;
}
/**
* Format rates from Bob Go API response and append to rate result instance of carrier.
*
......@@ -976,35 +772,6 @@ class BobGo extends AbstractCarrierOnline implements \Magento\Shipping\Model\Car
}
}
/**
* Prepare received checkpoints and activity from Bob Go Shipment Tracking API.
*
* @param array<string,mixed> $response The API response containing tracking checkpoints.
* @param array<string,array<int,array<string,string>>> $result The result array to be
* populated with activity details.
* @return array<string, array<int, array<string, string>>> The updated result array with activity details.
*/
private function prepareActivity(array $response, array $result): array
{
if (isset($response['checkpoints']) && is_array($response['checkpoints'])) {
foreach ($response['checkpoints'] as $checkpoint) {
if (is_array($checkpoint) &&
isset($checkpoint['status'], $checkpoint['time']) &&
is_string($checkpoint['status']) &&
is_string($checkpoint['time'])
) {
$result['progressdetail'][] = [
'activity' => $checkpoint['status'],
'deliverydate' => $this->formatDate($checkpoint['time']),
'deliverytime' => $this->formatTime($checkpoint['time']),
];
}
}
}
return $result;
}
/**
* Get Working Days between time of checkout and delivery date (min and max).
*
......@@ -1037,21 +804,6 @@ class BobGo extends AbstractCarrierOnline implements \Magento\Shipping\Model\Car
return $no_days - $weekends;
}
/**
* Curl request to Bob Go Shipment Tracking API.
*
* @param string $trackInfo The tracking information or tracking ID.
* @return mixed The decoded API response.
*/
private function trackBobGoShipment(string $trackInfo): mixed
{
$this->curl->get(UData::TRACKING . $trackInfo);
$response = $this->curl->getBody();
return json_decode($response, true);
}
/**
* Build the payload for Bob Go API request and return the response.
*
......@@ -1199,27 +951,6 @@ class BobGo extends AbstractCarrierOnline implements \Magento\Shipping\Model\Car
return $itemsArray;
}
/**
* Checks if the required data fields are present in the request.
*
* @param \Magento\Framework\DataObject $request The data object containing the request information.
* @return bool True if all required fields are present, otherwise false.
*/
public function hasRequiredData(\Magento\Framework\DataObject $request): bool
{
$requiredFields = [
'dest_country_id',
'dest_region_id',
];
foreach ($requiredFields as $field) {
if (!$request->getData($field)) {
return false;
}
}
return true;
}
/**
* Trigger a test for rates.
*
......
<?php
namespace BobGroup\BobGo\Plugin\Block\DataProviders\Tracking;
use BobGroup\BobGo\Model\Carrier;
use Magento\Shipping\Model\Tracking\Result\Status;
use Magento\Shipping\Block\DataProviders\Tracking\DeliveryDateTitle as Subject;
/**
* Plugin to change delivery date title with bobgo customized value
*/
class ChangeTitle
{
/**
* Title modification in case if bobgo used as carrier
*
* @param Subject $subject
* @param \Magento\Framework\Phrase|string $result
* @param Status $trackingStatus
* @return \Magento\Framework\Phrase|string
* @SuppressWarnings(PHPMD.UnusedFormalParameter)
*/
public function afterGetTitle(Subject $subject, $result, Status $trackingStatus)
{
if ($trackingStatus->getCarrier() === \BobGroup\BobGo\Model\Carrier\BobGo::CODE) {
$result = __('Expected delivery:');
}
return $result;
}
}
<?php
namespace BobGroup\BobGo\Plugin\Block\Tracking;
use Magento\Shipping\Block\Tracking\Popup;
use Magento\Shipping\Model\Tracking\Result\Status;
/*
* Plugin to update delivery date value in case if Bob Go is a carrier used
*/
class PopupDeliveryDate
{
/**
* Bob Go carrier code
*/
private const BOB_GO_CARRIER_CODE = 'bobgo_carrier_code'; // Replace with your actual carrier code
/**
* Show only date for expected delivery in case if Bob Go is a carrier
*
* @param Popup $subject
* @param string $result
* @param string $date
* @param string $time
* @return string
* @SuppressWarnings(PHPMD.UnusedFormalParameter)
*/
public function afterFormatDeliveryDateTime(Popup $subject, $result, $date, $time)
{
if ($this->getCarrier($subject) === self::BOB_GO_CARRIER_CODE) {
$result = $subject->formatDeliveryDate($date);
}
return $result;
}
/**
* Retrieve carrier name from tracking info
*
* @param Popup $subject
* @return string
*/
private function getCarrier(Popup $subject): string
{
foreach ($subject->getTrackingInfo() as $trackingData) {
foreach ($trackingData as $trackingInfo) {
if ($trackingInfo instanceof Status) {
return $trackingInfo->getCarrier() ?? '';
}
}
}
return '';
}
}
<?php
namespace BobGroup\BobGo\Test\Unit\Plugin\Block\DataProviders\Tracking;
use BobGroup\BobGo\Plugin\Block\DataProviders\Tracking\ChangeTitle;
use BobGroup\BobGo\Model\Carrier\BobGo;
use Magento\Shipping\Model\Tracking\Result\Status;
use Magento\Shipping\Block\DataProviders\Tracking\DeliveryDateTitle as Subject;
use PHPUnit\Framework\TestCase;
class ChangeTitleTest extends TestCase
{
/**
* @var ChangeTitle
*/
private $plugin;
protected function setUp(): void
{
// Instantiate the ChangeTitle plugin
$this->plugin = new ChangeTitle();
}
public function testAfterGetTitleWithBobGoCarrier(): void
{
// Create a custom Status object with BobGo carrier
$status = $this->getMockBuilder(Status::class)
->setMethods(['getCarrier'])
->getMock();
$status->method('getCarrier')->willReturn(BobGo::CODE);
// Mock the Subject class
$subjectMock = $this->createMock(Subject::class);
// Call the plugin method afterGetTitle
$result = $this->plugin->afterGetTitle($subjectMock, 'Original Title', $status);
// Assert that the title was changed for BobGo carrier
$this->assertEquals('Expected delivery:', $result);
}
public function testAfterGetTitleWithOtherCarrier(): void
{
// Create a custom Status object with a different carrier
$status = $this->getMockBuilder(Status::class)
->setMethods(['getCarrier'])
->getMock();
$status->method('getCarrier')->willReturn('other_carrier_code');
// Mock the Subject class
$subjectMock = $this->createMock(Subject::class);
// Call the plugin method afterGetTitle
$originalTitle = 'Original Title';
$result = $this->plugin->afterGetTitle($subjectMock, $originalTitle, $status);
// Assert that the title was not changed for other carriers
$this->assertEquals($originalTitle, $result);
}
}
<?php
namespace BobGroup\BobGo\Test\Unit\Plugin\Block\Tracking;
use BobGroup\BobGo\Plugin\Block\Tracking\PopupDeliveryDate;
use Magento\Shipping\Block\Tracking\Popup;
use Magento\Shipping\Model\Tracking\Result\Status;
use PHPUnit\Framework\TestCase;
class PopupDeliveryDateTest extends TestCase
{
/**
* @var PopupDeliveryDate
*/
private $plugin;
protected function setUp(): void
{
// Instantiate the PopupDeliveryDate plugin
$this->plugin = new PopupDeliveryDate();
}
public function testAfterFormatDeliveryDateTimeWithBobGoCarrier(): void
{
// Create an instance of the Status class
$status = new Status();
$status->setCarrier('bobgo_carrier_code');
// Mock the Popup class
$popupMock = $this->createMock(Popup::class);
$popupMock->method('getTrackingInfo')->willReturn([
['tracking_info' => $status],
]);
// Mock the formatDeliveryDate method
$popupMock->method('formatDeliveryDate')
->with('2024-08-19')
->willReturn('Aug 19, 2024');
// Call the plugin method afterFormatDeliveryDateTime
$result = $this->plugin->afterFormatDeliveryDateTime(
$popupMock,
'Aug 19, 2024 10:00 AM',
'2024-08-19',
'10:00 AM'
);
// Assert that the time was stripped for BobGo carrier
$this->assertEquals('Aug 19, 2024', $result);
}
public function testAfterFormatDeliveryDateTimeWithOtherCarrier(): void
{
// Create an instance of the Status class
$status = new Status();
$status->setCarrier('other_carrier_code');
// Mock the Popup class
$popupMock = $this->createMock(Popup::class);
$popupMock->method('getTrackingInfo')->willReturn([
['tracking_info' => $status],
]);
// Call the plugin method afterFormatDeliveryDateTime
$result = $this->plugin->afterFormatDeliveryDateTime(
$popupMock,
'Aug 19, 2024 10:00 AM',
'2024-08-19',
'10:00 AM'
);
// Assert that the time remains unchanged for other carriers
$this->assertEquals('Aug 19, 2024 10:00 AM', $result);
}
public function testGetCarrierWithNoTrackingInfo(): void
{
// Mock the Popup class with no tracking info
$popupMock = $this->createMock(Popup::class);
$popupMock->method('getTrackingInfo')->willReturn([]);
// Call the getCarrier method directly
$reflection = new \ReflectionClass($this->plugin);
$method = $reflection->getMethod('getCarrier');
$method->setAccessible(true);
$result = $method->invokeArgs($this->plugin, [$popupMock]);
// Assert that an empty string is returned when no tracking info is available
$this->assertEquals('', $result);
}
}
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment