<?php

class MeetupLocationParserTest extends DrupalUnitTestCase {

  public static function getInfo() {
    return array(
      'name' => 'Meetup Location Parser Tests',
      'description' => 'Test different meetup location patterns',
      'group' => 'Groups Feeds',
    );
  }

  public function setUp() {
    $modules = array();
    parent::setUp($modules);
    module_load_include('inc', 'groups_feeds', 'includes/groups_feeds_meetup_events');
  }

  function testMatchLocationUsByPostalCode() {
    $elements = array(
      'WA 98101', 'Seattle', 'door code 99999#', '119 Pine Street #200'
    );
    $result = _match_us_location($elements);
    $this->assertEqual('119 Pine Street #200, door code 99999#', $result['address']);
    $this->assertEqual('Seattle, WA', $result['city']);
    $this->assertEqual('United States', $result['country']);
  }

  function testMatchLocationByCountry() {
    $elements = array(
      'Slovenia', 'Ljubljana', 'Bratislavska cesta 8',
    );
    $result = _match_location_by_country($elements);
    $this->assertEqual('Bratislavska cesta 8', $result['address']);
    $this->assertEqual('Ljubljana', $result['city']);
    $this->assertEqual('Slovenia', $result['country']);
  }

  function testMatchLocationUsByState() {
    $elements = array(
      'TX', 'Austin', 'Suite 250', '8920 Business Park Dr',
    );
    $result = _match_us_location($elements);
    $this->assertEqual('8920 Business Park Dr, Suite 250', $result['address']);
    $this->assertEqual('Austin, TX', $result['city']);
    $this->assertEqual('United States', $result['country']);
  }

  function testNonUSLocation() {
    $location = 'Symbiosis Institute of Computer Studies and Research  (Atur Centre, Gokhale Cross Road, Model Colony, Pune, India)';
    $result = _parse_meetup_location($location);
    $this->assertEqual('Symbiosis Institute of Computer Studies and Research', $result['venue']);
    $this->assertEqual('Atur Centre, Gokhale Cross Road, Model Colony', $result['address']);
    $this->assertEqual('Pune', $result['city']);
    $this->assertEqual('India', $result['country']);
  }

  function testSimpleNonUSLocation() {
    $location = 'Hotel Plaza (Bratislavska cesta 8, Ljubljana, Slovenia)';
    $result = _parse_meetup_location($location);
    $this->assertEqual('Hotel Plaza', $result['venue']);
    $this->assertEqual('Bratislavska cesta 8', $result['address']);
    $this->assertEqual('Ljubljana', $result['city']);
    $this->assertEqual('Slovenia', $result['country']);
  }

  function testNonUSLocationWithBrackets() {
    $location = 'SICS SWEDISH ICT (Isafjordsgatan 22, 164 40 Kista, floor 6 (Elevator A) isafjordsgatan 22, Stockholm, Stockholm, Sweden)';
    $result = _parse_meetup_location($location);
    $this->assertEqual('SICS SWEDISH ICT', $result['venue']);
    $this->assertEqual('Isafjordsgatan 22, 164 40 Kista, floor 6 (Elevator A) isafjordsgatan 22, Stockholm', $result['address']);
    $this->assertEqual('Stockholm', $result['city']);
    $this->assertEqual('Sweden', $result['country']);
  }

  function testSimpleUSLocation() {
    $location = 'Tech Ranch Austin  (8920 Business Park Dr, Suite 250, Austin, TX)';
    $result = _parse_meetup_location($location);
    $this->assertEqual('Tech Ranch Austin', $result['venue']);
    $this->assertEqual('8920 Business Park Dr, Suite 250', $result['address']);
    $this->assertEqual('Austin, TX', $result['city']);
    $this->assertEqual('United States', $result['country']);
  }

  function testPOCodeUSLocation() {
    $location = 'Blue Box Group (119 Pine Street #200, door code 99999#, Seattle, WA 98101)';
    $result = _parse_meetup_location($location);
    $this->assertEqual('Blue Box Group', $result['venue']);
    $this->assertEqual('119 Pine Street #200, door code 99999#', $result['address']);
    $this->assertEqual('Seattle, WA', $result['city']);
    $this->assertEqual('United States', $result['country']);
  }

  function testVenueWithoutAddressPart() {
    $location = 'Blue Box Group (';
    $result = _parse_meetup_location($location);
    $this->assertEqual('Blue Box Group', $result['venue']);
  }

  function testAddressPartWithSlash() {
    $location = 'Redapt Systems (12226 134th Ct Ne\, Redmond\, WA\, USA)';
    $result = _parse_meetup_location($location);
    $this->assertEqual('Redapt Systems', $result['venue']);
    $this->assertEqual('12226 134th Ct Ne', $result['address']);
    $this->assertEqual('Redmond, WA', $result['city']);
    $this->assertEqual('United States', $result['country']);
  }

}