Skip to content

Commit

Permalink
MOL-1196: check tracking code lenght
Browse files Browse the repository at this point in the history
  • Loading branch information
Vitalij Mik committed Dec 1, 2023
1 parent 8f63450 commit 2dbf5af
Show file tree
Hide file tree
Showing 3 changed files with 99 additions and 7 deletions.
20 changes: 19 additions & 1 deletion src/Service/TrackingInfoStructFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,13 +8,23 @@

class TrackingInfoStructFactory
{
/**
* Mollie throws an error with length >= 100
*/
const MAX_TRACKING_CODE_LENGTH = 99;

public function createFromDelivery(OrderDeliveryEntity $orderDeliveryEntity):?ShipmentTrackingInfoStruct
{
$trackingCodes = $orderDeliveryEntity->getTrackingCodes();
$shippingMethod = $orderDeliveryEntity->getShippingMethod();
if ($shippingMethod === null) {
return null;
}
/**
* mollie accepts only one tracking struct with code per shippment
*
* https://docs.mollie.com/reference/v2/shipments-api/create-shipment
*/
if (count($trackingCodes) !== 1) {
return null;
}
Expand All @@ -36,12 +46,20 @@ public function create(string $trackingCarrier, string $trackingCode, string $tr
throw new \InvalidArgumentException('Missing Argument for Tracking Code!');
}

$trackingUrl = trim($trackingUrl . $trackingCode);
if (strpos($trackingUrl, '%s') === false) {
throw new \InvalidArgumentException('Missing %s as code placeholder in Tracking URL');
}

$trackingUrl = trim(sprintf($trackingUrl, $trackingCode));

if (filter_var($trackingUrl, FILTER_VALIDATE_URL) === false) {
$trackingUrl = '';
}

if (mb_strlen($trackingCode) > self::MAX_TRACKING_CODE_LENGTH) {
$trackingUrl = '';
}

/**
* following characters are not allowed in the tracking URL {,},<,>,#
*/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -123,6 +123,6 @@ public function testTrackingInfoStructWithCorrectData()
$this->assertInstanceOf(ShipmentTrackingInfoStruct::class, $trackingInfoStruct);
});

$this->shipmentFacade->shipOrder($this->order, 'Mollie', '123456789', '', $this->context);
$this->shipmentFacade->shipOrder($this->order, 'Mollie', '123456789', 'https://foo.bar?code=%s', $this->context);
}
}
84 changes: 79 additions & 5 deletions tests/PHPUnit/Service/TrackingInfoStructFactoryTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@

use Kiener\MolliePayments\Service\TrackingInfoStructFactory;
use PHPUnit\Framework\TestCase;
use Shopware\Core\Checkout\Order\Aggregate\OrderDelivery\OrderDeliveryEntity;
use Shopware\Core\Checkout\Shipping\ShippingMethodEntity;

/**
* @coversDefaultClass \Kiener\MolliePayments\Service\TrackingInfoStructFactory
Expand All @@ -21,26 +23,98 @@ public function setUp(): void
$this->factory = new TrackingInfoStructFactory();
}


public function testInfoStructCreatedByDelivery(): void
{
$expectedCode = '1234';
$expectedCarrier = 'Test carrier';
$expectedUrl = 'https://test.foo?code=1234';
$deliveryEntity = new OrderDeliveryEntity();
$deliveryEntity->setUniqueIdentifier('testDelivery');
$deliveryEntity->setTrackingCodes([
$expectedCode
]);

$shippingMethod = new ShippingMethodEntity();
$shippingMethod->setName($expectedCarrier);
$shippingMethod->setUniqueIdentifier('testShippingMethod');
$shippingMethod->setTrackingUrl('https://test.foo?code=%s');

$deliveryEntity->setShippingMethod($shippingMethod);
$trackingInfoStruct = $this->factory->createFromDelivery($deliveryEntity);

$this->assertNotNull($trackingInfoStruct);
$this->assertSame($expectedCode, $trackingInfoStruct->getCode());
$this->assertSame($expectedUrl, $trackingInfoStruct->getUrl());
$this->assertSame($expectedCarrier, $trackingInfoStruct->getCarrier());
}

public function testOnlyOneCodeAccepted(): void
{

$deliveryEntity = new OrderDeliveryEntity();
$deliveryEntity->setUniqueIdentifier('testDelivery');
$deliveryEntity->setTrackingCodes([
'1234',
'test'
]);

$shippingMethod = new ShippingMethodEntity();
$shippingMethod->setName('Test carrier');
$shippingMethod->setUniqueIdentifier('testShippingMethod');
$shippingMethod->setTrackingUrl('https://test.foo?code=%s');

$deliveryEntity->setShippingMethod($shippingMethod);
$trackingInfoStruct = $this->factory->createFromDelivery($deliveryEntity);
$this->assertNull($trackingInfoStruct);
}

public function testInfoStructCreatedByArguments(): void
{
$expectedCode = '1234';
$expectedCarrier = 'Test carrier';
$trackingInfoStruct = $this->factory->create($expectedCarrier, $expectedCode, 'https://test.foo?code=%s');
$expectedUrl = 'https://test.foo?code=1234';

$this->assertNotNull($trackingInfoStruct);
$this->assertSame($expectedCode, $trackingInfoStruct->getCode());
$this->assertSame($expectedUrl, $trackingInfoStruct->getUrl());
$this->assertSame($expectedCarrier, $trackingInfoStruct->getCarrier());

}

public function testExceptionIsThrownWithoutPlaceholderInUrl(): void
{
$this->expectException(\InvalidArgumentException::class);
$trackingInfoStruct = $this->factory->create('Test', 'testCode', 'https://test.foo?code');

$this->assertNull($trackingInfoStruct);
}

/**
* @dataProvider invalidCodes
* @param string $url
* @param string $trackingCode
* @return void
*/
public function testInvalidTrackingCodeCharacter(string $trackingCode):void{
public function testInvalidTrackingCodeCharacter(string $trackingCode): void
{

$trackingInfoStruct = $this->factory->create('test',$trackingCode,'https://foo.bar');
$expected ='';
$this->assertSame($expected,$trackingInfoStruct->getUrl());
$trackingInfoStruct = $this->factory->create('test', $trackingCode, 'https://foo.bar/%s');
$expected = '';
$this->assertSame($expected, $trackingInfoStruct->getUrl());

}
public function invalidCodes():array{

public function invalidCodes(): array
{
return [
['some{code'],
['some}code'],
['some<code'],
['some>code'],
['some#code'],
[str_repeat('1', 200)],
];
}
}

0 comments on commit 2dbf5af

Please sign in to comment.