Add unit tests for some legacy tests, too

This commit is contained in:
Kijin Sung 2016-03-16 11:36:36 +09:00
parent f9ea115c19
commit d325ef99bc
3 changed files with 236 additions and 3 deletions

View file

@ -421,7 +421,7 @@ function getCurrentPageUrl($escape = true)
*/
function isSiteID($domain)
{
return preg_match('/^([a-zA-Z0-9\_]+)$/', $domain);
return (bool)preg_match('/^([a-zA-Z0-9\_]+)$/', $domain);
}
/**

View file

@ -66,15 +66,28 @@ class DateTimeTest extends \Codeception\TestCase\Test
{
$timestamp = 1454000000;
// Test zdate() when the internal time zone is different from the default time zone.
// Test when the internal time zone is different from the default time zone.
Rhymix\Framework\Config::set('locale.internal_timezone', 10800);
$this->assertEquals('20160128195320', getInternalDateTime($timestamp));
// Test zdate() when the internal time zone is the same as the default time zone.
// Test when the internal time zone is the same as the default time zone.
Rhymix\Framework\Config::set('locale.internal_timezone', 32400);
$this->assertEquals('20160129015320', getInternalDateTime($timestamp));
}
public function testGetDisplayDateTime()
{
$timestamp = 1454000000;
// Test when the display time zone is different from the internal time zone.
$_SESSION['timezone'] = 'America/Los_Angeles';
$this->assertEquals('20160128085320', getDisplayDateTime($timestamp));
// Test when the display time zone is the same as the internal time zone.
$_SESSION['timezone'] = 'Etc/GMT-3';
$this->assertEquals('20160128195320', getDisplayDateTime($timestamp));
}
public function testGetTimeGap()
{
$GLOBALS['lang'] = Rhymix\Framework\Lang::getInstance('en');

View file

@ -2,5 +2,225 @@
class LegacyTest extends \Codeception\TestCase\Test
{
public function testGetModule()
{
$this->assertTrue(getModule('board', 'controller') instanceof BoardController);
$this->assertTrue(getModule('board', 'model') instanceof BoardModel);
$this->assertTrue(getModule('board', 'view', 'admin') instanceof BoardAdminView);
$this->assertTrue(getModule('board') instanceof BoardView);
$this->assertTrue(getAdminController('board') instanceof BoardAdminController);
$this->assertTrue(getAdminModel('board') instanceof BoardAdminModel);
$this->assertTrue(getAdminView('board') instanceof BoardAdminView);
$this->assertTrue(getController('board') instanceof BoardController);
$this->assertTrue(getModel('board') instanceof BoardModel);
$this->assertTrue(getView('board') instanceof BoardView);
$this->assertTrue(getAPI('board') instanceof BoardApi);
$this->assertTrue(getMobile('board') instanceof BoardMobile);
$this->assertTrue(getWAP('board') instanceof BoardWap);
$this->assertTrue(getClass('board') instanceof Board);
}
public function testGetNextSequence()
{
if (!file_exists(\RX_BASEDIR . 'files/config/config.php'))
{
return;
}
$this->assertGreaterThan(0, $sequence1 = getNextSequence());
$this->assertGreaterThan($sequence1, $sequence2 = getNextSequence());
$this->assertTrue(checkUserSequence($sequence1));
$this->assertTrue(checkUserSequence($sequence2));
$this->assertFalse(checkUserSequence(-1));
setUserSequence(-1);
$this->assertTrue(checkUserSequence(-1));
}
public function testGetURL()
{
/**
* TODO:
* - getUrl()
* - getNotEncodedUrl()
* - getAutoEncodedUrl()
* - getFullUrl()
* - getNotEncodedFullUrl()
* - getSiteUrl()
* - getNotEncodedSiteUrl()
* - getFullSiteUrl()
* - getCurrentPageUrl()
* - getScriptPath()
* - getRequestUriByServerEnviroment()
*/
}
public function testIsSiteID()
{
$this->assertTrue(isSiteID('rhymix_RHYMIX_1234'));
$this->assertFalse(isSiteID('www.rhymix.org'));
}
public function testCutStr()
{
$this->assertEquals('안녕하세요? 라이믹스...', cut_str('안녕하세요? 라이믹스입니다. 제목이 너무 길어서 잘립니다.', 20));
$this->assertEquals('Hello? This is Rhymix...', cut_str('Hello? This is Rhymix. This title is very long.', 20));
}
public function testTimeFunctions()
{
$this->assertEquals(0, get_time_zone_offset('00:00'));
$this->assertEquals(32400, get_time_zone_offset('+09:00'));
$this->assertEquals(32400, get_time_zone_offset('+0900'));
$this->assertEquals(32400, get_time_zone_offset('0900'));
$this->assertEquals(-18000, get_time_zone_offset('-05:00'));
$this->assertEquals(-18000, get_time_zone_offset('-0500'));
$this->assertEquals('Jan', getMonthName(1));
$this->assertEquals('Sep', getMonthName(9, true));
$this->assertEquals('September', getMonthName(9, false));
/**
* The following functions are tested in DateTimeTest:
* - zgap()
* - zdate()
* - ztime()
* - getInternalDateTime()
* - getDisplayDateTime()
* - getTimeGap()
*/
}
public function testGetEncodedEmailAddress()
{
$this->assertNotEquals('devops@rhymix.org', getEncodeEmailAddress('devops@rhymix.org'));
$this->assertContains('&#X', getEncodeEmailAddress('devops@rhymix.org'));
}
public function testGetMicrotime()
{
$microtime1 = microtime(true);
$microtime2 = getMicroTime();
$microtime3 = microtime(true);
$this->assertEquals('double', gettype($microtime2));
$this->assertGreaterThanOrEqual($microtime1, $microtime2);
$this->assertGreaterThanOrEqual($microtime2, $microtime3);
}
public function testDelObjectVars()
{
$target = (object)array('foo' => 1, 'bar' => 2, 'baz' => 3, 'rhymix' => 4);
$delete = (object)array('bar' => 5, 'baz' => 6);
$result = delObjectVars($target, $delete);
// Check if the keys were deleted from the result.
$this->assertTrue(isset($result->foo));
$this->assertFalse(isset($result->bar));
$this->assertFalse(isset($result->baz));
$this->assertTrue(isset($result->rhymix));
// Check if the keys are intact in the original target.
$this->assertTrue(isset($target->bar));
$this->assertTrue(isset($target->baz));
}
public function testGetDestroyXeVars()
{
// Test array. (Keys should be intact in the original target.)
$target = array('foo' => 1, 'bar' => 2, 'xe_validator_id' => 3);
$result = getDestroyXeVars($target);
$this->assertFalse(isset($result['xe_validator_id']));
$this->assertTrue(isset($target['xe_validator_id']));
// Test object. (Keys should be deleted from the original target.)
$target = (object)array('foo' => 1, 'bar' => 2, 'xe_validator_id' => 3);
$result = getDestroyXeVars($target);
$this->assertFalse(isset($result->xe_validator_id));
$this->assertFalse(isset($target->xe_validator_id));
}
public function testGetNumberingPath()
{
$this->assertEquals('001/', getNumberingPath(1));
$this->assertEquals('012/', getNumberingPath(12));
$this->assertEquals('123/', getNumberingPath(123));
$this->assertEquals('234/001/', getNumberingPath(1234));
$this->assertEquals('345/012/', getNumberingPath(12345));
$this->assertEquals('456/123/', getNumberingPath(123456));
$this->assertEquals('567/234/001/', getNumberingPath(1234567));
$this->assertEquals('678/345/012/', getNumberingPath(12345678));
$this->assertEquals('789/456/123/', getNumberingPath(123456789));
}
public function testMysqlPre4HashPassword()
{
$this->assertEquals('5d2e19393cc5ef67', mysql_pre4_hash_password('password'));
$this->assertEquals('25a4fb474e17c19a', mysql_pre4_hash_password('pass\'#word'));
}
public function testJsonEncode2()
{
$data = array('foo' => 1, 'bar' => 2, 'baz' => 3, 'rhymix' => 4);
$this->assertEquals(json_encode($data), json_encode2($data));
}
public function TestIsCrawler()
{
$original_user_agent = $_SERVER['HTTP_USER_AGENT'];
// Test automatic detection from User-Agent string.
$_SERVER['HTTP_USER_AGENT'] = 'Mozilla/5.0 (Windows NT 6.1; rv:45.0) Gecko/20100101 Firefox/45.0';
$this->assertFalse(isCrawler());
$_SERVER['HTTP_USER_AGENT'] = 'Mozilla/5.0 (compatible; Googlebot/2.1; +http://www.google.com/bot.html)';
$this->assertTrue(isCrawler());
// Test manual detection.
$this->assertTrue(isCrawler('Mozilla/5.0 (compatible; bingbot/2.0; +http://www.bing.com/bingbot.htm)'));
$this->assertTrue(isCrawler('Yeti/1.0 (NHN Corp.; http://help.naver.com/robots/)'));
$this->assertFalse(isCrawler('Mozilla/5.0 (Macintosh; Intel Mac OS X 10_10_1) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/41.0.2227.1 Safari/537.36'));
$this->assertFalse(isCrawler('Mozilla/5.0 (iPad; CPU OS 6_0 like Mac OS X) AppleWebKit/536.26 (KHTML, like Gecko) Version/6.0 Mobile/10A5355d Safari/8536.25'));
$_SERVER['HTTP_USER_AGENT'] = $original_user_agent;
}
public function testMiscUTF8Functions()
{
$this->assertEquals('<img>', url_decode('%3Cimg%3E'));
$this->assertEquals('한글 % English', utf8RawUrlDecode('%uD55C%uAE00%20%25%20English'));
$this->assertEquals('뷁', _code2utf(48577));
$this->assertTrue(detectUTF8('라이믹스'));
$this->assertTrue(detectUTF8(urlencode('라이믹스')));
$this->assertTrue(detectUTF8('%87%a9%43%cd%ef', false, false));
$this->assertFalse(detectUTF8(iconv('UTF-8', 'EUC-KR', '라이믹스')));
$this->assertFalse(detectUTF8(chr(129) . chr(214) . chr(181) . chr(73) . chr(97)));
$this->assertFalse(detectUTF8('%87%a9%43%cd%ef'));
$this->assertEquals(mb_convert_encoding('라이믹스', 'UTF-8', 'CP949'), detectUTF8('라이믹스', true));
$this->assertEquals('라이믹스', detectUTF8(iconv('UTF-8', 'EUC-KR', '라이믹스'), true));
}
public function testMiscSecurityFunctions()
{
/**
* TODO:
* - stripEmbedTagForAdmin()
* - checkCSRF()
*/
}
public function testRecurciveExposureCheck()
{
/**
* TODO
*/
}
public function testChangeValueInUrl()
{
/**
* TODO
*/
}
}