salesforce-8.x-4.x-dev/modules/salesforce_mapping/tests/src/Unit/MappedObjectTest.php
modules/salesforce_mapping/tests/src/Unit/MappedObjectTest.php
<?php
namespace Drupal\Tests\salesforce_mapping\Unit;
use Drupal\Component\Datetime\TimeInterface;
use Drupal\Core\DependencyInjection\ContainerBuilder;
use Drupal\Core\Entity\ContentEntityInterface;
use Drupal\Core\Entity\EntityTypeInterface;
use Drupal\Core\Entity\EntityTypeManagerInterface;
use Drupal\Core\Field\FieldItemListInterface;
use Drupal\salesforce\Exception;
use Drupal\salesforce\Rest\RestClientInterface;
use Drupal\salesforce\SFID;
use Drupal\salesforce\SObject;
use Drupal\salesforce_mapping\Entity\MappedObject;
use Drupal\salesforce_mapping\Entity\SalesforceMappingInterface;
use Drupal\Tests\UnitTestCase;
use Symfony\Component\EventDispatcher\EventDispatcherInterface;
/**
* Test Mapped Object instantiation.
*
* @coversDefaultClass \Drupal\salesforce_mapping\Entity\MappedObject
* @group salesforce_mapping
*/
class MappedObjectTest extends UnitTestCase {
/**
* Required modules.
*
* @var array
*/
static protected $modules = ['salesforce_mapping'];
protected $bundle;
protected $client;
protected $entity;
protected $entityType;
protected $entityTypeId;
protected $entity_id;
protected $etm;
protected $event_dispatcher;
protected $fieldTypePluginManager;
protected $mappedObjectEntityType;
protected $mapped_object;
protected $mapped_object_id;
protected $mapping;
protected $mapping_id;
protected $salesforce_id;
protected $sf_object;
protected $sfid;
protected $time;
/**
* {@inheritdoc}
*/
protected function setUp(): void {
parent::setUp();
$this->entityTypeId = $this->randomMachineName();
$this->bundle = $this->randomMachineName();
$this->mapped_object_id = 1;
$this->salesforce_id = '1234567890abcdeAAA';
$this->mapping_id = 1;
$this->entity_id = 1;
$this->sf_object = new SObject([
'id' => $this->salesforce_id,
'attributes' => ['type' => $this->randomMachineName()],
'foo' => 'bar',
]);
$this->sfid = $this->getMockBuilder(SFID::CLASS)
->setConstructorArgs([$this->salesforce_id])
->getMock();
$this->sfid->expects($this->any())
->method('__toString')
->willReturn($this->salesforce_id);
$this->entityType = $this->getMockBuilder(EntityTypeInterface::class)
->getMock();
$this->entityType->expects($this->any())
->method('getKeys')
->willReturn([
'id' => 'id',
'uuid' => 'uuid',
]);
$this->etm = $this->getMockBuilder(EntityTypeManagerInterface::class)
->getMock();
$this->etm->expects($this->any())
->method('getDefinition')
->with($this->entityTypeId)
->willReturn($this->entityType);
$this->mappedObjectEntityType = $this->getMockBuilder(EntityTypeInterface::class)
->getMock();
$this->mappedObjectEntityType->expects($this->any())
->method('getKeys')
->willReturn([
'id' => 'id',
'entity_id' => 'entity_id',
'salesforce_id' => 'salesforce_id',
]);
$this->etm->expects($this->any())
->method('getDefinition')
->with('salesforce_mapped_object')
->willReturn($this->mappedObjectEntityType);
$this->event_dispatcher = $this->getMockBuilder(EventDispatcherInterface::class)
->getMock();
$this->client = $this->getMockBuilder(RestClientInterface::CLASS)
->getMock();
$this->fieldTypePluginManager = $this->getMockBuilder('\Drupal\Core\Field\FieldTypePluginManager')
->disableOriginalConstructor()
->getMock();
$this->fieldTypePluginManager->expects($this->any())
->method('getDefaultStorageSettings')
->willReturn([]);
$this->fieldTypePluginManager->expects($this->any())
->method('getDefaultFieldSettings')
->willReturn([]);
$this->fieldTypePluginManager->expects($this->any())
->method('createFieldItemList')
->willReturn($this->getMockBuilder(FieldItemListInterface::class)->getMock());
$this->time = $this->getMockBuilder(TimeInterface::CLASS)->getMock();
$container = new ContainerBuilder();
$container->set('entity_type.manager', $this->etm);
$container->set('salesforce.client', $this->client);
$container->set('event_dispatcher', $this->event_dispatcher);
$container->set('plugin.manager.field.field_type', $this->fieldTypePluginManager);
$container->set('datetime.time', $this->time);
\Drupal::setContainer($container);
$this->entity = $this->getMockBuilder(ContentEntityInterface::class)
->getMock();
$this->entity
->expects($this->any())
->method('id')
->willReturn($this->entity_id);
$this->entity
->expects($this->any())
->method('isTranslatable')
->willReturn(FALSE);
// Mock salesforce mapping.
$this->mapping = $this->getMockBuilder(SalesforceMappingInterface::CLASS)
->getMock();
$this->mapping
->expects($this->any())
->method('getFieldMappings')
->willReturn([]);
$this->mapping
->expects($this->any())
->method('getPullFields')
->willReturn([]);
$this->mapping
->expects($this->any())
->method('getSalesforceObjectType')
->willReturn('dummy_sf_object_type');
$this->mapped_object = $this->getMockBuilder(MappedObject::CLASS)
->disableOriginalConstructor()
->onlyMethods([
'getMappedEntity',
'getMapping',
'getEntityType',
'sfid',
'set',
'save',
'setNewRevision',
'client',
])
->getMock();
$this->mapped_object->expects($this->any())
->method('getMappedEntity')
->willReturn($this->entity);
$this->mapped_object->expects($this->any())
->method('getMapping')
->willReturn($this->mapping);
$this->mapped_object->expects($this->any())
->method('getEntityType')
->willReturn($this->mappedObjectEntityType);
$this->mapped_object->expects($this->any())
->method('set')
->willReturn($this->mapped_object);
$this->mapped_object->expects($this->any())
->method('client')
->willReturn($this->client);
}
/**
* @covers ::push
*/
public function testPushUpsert() {
// First pass: test upsert.
$this->mapped_object->expects($this->any())
->method('sfid')
->willReturn(NULL);
$this->mapping->expects($this->any())
->method('alwaysUpsert')
->willReturn(FALSE);
$this->mapping->expects($this->any())
->method('hasKey')
->willReturn(TRUE);
$this->client->expects($this->once())
->method('objectUpsert')
->willReturn(NULL);
$this->assertNull($this->mapped_object->push());
}
/**
* @covers ::push
*/
public function testPushUpdate() {
// Second pass: test update.
$this->mapped_object->expects($this->any())
->method('sfid')
->willReturn($this->sfid);
$this->mapping->expects($this->any())
->method('alwaysUpsert')
->willReturn(FALSE);
$this->client->expects($this->once())
->method('objectUpdate')
->willReturn(NULL);
$this->assertNull($this->mapped_object->push());
}
/**
* @covers ::push
*/
public function testPushCreate() {
// Third pass: test create.
$this->mapping->expects($this->once())
->method('hasKey')
->willReturn(FALSE);
$this->mapped_object->expects($this->any())
->method('sfid')
->willReturn(FALSE);
$this->mapping->expects($this->any())
->method('alwaysUpsert')
->willReturn(FALSE);
$this->client->expects($this->once())
->method('objectCreate')
->willReturn($this->sfid);
$result = $this->mapped_object->push();
$this->assertTrue($result instanceof SFID);
$this->assertEquals($this->salesforce_id, (string) $result);
}
/**
* @covers ::push
*/
public function testAlwaysUpsert() {
// Fourth pass: test always upsert.
$this->mapped_object->expects($this->any())
->method('sfid')
->willReturn($this->sfid);
$this->mapping->expects($this->any())
->method('alwaysUpsert')
->willReturn(TRUE);
$this->mapping->expects($this->once())
->method('hasKey')
->willReturn(TRUE);
$this->client->expects($this->once())
->method('objectUpsert')
->willReturn(NULL);
$this->assertNull($this->mapped_object->push());
}
/**
* @covers ::pushDelete
*/
public function testPushDelete() {
$this->client->expects($this->once())
->method('objectDelete')
->willReturn(NULL);
$this->assertEquals($this->mapped_object, $this->mapped_object->pushDelete());
}
/**
* @covers ::pull
*/
public function testPullException() {
$this->mapped_object->expects($this->any())
->method('sfid')
->willReturn(FALSE);
$this->mapping->expects($this->any())
->method('hasKey')
->willReturn(FALSE);
$this->expectException(Exception::class);
$this->mapped_object->pull();
}
/**
* @covers ::pull
*/
public function testPullExisting() {
$this->mapped_object->expects($this->any())
->method('sfid')
->willReturn($this->sfid);
$this->client->expects($this->once())
->method('objectRead')
->willReturn($this->sf_object);
$this->assertNull($this->mapped_object->getSalesforceRecord());
$this->mapped_object->pull();
$this->assertEquals($this->sf_object, $this->mapped_object->getSalesforceRecord());
}
/**
* @covers ::pull
*/
public function testPull() {
// Set sf_object to mock coming from cron pull.
$this->mapped_object->setSalesforceRecord($this->sf_object);
$this->assertEquals($this->mapped_object, $this->mapped_object->pull());
}
}
