diff --git a/modules/salesforce_mapping/salesforce_mapping.install b/modules/salesforce_mapping/salesforce_mapping.install index 090461d159c5558f9882096368ff4444d23424fa..093b84992bfe61b6f7412661fd2bb04bf79b1b7f 100644 --- a/modules/salesforce_mapping/salesforce_mapping.install +++ b/modules/salesforce_mapping/salesforce_mapping.install @@ -8,7 +8,7 @@ use Drupal\salesforce_mapping\MappedObjectStorageSchema; /** * Copy entity_id-entity_type_id data into new mapped_entity field */ -function salesforce_mapping_update_8010(&$sandbox) { +function salesforce_mapping_update_8001(&$sandbox) { if (!\Drupal::moduleHandler()->moduleExists('dynamic_entity_reference')) { throw new \Exception('Please enable new dependency dynamic_entity_reference before continuing.'); } @@ -57,7 +57,7 @@ function salesforce_mapping_update_8010(&$sandbox) { /** * Drop vestigial fields manually, since entity update refuses to do it. */ -function salesforce_mapping_update_8011() { +function salesforce_mapping_update_8002() { try { // drop this index if it exists. db_drop_index('salesforce_mapped_object', 'entity__mapping'); diff --git a/modules/salesforce_mapping/src/Entity/MappedObject.php b/modules/salesforce_mapping/src/Entity/MappedObject.php index 25477ed2ad0b5b6d238548963ef1589d831a008a..c77b1a0a50d124ffb8fc48c6b8d8a436c9501b77 100644 --- a/modules/salesforce_mapping/src/Entity/MappedObject.php +++ b/modules/salesforce_mapping/src/Entity/MappedObject.php @@ -407,10 +407,7 @@ class MappedObject extends RevisionableContentEntityBase implements MappedObject * @return $this */ public function setDrupalEntity(EntityInterface $entity = NULL) { - $this->drupal_entity->setValue([ - 'target_id' => $entity->id(), - 'target_type' => $entity->getEntityTypeId() - ]); + $this->drupal_entity->setValue($entity); return $this; } @@ -497,9 +494,9 @@ class MappedObject extends RevisionableContentEntityBase implements MappedObject '@sfobj' => $mapping->getSalesforceObjectType(), '@sffield' => $sf_field, '@sfid' => $this->sfid(), - '@dobj' => $this->entity_type_id->value, + '@dobj' => $this->getMappedEntity()->getEntityTypeId(), '@dprop' => $drupal_field, - '@did' => $this->entity_id->value, + '@did' => $this->getMappedEntity()->id(), '@v' => $value, ]; $this->eventDispatcher()->dispatch(SalesforceEvents::WARNING, new SalesforceWarningEvent($e, $message, $args)); diff --git a/modules/salesforce_mapping/src/Form/MappedObjectForm.php b/modules/salesforce_mapping/src/Form/MappedObjectForm.php index 1f3f42faf0b040654894b9826944a585bec61c28..cfcae9899e36425573c90e846b656a562570e8f3 100644 --- a/modules/salesforce_mapping/src/Form/MappedObjectForm.php +++ b/modules/salesforce_mapping/src/Form/MappedObjectForm.php @@ -176,8 +176,10 @@ class MappedObjectForm extends ContentEntityForm { $mapped_object = $this->entity; $mapped_object ->set('salesforce_id', $form_state->getValue('salesforce_id')) - ->set('entity_id', $drupal_entity->id()) - ->set('entity_type_id', $drupal_entity->getEntityTypeId()) + ->set('drupal_entity', [ + 'target_id' => $drupal_entity->id(), + 'target_type' => $drupal_entity->getEntityTypeId() + ]) ->set('salesforce_mapping', $form_state->getValue('salesforce_mapping')); // Validate mapped object. Upon failure, rebuild form. @@ -216,8 +218,10 @@ class MappedObjectForm extends ContentEntityForm { $errors = $mapped_object ->set('salesforce_id', $form_state->getValue('salesforce_id')) - ->set('entity_id', $drupal_entity->id()) - ->set('entity_type_id', $drupal_entity->getEntityTypeId()) + ->set('drupal_entity', [ + 'target_id' => $drupal_entity->id(), + 'target_type' => $drupal_entity->getEntityTypeId() + ]) ->set('salesforce_mapping', $form_state->getValue('salesforce_mapping')) ->validate(); diff --git a/modules/salesforce_mapping/src/MappedObjectStorageSchema.php b/modules/salesforce_mapping/src/MappedObjectStorageSchema.php index 39fd6f6b9ffd467503311cecefceaa671fc3fb7f..644d586fc4731343675f91141f37550df1fa856f 100644 --- a/modules/salesforce_mapping/src/MappedObjectStorageSchema.php +++ b/modules/salesforce_mapping/src/MappedObjectStorageSchema.php @@ -29,9 +29,8 @@ class MappedObjectStorageSchema extends SqlContentEntityStorageSchema { ] ]; } - $schema['salesforce_mapped_object']['fields']['entity_type_id']['length'] = + $schema['salesforce_mapped_object']['fields']['salesforce_mapping']['length'] = - $schema['salesforce_mapped_object_revision']['fields']['entity_type_id']['length'] = $schema['salesforce_mapped_object_revision']['fields']['salesforce_mapping']['length'] = EntityTypeInterface::ID_MAX_LENGTH; diff --git a/modules/salesforce_mapping/src/Tests/SalesforceMappingCrudFormTest.php b/modules/salesforce_mapping/src/Tests/SalesforceMappingCrudFormTest.php index f6a60a20964c9d34291a04a031cf801eaa255890..f33140309f03a6c26c7168677abfba1a8f2fa617 100644 --- a/modules/salesforce_mapping/src/Tests/SalesforceMappingCrudFormTest.php +++ b/modules/salesforce_mapping/src/Tests/SalesforceMappingCrudFormTest.php @@ -21,6 +21,7 @@ class SalesforceMappingCrudFormTest extends WebTestBase { 'salesforce_test_rest_client', 'salesforce_mapping', 'user', + 'link', 'dynamic_entity_reference', ]; diff --git a/modules/salesforce_mapping/tests/src/Unit/MappedObjectListTest.php b/modules/salesforce_mapping/tests/src/Unit/MappedObjectListTest.php deleted file mode 100644 index 53f4c629ea47007851fad577db26b10f7af4f408..0000000000000000000000000000000000000000 --- a/modules/salesforce_mapping/tests/src/Unit/MappedObjectListTest.php +++ /dev/null @@ -1,166 +0,0 @@ -<?php - -/** - * @file - * Contains \Drupal\Tests\salesforce_mapping\Unit\MappedObjectListTest. - */ - -namespace Drupal\Tests\salesforce_mapping\Unit; - -use Drupal\Core\DependencyInjection\ContainerBuilder; -use Drupal\Core\Entity\ContentEntityBase; -use Drupal\Core\Entity\EntityStorageInterface; -use Drupal\Core\Entity\EntityTypeInterface; -use Drupal\Core\Entity\Query\QueryInterface; -use Drupal\Core\Field\Plugin\Field\FieldType\StringItem; -use Drupal\Tests\UnitTestCase; -use Drupal\salesforce_mapping\Tests\TestMappedObjectList; -use Prophecy\Argument; - -/** - * @coversDefaultClass \Drupal\salesforce_mapping\MappedObjectList - * @group salesforce_mapping - */ -class MappedObjectListTest extends UnitTestCase { - - /** - * The entity type used for testing. - * - * @var \Drupal\Core\Entity\EntityTypeInterface|\PHPUnit_Framework_MockObject_MockObject - */ - protected $entityType; - - /** - * The translation manager used for testing. - * - * @var \Drupal\Core\StringTranslation\TranslationInterface - */ - protected $translationManager; - - /** - * The entity storage used for testing. - * - * @var \Drupal\Core\Entity\EntityStorageInterface|\PHPUnit_Framework_MockObject_MockObject - */ - protected $storage; - - /** - * The service container used for testing. - * - * @var \Drupal\Core\DependencyInjection\ContainerBuilder - */ - protected $container; - - /** - * The entity used for testing. - * - * @var \Drupal\Core\Entity\ContentEntityBase|\PHPUnit_Framework_MockObject_MockObject - */ - protected $entity; - - /** - * The query interface used for testing. - * - * @var \Drupal\Core\Entity\Query\QueryInterface|\PHPUnit_Framework_MockObject_MockObject - */ - protected $entityQuery; - - /** - * {@inheritdoc} - */ - protected function setUp() { - parent::setUp(); - - // mock content entity - $this->entity = $this->getMockBuilder(ContentEntityBase::CLASS) - ->setMethods(['__construct', '__get', 'get', 'sfid', 'id', '__isset', 'getAccessControlHandler']) - ->disableOriginalConstructor() - ->getMock(); - $this->entity->expects($this->any()) - ->method('__get') - ->will($this->returnValueMap([ - ['entity_id',$this->setPropertyValue('1')], - ['entity_type_id', $this->setPropertyValue('Foo')], - ['changed', $this->setPropertyValue('12:00:00')] - ])); - $this->entity->expects($this->any()) - ->method('sfid') - ->willReturn('1234567890abcdeAAA'); - - $this->entityQuery = $this->prophesize(QueryInterface::class); - $this->entityQuery->sort(Argument::any())->willReturn($this->entityQuery); - $this->entityQuery->pager(Argument::any())->willReturn($this->entityQuery); - $this->entityQuery->execute()->willReturn(['foo', 'bar']); - - $this->entityType = $this->prophesize(EntityTypeInterface::class); - $this->translationManager = $this->getMock('\Drupal\Core\StringTranslation\TranslationInterface'); - $this->storage = $this->prophesize(EntityStorageInterface::class); - $this->storage->getQuery()->willReturn($this->entityQuery->reveal()); - $this->storage->loadMultiple(['foo', 'bar'])->willReturn([$this->entity]); - - $this->url_generator = $this->getMock('\Drupal\Core\Routing\UrlGeneratorInterface'); - $this->container = new ContainerBuilder(); - \Drupal::setContainer($this->container); - } - - /** - * @covers ::render - */ - public function testRender() { - $list = new TestMappedObjectList($this->entityType->reveal(), $this->storage->reveal(), $this->url_generator); - $list->setStringTranslation($this->translationManager); - $build = $list->render(); - $this->assertArrayHasKey('#markup', $build['description']); - $this->assertArrayHasKey('table', $build); - } - - /** - * @covers ::buildHeader - */ - public function testBuildHeader() { - $list = new TestMappedObjectList($this->entityType->reveal(), $this->storage->reveal(), $this->url_generator); - $list->setStringTranslation($this->translationManager); - $header = $list->buildHeader(); - $this->assertArrayHasKey('id', $header); - $this->assertArrayHasKey('entity_id', $header); - $this->assertArrayHasKey('entity_type', $header); - $this->assertArrayHasKey('salesforce_id', $header); - $this->assertArrayHasKey('changed', $header); - - } - - /** - * @covers ::buildRow - */ - public function testBuildRow() { - $list = new TestMappedObjectList($this->entityType->reveal(), $this->storage->reveal(), $this->url_generator); - $row = $list->buildRow($this->entity); - $this->assertArrayHasKey('id', $row); - $this->assertArrayHasKey('entity_id', $row); - $this->assertArrayHasKey('entity_type', $row); - $this->assertArrayHasKey('salesforce_id', $row); - $this->assertArrayHasKey('changed', $row); - } - - /** - * Creates a value object for $entity->property_name->value pattern calls. - * - * @param $value - * Value to be return by $property_name->value. - * - * @return \Drupal\Core\Field\Plugin\Field\FieldType\StringItem - */ - public function setPropertyValue($value) { - $valueObject = $this->getMockBuilder(StringItem::CLASS) - ->setMethods(['__get']) - ->disableOriginalConstructor() - ->getMock(); - $valueObject->expects($this->any()) - ->method('__get') - ->with($this->equalTo('value')) - ->willReturn($value); - return $valueObject; - } - -} - diff --git a/modules/salesforce_mapping/tests/src/Unit/MappedObjectTest.php b/modules/salesforce_mapping/tests/src/Unit/MappedObjectTest.php index edc550133b6f36ae808f95248b8ea49c3ffc43b4..6fc9bebe14d6dc11f19c75d7fb558dc10e1a980e 100644 --- a/modules/salesforce_mapping/tests/src/Unit/MappedObjectTest.php +++ b/modules/salesforce_mapping/tests/src/Unit/MappedObjectTest.php @@ -42,15 +42,6 @@ class MappedObjectTest extends UnitTestCase { 'foo' => 'bar', ]); - $values = array( - 'id' => $this->mapped_object_id, - // 'defaultLangcode' => array(LanguageInterface::LANGCODE_DEFAULT => 'en'), - 'entity_id' => $this->entity_id, - 'entity_type_id' => $this->entityTypeId, - 'salesforce_id' => $this->salesforce_id, - 'salesforce_mapping' => $this->mapping_id, - ); - $this->sfid = $this->getMockBuilder(SFID::CLASS) ->setConstructorArgs([$this->salesforce_id]) ->getMock(); @@ -126,13 +117,13 @@ class MappedObjectTest extends UnitTestCase { ->method('isTranslatable') ->willReturn(FALSE); - $this->fieldDefinitions = array( - 'id' => BaseFieldDefinition::create('integer'), - 'entity_id' => BaseFieldDefinition::create('integer'), - 'entity_type_id' => BaseFieldDefinition::create('string'), - 'salesforce_id' => BaseFieldDefinition::create('string'), - 'salesforce_mapping' => BaseFieldDefinition::create('entity_reference'), - ); + // $this->fieldDefinitions = array( + // 'id' => BaseFieldDefinition::create('integer'), + // 'entity_id' => BaseFieldDefinition::create('integer'), + // 'entity_type_id' => BaseFieldDefinition::create('string'), + // 'salesforce_id' => BaseFieldDefinition::create('string'), + // 'salesforce_mapping' => BaseFieldDefinition::create('entity_reference'), + // ); // $this->entityManager->expects($this->any()) // ->method('getFieldDefinitions') diff --git a/modules/salesforce_pull/src/Plugin/QueueWorker/PullBase.php b/modules/salesforce_pull/src/Plugin/QueueWorker/PullBase.php index 2cd67f23f107bbaa69f3f7bf4900ec2ed38a127d..91d40624d438011896f68852ef063f3c00d14cd0 100644 --- a/modules/salesforce_pull/src/Plugin/QueueWorker/PullBase.php +++ b/modules/salesforce_pull/src/Plugin/QueueWorker/PullBase.php @@ -134,7 +134,7 @@ abstract class PullBase extends QueueWorkerBase implements ContainerFactoryPlugi } try { - $entity = $this->getMappedEntity($mapped_object); + $entity = $mapped_object->getMappedEntity(); if (!$entity) { $this->eventDispatcher->dispatch(SalesforceEvents::ERROR, new SalesforceErrorEvent(NULL, 'Drupal entity existed at one time for Salesforce object %sfobjectid, but does not currently exist.', ['%sfobjectid' => (string) $sf_object->id()])); return; @@ -205,12 +205,6 @@ abstract class PullBase extends QueueWorkerBase implements ContainerFactoryPlugi } } - protected function getMappedEntity($mapped_object) { - return $this->etm - ->getStorage($mapped_object->entity_type_id->value) - ->load($mapped_object->entity_id->value); - } - /** * Create a Drupal entity and mapped object. * @@ -243,9 +237,11 @@ abstract class PullBase extends QueueWorkerBase implements ContainerFactoryPlugi ->getStorage($entity_type) ->create($values); - // Create mapping object. + // Create mapped object. $mapped_object = $this->mappedObjectStorage->create([ - 'entity_type_id' => $entity_type, + 'drupal_entity' => [ + 'target_type' => $entity_type, + ], 'salesforce_mapping' => $mapping->id, 'salesforce_id' => (string) $sf_object->id(), ]); diff --git a/modules/salesforce_pull/tests/src/Unit/DeleteHandlerTest.php b/modules/salesforce_pull/tests/src/Unit/DeleteHandlerTest.php index f294a6ea164ee1afb80e4bc365fb5bfcaad41db5..ba93574131418f5a34793d48247ccf7dd1a4278d 100644 --- a/modules/salesforce_pull/tests/src/Unit/DeleteHandlerTest.php +++ b/modules/salesforce_pull/tests/src/Unit/DeleteHandlerTest.php @@ -93,7 +93,7 @@ class DeleteHandlerTest extends UnitTestCase { $this->mappedObject ->expects($this->any()) ->method('getFieldDefinitions') - ->willReturn(['entity_type_id', 'entity_id', 'salesforce_mapping']); + ->willReturn(['drupal_entity', 'salesforce_mapping']); $this->mappedObject ->expects($this->any()) ->method('getMappedEntity') diff --git a/modules/salesforce_push/salesforce_push.module b/modules/salesforce_push/salesforce_push.module index b14a2fef480458f61bf7c5c4536ef70f034b1d3d..d53df7aeb319ccd716e084f74a50d0837582ff5b 100644 --- a/modules/salesforce_push/salesforce_push.module +++ b/modules/salesforce_push/salesforce_push.module @@ -92,8 +92,8 @@ function salesforce_push_entity_crud_mapping(EntityInterface $entity, $op, Sales // Look for existing mapped object or create a new one (except for deletes). $props = [ - 'entity_id' => $entity->id(), - 'entity_type_id' => $entity->getEntityTypeId(), + 'drupal_entity__target_type' => $entity->getEntityTypeId(), + 'drupal_entity__target_id' => $entity->id(), 'salesforce_mapping' => $mapping->id(), ]; diff --git a/modules/salesforce_push/src/Plugin/SalesforcePushQueueProcessor/Rest.php b/modules/salesforce_push/src/Plugin/SalesforcePushQueueProcessor/Rest.php index 002e47edebc6318f95eae8b7e51cb9949d1684a9..af3b69821a08685f6485b7d3a793c93ebceaea0f 100644 --- a/modules/salesforce_push/src/Plugin/SalesforcePushQueueProcessor/Rest.php +++ b/modules/salesforce_push/src/Plugin/SalesforcePushQueueProcessor/Rest.php @@ -162,8 +162,8 @@ class Rest extends PluginBase implements PushQueueProcessorInterface { $mapped_object = $this ->mapped_object_storage ->loadByProperties([ - 'entity_type_id' => $mapping->drupal_entity_type, - 'entity_id' => $item->entity_id, + 'drupal_entity__target_type' => $mapping->drupal_entity_type, + 'drupal_entity__target_id' => $item->entity_id, 'salesforce_mapping' => $mapping->id(), ]); } @@ -185,8 +185,10 @@ class Rest extends PluginBase implements PushQueueProcessorInterface { */ protected function createMappedObject(\stdClass $item, SalesforceMappingInterface $mapping) { return new MappedObject([ - 'entity_id' => $item->entity_id, - 'entity_type_id' => $mapping->drupal_entity_type, + 'drupal_entity' => [ + 'target_id' => $item->entity_id, + 'target_type' => $mapping->drupal_entity_type, + ], 'salesforce_mapping' => $mapping->id(), ]); }