<?php

/**
 * @file
 * Location saving test.
 */

require_once drupal_get_path('module', 'location') . '/tests/location_testcase.php';
require_once drupal_get_path('module', 'location') . '/tests/geocoder_api_keys.inc';

class LocationGoogleGeocoderTest extends LocationTestCase {
  function getInfo() {
    return array(
      'name' => t('Location Google Geocoder tests'),
      'description' => t('Test address mangling for the google geocoder.'),
      'group' => t('Location'),
    );
  }

  function setUp() {
    parent::setUp('location', 'location_node', 'devel');
    variable_set('location_geocode_google_apikey', TESTING_APIKEY_GOOGLE_MAPS);
    $web_admin = $this->drupalCreateUser(array('administer nodes', 'submit latitude/longitude', 'administer site configuration', 'access administration pages', 'administer content types'));
    $this->drupalLogin($web_admin);
  }

  function testUSA() {
    // Initialize the geocoder.
    $settings = array(
      'location_geocode_us' => 'google',
    );
    $this->drupalPost('admin/settings/location/geocoding', $settings, 'Save configuration');
    $this->refreshVariables();

    $settings = array();
    $location_type = $this->addLocationContentType($settings);

    $location1_name = $this->randomName();

    $node = $this->drupalCreateNode(array(
      'type' => $location_type,
      'locations' => array(
        0 => array(
          'name' => $location1_name,
          'location_settings' => $settings,
          'street' => '1600 Amphitheatre Parkway',
          'city' => 'Mountain View',
          'province' => 'CA',
          'postal_code' => '94043',
          'country' => 'us',
        ),
      ),
    ));

    // Reload the node.
    $node2 = node_load($node->nid, NULL, TRUE);

    $location = $node2->locations[0];

    $this->assertEqual($location['source'], LOCATION_LATLON_GEOCODED_EXACT);

    $expected = array(37.421972, -122.084143);
    $result = array($location['latitude'], $location['longitude']);
    $this->assertArrayEpsilon($result, $expected, 0.01, 'Google Headquarters');

  }

}
