<?php

/**
 * @file
 * Tests for location_cck.module.
 */

require_once drupal_get_path('module', 'location') . '/tests/location_testcase.test';

class LocationCCK2Test extends LocationTestCase {
  /**
   * A global administrative user.
   */
  public $admin_user;

  /**
   * A global normal user.
   */
  public $normal_user;

  /**
   * A default content type.
   */
  public $content_type;

  /**
   * A default content name.
   */
  static public $name;

  /**
   * A default content name.
   */
  static public $field_name;

  public static function getInfo() {
    return array(
      'name' => t('Location CCK2 checks'),
      'description' => t('Yet One Test corner cases of the CCK Location module.'),
      'group' => t('Location', array(), array('context' => 'geolocation')),
    );
  }

  public function setUp() {
    parent::setUp('location', 'location_cck', 'field_ui');
    $this->admin_user = $this->drupalCreateUser(array_keys(module_invoke_all('permission')));
    $this->normal_user = $this->drupalCreateUser(array('access content'));
    $this->drupalLogin($this->admin_user);
  }

  protected function getTestContentName() {
    if (!self::$name) {
      $this->addLocationContentType2();
    }

    return self::$name;
  }

  protected function getTestFieldName() {
    if (!self::$field_name) {
      $this->addLocationContentType2();
    }

    return self::$field_name;
  }

  // @todo move this to __construct
  protected function addLocationContentType2(&$settings, $add = array()) {

    if (!self::$name) {
      self::$name = strtolower($this->randomName(8));
      $form = array(
        'name' => self::$name,
        'type' => self::$name,
      );

      // creates content type
      $this->drupalPost('admin/structure/types/add', $form, 'Save content type');
      $this->assertText(t('The content type ' . self::$name . ' has been added'));
      unset($form);

      if (!self::$field_name) {
        self::$field_name = strtolower($this->randomName(8));

        $form = array(
          'fields[_add_new_field][label]' => self::$field_name,
          'fields[_add_new_field][field_name]' => self::$field_name,
          'fields[_add_new_field][type]' => 'location',
          'fields[_add_new_field][widget_type]' => 'location',
        );

        // adds field
        $this->drupalPost('admin/structure/types/manage/' . self::$name . '/fields', $form, t('Save'));
        unset($form);

        // check field defaults
        $form = array();
        $this->drupalPost(NULL, $form, t('Save field settings'));
        unset($form);
        $this->assertText(t('Updated field ' . self::$field_name . ' field settings.'));

        // fills field custom defaults
        $form = array(
          'field_' . self::$field_name . '[und][0][name]' => 'Kyiv',
          'field_' . self::$field_name . '[und][0][country]' => 'ua',
          'field_' . self::$field_name . '[und][0][locpick][user_latitude]' => 50,
          'field_' . self::$field_name . '[und][0][locpick][user_longitude]' => 30,
        );
        $this->drupalPost(NULL, $form, t('Save settings'));
        unset($form);
        $this->assertText(t('Saved ' . self::$field_name . ' configuration.'));

      }
    }
  }

  public function testCCK2() {
    $settings = array();
    $this->addLocationContentType2($settings, array());
    $this->assertTrue($this->getTestContentName(), t('Content type' . $this->getTestContentName() . ' created'));

    // test node creation
    $node2 = $this->drupalCreateNode(
      array(
        'type' => $this->getTestContentName(),
      )
    );
    $fieldname = 'field_' . $this->getTestFieldName();
    $fielddata = $node2->$fieldname;
    // @todo remove debug
    $this->pass('<pre>' . print_r($node2->$fieldname, TRUE) . '</pre>', 'Debug');
    $this->assertEqual('ua', $fielddata['und'][0]['country']);
    $this->assertEqual(50, $fielddata['und'][0]['latitude']);
    $this->assertEqual(30, $fielddata['und'][0]['longitude']);
    $this->assertEqual('Kyiv', $fielddata['und'][0]['name']);
  }

}
