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?
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