diff options
author | ctrlaltca <> | 2013-01-06 18:09:15 +0000 |
---|---|---|
committer | ctrlaltca <> | 2013-01-06 18:09:15 +0000 |
commit | 11f2517f24aca0580fec7d3560108458da787342 (patch) | |
tree | 85ae5c74c23dce62b68b2c3505345a486513c3e9 | |
parent | 37bd6206f9765a5ddfa8dc04b4e13e6bb268d0db (diff) |
backported r3233, r3234 to trunk/
-rw-r--r-- | HISTORY | 1 | ||||
-rw-r--r-- | UPGRADE | 3 | ||||
-rw-r--r-- | framework/Util/TDateTimeStamp.php | 631 | ||||
-rw-r--r-- | tests/unit/Collections/TPriorityMapTest.php | 2 | ||||
-rw-r--r-- | tests/unit/Security/TSecurityManagerTest.php | 7 | ||||
-rw-r--r-- | tests/unit/Util/TDateTimeStampTest.php | 53 | ||||
-rw-r--r-- | tests/unit/Util/TSimpleDateFormatterTest.php | 2 | ||||
-rw-r--r-- | tests/unit/Web/THttpRequestTest.php | 13 |
8 files changed, 93 insertions, 619 deletions
@@ -25,6 +25,7 @@ EHN: Permit to change the default cipher in TSecurityManager::setEncryption(); c EHN: Use php's hash_hmac() when available in TSecurityManager, and permit the use of all algorithms supported by php (ctrlaltca) EHN: Use mbstring when available in TSecurityManager to better handle multibyte text (ctrlaltca) EHN: Updated TinyMCE to 3.5.6 (ctrlaltca) +CHG: Rewrite TDateTimeStamp as a wrapper to php's native DateTime and deprecate it (ctrlaltca) Version 3.2.0 Jun 25, 2012 BUG: Fixed an inconsistency in TRegularExpressionValidator @@ -16,6 +16,9 @@ Upgrading from v3.2.0 algorithm supported by the local php installation. - TSecurityManager's Encryption property has been deprecated (it was unusable). Instead, use the new CryptAlgorithm property that permites the use of any algorithm supported by the local php installation. +- TDateTimeStamp has been deprecated in favour of php's native DateTime classes. TDateTimeStamp has been + rewritten as a wrapper to DateTime, so porting and testing old code should be as easy as just looking at + the new implementation. Upgrading from v3.1.10 --------------------- diff --git a/framework/Util/TDateTimeStamp.php b/framework/Util/TDateTimeStamp.php index 5619f6cd..a7a43b3c 100644 --- a/framework/Util/TDateTimeStamp.php +++ b/framework/Util/TDateTimeStamp.php @@ -1,13 +1,8 @@ <?php /** * TDateTimeStamp class file. - * - * COPYRIGHT - * (c) 2003-2005 John Lim and released under BSD-style license except for code by - * jackbbs, which includes getTimeStamp, getGMTDiff, isLeapYear - * and originally found at http://www.php.net/manual/en/function.mktime.php - * - * @author Wei Zhuo <weizhuo[at]gamil[dot]com> + + * @author Fabio Bas ctrlaltca[AT]gmail[DOT]com * @link http://www.pradosoft.com/ * @copyright Copyright © 2005-2012 PradoSoft * @license http://www.pradosoft.com/license/ @@ -15,50 +10,16 @@ * @package System.Util */ -/* - This code was originally for windows. But apparently this problem happens - also with Linux, RH 7.3 and later! - - glibc-2.2.5-34 and greater has been changed to return -1 for dates < - 1970. This used to work. The problem exists with RedHat 7.3 and 8.0 - echo (mktime(0, 0, 0, 1, 1, 1960)); // prints -1 - - References: - http://bugs.php.net/bug.php?id=20048&edit=2 - http://lists.debian.org/debian-glibc/2002/debian-glibc-200205/msg00010.html -*/ - -if (!defined('ADODB_ALLOW_NEGATIVE_TS') - && !defined('ADODB_NO_NEGATIVE_TS')) define('ADODB_NO_NEGATIVE_TS',1); - /** * TDateTimeStamp Class * - * TDateTimeStamp Class is adpated from the ADOdb Date Library, - * part of the ADOdb abstraction library - * Download: http://phplens.com/phpeverywhere/ + * TDateTimeStamp Class is a wrapper to DateTime: http://php.net/manual/book.datetime.php + * Prado implemented this class before php started shipping the DateTime extension. + * This class is deprecated and left here only for compatibility for legacy code. + * Please note that this class doesn't support automatic conversion from gregorian to + * julian dates anymore. * - * http://phplens.com/phpeverywhere/adodb_date_library - * - * PHP native date functions use integer timestamps for computations. - * Because of this, dates are restricted to the years 1901-2038 on Unix - * and 1970-2038 on Windows due to integer overflow for dates beyond - * those years. This library overcomes these limitations by replacing the - * native function's signed integers (normally 32-bits) with PHP floating - * point numbers (normally 64-bits). - * - * Dates from 100 A.D. to 3000 A.D. and later have been tested. The minimum - * is 100 A.D. as <100 will invoke the 2 => 4 digit year conversion. - * The maximum is billions of years in the future, but this is a theoretical - * limit as the computation of that year would take too long with the - * current implementation of {@link getTimeStamp}. - * - * PERFORMANCE - * For high speed, this library uses the native date functions where - * possible, and only switches to PHP code when the dates fall outside - * the 32-bit signed integer range. - * - * @author Wei Zhuo <weizhuo[at]gmail[dot]com> + * @author Fabio Bas ctrlaltca[AT]gmail[DOT]com * @version $Id$ * @package System.Util * @since 3.0.4 @@ -69,50 +30,16 @@ class TDateTimeStamp protected static $_month_leaf = array("",31,29,31,30,31,30,31,31,30,31,30,31); /** - * Gets day of week, 0 = Sunday,... 6=Saturday. - * Algorithm from PEAR::Date_Calc + * Returns the day of the week (0=Sunday, 1=Monday, .. 6=Saturday) + * @param int year + * @param int month + * @param int day */ public function getDayofWeek($year, $month, $day) { - /* - Pope Gregory removed 10 days - October 5 to October 14 - from the year 1582 and - proclaimed that from that time onwards 3 days would be dropped from the calendar - every 400 years. - - Thursday, October 4, 1582 (Julian) was followed immediately by Friday, October 15, 1582 (Gregorian). - */ - if ($year <= 1582) - { - if ($year < 1582 || - ($year == 1582 && ($month < 10 || ($month == 10 && $day < 15)))) - { - $greg_correction = 3; - } - else - { - $greg_correction = 0; - } - } - else - { - $greg_correction = 0; - } - - if($month > 2) - $month -= 2; - else - { - $month += 10; - $year--; - } - - $day = floor((13 * $month - 1) / 5) + - $day + ($year % 100) + - floor(($year % 100) / 4) + - floor(($year / 100) / 4) - 2 * - floor($year / 100) + 77 + $greg_correction; - - return $day - 7 * floor($day / 7); + $dt = new DateTime(); + $dt->setDate($year, $month, $day); + return (int) $dt->format('w'); } /** @@ -124,15 +51,9 @@ class TDateTimeStamp public function isLeapYear($year) { $year = $this->digitCheck($year); - if ($year % 4 != 0) - return false; - - if ($year % 400 == 0) - return true; - // if gregorian calendar (>1582), century not-divisible by 400 is not leap - else if ($year > 1582 && $year % 100 == 0 ) - return false; - return true; + $dt = new DateTime(); + $dt->setDate($year, 1, 1); + return (bool) $dt->format('L'); } /** @@ -170,13 +91,15 @@ class TDateTimeStamp /** * @return integer get local time zone offset from GMT */ - public function getGMTDiff() + public function getGMTDiff($ts=false) { - static $TZ; - if (isset($TZ)) return $TZ; + $dt = new DateTime(); + if($ts) + $dt->setTimeStamp($ts); + else + $dt->setDate(1970, 1, 2); - $TZ = mktime(0,0,0,1,2,1970) - gmmktime(0,0,0,1,2,1970); - return $TZ; + return (int) $dt->format('Z'); } /** @@ -185,236 +108,23 @@ class TDateTimeStamp function getDate($d=false,$fast=false) { if ($d === false) return getdate(); - // check if number in 32-bit signed range - if ((abs($d) <= 0x7FFFFFFF)) - { - if (!defined('ADODB_NO_NEGATIVE_TS') || $d >= 0) // if windows, must be +ve integer - return @getdate($d); - } - return $this->_getDateInternal($d); - } - - - /** - * Low-level function that returns the getdate() array. We have a special - * $fast flag, which if set to true, will return fewer array values, - * and is much faster as it does not calculate dow, etc. - * @param float original date - * @param boolean false to compute the day of the week, default is true - * @param boolean true to calculate the GMT dates - * @return array an array with date info. - */ - protected function _getDateInternal($origd=false,$fast=true,$is_gmt=false) - { - static $YRS; - - $d = $origd - ($is_gmt ? 0 : $this->getGMTDiff()); - - $_day_power = 86400; - $_hour_power = 3600; - $_min_power = 60; - - if ($d < -12219321600) - $d -= 86400*10; // if 15 Oct 1582 or earlier, gregorian correction - - $_month_table_normal =& self::$_month_normal; - $_month_table_leaf = & self::$_month_leaf; - - $d366 = $_day_power * 366; - $d365 = $_day_power * 365; - - if ($d < 0) - { - if (empty($YRS)) - $YRS = array( - 1970 => 0, - 1960 => -315619200, - 1950 => -631152000, - 1940 => -946771200, - 1930 => -1262304000, - 1920 => -1577923200, - 1910 => -1893456000, - 1900 => -2208988800, - 1890 => -2524521600, - 1880 => -2840140800, - 1870 => -3155673600, - 1860 => -3471292800, - 1850 => -3786825600, - 1840 => -4102444800, - 1830 => -4417977600, - 1820 => -4733596800, - 1810 => -5049129600, - 1800 => -5364662400, - 1790 => -5680195200, - 1780 => -5995814400, - 1770 => -6311347200, - 1760 => -6626966400, - 1750 => -6942499200, - 1740 => -7258118400, - 1730 => -7573651200, - 1720 => -7889270400, - 1710 => -8204803200, - 1700 => -8520336000, - 1690 => -8835868800, - 1680 => -9151488000, - 1670 => -9467020800, - 1660 => -9782640000, - 1650 => -10098172800, - 1640 => -10413792000, - 1630 => -10729324800, - 1620 => -11044944000, - 1610 => -11360476800, - 1600 => -11676096000); - - if ($is_gmt) - $origd = $d; - // The valid range of a 32bit signed timestamp is typically from - // Fri, 13 Dec 1901 20:45:54 GMT to Tue, 19 Jan 2038 03:14:07 GMT - // - - # old algorithm iterates through all years. new algorithm does it in - # 10 year blocks - - /* - # old algo - for ($a = 1970 ; --$a >= 0;) { - $lastd = $d; - - if ($leaf = _adodb_is_leap_year($a)) $d += $d366; - else $d += $d365; - - if ($d >= 0) { - $year = $a; - break; - } - } - */ - - $lastsecs = 0; - $lastyear = 1970; - foreach($YRS as $year => $secs) - { - if ($d >= $secs) - { - $a = $lastyear; - break; - } - $lastsecs = $secs; - $lastyear = $year; - } - - $d -= $lastsecs; - if (!isset($a)) $a = $lastyear; - - //echo ' yr=',$a,' ', $d,'.'; - - for (; --$a >= 0;) - { - $lastd = $d; - - if ($leaf = $this->isLeapYear($a)) - $d += $d366; - else - $d += $d365; - - if ($d >= 0) - { - $year = $a; - break; - } - } - /**/ - - $secsInYear = 86400 * ($leaf ? 366 : 365) + $lastd; - - $d = $lastd; - $mtab = ($leaf) ? $_month_table_leaf : $_month_table_normal; - for ($a = 13 ; --$a > 0;) - { - $lastd = $d; - $d += $mtab[$a] * $_day_power; - if ($d >= 0) - { - $month = $a; - $ndays = $mtab[$a]; - break; - } - } - - $d = $lastd; - $day = $ndays + ceil(($d+1) / ($_day_power)); - - $d += ($ndays - $day+1)* $_day_power; - $hour = floor($d/$_hour_power); - - } else { - for ($a = 1970 ;; $a++) - { - $lastd = $d; - - if ($leaf = $this->isLeapYear($a)) $d -= $d366; - else $d -= $d365; - if ($d < 0) - { - $year = $a; - break; - } - } - $secsInYear = $lastd; - $d = $lastd; - $mtab = ($leaf) ? $_month_table_leaf : $_month_table_normal; - for ($a = 1 ; $a <= 12; $a++) - { - $lastd = $d; - $d -= $mtab[$a] * $_day_power; - if ($d < 0) - { - $month = $a; - $ndays = $mtab[$a]; - break; - } - } - $d = $lastd; - $day = ceil(($d+1) / $_day_power); - $d = $d - ($day-1) * $_day_power; - $hour = floor($d /$_hour_power); - } - - $d -= $hour * $_hour_power; - $min = floor($d/$_min_power); - $secs = $d - $min * $_min_power; - if ($fast) - { - return array( - 'seconds' => $secs, - 'minutes' => $min, - 'hours' => $hour, - 'mday' => $day, - 'mon' => $month, - 'year' => $year, - 'yday' => floor($secsInYear/$_day_power), - 'leap' => $leaf, - 'ndays' => $ndays - ); - } - - - $dow = $this->getDayofWeek($year,$month,$day); + $dt = new DateTime(); + $dt->setTimestamp($d); return array( - 'seconds' => $secs, - 'minutes' => $min, - 'hours' => $hour, - 'mday' => $day, - 'wday' => $dow, - 'mon' => $month, - 'year' => $year, - 'yday' => floor($secsInYear/$_day_power), - 'weekday' => gmdate('l',$_day_power*(3+$dow)), - 'month' => gmdate('F',mktime(0,0,0,$month,2,1971)), - 0 => $origd - ); + 'seconds' => (int) $dt->format('s'), + 'minutes' => (int) $dt->format('i'), + 'hours' => (int) $dt->format('G'), + 'mday' => (int) $dt->format('j'), + 'wday' => (int) $dt->format('w'), + 'mon' => (int) $dt->format('n'), + 'year' => (int) $dt->format('Y'), + 'yday' => (int) $dt->format('z'), + 'weekday' => $dt->format('l'), + 'month' => $dt->format('F'), + 0 => (int) $d + ); } /** @@ -441,262 +151,29 @@ class TDateTimeStamp /** * @return string formatted date based on timestamp $d */ - function formatDate($fmt,$d=false,$is_gmt=false) + function formatDate($fmt,$ts=false,$is_gmt=false) { - if ($d === false) - return ($is_gmt)? @gmdate($fmt): @date($fmt); - - // check if number in 32-bit signed range - if ((abs($d) <= 0x7FFFFFFF)) - { - // if windows, must be +ve integer - if (!defined('ADODB_NO_NEGATIVE_TS') || $d >= 0) - return ($is_gmt)? @gmdate($fmt,$d): @date($fmt,$d); - } - - $_day_power = 86400; - - $arr = $this->getDate($d,true,$is_gmt); - - $year = $arr['year']; - $month = $arr['mon']; - $day = $arr['mday']; - $hour = $arr['hours']; - $min = $arr['minutes']; - $secs = $arr['seconds']; - - $max = strlen($fmt); - $dates = ''; - - $isphp5 = PHP_VERSION >= 5; - - /* - at this point, we have the following integer vars to manipulate: - $year, $month, $day, $hour, $min, $secs - */ - for ($i=0; $i < $max; $i++) - { - switch($fmt[$i]) - { - case 'T': $dates .= date('T');break; - // YEAR - case 'L': $dates .= $arr['leap'] ? '1' : '0'; break; - case 'r': // Thu, 21 Dec 2000 16:01:07 +0200 - - // 4.3.11 uses '04 Jun 2004' - // 4.3.8 uses ' 4 Jun 2004' - $dates .= gmdate('D',$_day_power*(3+$this->getDayOfWeek($year,$month,$day))).', ' - . ($day<10?'0'.$day:$day) . ' '.date('M',mktime(0,0,0,$month,2,1971)).' '.$year.' '; - - if ($hour < 10) $dates .= '0'.$hour; else $dates .= $hour; - - if ($min < 10) $dates .= ':0'.$min; else $dates .= ':'.$min; - - if ($secs < 10) $dates .= ':0'.$secs; else $dates .= ':'.$secs; - - $gmt = $this->getGMTDiff(); - if ($isphp5) - $dates .= sprintf(' %s%04d',($gmt<=0)?'+':'-',abs($gmt)/36); - else - $dates .= sprintf(' %s%04d',($gmt<0)?'+':'-',abs($gmt)/36); - break; + $dt = new DateTime(); + if($is_gmt) + $dt->setTimeZone(new DateTimeZone('UTC')); + $dt->setTimestamp($ts); - case 'Y': $dates .= $year; break; - case 'y': $dates .= substr($year,strlen($year)-2,2); break; - // MONTH - case 'm': if ($month<10) $dates .= '0'.$month; else $dates .= $month; break; - case 'Q': $dates .= ($month+3)>>2; break; - case 'n': $dates .= $month; break; - case 'M': $dates .= date('M',mktime(0,0,0,$month,2,1971)); break; - case 'F': $dates .= date('F',mktime(0,0,0,$month,2,1971)); break; - // DAY - case 't': $dates .= $arr['ndays']; break; - case 'z': $dates .= $arr['yday']; break; - case 'w': $dates .= $this->getDayOfWeek($year,$month,$day); break; - case 'l': $dates .= gmdate('l',$_day_power*(3+$this->getDayOfWeek($year,$month,$day))); break; - case 'D': $dates .= gmdate('D',$_day_power*(3+$this->getDayOfWeek($year,$month,$day))); break; - case 'j': $dates .= $day; break; - case 'd': if ($day<10) $dates .= '0'.$day; else $dates .= $day; break; - case 'S': - $d10 = $day % 10; - if ($d10 == 1) $dates .= 'st'; - else if ($d10 == 2 && $day != 12) $dates .= 'nd'; - else if ($d10 == 3) $dates .= 'rd'; - else $dates .= 'th'; - break; - - // HOUR - case 'Z': - $dates .= ($is_gmt) ? 0 : -$this->getGMTDiff(); break; - case 'O': - $gmt = ($is_gmt) ? 0 : $this->getGMTDiff(); - - if ($isphp5) - $dates .= sprintf('%s%04d',($gmt<=0)?'+':'-',abs($gmt)/36); - else - $dates .= sprintf('%s%04d',($gmt<0)?'+':'-',abs($gmt)/36); - break; - - case 'H': - if ($hour < 10) $dates .= '0'.$hour; - else $dates .= $hour; - break; - case 'h': - if ($hour > 12) $hh = $hour - 12; - else { - if ($hour == 0) $hh = '12'; - else $hh = $hour; - } - - if ($hh < 10) $dates .= '0'.$hh; - else $dates .= $hh; - break; - - case 'G': - $dates .= $hour; - break; - - case 'g': - if ($hour > 12) $hh = $hour - 12; - else { - if ($hour == 0) $hh = '12'; - else $hh = $hour; - } - $dates .= $hh; - break; - // MINUTES - case 'i': if ($min < 10) $dates .= '0'.$min; else $dates .= $min; break; - // SECONDS - case 'U': $dates .= $d; break; - case 's': if ($secs < 10) $dates .= '0'.$secs; else $dates .= $secs; break; - // AM/PM - // Note 00:00 to 11:59 is AM, while 12:00 to 23:59 is PM - case 'a': - if ($hour>=12) $dates .= 'pm'; - else $dates .= 'am'; - break; - case 'A': - if ($hour>=12) $dates .= 'PM'; - else $dates .= 'AM'; - break; - default: - $dates .= $fmt[$i]; break; - // ESCAPE - case "\\": - $i++; - if ($i < $max) $dates .= $fmt[$i]; - break; - } - } - return $dates; + return $dt->format($fmt); } /** - * Not a very fast algorithm - O(n) operation. Could be optimized to O(1). - * @return integer|float a timestamp given a local time. Originally by jackbbs. + * @return integer|float a timestamp given a local time */ function getTimeStamp($hr,$min,$sec,$mon=false,$day=false,$year=false,$is_gmt=false) { - - if ($mon === false) - return $is_gmt? @gmmktime($hr,$min,$sec): @mktime($hr,$min,$sec); - - // for windows, we don't check 1970 because with timezone differences, - // 1 Jan 1970 could generate negative timestamp, which is illegal - if (1971 <= $year && $year < 2038 || !defined('ADODB_NO_NEGATIVE_TS') - && (1901 < $year && $year < 2038)) - { - return $is_gmt ? @gmmktime($hr,$min,$sec,$mon,$day,$year) - : @mktime($hr,$min,$sec,$mon,$day,$year); - } - - $gmt_different = ($is_gmt) ? 0 : $this->getGMTDiff(); - - /* - # disabled because some people place large values in $sec. - # however we need it for $mon because we use an array... - $hr = intval($hr); - $min = intval($min); - $sec = intval($sec); - */ - $mon = (int)$mon; - $day = (int)$day; - $year = (int)$year; - - - $year = $this->digitCheck($year); - - if ($mon > 12) - { - $y = floor($mon / 12); - $year += $y; - $mon -= $y*12; - } - else if ($mon < 1) - { - $y = ceil((1-$mon) / 12); - $year -= $y; - $mon += $y*12; - } - - $_day_power = 86400; - $_hour_power = 3600; - $_min_power = 60; - - $_month_table_normal = & self::$_month_normal; - $_month_table_leaf = & self::$_month_leaf; - - $_total_date = 0; - if ($year >= 1970) - { - for ($a = 1970 ; $a <= $year; $a++) - { - $leaf = $this->isLeapYear($a); - if ($leaf == true) { - $loop_table = $_month_table_leaf; - $_add_date = 366; - } else { - $loop_table = $_month_table_normal; - $_add_date = 365; - } - if ($a < $year) { - $_total_date += $_add_date; - } else { - for($b=1;$b<$mon;$b++) { - $_total_date += $loop_table[$b]; - } - } - } - $_total_date +=$day-1; - $ret = $_total_date * $_day_power + $hr * $_hour_power + $min * $_min_power + $sec + $gmt_different; - - } else { - for ($a = 1969 ; $a >= $year; $a--) { - $leaf = $this->isLeapYear($a); - if ($leaf == true) { - $loop_table = $_month_table_leaf; - $_add_date = 366; - } else { - $loop_table = $_month_table_normal; - $_add_date = 365; - } - if ($a > $year) { $_total_date += $_add_date; - } else { - for($b=12;$b>$mon;$b--) { - $_total_date += $loop_table[$b]; - } - } - } - $_total_date += $loop_table[$mon] - $day; - - $_day_time = $hr * $_hour_power + $min * $_min_power + $sec; - $_day_time = $_day_power - $_day_time; - $ret = -( $_total_date * $_day_power + $_day_time - $gmt_different); - if ($ret < -12220185600) $ret += 10*86400; // if earlier than 5 Oct 1582 - gregorian correction - else if ($ret < -12219321600) $ret = -12219321600; // if in limbo, reset to 15 Oct 1582. - } - //print " dmy=$day/$mon/$year $hr:$min:$sec => " .$ret; - return $ret; + $dt = new DateTime(); + if($is_gmt) + $dt->setTimeZone(new DateTimeZone('UTC')); + $dt->setDate($year!==false ? $year : date('Y'), + $mon!==false ? $mon : date('m'), + $day!==false ? $day : date('d')); + $dt->setTime($hr, $min, $sec); + return $dt->format('U'); } } diff --git a/tests/unit/Collections/TPriorityMapTest.php b/tests/unit/Collections/TPriorityMapTest.php index c7d56243..7c6ed498 100644 --- a/tests/unit/Collections/TPriorityMapTest.php +++ b/tests/unit/Collections/TPriorityMapTest.php @@ -352,7 +352,7 @@ class TPriorityMapTest extends PHPUnit_Framework_TestCase { $this->assertEquals(1, $priorities[1]); $this->assertEquals(10, $priorities[2]); $this->assertEquals(100, $priorities[3]); - $this->assertEquals(null, $priorities[4]); + $this->assertEquals(false, isset($priorities[4])); } diff --git a/tests/unit/Security/TSecurityManagerTest.php b/tests/unit/Security/TSecurityManagerTest.php index 2daf6ede..d265d127 100644 --- a/tests/unit/Security/TSecurityManagerTest.php +++ b/tests/unit/Security/TSecurityManagerTest.php @@ -73,10 +73,11 @@ class TSecurityManagerTest extends PHPUnit_Framework_TestCase { $sec=new TSecurityManager (); $sec->init (null); try { - $sec->setEncryption ('DES'); + $sec->setCryptAlgorithm('NotExisting'); + $foo=$sec->encrypt('dummy'); self::fail ('Expected TNotSupportedException not thrown'); } catch (TNotSupportedException $e) { - self::assertEquals('3DES', $sec->getEncryption()); + self::assertEquals('NotExisting', $sec->getCryptAlgorithm()); } } @@ -96,6 +97,8 @@ class TSecurityManagerTest extends PHPUnit_Framework_TestCase { return; } $decrypted = $sec->decrypt($encrypted); + // the decrypted string is padded with \0 + $decrypted = strstr($decrypted, "\0", TRUE); self::assertEquals($plainText,$decrypted); diff --git a/tests/unit/Util/TDateTimeStampTest.php b/tests/unit/Util/TDateTimeStampTest.php index e9a0a22f..60ccf640 100644 --- a/tests/unit/Util/TDateTimeStampTest.php +++ b/tests/unit/Util/TDateTimeStampTest.php @@ -4,12 +4,15 @@ require_once dirname(__FILE__).'/../phpunit.php'; Prado::using('System.Util.TDateTimeStamp'); +/** + * @package System.Util + */ class TDateTimeStampTest extends PHPUnit_Framework_TestCase { public function testGetTimeStampAndFormat() { $s = new TDateTimeStamp; $t = $s->getTimeStamp(0,0,0); - $this->assertEquals($s->formatDate('Y-m-d'), date('Y-m-d')); + $this->assertEquals($s->formatDate('Y-m-d',$t), date('Y-m-d')); $t = $s->getTimeStamp(0,0,0,6,1,2102); $this->assertEquals($s->formatDate('Y-m-d',$t), '2102-06-01'); @@ -18,33 +21,6 @@ class TDateTimeStampTest extends PHPUnit_Framework_TestCase { $this->assertEquals($s->formatDate('Y-m-d',$t), '2102-02-01'); } - public function testGregorianToJulianConversion() { - $s = new TDateTimeStamp; - $t = $s->getTimeStamp(0,0,0,10,11,1492); - - //http://www.holidayorigins.com/html/columbus_day.html - Friday check - $this->assertEquals($s->formatDate('D Y-m-d',$t), 'Fri 1492-10-11'); - - $t = $s->getTimeStamp(0,0,0,2,29,1500); - $this->assertEquals($s->formatDate('Y-m-d',$t), '1500-02-29'); - - $t = $s->getTimeStamp(0,0,0,2,29,1700); - $this->assertEquals($s->formatDate('Y-m-d',$t), '1700-03-01'); - - } - - public function testGregorianCorrection() { - $s = new TDateTimeStamp; - $diff = $s->getTimeStamp(0,0,0,10,15,1582) - $s->getTimeStamp(0,0,0,10,4,1582); - - //This test case fails on my windows machine! - //$this->assertEquals($diff, 3600*24, - // "Error in gregorian correction = ".($diff/3600/24)." days"); - - $this->assertEquals($s->getDayOfWeek(1582,10,15), 5.0); - $this->assertEquals($s->getDayOfWeek(1582,10,4), 4.0); - } - public function testOverFlow() { $s = new TDateTimeStamp; $t = $s->getTimeStamp(0,0,0,3,33,1965); @@ -53,13 +29,13 @@ class TDateTimeStampTest extends PHPUnit_Framework_TestCase { $t = $s->getTimeStamp(0,0,0,4,33,1971); $this->assertEquals($s->formatDate('Y-m-d',$t), '1971-05-03', 'Error in day overflow 2'); $t = $s->getTimeStamp(0,0,0,1,60,1965); - $this->assertEquals($s->formatDate('Y-m-d',$t), '1965-03-01', 'Error in day overflow 3 '.$s->getDate('Y-m-d',$t)); + $this->assertEquals($s->formatDate('Y-m-d',$t), '1965-03-01', 'Error in day overflow 3 '.$s->formatDate('Y-m-d',$t)); $t = $s->getTimeStamp(0,0,0,12,32,1965); - $this->assertEquals($s->formatDate('Y-m-d',$t), '1966-01-01', 'Error in day overflow 4 '.$s->getDate('Y-m-d',$t)); + $this->assertEquals($s->formatDate('Y-m-d',$t), '1966-01-01', 'Error in day overflow 4 '.$s->formatDate('Y-m-d',$t)); $t = $s->getTimeStamp(0,0,0,12,63,1965); - $this->assertEquals($s->formatDate('Y-m-d',$t), '1966-02-01', 'Error in day overflow 5 '.$s->getDate('Y-m-d',$t)); + $this->assertEquals($s->formatDate('Y-m-d',$t), '1966-02-01', 'Error in day overflow 5 '.$s->formatDate('Y-m-d',$t)); $t = $s->getTimeStamp(0,0,0,13,3,1965); - $this->assertEquals($s->formatDate('Y-m-d',$t), '1966-01-03', 'Error in mth overflow 1'); + $this->assertEquals($s->formatDate('Y-m-d',$t), '1966-01-03', 'Error in math overflow 1'); } public function test2DigitTo4DigitYearConversion() { @@ -93,11 +69,11 @@ class TDateTimeStampTest extends PHPUnit_Framework_TestCase { } } - public function testRandomDatesBetween100And4000() { - $this->assertIsValidDate(100,1); + public function testRandomDatesBetween1000And4000() { + $this->assertIsValidDate(1000,1); //echo "Testing year "; for ($i=10; --$i >= 0;) { - $y1 = 100+rand(0,1970-100); + $y1 = 1000+rand(0,1970-1000); //echo $y1." "; $m = rand(1,12); $this->assertIsValidDate($y1,$m); @@ -137,7 +113,12 @@ class TDateTimeStampTest extends PHPUnit_Framework_TestCase { $newi = $s->getTimestamp($arr[3],$arr[4],$arr[5],$arr[0],$arr[1],$arr[2]); if ($i != $newi) { - $fails++; + // This actually can fail if $i is in the middle of a time change due to DST + $tz = new DateTimeZone(date_default_timezone_get()); + $transitions = $tz->getTransitions($i-3600, $i+3600); + if(count($transitions) == 0) + $fails++; + //$j = mktime($arr[3],$arr[4],$arr[5],$arr[0],$arr[1],$arr[2]); //print "Error at $i, $j, getTimestamp() returned $newi ($ret)\n"; } diff --git a/tests/unit/Util/TSimpleDateFormatterTest.php b/tests/unit/Util/TSimpleDateFormatterTest.php index 13ad25a9..bfda4712 100644 --- a/tests/unit/Util/TSimpleDateFormatterTest.php +++ b/tests/unit/Util/TSimpleDateFormatterTest.php @@ -45,7 +45,7 @@ class TSimpleDateFormatterTest extends PHPUnit_Framework_TestCase { public function testMissingYearPattern() { $formatter = new TSimpleDateFormatter("MM/dd"); - self::assertEquals("2008-10-22", date('Y-m-d', $formatter->parse("10/22"))); + self::assertEquals(date("Y-10-22"), date('Y-m-d', $formatter->parse("10/22"))); } public function testDayMonthYearOrdering() { diff --git a/tests/unit/Web/THttpRequestTest.php b/tests/unit/Web/THttpRequestTest.php index 6af8cd78..e8b86ac8 100644 --- a/tests/unit/Web/THttpRequestTest.php +++ b/tests/unit/Web/THttpRequestTest.php @@ -197,7 +197,9 @@ class THttpRequestTest extends PHPUnit_Framework_TestCase { } public function testGetBrowser() { - /*$request = new THttpRequest(); + /* + // requires browscap configuration in php.ini + $request = new THttpRequest(); $request->init(null); // Reset UserAgent, because constructor of THttpRequest unset it if called from cli ! $_SERVER['HTTP_USER_AGENT'] = 'Mozilla/5.0 (Macintosh; U; Intel Mac OS X; en-US; rv:1.8.1.3) Gecko/20070309 Firefox/2.0.0.3'; @@ -240,13 +242,20 @@ class THttpRequestTest extends PHPUnit_Framework_TestCase { } public function testGetUserLanguages() { + /* + // this actually doesn't work because: + // - THttpRequest::getUserLanguages() is a wrapper for PradoBase::getUserLanguages() + // - PradoBase is using a static variable to hold the user languages array + // - PradoBase exists before we set $_SERVER['HTTP_ACCEPT_LANGUAGE'] $request = new THttpRequest(); $request->init(null); // Browser sent fr,en-us;q=0.8,fr-fr;q=0.5,en;q=0.3 // that means that browser want fr (1) first, next en-us (0.8), then fr-fr(0.5)n and last en (0.3) // So, we expect method to return an array with these languages, and this order - $acceptLanguages=array ('fr', 'en-us','fr-fr','en'); + $acceptLanguages=array('fr', 'en-us','fr-fr','en'); self::assertEquals($acceptLanguages, $request->getUserLanguages()); + */ + throw new PHPUnit_Framework_IncompleteTestError(); } public function testSetEnableCookieValidation() { |