PHP - How to change machine's DateTime only during selenium testing

270 views Asked by At

So, what I'm achieving is to prevent users to login to the site IF the current machine date is different from remote machine.

I've created a validation where checks and validates the machine with remote server's datetime. I've tried manual testing by changing my machine date manually and everything works fine.

The problem is, what if I want to test it by using selenium testing? I would say its just annoying to change my machine time manually everytime I want to run the test.

Here's my test function with an approach of changing default timezone (but, it's not working)

public function testLogin1()
{
  date_default_timezone_set('America/Anchorage');

  $testDate = getdate();

  var_dump(date_default_timezone_get());
  var_dump($testDate);

  $this->login('user', 'pass');

  sleep(3);
}

public function testLogin2()
{
  date_default_timezone_set('Asia/Bangkok');

  $testDate = getdate();

  var_dump(date_default_timezone_get());
  var_dump($testDate);

  $this->login('user', 'pass');

  sleep(3);
}

Is there any specific function where I can set global php DateTime / getdate() ?

Below is my TestTime class function.

public static function isValid($currentDateTime) {

self::initRemoteServerDateTime();
self::initLatestUserActivityDateTime();

if (self::$remoteServerDateTime || self::$latestUserActivityDateTime) {
  $refDate = (self::$remoteServerDateTime) ? self::$remoteServerDateTime : self::$latestUserActivityDateTime;

  if ($currentDateTime->format('Y-m-d') == $refDate->format('Y-m-d')) {
      return true;
    }
  }
  return false;
}

And here is my action class where I call my TestTime function.

$currentDateTime = new DateTime();
if (!TestTime::isValid($currentDateTime)) {
  $errorMessage = 'Server\'s date and time is not set properly ['. $currentDateTime->format('j M Y H:i') .']. </br>  Please contact your system administrator.';
  throw new sfValidatorErrorSchema($this, array($this->getOption('username_field') => new sfValidatorError($this, $errorMessage)));
}

My action class is happening during the validation when user clicks the login button and submits the form.

Or, is there any different approach to do this?

1

There are 1 answers

0
choz On BEST ANSWER

Since I don't think there's a way to do it yet. I finally just came up with a tmp file. It's not my desired approach, but so far it works perfectly.

Here's my code before cleanup

protected function setUp()
{
  $my_file = '/tmp/lgnTimeValidation.tmp';
  unlink($my_file);
  $this->loadUpSession();
}

public function testLoginFromFarPast()
{
  $my_file = '/tmp/lgnTimeValidation.tmp';
  $handle = fopen($my_file, 'w+') or die('Cannot open file:  '.$my_file);
  $dateTxt = "2012-12-12 12:12";
  fwrite($handle, $dateTxt);
  fseek($handle, 0);
  $fileData = fread($handle, filesize($my_file));

  $this->login('user', 'pass', 'User');

  $theText = $this->session->execute(array(
    'script' => 'return jQuery(".form_error").html();',
    'args' => array(),
    ));
  $this->assertTrue((strpos($theText, "Server's date and time is not set properly") == true));

  fclose($handle);
}

public function testLoginFromFarFuture()
{
  $my_file = '/tmp/lgnTimeValidation.tmp';
  $handle = fopen($my_file, 'w+') or die('Cannot open file:  '.$my_file);
  $dateTxt = "2030-03-28 06:07";
  fwrite($handle, $dateTxt);
  fseek($handle, 0);
  $fileData = fread($handle, filesize($my_file));

  $this->login('user', 'pass', 'User');

  $theText = $this->session->execute(array(
    'script' => 'return jQuery(".form_error").html();',
    'args' => array(),
    ));
  $this->assertTrue((strpos($theText, "Server's date and time is not set properly") == true));

  fclose($handle);
}

public function testNormalLogin()
{
  $my_file = '/tmp/lgnTimeValidation.tmp';
  $handle = fopen($my_file, 'w+') or die('Cannot open file:  '.$my_file);
  $dt = new DateTime();
  $dateTxt = $dt->format("Y-m-d H:i:s");
  fwrite($handle, $dateTxt);
  fseek($handle, 0);
  $fileData = fread($handle, filesize($my_file));

  $this->login('user', 'pass', 'User');

  fclose($handle);
}

protected function tearDown()
{
  $my_file = '/tmp/lgnTimeValidation.tmp';
  unlink($my_file);
  $this->session->close();
}