Skip to content
Snippets Groups Projects
Commit 4f57957a authored by Aaron Bauman's avatar Aaron Bauman
Browse files

Merge branch 'mads-30' of bitbucket.org:messageagency/sf into mads-30

# By git
# Via git
* 'mads-30' of bitbucket.org:messageagency/sf:
  Fixes various issues causing unit tests to fail

Conflicts:
	modules/salesforce_mapping/src/Entity/SalesforceMappingInterface.php
parents 38d1d13d 9fba2d84
No related branches found
No related tags found
No related merge requests found
......@@ -323,7 +323,7 @@ class SalesforceMapping extends ConfigEntityBase implements SalesforceMappingInt
return FALSE;
}
protected function fieldManager() {
public function fieldManager() {
return \Drupal::service('plugin.manager.salesforce_mapping_field');
}
......
......@@ -2,6 +2,8 @@
namespace Drupal\salesforce_mapping\Entity;
use Drupal\Core\Entity\EntityInterface;
/**
*
*/
......@@ -46,6 +48,6 @@ interface SalesforceMappingInterface {
*
* @return string
*/
public function getKeyValue();
public function getKeyValue(EntityInterface $entity);
}
......@@ -3,7 +3,6 @@ namespace Drupal\Tests\salesforce_mapping\Unit;
use Drupal\Tests\UnitTestCase;
use Drupal\salesforce_mapping\SalesforceMappingFieldPluginManager;
use Drupal\salesforce_mapping\Entity\SalesforceMappingInterface;
use Drupal\salesforce_mapping\Entity\SalesforceMapping;
use Drupal\Core\Config\Entity\ConfigEntityTypeInterface;
use Drupal\Core\Entity\EntityTypeManagerInterface;
......@@ -83,7 +82,7 @@ class SalesforceMappingTest extends UnitTestCase {
$prophecy->getDefinition($this->entityTypeId)->willReturn($this->entityDefinition);
$this->etm = $prophecy->reveal();
// moch Properties SalesforceMappingField
// mock Properties SalesforceMappingField
$prophecy = $this->prophesize(Properties::CLASS);
$prophecy->pull()->willReturn(true);
$sf_mapping_field = $prophecy->reveal();
......@@ -106,9 +105,8 @@ class SalesforceMappingTest extends UnitTestCase {
* Test object instantiation
*/
public function testObject() {
$mapping = new SalesforceMapping($this->values, $this->entityTypeId);
$this->assertTrue($mapping instanceof SalesforceMapping);
//$this->assertEquals(1, $item->mapping_id);
$this->assertTrue($this->mapping instanceof SalesforceMapping);
$this->assertEquals($this->id, $this->mapping->id);
}
/**
......
......@@ -64,17 +64,17 @@ class DeleteHandler {
*/
public function processDeletedRecords() {
// @TODO Add back in SOAP, and use autoloading techniques
foreach (array_reverse($this->mapping_storage->getMappedSobjectTypes()) as $type) {
$last_delete_sync = $this->state->get('salesforce_pull_last_delete_' . $type, $this->request->server->get('REQUEST_TIME'));
foreach (array_reverse($this->getMappingStorage()->getMappedSobjectTypes()) as $type) {
$last_delete_sync = $this->state()->get('salesforce_pull_last_delete_' . $type, $this->request()->server->get('REQUEST_TIME'));
$now = time();
// getDeleted() restraint: startDate must be at least one minute
// greater than endDate.
$now = $now > $last_delete_sync + 60 ? $now : $now + 60;
$last_delete_sync_sf = gmdate('Y-m-d\TH:i:s\Z', $last_delete_sync);
$now_sf = gmdate('Y-m-d\TH:i:s\Z', $now);
$deleted = $this->sfapi->getDeleted($type, $last_delete_sync_sf, $now_sf);
$deleted = $this->sfapi()->getDeleted($type, $last_delete_sync_sf, $now_sf);
$this->handleDeletedRecords($deleted, $type);
$this->state->set('salesforce_pull_last_delete_' . $type, $this->request->server->get('REQUEST_TIME'));
$this->state()->set('salesforce_pull_last_delete_' . $type, $this->request()->server->get('REQUEST_TIME'));
}
return true;
}
......@@ -84,7 +84,7 @@ class DeleteHandler {
return;
}
$sf_mappings = $this->mapping_storage->loadByProperties(
$sf_mappings = $this->getMappingStorage()->loadByProperties(
['salesforce_object_type' => $type]
);
if (empty($sf_mappings)) {
......@@ -96,18 +96,40 @@ class DeleteHandler {
}
}
protected function logger() {
return $this->logger;
}
protected function request() {
return $this->request;
}
protected function state() {
return $this->state;
}
protected function sfapi() {
return $this->sfapi;
}
protected function getMappingStorage() {
return $this->mapping_storage;
}
protected function getMappedObjectStorage() {
return $this->mapped_object_storage;
}
protected function handleDeletedRecord($record, $type) {
$mapped_objects = $this->mapped_object_storage->loadBySfid(new SFID($record['id']));
$mapped_objects = $this->getMappedObjectStorage()->loadBySfid(new SFID($record['id']));
if (empty($mapped_objects)) {
return;
}
foreach ($mapped_objects as $mapped_object) {
$entity = $this->etm
->getStorage($mapped_object->entity_type_id->value)
->load($mapped_object->entity_id->value);
$entity = $mapped_object->getMappedEntity();
if (!$entity) {
$this->logger->log(
$this->logger()->log(
LogLevel::NOTICE,
'No entity found for ID %id associated with Salesforce Object ID: %sfid ',
[
......@@ -121,12 +143,9 @@ class DeleteHandler {
}
// The mapping entity is an Entity reference field on mapped object, so we need to get the id value this way.
$foo = $mapped_object->salesforce_mapping->id;
$sf_mapping = $this->mapping_storage
// ->load($mapped_object->salesforce_mapping->id);
->load($mapped_object->salesforce_mapping->entity->id());
$sf_mapping = $mapped_object->getMapping();
if (!$sf_mapping) {
$this->logger->log(
$this->logger()->log(
LogLevel::NOTICE,
'No mapping exists for mapped object %id with Salesforce Object ID: %sfid',
[
......@@ -134,6 +153,7 @@ class DeleteHandler {
'%sfid' => $record['id'],
]
);
print "NO MAPPING\n";
// @TODO should we delete a mapped object whose parent mapping no longer exists? Feels like someone else's job.
// $mapped_object->delete();
return;
......@@ -145,7 +165,7 @@ class DeleteHandler {
try {
$entity->delete();
$this->logger->log(
$this->logger()->log(
LogLevel::NOTICE,
'Deleted entity %label with ID: %id associated with Salesforce Object ID: %sfid',
[
......@@ -156,7 +176,7 @@ class DeleteHandler {
);
}
catch (\Exception $e) {
$this->logger->log(
$this->logger()->log(
LogLevel::ERROR,
'%type: @message in %function (line %line of %file).',
Error::decodeException($e)
......
......@@ -267,7 +267,7 @@ abstract class PullBase extends QueueWorkerBase implements ContainerFactoryPlugi
// Create mapping object.
$mapped_object = $this->mapped_object_storage->create([
'entity_type_id' => $entity_type,
'salesforce_mapping' => $mapping->id(),
'salesforce_mapping' => $mapping->id,
'salesforce_id' => (string)$sf_object->id(),
]);
$mapped_object
......
<?php
namespace Drupal\Tests\salesforce_pull\Unit;
//use Drupal\Core\Config\Entity\ConfigEntityStorage;
use Drupal\Core\Entity\Entity;
use Drupal\Core\Entity\EntityStorageBase;
//use Drupal\Component\EventDispatcher\ContainerAwareEventDispatcher;
use Drupal\Core\Entity\EntityTypeManagerInterface;
use Drupal\Core\Entity\Entity;
use Drupal\Core\Queue\QueueInterface;
use Drupal\Core\State\StateInterface;
use Drupal\salesforce_mapping\Entity\MappedObject;
use Drupal\salesforce_mapping\Entity\MappedObjectInterface;
use Drupal\salesforce_mapping\Entity\SalesforceMappingInterface;
use Drupal\salesforce_mapping\SalesforceMappingStorage;
use Drupal\salesforce_mapping\MappedObjectStorage;
use Drupal\salesforce_mapping\MappingConstants;
use Drupal\salesforce_mapping\SalesforceMappingStorage;
use Drupal\salesforce_pull\DeleteHandler;
use Drupal\salesforce\Rest\RestClient;
use Drupal\salesforce\SelectQueryResult;
......@@ -28,7 +27,7 @@ use Symfony\Component\HttpFoundation\ServerBag;
* @group salesforce_pull
*/
class DeleteHanderTest extends UnitTestCase {
class DeleteHandlerTest extends UnitTestCase {
static $modules = ['salesforce_pull'];
/**
......@@ -53,13 +52,23 @@ class DeleteHanderTest extends UnitTestCase {
->willReturn($result); // revisit
$this->sfapi = $prophecy->reveal();
$this->mapping = $this->getMockBuilder(SalesforceMappingInterface::CLASS)
->setMethods(['__construct', '__get', 'get', 'getSalesforceObjectType', 'getPullFieldsArray','checkTriggers'])
->getMock();
// mock Drupal entity
$prophecy = $this->prophesize(Entity::CLASS);
$prophecy->delete()->willReturn(true);
$prophecy->id()->willReturn(1);
$this->entity = $prophecy->reveal();
$this->mapping = $this->getMock(SalesforceMappingInterface::CLASS);
// ->setMethods(['__get', 'getSalesforceObjectType', 'getPullFieldsArray', 'checkTriggers'])
// ->getMock();
$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');
......@@ -68,6 +77,7 @@ class DeleteHanderTest extends UnitTestCase {
->willReturn(['Name' => 'Name', 'Account Number' => 'Account Number']);
$this->mapping->expects($this->any())
->method('checkTriggers')
->with([MappingConstants::SALESFORCE_MAPPING_SYNC_SF_DELETE])
->willReturn(true);
// mock mapped object
......@@ -78,33 +88,40 @@ class DeleteHanderTest extends UnitTestCase {
$this->entityId->value = '1';
$this->entityRef->entity = $this->mapping;
$prophecy = $this->prophesize(MappedObject::CLASS);
$prophecy->delete()->willReturn(true);
$prophecy->getFieldDefinitions()->willReturn(['entity_type_id','entity_id','salesforce_mapping']);
$prophecy->entity_type_id = $this->entityTypeId;
$prophecy->entity_id = $this->entityId;
$prophecy->salesforce_mapping = $this->mapping;
$this->mappedObject = $prophecy->reveal();
//print_r($this->mappedObject);
// mock Drupal entity
$prophecy = $this->prophesize(Entity::CLASS);
$prophecy->delete()->willReturn(true);
$this->entity = $prophecy->reveal();
$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(['entity_type_id','entity_id','salesforce_mapping']);
$this->mappedObject
->expects($this->any())
->method('getMappedEntity')
->willReturn($this->entity);
// mock mapping ConfigEntityStorage object
$prophecy = $this->prophesize(SalesforceMappingStorage::CLASS);
$prophecy->loadByProperties(Argument::any())->willReturn([$this->mapping]);
$prophecy->load(Argument::any())->willReturn([$this->mapping]);
$prophecy->load(Argument::any())->willReturn($this->mapping);
$prophecy->getMappedSobjectTypes()->willReturn([
'default'
]);
$this->configStorage = $prophecy->reveal();
// mock mapped object EntityStorage object
$prophecy = $this->prophesize(MappedObjectStorage::CLASS);
$prophecy->loadBySfid(Argument::any())->willReturn([$this->mappedObject]);
$this->entityStorage = $prophecy->reveal();
$this->entityStorage = $this->getMockBuilder(MappedObjectStorage::CLASS)
->disableOriginalConstructor()
->getMock();
$this->entityStorage->expects($this->any())
->method('loadBySfid')
->willReturn([$this->mappedObject]);
// mock Drupal entity EntityStorage object
$prophecy = $this->prophesize(EntityStorageBase::CLASS);
......@@ -139,20 +156,35 @@ class DeleteHanderTest extends UnitTestCase {
$prophecy->server = $this->server;
$this->request = $prophecy->reveal();
$this->dh = DeleteHandler::create(
$this->sfapi,
$this->etm,
$this->state,
$this->logger,
$this->request
);
$this->dh = $this->getMockBuilder(DeleteHandler::CLASS)
->disableOriginalConstructor()
->setMethods(['logger', 'request', 'state', 'sfapi', 'getMappingStorage', 'getMappedObjectStorage'])
->getMock();
$this->dh->expects($this->any())
->method('logger')
->willReturn($this->logger);
$this->dh->expects($this->any())
->method('request')
->willReturn($this->request);
$this->dh->expects($this->any())
->method('state')
->willReturn($this->state);
$this->dh->expects($this->any())
->method('sfapi')
->willReturn($this->sfapi);
$this->dh->expects($this->any())
->method('getMappingStorage')
->willReturn($this->configStorage);
$this->dh->expects($this->any())
->method('getMappedObjectStorage')
->willReturn($this->entityStorage);
}
/**
* Test object instantiation
*/
public function testObject() {
$this->assertTrue($this->dh instanceof DeleteHander);
$this->assertTrue($this->dh instanceof DeleteHandler);
}
/**
......
......@@ -60,15 +60,12 @@ class PullBaseTest extends UnitTestCase {
// mock mapping object
$this->mapping = $this->getMockBuilder(SalesforceMappingInterface::CLASS)
->setMethods(['__construct', '__get', 'checkTriggers', 'getDrupalEntityType', 'getDrupalBundle', 'id', 'getFieldMappings', 'getSalesforceObjectType'])
->setMethods(['__construct', '__get', 'checkTriggers', 'getDrupalEntityType', 'getDrupalBundle', 'getFieldMappings', 'getSalesforceObjectType'])
->getMock();
$this->mapping->expects($this->any())
->method('__get')
->with($this->equalTo('id'))
->willReturn(1);
$this->mapping->expects($this->any())
->method('id')
->willReturn(1);
$this->mapping->expects($this->any())
->method('checkTriggers')
->willReturn(true);
......@@ -239,15 +236,13 @@ class PullBaseTest extends UnitTestCase {
public function testProcessItemUpdate() {
$sobject = new SObject(['id' => $this->salesforce_id, 'attributes' => ['type' => 'test',]]);
$item = new PullQueueItem($sobject, $this->mapping);
;
$this->assertEquals(MappingConstants::SALESFORCE_MAPPING_SYNC_SF_UPDATE, $this->pullWorker->processItem($item));
}
/**
* Test handler operation, create with good data
*/
public function testProcessItemCreate() {
public function testProcessItemCreate() {
// mock EntityTypeManagerInterface
$prophecy = $this->prophesize(EntityTypeManagerInterface::CLASS);
$prophecy
......
......@@ -505,7 +505,7 @@ class RestClient {
* @addtogroup salesforce_apicalls
*/
public function query(SelectQuery $query) {
// $this->moduleHander->alter('salesforce_query', $query);
// $this->moduleHandler->alter('salesforce_query', $query);
// Casting $query as a string calls SelectQuery::__toString().
return new SelectQueryResult($this->apiCall('query?q=' . (string) $query));
}
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment