diff --git a/modules/salesforce_example/src/EventSubscriber/SalesforceExampleSubscriber.php b/modules/salesforce_example/src/EventSubscriber/SalesforceExampleSubscriber.php index a79bae8f5e5381c50a20f4d20da59e448b9fe976..887522233d240c54c5f2e43c0080d3f478d03f64 100644 --- a/modules/salesforce_example/src/EventSubscriber/SalesforceExampleSubscriber.php +++ b/modules/salesforce_example/src/EventSubscriber/SalesforceExampleSubscriber.php @@ -104,7 +104,7 @@ class SalesforceExampleSubscriber implements EventSubscriberInterface { case 'contact': // Add attachments to the Contact pull mapping so that we can save // profile pics. See also ::pullPresave. - $query = $event->getQuery(); + $query = $event->getQuery()->accessCheck(false); // Add a subquery: $query->fields[] = "(SELECT Id FROM Attachments WHERE Name = 'example.jpg' LIMIT 1)"; // Add a field from lookup: diff --git a/modules/salesforce_jwt/tests/src/FunctionalJavascript/SalesforceJwtTest.php b/modules/salesforce_jwt/tests/src/FunctionalJavascript/SalesforceJwtTest.php index 6ca84505106bbf0ae3ce7a75df3d7dc5c2ca7716..da9d5087e0442ca301b840bc4396dad79bdc5298 100644 --- a/modules/salesforce_jwt/tests/src/FunctionalJavascript/SalesforceJwtTest.php +++ b/modules/salesforce_jwt/tests/src/FunctionalJavascript/SalesforceJwtTest.php @@ -30,7 +30,7 @@ class SalesforceJwtTest extends WebDriverTestBase { * * @var array */ - public static $modules = [ + protected static $modules = [ 'key', 'typed_data', 'dynamic_entity_reference', @@ -54,7 +54,7 @@ class SalesforceJwtTest extends WebDriverTestBase { /** * {@inheritdoc} */ - protected function setUp() { + protected function setUp(): void { parent::setUp(); $this->adminUser = $this->drupalCreateUser(['authorize salesforce']); $this->drupalLogin($this->adminUser); diff --git a/modules/salesforce_mapping/src/Entity/MappedObject.php b/modules/salesforce_mapping/src/Entity/MappedObject.php index 630e7faf44c56d71800d561d3e6a39bca1867f02..8cb2af9f1ee705b4328369d033fcf94915ae1685 100644 --- a/modules/salesforce_mapping/src/Entity/MappedObject.php +++ b/modules/salesforce_mapping/src/Entity/MappedObject.php @@ -150,6 +150,7 @@ class MappedObject extends RevisionableContentEntityBase implements MappedObject } $count = $storage ->getQuery() + ->accessCheck(false) ->allRevisions() ->condition('id', $this->id()) ->count() @@ -161,6 +162,7 @@ class MappedObject extends RevisionableContentEntityBase implements MappedObject } $vids_to_delete = $storage ->getQuery() + ->accessCheck(false) ->allRevisions() ->condition('id', $this->id()) ->range($limit, $count) diff --git a/modules/salesforce_mapping/src/SalesforceMappingStorage.php b/modules/salesforce_mapping/src/SalesforceMappingStorage.php index dff758807008e67ad42e83896ed6eb0d3ea3a25a..d02aead271ccf0bc6f37306c5432df15a2d8b93e 100644 --- a/modules/salesforce_mapping/src/SalesforceMappingStorage.php +++ b/modules/salesforce_mapping/src/SalesforceMappingStorage.php @@ -157,7 +157,7 @@ class SalesforceMappingStorage extends ConfigEntityStorage { */ public function loadByProperties(array $values = []) { // Build a query to fetch the entity IDs. - $entity_query = $this->getQuery(); + $entity_query = $this->getQuery()->accessCheck(false); $this->buildPropertyQuery($entity_query, $values); // Sort by the mapping weight to ensure entities/objects are processed in // the correct order. diff --git a/modules/salesforce_mapping/tests/src/Unit/SalesforceMappingStorageTest.php b/modules/salesforce_mapping/tests/src/Unit/SalesforceMappingStorageTest.php index c6c3621bb2e48bb3375ad10aa43543f4dfce0495..35f68c0ac2283eda8972a36ae9164cf4a1d312bc 100644 --- a/modules/salesforce_mapping/tests/src/Unit/SalesforceMappingStorageTest.php +++ b/modules/salesforce_mapping/tests/src/Unit/SalesforceMappingStorageTest.php @@ -120,25 +120,31 @@ class SalesforceMappingStorageTest extends UnitTestCase { $config_object = $this->prophesize(SalesforceMapping::class); $this->salesforceMappingStorage - ->expects($this->at(0)) + ->expects($this->once()) ->method('loadByProperties') ->with($this->equalTo(['drupal_entity_type' => $this->entityTypeId])) ->willReturn([$config_object->reveal()]); + // Good entity type id provided means config object should be returned. + $entities = $this->salesforceMappingStorage->loadByDrupal($this->entityTypeId); + $this->assertEquals([$config_object->reveal()], $entities); + } + + /** + * @covers ::loadByDrupal + */ + public function testLoadByDrupalEmpty() { + $config_object = $this->prophesize(SalesforceMapping::class); + $this->salesforceMappingStorage - ->expects($this->at(1)) + ->expects($this->once()) ->method('loadByProperties') ->with($this->equalTo(['drupal_entity_type' => 'dummy'])) ->willReturn([]); - // Good entity type id provided means config object should be returned. - $entities = $this->salesforceMappingStorage->loadByDrupal($this->entityTypeId); - $this->assertEquals([$config_object->reveal()], $entities); - // Bad entity type provided means config should not be returned. $entities = $this->salesforceMappingStorage->loadByDrupal('dummy'); $this->assertEquals([], $entities); - } /** diff --git a/modules/salesforce_oauth/tests/src/FunctionalJavascript/SalesforceOAuthTest.php b/modules/salesforce_oauth/tests/src/FunctionalJavascript/SalesforceOAuthTest.php index e1cc9c1fc9d5d68880c11839f06235a962913129..74897f2622b94fb8ef57f3afdc2e4358674ca76a 100644 --- a/modules/salesforce_oauth/tests/src/FunctionalJavascript/SalesforceOAuthTest.php +++ b/modules/salesforce_oauth/tests/src/FunctionalJavascript/SalesforceOAuthTest.php @@ -23,7 +23,7 @@ class SalesforceOAuthTest extends WebDriverTestBase { * * @var array */ - public static $modules = [ + protected static $modules = [ 'key', 'typed_data', 'dynamic_entity_reference', @@ -42,7 +42,7 @@ class SalesforceOAuthTest extends WebDriverTestBase { /** * {@inheritdoc} */ - protected function setUp() { + protected function setUp(): void { parent::setUp(); $this->adminUser = $this->drupalCreateUser(['authorize salesforce']); $this->drupalLogin($this->adminUser); diff --git a/modules/salesforce_push/tests/src/Unit/PushQueueTest.php b/modules/salesforce_push/tests/src/Unit/PushQueueTest.php index 548a009b3fe272507540a9fb95c13ecad0d33362..04a56638c647a32801a170a1b296825998dba236 100644 --- a/modules/salesforce_push/tests/src/Unit/PushQueueTest.php +++ b/modules/salesforce_push/tests/src/Unit/PushQueueTest.php @@ -71,15 +71,12 @@ class PushQueueTest extends UnitTestCase { $this->entityStorage = $this->getMockBuilder(SqlEntityStorageInterface::CLASS)->getMock(); - $this->entityTypeManager->expects($this->at(0)) + $this->entityTypeManager->expects($this->exactly(2)) ->method('getStorage') - ->with($this->equalTo('salesforce_mapping')) - ->willReturn($this->mappingStorage); - - $this->entityTypeManager->expects($this->at(1)) - ->method('getStorage') - ->with($this->equalTo('salesforce_mapped_object')) - ->willReturn($this->mappedObjectStorage); + ->willReturnOnConsecutiveCalls( + $this->mappingStorage, + $this->mappedObjectStorage + ); // Mock config. $prophecy = $this->prophesize(Config::CLASS); @@ -150,7 +147,6 @@ class PushQueueTest extends UnitTestCase { * @covers ::processQueues */ public function testProcessQueue() { - $items = [1, 2, 3]; $mapping1 = $this->getMockBuilder(SalesforceMappingInterface::CLASS)->getMock(); $mapping1->expects($this->any()) ->method('getNextPushTime') @@ -162,7 +158,7 @@ class PushQueueTest extends UnitTestCase { $mapping1->push_retries = 1; $this->worker = $this->getMockBuilder(PushQueueProcessorInterface::class)->getMock(); - $this->worker->expects($this->once()) + $this->worker->expects($this->any()) ->method('process') ->willReturn(NULL); $this->push_queue_processor_plugin_manager->expects($this->once()) @@ -192,10 +188,11 @@ class PushQueueTest extends UnitTestCase { $this->queue->expects($this->any()) ->method('garbageCollection') ->willReturn(NULL); - // I don't know why at(2) works. - $this->queue->expects($this->at(2)) + $this->queue->expects($this->exactly(4)) ->method('claimItems') - ->willReturn($items); + ->willReturnOnConsecutiveCalls( + [1], [2], [3], [] + ); $this->assertEquals(3, $this->queue->processQueue($mapping1)); diff --git a/tests/src/Unit/RestClientTest.php b/tests/src/Unit/RestClientTest.php index 606e028154d624fca9b43b51a702439d76b65741..2eff70c3b5a6db8014ee5d01a05117151e4bf0f0 100644 --- a/tests/src/Unit/RestClientTest.php +++ b/tests/src/Unit/RestClientTest.php @@ -195,12 +195,12 @@ class RestClientTest extends UnitTestCase { $response_200 = new GuzzleResponse(200); // @TODO this is extremely brittle, exposes complexity in underlying client. Refactor this. - $this->client->expects($this->at(0)) + $this->client->expects($this->exactly(2)) ->method('httpRequest') - ->willReturn($response_401); - $this->client->expects($this->at(1)) - ->method('httpRequest') - ->willReturn($response_200); + ->willReturnOnConsecutiveCalls( + $response_401, + $response_200 + ); $this->client->apiCall(''); } @@ -208,41 +208,49 @@ class RestClientTest extends UnitTestCase { /** * @covers ::objects */ - public function testObjects() { + public function testObjectsFromCache() { $this->initClient(array_merge($this->methods, ['apiCall'])); $objects = [ 'sobjects' => [ 'Test' => [ 'name' => 'Test', 'updateable' => TRUE, - ], - 'NonUpdateable' => [ - 'name' => 'NonUpdateable', - 'updateable' => FALSE, - ], + ] ], ]; $cache = (object) [ 'created' => time(), 'data' => $objects, ]; - unset($cache->data['sobjects']['NonUpdateable']); - $this->cache->expects($this->at(0)) + $this->cache->expects($this->once()) ->method('get') ->willReturn($cache); - $this->cache->expects($this->at(1)) + + // Get objects from cache: + $this->assertEquals($cache->data['sobjects'], $this->client->objects()); + + } + + public function testObjectsFromApiCall() { + $this->initClient(array_merge($this->methods, ['apiCall'])); + $objects = [ + 'sobjects' => [ + 'Test' => [ + 'name' => 'Test', + 'updateable' => TRUE, + ], + ], + ]; + $this->cache->expects($this->once()) ->method('get') ->willReturn(FALSE); $this->client->expects($this->once()) ->method('apiCall') ->willReturn($objects); - // First call, from cache: - $this->assertEquals($cache->data['sobjects'], $this->client->objects()); - - // Second call, from apiCall() - $this->assertEquals($cache->data['sobjects'], $this->client->objects()); + // Get objects from apiCall() + $this->assertEquals($objects['sobjects'], $this->client->objects()); } /** @@ -368,13 +376,12 @@ class RestClientTest extends UnitTestCase { 'id' => $this->salesforce_id, 'attributes' => ['type' => 'dummy'], ]); - $this->client->expects($this->at(0)) + $this->client->expects($this->exactly(2)) ->method('apiCall') - ->willReturn($createResponse); - - $this->client->expects($this->at(1)) - ->method('apiCall') - ->willReturn($updateResponse); + ->willReturnOnConsecutiveCalls( + $createResponse, + $updateResponse + ); $this->client->expects($this->once()) ->method('objectReadbyExternalId') @@ -434,36 +441,53 @@ class RestClientTest extends UnitTestCase { /** * @covers ::objectDelete + * + * 3 tests for objectDelete: + * 1. test that a successful delete returns null + * 2. test that a 404 response gets eaten + * 3. test that any other error response percolates. */ - public function testObjectDelete() { + public function testObjectDeleteSuccess() { $this->initClient(array_merge($this->methods, [ 'apiCall', ])); - // 3 tests for objectDelete: - // 1. test that a successful delete returns null - // 2. test that a 404 response gets eaten - // 3. test that any other error response percolates. - $this->client->expects($this->exactly(3)) - ->method('apiCall'); - - $this->client->expects($this->at(0)) + $this->client->expects($this->once()) ->method('apiCall') ->willReturn(NULL); + $this->assertNull($this->client->objectDelete('', '')); + } + + /** + * @covers ::objectDelete + */ + public function testObjectDelete404() { + $this->initClient(array_merge($this->methods, [ + 'apiCall', + ])); + $exception404 = new RequestException('', new GuzzleRequest('GET', ''), new GuzzleResponse(404, [], '')); - $this->client->expects($this->at(1)) + $this->client->expects($this->once()) ->method('apiCall') ->will($this->throwException($exception404)); + $this->assertNull($this->client->objectDelete('', '')); + } + + /** + * @covers ::objectDelete + */ + public function testObjectDeleteException() { + $this->initClient(array_merge($this->methods, [ + 'apiCall', + ])); + // Test the objectDelete throws any other exception. $exceptionOther = new RequestException('', new GuzzleRequest('GET', ''), new GuzzleResponse(456, [], '')); - $this->client->expects($this->at(2)) + $this->client->expects($this->once()) ->method('apiCall') ->will($this->throwException($exceptionOther)); - - $this->assertNull($this->client->objectDelete('', '')); - $this->assertNull($this->client->objectDelete('', '')); $this->expectException(RequestException::class); $this->client->objectDelete('', ''); } @@ -488,7 +512,7 @@ class RestClientTest extends UnitTestCase { /** * @covers ::getRecordTypes */ - public function testGetRecordTypes() { + public function testGetRecordTypesAll() { $this->initClient(array_merge($this->methods, ['query'])); $sObjectType = $this->randomMachineName(); $developerName = $this->randomMachineName(); @@ -514,29 +538,98 @@ class RestClientTest extends UnitTestCase { new SObject($rawQueryResult['records'][0]), ], ]; - $cache = (object) [ - 'created' => time(), - 'data' => $recordTypes, - ]; - $this->cache->expects($this->at(1)) + $this->cache->expects($this->once()) ->method('get') ->willReturn(FALSE); - $this->cache->expects($this->at(2)) - ->method('get') - ->willReturn($cache); - $this->cache->expects($this->at(3)) - ->method('get') - ->willReturn($cache); $this->client->expects($this->once()) ->method('query') ->willReturn(new SelectQueryResult($rawQueryResult)); $this->assertEquals($recordTypes, $this->client->getRecordTypes()); + } + + /** + * @covers ::getRecordTypes + */ + public function testGetRecordTypesSingle() { + $this->initClient(array_merge($this->methods, ['query'])); + $sObjectType = $this->randomMachineName(); + $developerName = $this->randomMachineName(); + + $rawQueryResult = [ + 'totalSize' => 1, + 'done' => TRUE, + 'records' => [ + 0 => [ + 'attributes' => [ + 'type' => 'Foo', + 'url' => 'Bar', + ], + 'SobjectType' => $sObjectType, + 'DeveloperName' => $developerName, + 'Id' => $this->salesforce_id, + ], + ], + ]; + $recordTypes = [ + $sObjectType => [ + $developerName => + new SObject($rawQueryResult['records'][0]), + ], + ]; + $cache = (object) [ + 'created' => time(), + 'data' => $recordTypes, + ]; + + $this->cache->expects($this->once()) + ->method('get') + ->willReturn($cache); $this->assertEquals($recordTypes[$sObjectType], $this->client->getRecordTypes($sObjectType)); + } + + /** + * @covers ::getRecordTypes + */ + public function testGetRecordTypesNonexistent() { + $this->initClient(array_merge($this->methods, ['query'])); + $sObjectType = $this->randomMachineName(); + $developerName = $this->randomMachineName(); + + $rawQueryResult = [ + 'totalSize' => 1, + 'done' => TRUE, + 'records' => [ + 0 => [ + 'attributes' => [ + 'type' => 'Foo', + 'url' => 'Bar', + ], + 'SobjectType' => $sObjectType, + 'DeveloperName' => $developerName, + 'Id' => $this->salesforce_id, + ], + ], + ]; + $recordTypes = [ + $sObjectType => [ + $developerName => + new SObject($rawQueryResult['records'][0]), + ], + ]; + $cache = (object) [ + 'created' => time(), + 'data' => $recordTypes, + ]; + + $this->cache->expects($this->once()) + ->method('get') + ->willReturn($cache); $this->assertFalse($this->client->getRecordTypes('fail')); } + }