View source
<?php
namespace Drupal\Tests\salesforce_push\Functional;
use Drupal\node\Entity\Node;
use Drupal\salesforce_mapping\Entity\SalesforceMapping;
use Drupal\Tests\BrowserTestBase;
class PushQueueTest extends BrowserTestBase {
protected $defaultTheme = 'stark';
protected static $modules = [
'typed_data',
'dynamic_entity_reference',
'salesforce_mapping',
'salesforce_mapping_test',
'salesforce_push',
];
public function testQueue() {
$queue = \Drupal::service('queue.salesforce_push');
$mapping = SalesforceMapping::load('test_mapping');
$mapping
->set('async', TRUE);
$mapping
->save();
$mappedObjectStorage = \Drupal::entityTypeManager()
->getStorage('salesforce_mapped_object');
$this
->assertEquals(0, $queue
->numberOfItems());
$node1 = Node::create([
'type' => 'salesforce_mapping_test_content',
'title' => 'Test Example',
]);
$node1
->save();
$this
->assertEquals(1, $queue
->numberOfItems());
$node2 = Node::create([
'type' => 'salesforce_mapping_test_content',
'title' => 'Test Example 2',
]);
$node2
->save();
$this
->assertEquals(2, $queue
->numberOfItems());
$queue
->processQueue($mapping);
$this
->assertEquals(0, $queue
->numberOfItems());
$mappedObject1 = $mappedObjectStorage
->loadByEntityAndMapping($node1, $mapping);
$this
->assertNotNull($mappedObject1);
$mappedObject1Vid = $mappedObject1
->getRevisionId();
$mappedObject2 = $mappedObjectStorage
->loadByEntityAndMapping($node2, $mapping);
$this
->assertNotNull($mappedObject2);
$mappedObject2Vid = $mappedObject2
->getRevisionId();
$node1
->setTitle('Updated Title')
->save();
$this
->assertEquals(1, $queue
->numberOfItems());
$node2
->setTitle('Updated Title 2')
->save();
$this
->assertEquals(2, $queue
->numberOfItems());
$queue
->processQueue($mapping);
$this
->assertEquals(0, $queue
->numberOfItems());
$mappedObjectStorage
->resetCache([
$mappedObject1
->id(),
$mappedObject2
->id(),
]);
$mappedObject1 = $mappedObjectStorage
->loadByEntityAndMapping($node1, $mapping);
$mappedObject1UpdatedVid = $mappedObject1
->getRevisionId();
$this
->assertNotEquals($mappedObject1Vid, $mappedObject1UpdatedVid);
$mappedObject2 = $mappedObjectStorage
->loadByEntityAndMapping($node2, $mapping);
$mappedObject2UpdatedVid = $mappedObject2
->getRevisionId();
$this
->assertNotEquals($mappedObject2Vid, $mappedObject2UpdatedVid);
$node1
->delete();
$node2
->delete();
$mappedObjectStorage
->resetCache([
$mappedObject1
->id(),
$mappedObject2
->id(),
]);
$mappedObject1 = $mappedObjectStorage
->load($mappedObject1
->id());
$this
->assertNotNull($mappedObject1);
$mappedObject2 = $mappedObjectStorage
->load($mappedObject2
->id());
$this
->assertNotNull($mappedObject2);
$this
->assertEquals(2, $queue
->numberOfItems());
$queue
->processQueue($mapping);
$this
->assertEquals(0, $queue
->numberOfItems());
$mappedObjectStorage
->resetCache([
$mappedObject1
->id(),
$mappedObject2
->id(),
]);
$this
->assertNull($mappedObjectStorage
->load($mappedObject1
->id()));
$this
->assertNull($mappedObjectStorage
->load($mappedObject2
->id()));
}
}