You are here

public function WeatherCommonTestTrait::weatherGetLinkForGeoId in Weather 2.0.x

Same name and namespace in other branches
  1. 8 tests/src/Functional/WeatherCommonTestTrait.php \Drupal\Tests\weather\Functional\WeatherCommonTestTrait::weatherGetLinkForGeoId()

Construct the link for the given GeoID.

Parameters

string $geoid: The GeoID to construct the link for.

string $destination: Destination to create the link for. Currently supported are 'system-wide', 'default', 'user', 'yr', and 'autocomplete'.

int $number: Optional number of display. Applies only to 'system-wide'.

Return value

string The link for the GeoID.

2 calls to WeatherCommonTestTrait::weatherGetLinkForGeoId()
FunctionsTest::testFunctionWeatherGetLinkForGeoId in tests/src/Functional/FunctionsTest.php
Test _weather_get_link_for_geoid().
ParserTest::weatherDownloadForecast in tests/src/Functional/ParserTest.php
Downloads a new forecast from yr.no.

File

tests/src/Functional/WeatherCommonTestTrait.php, line 184

Class

WeatherCommonTestTrait
Provides a helper method for testing Weather module.

Namespace

Drupal\Tests\weather\Functional

Code

public function weatherGetLinkForGeoId($geoid, $destination, $number = 1) {
  $info = $this
    ->weatherGetInformationAboutGeoid($geoid);

  // Conversion rules for all link parts:
  // - Replace all spaces with an underscore.
  // - If the link part ends with a dot, use an underscore.
  $country = str_replace(' ', '_', $info['country']);
  if (substr($country, -1) == '.') {
    $country[strlen($country) - 1] = '_';
  }
  $link = $country . '/' . $info['link'];
  switch ($destination) {
    case 'system-wide':
      $link = 'weather/' . $link . '/' . $number;
      break;
    case 'default':
      $link = 'weather/' . $link;
      break;
    case 'user':
      $link = 'weather/' . $link . '/u';
      break;
    case 'yr':

      // Encode special characters except the '/' in the URL.
      // Otherwise, the request will fail on yr.no.
      $link = rawurlencode($link);
      $link = str_replace('%2F', '/', $link);
      $link = 'https://www.yr.no/place/' . $link . '/forecast.xml';
      break;
    case 'yr.no':
      $link = 'https://www.yr.no/place/' . $link . '/';
      break;
    case 'autocomplete':

      // Nothing to do here.
      break;
  }
  return $link;
}