<?php
// $Id: date_rounding.test,v 1.1 2007/09/14 16:17:55 dww Exp $

class ProjectUsageGetDateTests extends DrupalTestCase {
  function get_info() {
    return array(
      'name' => 'project_usage_gmgetdate()',
      'desc' => 'Test the project_usage_gmgetdate() function.',
      'group' => 'Project Usage',
    );
  }

  function test_timestamp_param() {
    $time = time();
    $parts = project_usage_gmgetdate($time);
    
    // PHP's getdate() takes the timezone into account. project_usage_gmgetdate() 
    // should subtract the timezone from the timestamp to cancel it out. We
    // can compare the input to the timestamp in the returned array and make 
    // sure they differ by the timezone offset.
    $offset = date('Z', $time);
    $expected = $time - $offset;

    $this->assertTrue(is_array($parts), "Returns an array");
    $this->assertEqual($parts[0], $expected, "Timestamps differ by the server's offset ($offset seconds)");
  }

  function test_default_param() {
    // This is actually a little dodgy... we're assuming that these two lines
    // will be executed in the same second.
    $time = time();
    $parts = project_usage_gmgetdate();
    
    // PHP's getdate() takes the timezone into account. project_usage_gmgetdate() 
    // should subtract the timezone from the timestamp to cancel it out. We
    // can compare the input to the timestamp in the returned array and make 
    // sure they differ by the timezone offset.
    $offset = date('Z', $time);
    $expected = $time - $offset;

    $this->assertTrue(is_array($parts), "Returns an array");
    $this->assertEqual($parts[0], $expected, "Timestamps differ by the server's offset ($offset seconds)");
  }
}



class ProjectUsageUsageDailyTimestampTests extends DrupalTestCase {
  function get_info() {
    return array(
      'name' => 'project_usage_daily_timestamp()',
      'desc' => 'Test the project_usage_daily_timestamp() function.',
      'group' => 'Project Usage',
    );
  }

  function format_date($timestamp) {
    return format_date($timestamp, 'short', NULL, 0);
  }
  
  function test_default_param() {
    $time = time();
    $expected = project_usage_daily_timestamp($time);
    $actual = project_usage_daily_timestamp();

    $this->assertEqual($expected, $actual, $this->format_date($time) .' should round to '. $this->format_date($expected) .' got '. $this->format_date($actual));
  }  
  function test_timestamp_param() {
    $time = 1189700682;
    $expected = 1189641600;
    $actual = project_usage_daily_timestamp($time);

    $this->assertEqual($expected, $actual, $this->format_date($time) .' should round to '. $this->format_date($expected) .' got '. $this->format_date($actual));
  }
  function test_array_param() {
    $time = 1189700682;
    $expected = 1189641600;
    $actual = project_usage_daily_timestamp(project_usage_gmgetdate($time));

    $this->assertEqual($expected, $actual, $this->format_date($time) .' should round to '. $this->format_date($expected) .' got '. $this->format_date($actual));
  }
}


class ProjectUsageUsageWeeklyTimestampTests extends DrupalTestCase {
  function get_info() {
    return array(
      'name' => 'project_usage_weekly_timestamp()',
      'desc' => 'Test the project_usage_weekly_timestamp() function.',
      'group' => 'Project Usage',
    );
  }

  function format_date($timestamp) {
    return format_date($timestamp, 'short', NULL, 0);
  }

  function test_default_param() {
    $time = time();
    $expected = project_usage_weekly_timestamp($time);
    $actual = project_usage_weekly_timestamp();

    $this->assertEqual($expected, $actual, $this->format_date($time) .' should round to '. $this->format_date($expected) .' got '. $this->format_date($actual));
  }
  function test_timestamp_param() {
    $time = 1189700682;
    $expected = 1189296000;
    $actual = project_usage_weekly_timestamp($time);

    $this->assertEqual($expected, $actual, $this->format_date($time) .' should round to '. $this->format_date($expected) .' got '. $this->format_date($actual));
  }
  function test_array_param() {
    $time = 1189700682;
    $expected = 1189296000;
    $actual = project_usage_weekly_timestamp(project_usage_gmgetdate($time));

    $this->assertEqual($expected, $actual, $this->format_date($time) .' should round to '. $this->format_date($expected) .' got '. $this->format_date($actual));
  }
}


class ProjectUsageGetWeeksSinceTests extends DrupalTestCase {
  function get_info() {
    return array(
      'name' => 'project_usage_get_weeks_since()',
      'desc' => 'Test the project_usage_get_weeks_since() function.',
      'group' => 'Project Usage',
    );
  }

  function format_date($timestamp) {
    return format_date($timestamp, 'short', NULL, 0);
  }

  function testIt() {
    $time = 1188600682;
    $expected_first = project_usage_weekly_timestamp($time);
    $expected_last = project_usage_weekly_timestamp();

    $actual = project_usage_get_weeks_since($time);
    $actual_first = array_shift($actual);
    $actual_last = array_pop($actual);

    $this->assertTrue(is_array($actual), "Returns an array");
    $this->assertEqual($actual_first, $expected_first, "First value should be ". $this->format_date($expected_first) .' got '. $this->format_date($actual_first));
    $this->assertEqual($actual_last, $expected_last, "Last value should be ". $this->format_date($expected_last) .' got '. $this->format_date($actual_last));
  }
}

