Commit 5783be27 authored by Colin Corrigan's avatar Colin Corrigan Committed by Aaron Bauman
Browse files

Issue #3074441 by AaronBauman, chrisolof, cwcorrigan: Incorrect time pushed...

Issue #3074441 by AaronBauman, chrisolof, cwcorrigan: Incorrect time pushed from Drupal date to SF datetime fields
parent 2bf8c136
Loading
Loading
Loading
Loading
+4 −7
Original line number Diff line number Diff line
@@ -4,6 +4,7 @@ namespace Drupal\salesforce_mapping;

use Drupal\Component\Plugin\ConfigurablePluginInterface;
use Drupal\Core\Datetime\DateFormatterInterface;
use Drupal\Core\Datetime\DrupalDateTime;
use Drupal\Core\Entity\EntityFieldManagerInterface;
use Drupal\Core\Entity\EntityInterface;
use Drupal\Core\Entity\EntityManagerInterface;
@@ -25,6 +26,7 @@ use Drupal\salesforce\SObject;
use Drupal\salesforce_mapping\Entity\SalesforceMappingInterface;
use Symfony\Component\DependencyInjection\ContainerInterface;
use Symfony\Component\EventDispatcher\EventDispatcherInterface;
use DateTime;

/**
 * Defines a base Salesforce Mapping Field Plugin implementation.
@@ -200,13 +202,8 @@ abstract class SalesforceMappingFieldPluginBase extends PluginBase implements Sa

      case 'date':
      case 'datetime':
        $tmp = $value;
        if (!is_int($tmp)) {
          $tmp = strtotime($tmp);
        }
        if (!empty($tmp)) {
          $value = $this->dateFormatter->format($tmp, 'custom', 'c');
        }
        $date = new DrupalDateTime($value, 'UTC');
        $value = $date->format(DateTime::ISO8601);
        break;

      case 'double':
+79 −0
Original line number Diff line number Diff line
<?php

namespace Drupal\Tests\salesforce_mapping\Functional;

use Drupal\Core\Datetime\DrupalDateTime;
use Drupal\datetime\Plugin\Field\FieldType\DateTimeItemInterface;
use Drupal\node\Entity\Node;
use Drupal\salesforce_mapping\Entity\MappedObject;
use Drupal\salesforce_mapping\Entity\SalesforceMapping;
use Drupal\salesforce_mapping\PushParams;
use Drupal\Tests\BrowserTestBase;
use DateTime;

/**
 * Test that PushParams correctly creates data structures for Salesforce.
 *
 * @group salesforce_mapping
 */
class PushParamsTest extends BrowserTestBase {

  public static $modules = ['typed_data', 'dynamic_entity_reference', 'salesforce_mapping', 'salesforce_mapping_test'];

  /**
   * Test PushParams instantiation, where all the work gets done.
   */
  public function testPushParams() {
    date_default_timezone_set('America/New_York');
    $mapping = SalesforceMapping::load('test_mapping');
    $storedDate = date(DateTimeItemInterface::DATETIME_STORAGE_FORMAT, \Drupal::time()->getRequestTime());

    // Entity 1 is the target reference.
    $entity1 = Node::create([
        'type' => 'salesforce_mapping_test_content',
        'title' => 'Test Example',
      ]
    );
    $entity1->save();

    // Mapped Object to be used for RelatedIDs push params property.
    $mappedObject = MappedObject::create([
      'drupal_entity' => $entity1,
      'salesforce_mapping' => $mapping,
      'salesforce_id' => '0123456789ABCDEFGH',
      'salesforce_link' => NULL,
    ]);
    $mappedObject->save();

    // Entity 2 to be mapped to Salesforce.
    $entity2 = Node::create([
      'type' => 'salesforce_mapping_test_content',
      'title' => 'Test Example 2',
      'field_salesforce_test_bool' => 1,
      'field_salesforce_test_date' => $storedDate,
      'field_salesforce_test_email' => 'test2@example.com',
      'field_salesforce_test_link' => 'https://example.com',
      'field_salesforce_test_reference' => $entity1,
    ]);
    $entity2->save();

    $expectedDate = new DrupalDateTime($storedDate, 'UTC');

    // Create a PushParams and assert it's created as we expect.
    $pushParams = new PushParams($mapping, $entity2);
    $expected = [
      'FirstName' => 'SALESFORCE TEST',
      'Email' => 'test2@example.com',
      'Birthdate' => $expectedDate->format(DateTime::ISO8601),
      'd5__Do_Not_Mail__c' => TRUE,
      'ReportsToId' => '0123456789ABCDEFGH',
      'RecordTypeId' => '012i0000001B15mAAC',
      'Description' => 'https://example.com',
    ];
    $actual = $pushParams->getParams();
    foreach ($expected as $key => $value) {
      $this->assertSame($value, $actual[$key]);
    }
  }

}