Created
April 12, 2012 17:32
-
-
Save jamband/2369401 to your computer and use it in GitHub Desktop.
Yii Framework: Test case in LoginAttemptTest.php
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
/** | |
* LoginAttempt class file. | |
* | |
* The followings are the available columns in table 'login_attempt': | |
* @property integer $id | |
* @property string $ip | |
* @property string $login | |
* @property integer $create_time | |
* @property integer $expiration_time | |
*/ | |
class LoginAttempt extends LoginAttemptBase | |
{ | |
/** | |
* @see CActiveRecord::model() | |
*/ | |
public static function model($className=__CLASS__) | |
{ | |
return parent::model($className); | |
} | |
/** | |
* @see CActiveRecord::beforeSave() | |
*/ | |
protected function beforeSave() | |
{ | |
$this->create_time = time(); | |
$this->expiration_time = strtotime(Yii::app()->getModule('user')->bannedIpExpirationTime, $this->create_time); | |
return true; | |
} | |
/** | |
* ログインが禁止されたIPアドレスか、そうでないか | |
* @param string $ip IP Address | |
* @return boolean | |
*/ | |
public function isBanned($ip) | |
{ | |
$c = $this->getCriteriaByIp($ip); | |
if ($this->count($c) >= Yii::app()->getModule('user')->loginAttemptLimit) | |
return true; | |
return false; | |
} | |
/** | |
* ログインが禁止されたIPアドレスを期限切れの場合は削除する | |
* @param string $id IP Address | |
*/ | |
public function purgeBannedIp($ip) | |
{ | |
$c = $this->getCriteriaByIp($ip); | |
if ($this->count($c) >= Yii::app()->getModule('user')->loginAttemptLimit) | |
{ | |
$c->order = 't.create_time DESC'; | |
$model = $this->find($c); | |
if (time() >= $model->expiration_time) | |
$this->deleteAll('ip = :ip', array(':ip' => $ip)); | |
} | |
} | |
/** | |
* IPアドレスを条件にしたcriteriaを取得する | |
* @param string $id IP Address | |
* @return object criteria | |
*/ | |
protected function getCriteriaByIp($ip) | |
{ | |
$c = new CDbCriteria; | |
$c->condition = 't.ip = :ip'; | |
$c->params[':ip'] = $ip; | |
return $c; | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
Yii::import('user.models.LoginAttempt'); | |
class LoginAttemptTest extends CDbTestCase | |
{ | |
private $_ip; | |
private $_limit; | |
public $fixtures = array( | |
'loginAttempts' => 'LoginAttempt', | |
); | |
protected function setUp() | |
{ | |
parent::setUp(); | |
$this->_ip = req()->userHostAddress; | |
$this->_limit = Yii::app()->getModule('user')->loginAttemptLimit; | |
} | |
public function test_beforeSave() | |
{ | |
$loginAttempt = new loginAttempt(); | |
$attr['ip'] = $this->_ip; | |
$attr['login'] = 'test'; | |
$loginAttempt->setAttributes($attr, false); | |
$this->assertTrue($loginAttempt->save(false)); | |
$this->assertGreaterThan($loginAttempt->create_time, $loginAttempt->expiration_time); | |
} | |
public function test_isBanned() | |
{ | |
// 行を loginAttemptLimit - 1 件挿入してisBannedがすべてfalseになるかを確認 | |
for ($i = 1; $i <= $this->_limit - 1; $i++) | |
{ | |
$loginAttempt = new LoginAttempt(); | |
$attr['ip'] = $this->_ip; | |
$attr['login'] = 'test'.$i; | |
$loginAttempt->setAttributes($attr, false); | |
$this->assertTrue($loginAttempt->save(false)); | |
$this->assertFalse($loginAttempt->isBanned($attr['ip'])); | |
} | |
// さらに1件挿入後、isBannedがtrueになるかを確認 | |
$loginAttempt = new LoginAttempt(); | |
$loginAttempt->setAttributes($attr, false); | |
$this->assertTrue($loginAttempt->save(false)); | |
$this->assertTrue($loginAttempt->isBanned($attr['ip'])); | |
} | |
public function test_purgeBannedIp() | |
{ | |
for ($i = 1; $i <= $this->_limit; $i++) | |
{ | |
$loginAttempt = new LoginAttempt(); | |
$attr['ip'] = $this->_ip; | |
$attr['login'] = 'test'.$i; | |
$loginAttempt->setAttributes($attr, false); | |
$this->assertTrue($loginAttempt->save(false)); | |
} | |
$this->assertEquals($this->_limit, count($loginAttempt->findAll())); | |
// 上記で挿入したすべてのexpiration_timeをtime()に更新後purgeBannedIpをテスト | |
$loginAttempt->updateAll(array('expiration_time' => time()), 'ip = :ip', array(':ip' => $attr['ip'])); | |
$loginAttempt->purgeBannedIp($attr['ip']); | |
$this->assertEquals(0, count($loginAttempt->findAll())); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment