donglanfu5831 2015-06-12 08:27
浏览 68
已采纳

PHP - 如何仅在硒测试期间更改机器的DateTime

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条回答 默认 最新

  • dongshi4589 2015-07-10 07:40
    关注

    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();
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 C++ yoloV5改写遇到的问题
  • ¥20 win11修改中文用户名路径
  • ¥15 win2012磁盘空间不足,c盘正常,d盘无法写入
  • ¥15 用土力学知识进行土坡稳定性分析与挡土墙设计
  • ¥70 PlayWright在Java上连接CDP关联本地Chrome启动失败,貌似是Windows端口转发问题
  • ¥15 帮我写一个c++工程
  • ¥30 Eclipse官网打不开,官网首页进不去,显示无法访问此页面,求解决方法
  • ¥15 关于smbclient 库的使用
  • ¥15 微信小程序协议怎么写
  • ¥15 c语言怎么用printf(“\b \b”)与getch()实现黑框里写入与删除?