PullQueueTest.php in Salesforce Suite 5.0.x
File
modules/salesforce_pull/tests/src/Functional/PullQueueTest.php
View source
<?php
namespace Drupal\Tests\salesforce_pull\Functional;
use Drupal\salesforce\SFID;
use Drupal\salesforce\Tests\TestRestClient;
use Drupal\salesforce_mapping\Entity\SalesforceMapping;
use Drupal\Tests\BrowserTestBase;
class PullQueueTest extends BrowserTestBase {
protected $defaultTheme = 'stark';
protected static $modules = [
'typed_data',
'dynamic_entity_reference',
'salesforce_mapping',
'salesforce_mapping_test',
'salesforce_pull',
];
public function testEnqueue() {
\Drupal::service('salesforce_pull.queue_handler')
->getUpdatedRecords();
$items = TestRestClient::getContactQueryResults();
$queue_name = 'cron_salesforce_pull';
$queueFactory = \Drupal::service('queue');
$queue = $queueFactory
->get($queue_name);
$this
->assertEquals($items['totalSize'], $queue
->numberOfItems());
$queueManager = \Drupal::service('plugin.manager.queue_worker');
$queueFactory
->get('cron_salesforce_pull')
->createQueue();
$queue_worker = $queueManager
->createInstance($queue_name);
$mapping = SalesforceMapping::load('test_mapping');
$mappedObjectStorage = \Drupal::entityTypeManager()
->getStorage('salesforce_mapped_object');
for ($i = 0; $i < $items['totalSize']; $i++) {
$item = $queue
->claimItem();
$data = $item->data;
$queue_worker
->processItem($item->data);
$queue
->deleteItem($item);
$this
->assertEquals($items['totalSize'] - ($i + 1), $queue
->numberOfItems());
$sfid = $data
->getSobject()
->id();
$mappedObject = $mappedObjectStorage
->loadBySfidAndMapping($sfid, $mapping);
$createdEntity = $mappedObject
->getMappedEntity();
$this
->assertEquals('SALESFORCE TEST', $createdEntity
->getTitle());
$this
->assertEquals($data
->getSobject()
->field('Email'), $createdEntity->field_salesforce_test_email->value);
$this
->assertEquals(date('Y-m-d', strtotime($data
->getSobject()
->field('Birthdate'))), date('Y-m-d', strtotime($createdEntity->field_salesforce_test_date->value)));
$this
->assertEquals((bool) $data
->getSobject()
->field('d5__Do_Not_Mail__c'), (bool) $createdEntity->field_salesforce_test_bool->value);
$this
->assertEquals($data
->getSobject()
->field('Description'), $createdEntity->field_salesforce_test_link->uri);
}
drupal_flush_all_caches();
\Drupal::service('salesforce_pull.queue_handler')
->getUpdatedRecords();
$this
->assertEquals($items['totalSize'], $queue
->numberOfItems());
for ($i = 0; $i < $items['totalSize']; $i++) {
$item = $queue
->claimItem();
$data = $item->data;
$queue_worker
->processItem($data);
$queue
->deleteItem($item);
$this
->assertEquals($items['totalSize'] - ($i + 1), $queue
->numberOfItems());
$sfid = $data
->getSobject()
->id();
$mappedObject = $mappedObjectStorage
->loadBySfidAndMapping($sfid, $mapping);
$createdEntity = $mappedObject
->getMappedEntity();
if (!empty($data
->getSobject()
->field('ReportsToId'))) {
$referencedSfid = new SFID($data
->getSobject()
->field('ReportsToId'));
$referencedMappedObject = $mappedObjectStorage
->loadBySfidAndMapping($referencedSfid, $mapping);
$referencedEntity = $referencedMappedObject
->getMappedEntity();
$this
->assertEquals($referencedEntity
->uuid(), $createdEntity->field_salesforce_test_reference->entity
->uuid());
}
}
}
}