所以,我实现的是防止用户登录到站点,如果当前机器日期与远程机器不同。
我创建了一个验证,其中使用远程服务器的日期时间检查和验证计算机。我已经尝试通过手动更改机器日期进行手动测试,一切正常。
问题是,如果我想通过使用硒测试来测试它怎么办?我会说每次我想运行测试时手动更改我的机器时间很烦人。
这是我的测试函数,采用更改默认时区的方法(但是,它不起作用)
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);
}
有什么特定的函数可以设置全局 php 日期时间/getdate() 吗?
下面是我的测试时间类函数。
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;
}
这是我的操作类,我在其中调用了 TestTime 函数。
$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)));
}
当用户单击登录按钮并提交表单时,我的操作类在验证期间发生。
或者,是否有任何不同的方法来做到这一点?
因为我认为还没有办法做到这一点。我终于想出了一个tmp文件。这不是我想要的方法,但到目前为止它非常有效。
这是我清理前的代码
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();
}