1 image.test private ImageStyleFloodProtection::assertMultiStyleGenerated($urls, $expected_count)

File

core/modules/image/tests/image.test, line 1810
Tests for image.module.

Class

ImageStyleFloodProtection
Tests the functions for generating paths and URLs for image styles.

Code

private function assertMultiStyleGenerated($urls, $expected_count) {
  $count = count($urls);
  $multi_handle = curl_multi_init();

  for ($n = 0; $n < $count; $n++) {
    ${'handle' . $n}= curl_init();
    $curl_options = array(
      CURLOPT_URL => $urls[$n],
      CURLOPT_FOLLOWLOCATION => FALSE,
      CURLOPT_RETURNTRANSFER => TRUE,
      CURLOPT_HEADER => TRUE,
      CURLOPT_SSL_VERIFYPEER => FALSE,
      CURLOPT_SSL_VERIFYHOST => FALSE,
      CURLOPT_USERAGENT => backdrop_generate_test_ua($this->databasePrefix),
    );
    if (isset($this->httpauth_credentials)) {
      $curl_options[CURLOPT_HTTPAUTH] = $this->httpauth_method;
      $curl_options[CURLOPT_USERPWD] = $this->httpauth_credentials;
    }
    curl_setopt_array(${'handle' . $n}, $curl_options);
    curl_multi_add_handle($multi_handle, ${'handle' . $n});
  }

  $running = NULL;
  do {
    curl_multi_exec($multi_handle, $running);
    curl_multi_select($multi_handle);
  } while ($running > 0);

  // Because we do all requests simultaneously, we don't know which will be
  // served and which will return a 403 when the max is hit.
  $http_200_count = 0;
  $http_403_count = 0;
  for ($n = 0; $n < $count; $n++) {
    $response_code = curl_getinfo(${'handle' . $n}, CURLINFO_HTTP_CODE);
    if ($response_code == 200) {
      $http_200_count++;
    }
    elseif ($response_code == 403) {
      $http_403_count++;
    }
  }

  $this->assertEqual($http_200_count, $expected_count, 'Correct number of images (' . $expected_count . ') generated.');
  $this->assertEqual($http_403_count, $count - $expected_count, 'Correct number of images (' . ($count - $expected_count) . ') return 403 access denied.');

  // Close the handles
  for ($n = 0; $n < $count; $n++) {
    curl_close(${'handle' . $n});
    curl_multi_remove_handle($multi_handle, ${'handle' . $n});
  }
  curl_multi_close($multi_handle);
}