You are here

public function AcquiaSearchApiClientTest::testAcquiaSearchApiCall in Acquia Search 3.x

Tests call to Acquia Search V3 API.

File

tests/src/Unit/AcquiaSearchApiClientTest.php, line 135

Class

AcquiaSearchApiClientTest
@coversDefaultClass \Drupal\acquia_search\AcquiaSearchApiClient @group Acquia Search Solr

Namespace

Drupal\Tests\acquia_search\Unit

Code

public function testAcquiaSearchApiCall() {
  $indexes = [
    'WXYZ-12345' => [
      'balancer' => 'example.com',
      'core_id' => 'WXYZ-12345',
      'data' => [
        'key' => 'WXYZ-12345',
        'secret_key' => 'secret_key',
        'product_policies' => [
          'salt' => 'salt',
        ],
        'host' => 'example.com',
      ],
    ],
    'WXYZ-12345.dev.drupal8' => [
      'balancer' => 'example.com',
      'core_id' => 'WXYZ-12345.dev.drupal8',
      'data' => [
        'key' => 'WXYZ-12345.dev.drupal8',
        'secret_key' => 'secret_key',
        'product_policies' => [
          'salt' => 'salt',
        ],
        'host' => 'example.com',
      ],
    ],
  ];
  $auth_info = [
    'host' => Storage::getApiHost(),
    'app_uuid' => Storage::getUuid(),
    'key' => Storage::getApiKey(),
  ];
  $this
    ->assertEquals('WXYZ-12345', Storage::getIdentifier());
  $client = new AcquiaSearchApiClient($auth_info, $this->guzzleClient
    ->reveal(), $this->cacheBackend
    ->reveal());

  // No Network Id.
  $this
    ->assertFalse($client
    ->getSearchIndexes(''));
  $this->cacheBackend
    ->set()
    ->shouldNotBeCalled();
  $this->cacheBackend
    ->get()
    ->shouldNotBeCalled();

  // Invalid Network Id - cache FALSE for minute.
  $randomId = $this
    ->randomMachineName();
  $this->cacheBackend
    ->get('acquia_search.indexes.' . $randomId)
    ->willReturn();
  $this->cacheBackend
    ->set('acquia_search.indexes.' . $randomId, FALSE, \Drupal::time()
    ->getRequestTime() + 60)
    ->willReturn();
  $this
    ->assertFalse($client
    ->getSearchIndexes($randomId));

  // Valid Network Id - cache it for a day.
  $this->cacheBackend
    ->get('acquia_search.indexes.WXYZ-12345')
    ->willReturn();
  $this->cacheBackend
    ->set('acquia_search.indexes.WXYZ-12345', Argument::any(), \Drupal::time()
    ->getRequestTime() + 86400)
    ->willReturn();
  $this
    ->assertEquals($indexes, $client
    ->getSearchIndexes('WXYZ-12345'));
  $this->cacheBackend
    ->get('acquia_search.indexes.WXYZ-12345')
    ->shouldHaveBeenCalledTimes(1);
  $this->cacheBackend
    ->set('acquia_search.indexes.WXYZ-12345', Argument::any(), \Drupal::time()
    ->getRequestTime() + 86400)
    ->shouldHaveBeenCalledTimes(1);
}