You are here

trait ThunderAwsTestFixtureTrait in Thunder 6.2.x

Same name and namespace in other branches
  1. 8.5 tests/src/Traits/ThunderAwsTestFixtureTrait.php \Drupal\Tests\thunder\Traits\ThunderAwsTestFixtureTrait
  2. 8.2 tests/src/Traits/ThunderAwsTestFixtureTrait.php \Drupal\Tests\thunder\Traits\ThunderAwsTestFixtureTrait
  3. 8.3 tests/src/Traits/ThunderAwsTestFixtureTrait.php \Drupal\Tests\thunder\Traits\ThunderAwsTestFixtureTrait
  4. 8.4 tests/src/Traits/ThunderAwsTestFixtureTrait.php \Drupal\Tests\thunder\Traits\ThunderAwsTestFixtureTrait
  5. 6.0.x tests/src/Traits/ThunderAwsTestFixtureTrait.php \Drupal\Tests\thunder\Traits\ThunderAwsTestFixtureTrait
  6. 6.1.x tests/src/Traits/ThunderAwsTestFixtureTrait.php \Drupal\Tests\thunder\Traits\ThunderAwsTestFixtureTrait

Trait to download test fixtures from AWS.

Objects should be uploaded to the thunder-public bucket and placed in the test_fixtures directory. To enable the SHA1 checking the object should have the "x-amz-meta-sha" metadata value set to the SHA1 of the file. This prevents unnecessary downloads of the file.

Hierarchy

1 file declares its use of ThunderAwsTestFixtureTrait
Thunder2UpdateTest.php in tests/src/Functional/Thunder2UpdateTest.php

File

tests/src/Traits/ThunderAwsTestFixtureTrait.php, line 17

Namespace

Drupal\Tests\thunder\Traits
View source
trait ThunderAwsTestFixtureTrait {

  /**
   * Gets a test fixture from AWS.
   *
   * @param string $filename
   *   The test fixture filename.
   *
   * @return string
   *   The local path to the test fixture.
   */
  protected function getTestFixture($filename) {

    // Statically cache to prevent unnecessary requests.
    static $files = [];
    if (!isset($files[$filename])) {
      $local_dir = sys_get_temp_dir() . '/thunder_test_fixtures';
      @mkdir($local_dir);
      $local = $local_dir . '/' . $filename;
      $remote = 'https://s3-eu-west-1.amazonaws.com/thunder-public-files/test_fixtures/' . $filename;
      $client = $this
        ->getHttpClient();
      if (!file_exists($local) || sha1_file($local) !== $client
        ->head($remote)
        ->getHeaderLine('x-amz-meta-sha')) {
        $client
          ->get($remote, [
          'sink' => $local,
        ]);
      }
      $files[$filename] = $local;
    }
    return $files[$filename];
  }

  /**
   * Creates a HTTP client.
   *
   * Does not use the container because it is not always available in testing.
   *
   * @return \GuzzleHttp\Client
   *   The HTTP client.
   */
  protected function getHttpClient() {
    $default_config = [
      // Security consideration: we must not use the certificate authority
      // file shipped with Guzzle because it can easily get outdated if a
      // certificate authority is hacked. Instead, we rely on the certificate
      // authority file provided by the operating system which is more likely
      // going to be updated in a timely fashion. This overrides the default
      // path to the pem file bundled with Guzzle.
      'verify' => TRUE,
      'timeout' => 30,
      'headers' => [
        'User-Agent' => 'Drupal/' . \Drupal::VERSION . ' (+https://www.drupal.org/) ' . \GuzzleHttp\default_user_agent(),
      ],
      // Security consideration: prevent Guzzle from using environment variables
      // to configure the outbound proxy.
      'proxy' => [
        'http' => NULL,
        'https' => NULL,
        'no' => [],
      ],
    ];
    $config = NestedArray::mergeDeep($default_config, Settings::get('http_client_config', []));
    return new Client($config);
  }

}

Members

Namesort descending Modifiers Type Description Overrides
ThunderAwsTestFixtureTrait::getHttpClient protected function Creates a HTTP client.
ThunderAwsTestFixtureTrait::getTestFixture protected function Gets a test fixture from AWS.