Commit d7b6f049 authored by Sascha Grossenbacher's avatar Sascha Grossenbacher
Browse files

Issue #3216580 by Berdir: Issues if house number is in address line 2

parent 3cbe5590
Loading
Loading
Loading
Loading
+2 −3
Original line number Diff line number Diff line
@@ -165,11 +165,10 @@ class LabelGenerator implements LabelGeneratorInterface {
    $house_no = NULL;
    /** @var \Drupal\address\AddressInterface $shipping_address */
    $shipping_address = NULL;
    if ($shipping_profile && $shipping_profile->hasField('address') && !$shipping_profile->get('address')
        ->isEmpty()) {
    if ($shipping_profile && $shipping_profile->hasField('address') && !$shipping_profile->get('address')->isEmpty()) {
      $shipping_address = $shipping_profile->get('address')->get(0);
      $street = $shipping_address->getAddressLine1();
      if (preg_match('/(.+) (\d*[a-z]*)/', $shipping_address->getAddressLine1(), $matches)) {
      if (preg_match('/^(.+) (\d+[a-z]*)$/', trim($street), $matches)) {
        $street = $matches[1];
        $house_no = $matches[2];
      }
+7 −0
Original line number Diff line number Diff line
name: Commerce Swiss Post Test
type: module
description: Contains various non-specific things needed in tests.
package: Testing
core_version_requirement: ^8.8 || ^9
dependencies:
  - commerce_swiss_post:commerce_swiss_post
+5 −0
Original line number Diff line number Diff line
services:
  commerce_swiss_post_test.barcode_subscriber:
    class: Drupal\commerce_swiss_post_test\EventSubscriber\ShipmentToBarcodeSubscriber
    tags:
      - { name: event_subscriber }
+68 −0
Original line number Diff line number Diff line
<?php

namespace Drupal\commerce_swiss_post_test\EventSubscriber;

use Drupal\commerce_swiss_post\Events\ShipmentToBarcodeEvent;
use Symfony\Component\EventDispatcher\EventSubscriberInterface;

class ShipmentToBarcodeSubscriber implements EventSubscriberInterface {

  /**
   * {@inheritdoc}
   */
  public static function getSubscribedEvents() {
    return [
      ShipmentToBarcodeEvent::class => 'alterBarcodeData',
    ];
  }

  /**
   * Test changes to the barcode data.
   *
   * @param \Drupal\commerce_swiss_post\Events\ShipmentToBarcodeEvent $event
   *   The event.
   */
  public function alterBarcodeData(ShipmentToBarcodeEvent $event) {

    $shipping_profile = $event->getShipment()->getShippingProfile();
    /** @var \Drupal\address\AddressInterface $shipping_address */
    if ($shipping_profile && $shipping_profile->hasField('address') && !$shipping_profile->get('address')->isEmpty() && $shipping_address = $shipping_profile->get('address')->get(0)) {

      $barcode_data = $event->getBarcodeData();

      // If there is no house number but address line 2 matches the house number
      // format then use that as house number.
      if (empty($barcode_data['item']['recipient']['houseNo'])) {
        $address_line2 = $shipping_address->getAddressLine2();
        if (preg_match('/^(\d+[a-z]*)$/', trim($address_line2), $matches)) {
          $barcode_data['item']['recipient']['houseNo'] = trim($address_line2);
        }
      }
    }

    // Look for a c/o in address line 2.
    if ($shipping_address->getAddressLine2() && \preg_match('/^c\/o.+$/i', trim($shipping_address->getAddressLine2()))) {
      $barcode_data['item']['recipient']['name3'] = trim($shipping_address->getAddressLine2());
    }

    // Look for a c/o in address line 1.
    if ($shipping_address->getAddressLine1() && \preg_match('/^c\/o.+/i', trim($shipping_address->getAddressLine1()))) {
      $barcode_data['item']['recipient']['name3'] = $shipping_address->getAddressLine1();

      // If address line 2 is not empty, use it as street.
      if ($shipping_address->getAddressLine2()) {
        $street = $shipping_address->getAddressLine2();
        $house_no = NULL;
        if (preg_match('/^(.+) (\d+[a-z]*)$/', trim($street), $matches)) {
          $street = $matches[1];
          $house_no = $matches[2];
        }

        $barcode_data['item']['recipient']['street'] = $street;
        $barcode_data['item']['recipient']['houseNo'] = $house_no;
      }
    }

    $event->setBarcodeData($barcode_data);
  }
}
+63 −0
Original line number Diff line number Diff line
@@ -65,6 +65,7 @@ class LabelGeneratorTest extends SwissPostKernelTestBase {
    'file',
    'commerce_order',
    'commerce_swiss_post',
    'commerce_swiss_post_test',
  ];

  /**
@@ -160,6 +161,68 @@ class LabelGeneratorTest extends SwissPostKernelTestBase {
    $this->assertEquals($this->pdfContent, \file_get_contents($file_uri));
  }

  /**
   * Tests the alter hook.
   */
  public function testRequestDataEvent() {
    $request_data = $this->getRequestData();

    $this->mockSuccessResponse($request_data);
    $shipment = $this->createShipment('PRI', [
      'address_line1' => 'Example Street',
      'address_line2' => '77',
    ]);

    $label_generator = $this->container->get('commerce_swiss_post.label_generator');
    $this->assertEquals($this->pdfContent, $label_generator->generateLabels([$shipment]));

    // Assert that the file was created.
    $file_uri = $shipment->get('commerce_swisspost_barcode_label')->entity->getFileUri();
    $this->assertEquals($this->pdfContent, \file_get_contents($file_uri));
  }

  /**
   * Tests the alter hook.
   */
  public function testRequestDataEventCo1() {
    $request_data = $this->getRequestData();
    $request_data['item']['recipient']['name3'] = 'c/o John Snow';

    $this->mockSuccessResponse($request_data);
    $shipment = $this->createShipment('PRI', [
      'address_line1' => 'c/o John Snow',
      'address_line2' => 'Example Street 77',
    ]);

    $label_generator = $this->container->get('commerce_swiss_post.label_generator');
    $this->assertEquals($this->pdfContent, $label_generator->generateLabels([$shipment]));

    // Assert that the file was created.
    $file_uri = $shipment->get('commerce_swisspost_barcode_label')->entity->getFileUri();
    $this->assertEquals($this->pdfContent, \file_get_contents($file_uri));
  }

  /**
   * Tests the alter hook.
   */
  public function testRequestDataEventCo2() {
    $request_data = $this->getRequestData();
    $request_data['item']['recipient']['name3'] = 'C/o John Snow';

    $this->mockSuccessResponse($request_data);
    $shipment = $this->createShipment('PRI', [
      'address_line1' => 'Example Street 77',
      'address_line2' => 'C/o John Snow',
    ]);

    $label_generator = $this->container->get('commerce_swiss_post.label_generator');
    $this->assertEquals($this->pdfContent, $label_generator->generateLabels([$shipment]));

    // Assert that the file was created.
    $file_uri = $shipment->get('commerce_swisspost_barcode_label')->entity->getFileUri();
    $this->assertEquals($this->pdfContent, \file_get_contents($file_uri));
  }

  /**
   * @covers ::generateLabels
   * @covers ::buildRequestData