diff --git a/modules/salesforce_encrypt/src/Rest/RestClient.php b/modules/salesforce_encrypt/src/Rest/RestClient.php index 1e5cd1df91cb5c8a45ae8caff4cba6cd11083240..cd2bb8b9fd896daa3f609a7a1c61c7c6b84ab533 100644 --- a/modules/salesforce_encrypt/src/Rest/RestClient.php +++ b/modules/salesforce_encrypt/src/Rest/RestClient.php @@ -16,6 +16,7 @@ use Drupal\encrypt\EncryptionProfileManagerInterface; use Drupal\salesforce\EntityNotFoundException; use Drupal\salesforce\Rest\RestClient as SalesforceRestClient; use GuzzleHttp\ClientInterface; +use Drupal\Component\Datetime\TimeInterface; /** * Objects, properties, and methods to communicate with the Salesforce REST API. @@ -48,8 +49,8 @@ class RestClient extends SalesforceRestClient implements EncryptedRestClientInte * @param \Drupal\Core\Lock\LockBackendInterface $lock * The lock backend service. */ - public function __construct(ClientInterface $http_client, ConfigFactoryInterface $config_factory, StateInterface $state, CacheBackendInterface $cache, Json $json, EncryptServiceInterface $encryption, EncryptionProfileManagerInterface $encryptionProfileManager, LockBackendInterface $lock) { - parent::__construct($http_client, $config_factory, $state, $cache, $json); + public function __construct(ClientInterface $http_client, ConfigFactoryInterface $config_factory, StateInterface $state, CacheBackendInterface $cache, Json $json, TimeInterface $time, EncryptServiceInterface $encryption, EncryptionProfileManagerInterface $encryptionProfileManager, LockBackendInterface $lock) { + parent::__construct($http_client, $config_factory, $state, $cache, $json, $time); $this->encryption = $encryption; $this->encryptionProfileId = $state->get('salesforce_encrypt.profile'); $this->encryptionProfileManager = $encryptionProfileManager; diff --git a/modules/salesforce_encrypt/tests/src/Unit/RestClientTest.php b/modules/salesforce_encrypt/tests/src/Unit/RestClientTest.php index c4c6cdfd251f9b8f9ac9993928a1ef19ae199e85..bb5d8b83227a5a3e9e766624a6a1175970da9502 100644 --- a/modules/salesforce_encrypt/tests/src/Unit/RestClientTest.php +++ b/modules/salesforce_encrypt/tests/src/Unit/RestClientTest.php @@ -13,6 +13,7 @@ use Drupal\encrypt\EncryptionProfileInterface; use Drupal\encrypt\EncryptionProfileManagerInterface; use Drupal\salesforce_encrypt\Rest\RestClient; use GuzzleHttp\Client; +use Drupal\Component\Datetime\TimeInterface; /** * @coversDefaultClass \Drupal\salesforce_encrypt\Rest\RestClient @@ -44,7 +45,8 @@ class RestClientTest extends UnitTestCase { $this->lock = $this->getMock(LockBackendInterface::CLASS); $this->encryptionProfile = $this->getMock(EncryptionProfileInterface::CLASS); $this->json = $this->getMock(Json::CLASS); - $this->client = $this->getMock(RestClient::CLASS, ['getEncryptionProfile'], [$this->httpClient, $this->configFactory, $this->state, $this->cache, $this->json, $this->encryption, $this->profileManager, $this->lock]); + $this->time = $this->getMock(TimeInterface::CLASS); + $this->client = $this->getMock(RestClient::CLASS, ['getEncryptionProfile'], [$this->httpClient, $this->configFactory, $this->state, $this->cache, $this->json, $this->time, $this->encryption, $this->profileManager, $this->lock]); } /** diff --git a/modules/salesforce_mapping/salesforce_mapping.module b/modules/salesforce_mapping/salesforce_mapping.module index d39ae5dea511e3b6435a8f9f65f66040ead8961a..96160cb6fc787345d9a2318a7e2ff7adeb8dc8c5 100644 --- a/modules/salesforce_mapping/salesforce_mapping.module +++ b/modules/salesforce_mapping/salesforce_mapping.module @@ -91,7 +91,7 @@ function salesforce_mapping_entity_update(EntityInterface $entity) { // Resaving the object should update the timestamp. // NB: we are purposefully not creating a new revision here. $mapped_object - ->set('entity_updated', REQUEST_TIME) + ->set('entity_updated', \Drupal::time()->getRequestTime()) ->save(); } } diff --git a/modules/salesforce_mapping/src/Entity/MappedObject.php b/modules/salesforce_mapping/src/Entity/MappedObject.php index 0dac156cf5d56950085284a8748f1a27d651b1d3..95be1d1af176cd7a014b7fe833152563bb65b9aa 100644 --- a/modules/salesforce_mapping/src/Entity/MappedObject.php +++ b/modules/salesforce_mapping/src/Entity/MappedObject.php @@ -102,9 +102,9 @@ class MappedObject extends RevisionableContentEntityBase implements MappedObject * {@inheritdoc} */ public function save() { - $this->changed = REQUEST_TIME; + $this->changed = $this->getRequestTime(); if ($this->isNew()) { - $this->created = REQUEST_TIME; + $this->created = $this->getRequestTime(); } return parent::save(); } @@ -553,14 +553,11 @@ class MappedObject extends RevisionableContentEntityBase implements MappedObject /** * Testable func to return the request time server variable. * - * @return string REQUEST_TIME - * The request time contanct. + * @return int REQUEST_TIME + * The request time. */ protected function getRequestTime() { - // @TODO Replace this with a better implementation when available. - // see https://www.drupal.org/node/2820345, - // https://www.drupal.org/node/2785211 - return defined('REQUEST_TIME') ? REQUEST_TIME : (int) $_SERVER['REQUEST_TIME']; + return \Drupal::time()->getRequestTime(); } /** diff --git a/modules/salesforce_mapping/src/Entity/SalesforceMapping.php b/modules/salesforce_mapping/src/Entity/SalesforceMapping.php index 7ba64dc4e7dd1744ca2df838b0d953d56ca856e1..0316b8180b301b7a3529af39aa584ab5f03688f9 100644 --- a/modules/salesforce_mapping/src/Entity/SalesforceMapping.php +++ b/modules/salesforce_mapping/src/Entity/SalesforceMapping.php @@ -257,13 +257,23 @@ class SalesforceMapping extends ConfigEntityBase implements SalesforceMappingInt * The newly saved version of the entity. */ public function save() { - $this->updated = REQUEST_TIME; + $this->updated = $this->getRequestTime(); if (isset($this->is_new) && $this->is_new) { - $this->created = REQUEST_TIME; + $this->created = $this->getRequestTime(); } return parent::save(); } + /** + * Testable func to return the request time server variable. + * + * @return int REQUEST_TIME + * The request time. + */ + protected function getRequestTime() { + return \Drupal::time()->getRequestTime(); + } + /** * {@inheritdoc} */ diff --git a/modules/salesforce_mapping/tests/src/Unit/MappedObjectTest.php b/modules/salesforce_mapping/tests/src/Unit/MappedObjectTest.php index f1986011bb8ea5905467940e43616c5b93de5f5c..ec5cdbdc57aee1fd37f7f0c96339dcb58b61a32c 100644 --- a/modules/salesforce_mapping/tests/src/Unit/MappedObjectTest.php +++ b/modules/salesforce_mapping/tests/src/Unit/MappedObjectTest.php @@ -13,6 +13,7 @@ use Drupal\salesforce\SFID; use Drupal\salesforce\SObject; use Drupal\Tests\UnitTestCase; use Psr\Log\LogLevel; +use Drupal\Component\Datetime\TimeInterface; /** * Test Mapped Object instantitation @@ -104,11 +105,14 @@ class MappedObjectTest extends UnitTestCase { ->will($this->returnValue( $this->getMock('Drupal\Core\Field\FieldItemListInterface'))); + $this->time = $this->getMock(TimeInterface::CLASS); + $container = new ContainerBuilder(); $container->set('entity.manager', $this->entityManager); $container->set('salesforce.client', $this->client); $container->set('event_dispatcher', $this->event_dispatcher); $container->set('plugin.manager.field.field_type', $this->fieldTypePluginManager); + $container->set('datetime.time', $this->time); \Drupal::setContainer($container); $this->entity = $this->getMock('\Drupal\Core\Entity\ContentEntityInterface'); diff --git a/modules/salesforce_pull/src/DeleteHandler.php b/modules/salesforce_pull/src/DeleteHandler.php index 466e2d0d13015fa29bb0d331124171b5f3ec283b..3c10da061aebd8c571de03fe775cf6b0cfeadc59 100644 --- a/modules/salesforce_pull/src/DeleteHandler.php +++ b/modules/salesforce_pull/src/DeleteHandler.php @@ -13,7 +13,7 @@ use Drupal\salesforce\SFID; use Drupal\salesforce_mapping\MappedObjectStorage; use Drupal\salesforce_mapping\MappingConstants; use Symfony\Component\EventDispatcher\EventDispatcherInterface; -use Symfony\Component\HttpFoundation\RequestStack; +use Drupal\Component\Datetime\TimeInterface; /** * Handles pull cron deletion of Drupal entities based onSF mapping settings. @@ -60,9 +60,9 @@ class DeleteHandler { /** * Request service. * - * @var \Symfony\Component\HttpFoundation\Request + * @var \Drupal\Component\Datetime\TimeInterface */ - protected $request; + protected $time; protected $eventDispatcher; @@ -76,14 +76,13 @@ class DeleteHandler { * @param \Drupal\Core\State\StateInterface $state * State service. */ - public function __construct(RestClientInterface $sfapi, EntityTypeManagerInterface $entity_type_manager, StateInterface $state, EventDispatcherInterface $event_dispatcher, RequestStack $request_stack) { + public function __construct(RestClientInterface $sfapi, EntityTypeManagerInterface $entity_type_manager, StateInterface $state, EventDispatcherInterface $event_dispatcher) { $this->sfapi = $sfapi; $this->etm = $entity_type_manager; $this->mappingStorage = $this->etm->getStorage('salesforce_mapping'); $this->mappedObjectStorage = $this->etm->getStorage('salesforce_mapped_object'); $this->state = $state; $this->eventDispatcher = $event_dispatcher; - $this->request = $request_stack->getCurrentRequest(); } /** diff --git a/modules/salesforce_pull/src/QueueHandler.php b/modules/salesforce_pull/src/QueueHandler.php index c45e50da3f3ab17820c8956ca52d4f87d35a70c7..b170185425b7f8e9d75fecd606705582ccd49503 100644 --- a/modules/salesforce_pull/src/QueueHandler.php +++ b/modules/salesforce_pull/src/QueueHandler.php @@ -16,7 +16,7 @@ use Drupal\salesforce\SelectQueryResult; use Drupal\salesforce_mapping\Entity\SalesforceMappingInterface; use Drupal\salesforce_mapping\Event\SalesforceQueryEvent; use Symfony\Component\EventDispatcher\EventDispatcherInterface; -use Symfony\Component\HttpFoundation\RequestStack; +use Drupal\Component\Datetime\TimeInterface; /** * Handles pull cron queue set up. @@ -62,15 +62,14 @@ class QueueHandler { * @param QueueInterface $queue * @param StateInterface $state * @param EventDispatcherInterface $event_dispatcher - * @param RequestStack $request_stack */ - public function __construct(RestClientInterface $sfapi, EntityTypeManagerInterface $entity_type_manager, QueueDatabaseFactory $queue_factory, StateInterface $state, EventDispatcherInterface $event_dispatcher, RequestStack $request_stack) { + public function __construct(RestClientInterface $sfapi, EntityTypeManagerInterface $entity_type_manager, QueueDatabaseFactory $queue_factory, StateInterface $state, EventDispatcherInterface $event_dispatcher, TimeInterface $time) { $this->sfapi = $sfapi; $this->queue = $queue_factory->get('cron_salesforce_pull'); $this->state = $state; $this->eventDispatcher = $event_dispatcher; - $this->request = $request_stack->getCurrentRequest(); + $this->time = $time; $this->mappings = $entity_type_manager ->getStorage('salesforce_mapping') ->loadPullMappings(); @@ -100,7 +99,7 @@ class QueueHandler { // Iterate over each field mapping to determine our query parameters. foreach ($this->mappings as $mapping) { - if ($mapping->getNextPullTime() > $this->request->server->get('REQUEST_TIME')) { + if ($mapping->getNextPullTime() > $this->time->getRequestTime()) { // Skip this mapping, based on pull frequency. continue; } @@ -109,7 +108,7 @@ class QueueHandler { $this->enqueueAllResults($mapping, $results); // @TODO Replace this with a better implementation when available, // see https://www.drupal.org/node/2820345, https://www.drupal.org/node/2785211 - $mapping->setLastPullTime($this->request->server->get('REQUEST_TIME')); + $mapping->setLastPullTime($this->time->getRequestTime()); } } return TRUE; diff --git a/modules/salesforce_pull/tests/src/Unit/DeleteHandlerTest.php b/modules/salesforce_pull/tests/src/Unit/DeleteHandlerTest.php index 348e349569292ebb989b0f743b83422607df0b9c..a44db437bb5e035dd7b759258fb4d6347c321d49 100644 --- a/modules/salesforce_pull/tests/src/Unit/DeleteHandlerTest.php +++ b/modules/salesforce_pull/tests/src/Unit/DeleteHandlerTest.php @@ -15,8 +15,6 @@ use Drupal\salesforce_pull\DeleteHandler; use Drupal\salesforce\Rest\RestClientInterface; use Drupal\Tests\UnitTestCase; use Prophecy\Argument; -use Symfony\Component\HttpFoundation\RequestStack; -use Symfony\Component\HttpFoundation\ServerBag; /** * Test Object instantitation. @@ -141,22 +139,11 @@ class DeleteHandlerTest extends UnitTestCase { $prophecy->dispatch(Argument::any(), Argument::any())->willReturn(); $this->ed = $prophecy->reveal(); - // Mock server. - $prophecy = $this->prophesize(ServerBag::CLASS); - $prophecy->get(Argument::any())->willReturn('1485787434'); - $this->server = $prophecy->reveal(); - - // Mock request. - $prophecy = $this->prophesize(RequestStack::CLASS); - $prophecy->server = $this->server; - $this->request_stack = $prophecy->reveal(); - $this->dh = new DeleteHandler( $this->sfapi, $this->etm, $this->state, - $this->ed, - $this->request_stack + $this->ed ); } diff --git a/modules/salesforce_pull/tests/src/Unit/QueueHandlerTest.php b/modules/salesforce_pull/tests/src/Unit/QueueHandlerTest.php index 9e6942c5684da7205847c151b3a76b3fb755d18a..2122a8fd304c02892eac70bda625ba480ff1c172 100644 --- a/modules/salesforce_pull/tests/src/Unit/QueueHandlerTest.php +++ b/modules/salesforce_pull/tests/src/Unit/QueueHandlerTest.php @@ -13,9 +13,7 @@ use Drupal\salesforce\Rest\RestClientInterface; use Drupal\salesforce\SelectQueryResult; use Drupal\Tests\UnitTestCase; use Prophecy\Argument; -use Symfony\Component\HttpFoundation\Request; -use Symfony\Component\HttpFoundation\RequestStack; -use Symfony\Component\HttpFoundation\ServerBag; +use Drupal\Component\Datetime\TimeInterface; /** * Test Object instantitation @@ -96,23 +94,11 @@ class QueueHandlerTest extends UnitTestCase { $prophecy->dispatch(Argument::any(), Argument::any())->willReturn(); $this->ed = $prophecy->reveal(); - // mock server - $prophecy = $this->prophesize(ServerBag::CLASS); - $prophecy->get(Argument::any())->willReturn('1485787434'); - $this->server = $prophecy->reveal(); - - // mock request - $request = $this->prophesize(Request::CLASS); - $request->server = $this->server; - - // mock request stack - $prophecy = $this->prophesize(RequestStack::CLASS); - $prophecy->getCurrentRequest()->willReturn($request->reveal()); - $this->request_stack = $prophecy->reveal(); + $this->time = $this->getMock(TimeInterface::CLASS); $this->qh = $this->getMockBuilder(QueueHandler::CLASS) ->setMethods(['parseUrl']) - ->setConstructorArgs([$this->sfapi, $this->etm, $this->queue_factory, $this->state, $this->ed, $this->request_stack]) + ->setConstructorArgs([$this->sfapi, $this->etm, $this->queue_factory, $this->state, $this->ed, $this->time]) ->getMock(); $this->qh->expects($this->any()) ->method('parseUrl') @@ -150,7 +136,7 @@ class QueueHandlerTest extends UnitTestCase { $this->qh = $this->getMockBuilder(QueueHandler::CLASS) ->setMethods(['parseUrl']) - ->setConstructorArgs([$this->sfapi, $this->etm, $this->queue_factory, $this->state, $this->ed, $this->request_stack]) + ->setConstructorArgs([$this->sfapi, $this->etm, $this->queue_factory, $this->state, $this->ed, $this->time]) ->getMock(); $this->qh->expects($this->any()) ->method('parseUrl') diff --git a/modules/salesforce_push/salesforce_push.install b/modules/salesforce_push/salesforce_push.install index 8f442a57fca1350c8aadc67a509db92e282dba55..ac13c0bc2d35d41a983f6c882a4a15f3acc6c92a 100644 --- a/modules/salesforce_push/salesforce_push.install +++ b/modules/salesforce_push/salesforce_push.install @@ -7,7 +7,7 @@ use Drupal\salesforce_push\PushQueue; */ function salesforce_push_install() { \Drupal::state()->set('salesforce.mapping_push_limit', PushQueue::MAPPING_CRON_PUSH_LIMIT); - \Drupal::state()->set('salesforce.global_push_limit', PushQueue::GLOBAL_CRON_PUSH_LIMIT); + \Drupal::state()->set('salesforce.global_push_limit', PushQueue::DEFAULT_GLOBAL_LIMIT); \Drupal::state()->set('salesforce.push_queue_processor', PushQueue::DEFAULT_QUEUE_PROCESSOR); \Drupal::state()->set('salesforce.push_queue_max_fails', PushQueue::DEFAULT_MAX_FAILS); } @@ -41,7 +41,7 @@ function salesforce_push_update_1() { * Create new variables for more granualar push limits. */ function salesforce_push_update_2() { - \Drupal::state()->set('salesforce.global_push_limit', PushQueue::GLOBAL_CRON_PUSH_LIMIT); + \Drupal::state()->set('salesforce.global_push_limit', PushQueue::DEFAULT_GLOBAL_LIMIT); \Drupal::state()->delete('salesforce.push_limit'); } \ No newline at end of file diff --git a/modules/salesforce_push/salesforce_push.services.yml b/modules/salesforce_push/salesforce_push.services.yml index d9311e73a1c883fd19f62bcb07dac31b820a279b..4fa9be22bdf57a11c21f377f4c89ac1874fbf00c 100644 --- a/modules/salesforce_push/salesforce_push.services.yml +++ b/modules/salesforce_push/salesforce_push.services.yml @@ -5,4 +5,4 @@ services: queue.salesforce_push: class: Drupal\salesforce_push\PushQueue - arguments: ['@database', '@state', '@plugin.manager.salesforce_push_queue_processor', '@entity_type.manager', '@event_dispatcher', '@request_stack'] + arguments: ['@database', '@state', '@plugin.manager.salesforce_push_queue_processor', '@entity_type.manager', '@event_dispatcher', '@datetime.time'] diff --git a/modules/salesforce_push/src/PushQueue.php b/modules/salesforce_push/src/PushQueue.php index 461419f2e5f02ae2c57c4a9b79452bd525ab9fa4..0b7edb2d58c861c5808209e088a5f6208dd0e797 100644 --- a/modules/salesforce_push/src/PushQueue.php +++ b/modules/salesforce_push/src/PushQueue.php @@ -15,7 +15,7 @@ use Drupal\salesforce\Event\SalesforceErrorEvent; use Drupal\salesforce\Event\SalesforceNoticeEvent; use Symfony\Component\EventDispatcher\EventDispatcherInterface; use Drupal\salesforce\Event\SalesforceEvents; -use Symfony\Component\HttpFoundation\RequestStack; +use Drupal\Component\Datetime\TimeInterface; use Symfony\Component\DependencyInjection\ContainerInterface; /** @@ -30,7 +30,7 @@ class PushQueue extends DatabaseQueue { */ const TABLE_NAME = 'salesforce_push_queue'; - const GLOBAL_CRON_PUSH_LIMIT = 10000; + const DEFAULT_GLOBAL_LIMIT = 10000; const DEFAULT_QUEUE_PROCESSOR = 'rest'; @@ -62,9 +62,9 @@ class PushQueue extends DatabaseQueue { protected $mapped_object_storage; /** - * @var \Symfony\Component\HttpFoundation\Request + * @var \Drupal\Component\Datetime\TimeInterface */ - protected $request; + protected $time; /** * Constructs a \Drupal\Core\Queue\DatabaseQueue object. @@ -72,7 +72,7 @@ class PushQueue extends DatabaseQueue { * @param \Drupal\Core\Database\Connection $connection * The Connection object containing the key-value tables. */ - public function __construct(Connection $connection, StateInterface $state, PushQueueProcessorPluginManager $queue_manager, EntityTypeManagerInterface $entity_manager, EventDispatcherInterface $event_dispatcher, RequestStack $request_stack) { + public function __construct(Connection $connection, StateInterface $state, PushQueueProcessorPluginManager $queue_manager, EntityTypeManagerInterface $entity_manager, EventDispatcherInterface $event_dispatcher, TimeInterface $time) { $this->connection = $connection; $this->state = $state; $this->queueManager = $queue_manager; @@ -80,9 +80,9 @@ class PushQueue extends DatabaseQueue { $this->mapping_storage = $entity_manager->getStorage('salesforce_mapping'); $this->mapped_object_storage = $entity_manager->getStorage('salesforce_mapped_object'); $this->eventDispatcher = $event_dispatcher; - $this->request = $request_stack->getCurrentRequest(); + $this->time = $time; - $this->global_limit = $state->get('salesforce.global_push_limit', static::GLOBAL_CRON_PUSH_LIMIT); + $this->global_limit = $state->get('salesforce.global_push_limit', static::DEFAULT_GLOBAL_LIMIT); $this->max_fails = $state->get('salesforce.push_queue_max_fails', static::DEFAULT_MAX_FAILS); } @@ -93,7 +93,7 @@ class PushQueue extends DatabaseQueue { $container->get('plugin.manager.salesforce_push_queue_processor'), $container->get('entity_type.manager'), $container->get('event_dispatcher'), - $container->get('request_stack') + $container->get('datetime.time') ); } @@ -136,7 +136,7 @@ class PushQueue extends DatabaseQueue { throw new \Exception('Salesforce push queue data values are required for "name", "entity_id" and "op"'); } $this->name = $data['name']; - $time = $this->request->server->get('REQUEST_TIME'); + $time = $this->time->getRequestTime(); $fields = [ 'name' => $this->name, 'entity_id' => $data['entity_id'], @@ -193,7 +193,7 @@ class PushQueue extends DatabaseQueue { // should really expire. $update = $this->connection->update(static::TABLE_NAME) ->fields(array( - 'expire' => $this->request->server->get('REQUEST_TIME') + $lease_time, + 'expire' => $this->time->getRequestTime() + $lease_time, )) ->condition('item_id', array_keys($items), 'IN') ->condition('expire', 0); @@ -312,7 +312,7 @@ class PushQueue extends DatabaseQueue { foreach ($mappings as $mapping) { $j = 0; // Check mapping frequency before proceeding. - if ($mapping->getNextPushTime() > $this->request->server->get('REQUEST_TIME')) { + if ($mapping->getNextPushTime() > $this->time->getRequestTime()) { continue; } @@ -326,7 +326,7 @@ class PushQueue extends DatabaseQueue { // Claim as many items as we can from this queue and advance our counter. If this queue is empty, move to the next mapping. $items = $this->claimItems($mapping->push_limit, $mapping->push_retries); if (empty($items)) { - $mapping->setLastPushTime($this->request->server->get('REQUEST_TIME')); + $mapping->setLastPushTime($this->time->getRequestTime()); continue 2; } diff --git a/modules/salesforce_push/tests/src/Unit/PushQueueTest.php b/modules/salesforce_push/tests/src/Unit/PushQueueTest.php index 5d245cf268d377acfbae335dfc7c99a433cd4db0..58714857db72ca9571ec3043299b381c3da09e44 100644 --- a/modules/salesforce_push/tests/src/Unit/PushQueueTest.php +++ b/modules/salesforce_push/tests/src/Unit/PushQueueTest.php @@ -16,11 +16,9 @@ use Symfony\Component\EventDispatcher\EventDispatcherInterface; use Drupal\salesforce_push\PushQueueProcessorPluginManager; use Drupal\Core\Database\StatementInterface; use Drupal\Core\Database\Query\Update; -use Symfony\Component\HttpFoundation\Request; -use Symfony\Component\HttpFoundation\RequestStack; -use Symfony\Component\HttpFoundation\ServerBag; use Drupal\salesforce_mapping\SalesforceMappingStorage; use Drupal\salesforce_push\PushQueueProcessorInterface; +use Drupal\Component\Datetime\TimeInterface; /** * Test Object instantitation. @@ -52,21 +50,7 @@ class PushQueueTest extends UnitTestCase { ->method('dispatch') ->willReturn(NULL); $this->string_translation = $this->getMock(TranslationInterface::class); - $this->request_stack = $this->getMock(RequestStack::class); - - // Mock server. - $this->server = $this->getMock(ServerBag::class); - $this->server->expects($this->any()) - ->method('get') - ->willReturn('1485787434'); - - // Mock request. - $this->request = $this->getMock(Request::CLASS); - $this->request->server = $this->server; - - $this->request_stack->expects($this->any()) - ->method('getCurrentRequest') - ->willReturn($this->request); + $this->time = $this->getMock(TimeInterface::class); $this->mapping_storage = $this->getMockBuilder(SalesforceMappingStorage::CLASS) ->disableOriginalConstructor() @@ -94,7 +78,7 @@ class PushQueueTest extends UnitTestCase { $container->set('string_translation', $this->string_translation); $container->set('entity.manager', $this->entity_manager); $container->set('plugin.manager.salesforce_push_queue_processor', $this->push_queue_processor_plugin_manager); - $container->set('request_stack', $this->request_stack); + $container->set('datetime.time', $this->time); \Drupal::setContainer($container); } @@ -171,7 +155,7 @@ class PushQueueTest extends UnitTestCase { ->method('createInstance') ->willReturn($this->worker); - $this->queue = $this->getMock(PushQueue::class, ['claimItems', 'setName'], [$this->database, $this->state, $this->push_queue_processor_plugin_manager, $this->entityTypeManager, $this->eventDispatcher, $this->request_stack]); + $this->queue = $this->getMock(PushQueue::class, ['claimItems', 'setName'], [$this->database, $this->state, $this->push_queue_processor_plugin_manager, $this->entityTypeManager, $this->eventDispatcher, $this->time]); $this->queue->expects($this->once()) ->method('claimItems') ->willReturn($items); diff --git a/salesforce.services.yml b/salesforce.services.yml index 951ca544bbf9fd44e4f2cf86cf0ad63b4792e9a0..a91e46c9a040f0719e44810ba1e893ccf22a4ed6 100644 --- a/salesforce.services.yml +++ b/salesforce.services.yml @@ -1,4 +1,4 @@ services: salesforce.client: class: Drupal\salesforce\Rest\RestClient - arguments: ['@http_client', '@config.factory', '@state', '@cache.default', '@serialization.json'] + arguments: ['@http_client', '@config.factory', '@state', '@cache.default', '@serialization.json', '@datetime.time'] diff --git a/src/Rest/RestClient.php b/src/Rest/RestClient.php index 760d94192c3c9ca5580d3912e1bfbee55a56c77c..a7b6d477aa4132faa74354562afbd7770dc4607d 100644 --- a/src/Rest/RestClient.php +++ b/src/Rest/RestClient.php @@ -16,6 +16,7 @@ use Drupal\salesforce\SObject; use GuzzleHttp\ClientInterface; use GuzzleHttp\Exception\RequestException; use GuzzleHttp\Psr7\Response; +use Drupal\Component\Datetime\TimeInterface; /** * Objects, properties, and methods to communicate with the Salesforce REST API. @@ -95,13 +96,14 @@ class RestClient implements RestClientInterface { * @param \Drupal\Component\Serialization\Json $json * The JSON serializer service. */ - public function __construct(ClientInterface $http_client, ConfigFactoryInterface $config_factory, StateInterface $state, CacheBackendInterface $cache, Json $json) { + public function __construct(ClientInterface $http_client, ConfigFactoryInterface $config_factory, StateInterface $state, CacheBackendInterface $cache, Json $json, TimeInterface $time) { $this->configFactory = $config_factory; $this->httpClient = $http_client; $this->config = $this->configFactory->getEditable('salesforce.settings'); $this->state = $state; $this->cache = $cache; $this->json = $json; + $this->time = $time; return $this; } @@ -965,7 +967,7 @@ class RestClient implements RestClientInterface { * The REQUEST_TIME server variable. */ protected function getRequestTime() { - return defined('REQUEST_TIME') ? REQUEST_TIME : (int) $_SERVER['REQUEST_TIME']; + return $this->time->getRequestTime(); } } diff --git a/tests/src/Unit/RestClientTest.php b/tests/src/Unit/RestClientTest.php index 035332ba5d087f897015ec6189d610d02628bf44..5248ee910418315680e778c0d2f3c1e30f355e58 100644 --- a/tests/src/Unit/RestClientTest.php +++ b/tests/src/Unit/RestClientTest.php @@ -15,6 +15,7 @@ use Drupal\salesforce\SelectQuery; use GuzzleHttp\Psr7\Response as GuzzleResponse; use GuzzleHttp\Psr7\Request as GuzzleRequest; use GuzzleHttp\Exception\RequestException; +use Drupal\Component\Datetime\TimeInterface; /** * @coversDefaultClass \Drupal\salesforce\Rest\RestClient @@ -51,6 +52,7 @@ class RestClientTest extends UnitTestCase { ->getMock(); $this->cache = $this->getMock('\Drupal\Core\Cache\CacheBackendInterface'); $this->json = $this->getMock(Json::CLASS); + $this->time = $this->getMock(TimeInterface::CLASS); } /** @@ -61,7 +63,7 @@ class RestClientTest extends UnitTestCase { $methods = $this->methods; } - $args = [$this->httpClient, $this->configFactory, $this->state, $this->cache, $this->json]; + $args = [$this->httpClient, $this->configFactory, $this->state, $this->cache, $this->json, $this->time]; $this->client = $this->getMock(RestClient::CLASS, $methods, $args); diff --git a/tests/src/Unit/SalesforceControllerTest.php b/tests/src/Unit/SalesforceControllerTest.php index fc442150870b5f6e54d7823525445b24924ed509..00540cb161d029c3d818f4ed174eebf826ab848d 100644 --- a/tests/src/Unit/SalesforceControllerTest.php +++ b/tests/src/Unit/SalesforceControllerTest.php @@ -13,6 +13,7 @@ use Symfony\Component\DependencyInjection\ContainerBuilder; use Symfony\Component\HttpFoundation\RedirectResponse; use Symfony\Component\HttpFoundation\Request; use Symfony\Component\HttpFoundation\RequestStack; +use Drupal\Component\Datetime\TimeInterface; /** * @coversDefaultClass \Drupal\salesforce\Controller\SalesforceController @@ -42,8 +43,9 @@ class SalesforceControllerTest extends UnitTestCase { ->getMock(); $this->cache = $this->getMock('\Drupal\Core\Cache\CacheBackendInterface'); $this->json = $this->getMock(Json::CLASS); + $this->time = $this->getMock(TimeInterface::CLASS); - $args = [$this->httpClient, $this->configFactory, $this->state, $this->cache, $this->json]; + $args = [$this->httpClient, $this->configFactory, $this->state, $this->cache, $this->json, $this->time]; $this->client = $this->getMock(RestClient::class, ['getConsumerKey', 'getConsumerSecret', 'getAuthCallbackUrl', 'getAuthTokenUrl', 'handleAuthResponse'], $args); $this->client->expects($this->once()) @@ -83,6 +85,7 @@ class SalesforceControllerTest extends UnitTestCase { $container->set('http_client', $this->httpClient); $container->set('request_stack', $this->request_stack); $container->set('url.generator', $this->url_generator->reveal()); + $container->set('datetime.time', $this->time); \Drupal::setContainer($container); }