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

Fix query access check deprecations, and some PHPUnit deprecations. See...

Fix query access check deprecations, and some PHPUnit deprecations. See https://www.drupal.org/node/3201242
parent e6575fb3
No related branches found
No related tags found
1 merge request!40Added check for null value.
......@@ -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:
......
......@@ -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);
......
......@@ -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)
......
......@@ -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.
......
......@@ -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);
}
/**
......
......@@ -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);
......
......@@ -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));
......
......@@ -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'));
}
}
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