View source
<?php
namespace Drupal\Tests\salesforce_pull\Unit;
use Drupal\Component\EventDispatcher\ContainerAwareEventDispatcher;
use Drupal\Core\Entity\EntityStorageBase;
use Drupal\Core\Entity\EntityTypeManagerInterface;
use Drupal\Core\State\StateInterface;
use Drupal\salesforce_mapping\Entity\MappedObjectInterface;
use Drupal\salesforce_mapping\Entity\SalesforceMappingInterface;
use Drupal\salesforce_mapping\MappedObjectStorage;
use Drupal\salesforce_mapping\MappingConstants;
use Drupal\salesforce_mapping\SalesforceMappingStorage;
use Drupal\salesforce_pull\DeleteHandler;
use Drupal\salesforce\Rest\RestClientInterface;
use Drupal\Tests\UnitTestCase;
use Drupal\user\Entity\User;
use Prophecy\Argument;
class DeleteHandlerTest extends UnitTestCase {
protected static $modules = [
'salesforce_pull',
];
protected function setUp() {
parent::setUp();
$result = [
'totalSize' => 1,
'done' => TRUE,
'deletedRecords' => [
[
'id' => '1234567890abcde',
'attributes' => [
'type' => 'dummy',
],
'name' => 'Example',
],
],
];
$prophecy = $this
->prophesize(RestClientInterface::CLASS);
$prophecy
->getDeleted(Argument::any(), Argument::any(), Argument::any())
->willReturn($result);
$this->sfapi = $prophecy
->reveal();
$prophecy = $this
->prophesize(User::CLASS);
$prophecy
->delete()
->willReturn(TRUE);
$prophecy
->id()
->willReturn(1);
$prophecy
->label()
->willReturn('foo');
$this->entity = $prophecy
->reveal();
$this->mapping = $this
->getMock(SalesforceMappingInterface::CLASS);
$this->mapping
->expects($this
->any())
->method('__get')
->with($this
->equalTo('id'))
->willReturn(1);
$this->mapping
->expects($this
->any())
->method('__get')
->with($this
->equalTo('entity'))
->willReturn($this->entity);
$this->mapping
->expects($this
->any())
->method('getSalesforceObjectType')
->willReturn('default');
$this->mapping
->expects($this
->any())
->method('getPullFieldsArray')
->willReturn([
'Name' => 'Name',
'Account Number' => 'Account Number',
]);
$this->mapping
->expects($this
->any())
->method('checkTriggers')
->with([
MappingConstants::SALESFORCE_MAPPING_SYNC_SF_DELETE,
])
->willReturn(TRUE);
$this->entityTypeId = new \stdClass();
$this->entityId = new \stdClass();
$this->entityRef = new \stdClass();
$this->entityTypeId->value = 'test';
$this->entityId->value = '1';
$this->entityRef->entity = $this->mapping;
$this->mappedObject = $this
->getMock(MappedObjectInterface::CLASS);
$this->mappedObject
->expects($this
->any())
->method('delete')
->willReturn(TRUE);
$this->mappedObject
->expects($this
->any())
->method('getMapping')
->willReturn($this->mapping);
$this->mappedObject
->expects($this
->any())
->method('getFieldDefinitions')
->willReturn([
'drupal_entity',
'salesforce_mapping',
]);
$this->mappedObject
->expects($this
->any())
->method('getMappedEntity')
->willReturn($this->entity);
$prophecy = $this
->prophesize(SalesforceMappingStorage::CLASS);
$prophecy
->loadByProperties(Argument::any())
->willReturn([
$this->mapping,
]);
$prophecy
->load(Argument::any())
->willReturn($this->mapping);
$prophecy
->loadMultiple()
->willReturn([
$this->mapping,
]);
$this->configStorage = $prophecy
->reveal();
$this->entityStorage = $this
->getMockBuilder(MappedObjectStorage::CLASS)
->disableOriginalConstructor()
->getMock();
$this->entityStorage
->expects($this
->any())
->method('loadBySfid')
->willReturn([
$this->mappedObject,
]);
$prophecy = $this
->prophesize(EntityStorageBase::CLASS);
$prophecy
->load(Argument::any())
->willReturn($this->entity);
$this->drupalEntityStorage = $prophecy
->reveal();
$prophecy = $this
->prophesize(EntityTypeManagerInterface::CLASS);
$prophecy
->getStorage('salesforce_mapping')
->willReturn($this->configStorage);
$prophecy
->getStorage('salesforce_mapped_object')
->willReturn($this->entityStorage);
$prophecy
->getStorage('test')
->willReturn($this->drupalEntityStorage);
$this->etm = $prophecy
->reveal();
$prophecy = $this
->prophesize(StateInterface::CLASS);
$prophecy
->get('salesforce.mapping_pull_info', Argument::any())
->willReturn([
1 => [
'last_delete_timestamp' => '1485787434',
],
]);
$prophecy
->set('salesforce.mapping_pull_info', Argument::any())
->willReturn(NULL);
$this->state = $prophecy
->reveal();
$prophecy = $this
->prophesize(ContainerAwareEventDispatcher::CLASS);
$prophecy
->dispatch(Argument::any(), Argument::any())
->willReturn();
$this->ed = $prophecy
->reveal();
$this->dh = new DeleteHandler($this->sfapi, $this->etm, $this->state, $this->ed);
}
public function testObject() {
$this
->assertTrue($this->dh instanceof DeleteHandler);
}
public function testGetUpdatedRecords() {
$result = $this->dh
->processDeletedRecords();
$this
->assertTrue($result);
}
}