private function ParserTest::weatherDownloadForecast in Weather 8
Same name and namespace in other branches
- 2.0.x tests/src/Functional/ParserTest.php \Drupal\Tests\weather\Functional\ParserTest::weatherDownloadForecast()
Downloads a new forecast from yr.no.
Parameters
string $geoid: The GeoID for which the forecasts should be downloaded.
Return value
bool TRUE on success, FALSE on failure.
Throws
\ReflectionException
1 call to ParserTest::weatherDownloadForecast()
- ParserTest::weatherGetWeather in tests/src/ Functional/ ParserTest.php 
- Returns a weather object for the specified GeoID.
File
- tests/src/ Functional/ ParserTest.php, line 346 
Class
- ParserTest
- Tests parsing of XML weather forecasts.
Namespace
Drupal\Tests\weather\FunctionalCode
private function weatherDownloadForecast($geoid) {
  // Do not download anything if the variable
  // 'weather_time_for_testing' is set.
  // In this case, we are in testing mode and only load defined
  // forecasts to get always the same results.
  $config = \Drupal::configFactory()
    ->getEditable('weather.settings');
  $time = $config
    ->get('weather_time_for_testing');
  if ($time !== \Drupal::time()
    ->getRequestTime()) {
    $path = dirname((new ReflectionClass(static::class))
      ->getFileName()) . '/data/' . $geoid . '.xml';
    if (is_readable($path)) {
      $xml = file_get_contents($path);
    }
    else {
      $xml = '';
    }
    return $this
      ->weatherParseForecast($xml, $geoid);
  }
  // Specify timeout in seconds.
  $timeout = 10;
  $url = $this
    ->weatherGetLinkForGeoId($geoid, 'yr');
  $response = $this
    ->drupalGet($url, [
    'timeout' => $timeout,
  ]);
  // Extract XML data from the received forecast.
  if (!isset($response->error)) {
    return $this
      ->weatherParseForecast($response->data, $geoid);
  }
  else {
    // Make an entry about this error into the watchdog table.
    \Drupal::logger('weather')
      ->error($response->error);
    // Get the current user.
    $user = \Drupal::currentUser();
    // Check for permission.
    $user
      ->hasPermission('administer site configuration');
    // Show a message to users with administration priviledges.
    if ($user
      ->hasPermission('administer custom weather block') or $user
      ->hasPermission('administer site configuration')) {
      \Drupal::messenger()
        ->addMessage(t('Download of forecast failed: @error', [
        '@error' => $response->error,
      ]), 'error');
    }
  }
}