From a9d6cf598d30a73f959277fdc616e178b51a95e1 Mon Sep 17 00:00:00 2001 From: Kijin Sung Date: Thu, 17 Mar 2016 15:45:31 +0900 Subject: [PATCH 01/21] Add MIME class --- classes/mail/Mail.class.php | 76 +-------------- common/framework/mime.php | 152 ++++++++++++++++++++++++++++++ tests/unit/framework/MIMETest.php | 22 +++++ 3 files changed, 175 insertions(+), 75 deletions(-) create mode 100644 common/framework/mime.php create mode 100644 tests/unit/framework/MIMETest.php diff --git a/classes/mail/Mail.class.php b/classes/mail/Mail.class.php index c2d9725db..465e623be 100644 --- a/classes/mail/Mail.class.php +++ b/classes/mail/Mail.class.php @@ -460,81 +460,7 @@ class Mail */ function returnMIMEType($filename) { - preg_match("|\.([a-z0-9]{2,4})$|i", $filename, $fileSuffix); - switch(strtolower($fileSuffix[1])) - { - case "js" : - return "application/x-javascript"; - case "json" : - return "application/json"; - case "jpg" : - case "jpeg" : - case "jpe" : - return "image/jpg"; - case "png" : - case "gif" : - case "bmp" : - case "tiff" : - return "image/" . strtolower($fileSuffix[1]); - case "css" : - return "text/css"; - case "xml" : - return "application/xml"; - case "doc" : - case "docx" : - return "application/msword"; - case "xls" : - case "xlt" : - case "xlm" : - case "xld" : - case "xla" : - case "xlc" : - case "xlw" : - case "xll" : - return "application/vnd.ms-excel"; - case "ppt" : - case "pps" : - return "application/vnd.ms-powerpoint"; - case "rtf" : - return "application/rtf"; - case "pdf" : - return "application/pdf"; - case "html" : - case "htm" : - case "php" : - return "text/html"; - case "txt" : - return "text/plain"; - case "mpeg" : - case "mpg" : - case "mpe" : - return "video/mpeg"; - case "mp3" : - return "audio/mpeg3"; - case "wav" : - return "audio/wav"; - case "aiff" : - case "aif" : - return "audio/aiff"; - case "avi" : - return "video/msvideo"; - case "wmv" : - return "video/x-ms-wmv"; - case "mov" : - return "video/quicktime"; - case "zip" : - return "application/zip"; - case "tar" : - return "application/x-tar"; - case "swf" : - return "application/x-shockwave-flash"; - default : - if(function_exists("mime_content_type")) - { - $fileSuffix = mime_content_type($filename); - } - return "unknown/" . trim($fileSuffix[0], "."); - } + return Rhymix\Framework\MIME::getTypeByFilename($filename); } } diff --git a/common/framework/mime.php b/common/framework/mime.php new file mode 100644 index 000000000..00488659a --- /dev/null +++ b/common/framework/mime.php @@ -0,0 +1,152 @@ + $mime) + { + if (!strncasecmp($type, $mime, strlen($type))) return $extension; + } + return false; + } + + /** + * The default MIME type for unknown extensions. + */ + protected static $_default = 'application/octet-stream'; + + /** + * The list of known MIME types. + */ + protected static $_types = array( + + // Text-based document formats. + 'html' => 'text/html', + 'htm' => 'text/html', + 'shtml' => 'text/html', + 'txt' => 'text/plain', + 'text' => 'text/plain', + 'log' => 'text/plain', + 'rtf' => 'text/rtf', + 'xml' => 'text/xml', + 'xsl' => 'text/xml', + 'css' => 'text/css', + 'csv' => 'text/csv', + + // Binary document formats. + 'doc' => 'application/msword', + 'dot' => 'application/msword', + 'xls' => 'application/vnd.ms-excel', + 'ppt' => 'application/vnd.ms-powerpoint', + 'docx' => 'application/vnd.openxmlformats-officedocument.wordprocessingml.document', + 'dotx' => 'application/vnd.openxmlformats-officedocument.wordprocessingml.document', + 'xlsx' => 'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet', + 'pptx' => 'application/vnd.openxmlformats-officedocument.presentationml.presentation', + 'odt' => 'application/vnd.oasis.opendocument.text', + 'ods' => 'application/vnd.oasis.opendocument.spreadsheet', + 'odp' => 'application/vnd.oasis.opendocument.presentation', + 'odg' => 'application/vnd.oasis.opendocument.graphics', + 'odb' => 'application/vnd.oasis.opendocument.database', + 'pdf' => 'application/pdf', + + // Images. + 'bmp' => 'image/bmp', + 'gif' => 'image/gif', + 'jpg' => 'image/jpeg', + 'jpeg' => 'image/jpeg', + 'jpe' => 'image/jpeg', + 'png' => 'image/png', + 'svg' => 'image/svg+xml', + 'tiff' => 'image/tiff', + 'tif' => 'image/tiff', + 'ico' => 'image/vnd.microsoft.icon', + + // Audio. + 'mid' => 'audio/midi', + 'midi' => 'audio/midi', + 'mpga' => 'audio/mpeg', + 'mp2' => 'audio/mpeg', + 'mp3' => 'audio/mpeg', + 'aif' => 'audio/x-aiff', + 'aiff' => 'audio/x-aiff', + 'ra' => 'audio/x-realaudio', + 'wav' => 'audio/x-wav', + 'ogg' => 'audio/ogg', + + // Video. + 'avi' => 'video/x-msvideo', + 'flv' => 'video/x-flv', + 'mpeg' => 'video/mpeg', + 'mpg' => 'video/mpeg', + 'mpe' => 'video/mpeg', + 'mp4' => 'video/mpeg', + 'qt' => 'video/quicktime', + 'mov' => 'video/quicktime', + 'movie' => 'video/x-sgi-movie', + 'rv' => 'video/vnd.rn-realvideo', + 'dvi' => 'application/x-dvi', + + // Other multimedia file formats. + 'psd' => 'application/x-photoshop', + 'swf' => 'application/x-shockwave-flash', + 'ai' => 'application/postscript', + 'eps' => 'application/postscript', + 'ps' => 'application/postscript', + 'mif' => 'application/vnd.mif', + 'xul' => 'application/vnd.mozilla.xul+xml', + + // Source code formats. + 'phps' => 'application/x-httpd-php-source', + 'js' => 'application/x-javascript', + + // Archives. + 'bz2' => 'application/x-bzip', + 'gz' => 'application/x-gzip', + 'tar' => 'application/x-tar', + 'tgz' => 'application/x-tar', + 'gtar' => 'application/x-gtar', + 'rar' => 'application/x-rar-compressed', + 'zip' => 'application/x-zip', + + // RFC822 email message. + 'eml' => 'message/rfc822', + ); +} diff --git a/tests/unit/framework/MIMETest.php b/tests/unit/framework/MIMETest.php new file mode 100644 index 000000000..f62324ce7 --- /dev/null +++ b/tests/unit/framework/MIMETest.php @@ -0,0 +1,22 @@ +assertEquals('audio/ogg', Rhymix\Framework\MIME::getTypeByExtension('ogg')); + $this->assertEquals('image/gif', Rhymix\Framework\MIME::getTypeByExtension('gif')); + $this->assertEquals('text/html', Rhymix\Framework\MIME::getTypeByExtension('htm')); + + $this->assertEquals('application/msword', Rhymix\Framework\MIME::getTypeByFilename('attachment.doc')); + $this->assertEquals('application/pdf', Rhymix\Framework\MIME::getTypeByFilename('라이믹스.pdf')); + $this->assertEquals('application/postscript', Rhymix\Framework\MIME::getTypeByFilename('MyGraphics.v2.eps')); + $this->assertEquals('application/vnd.ms-excel', Mail::returnMIMEType('MySpreadsheet.xls')); + $this->assertEquals('application/octet-stream', Mail::returnMIMEType('Untitled File')); + + $this->assertEquals('odt', Rhymix\Framework\MIME::getExtensionByType('application/vnd.oasis.opendocument.text')); + $this->assertEquals('jpg', Rhymix\Framework\MIME::getExtensionByType('image/jpeg')); + $this->assertEquals('mpeg', Rhymix\Framework\MIME::getExtensionByType('video/mpeg')); + $this->assertFalse(Rhymix\Framework\MIME::getExtensionByType('application/octet-stream')); + } +} From 63006e025a0ba871a29c77e120b4e69ae17fc36f Mon Sep 17 00:00:00 2001 From: Kijin Sung Date: Thu, 17 Mar 2016 16:04:36 +0900 Subject: [PATCH 02/21] Add Timer class --- common/framework/timer.php | 109 +++++++++++++++++++++++++++++ tests/unit/framework/TimerTest.php | 63 +++++++++++++++++ 2 files changed, 172 insertions(+) create mode 100644 common/framework/timer.php create mode 100644 tests/unit/framework/TimerTest.php diff --git a/common/framework/timer.php b/common/framework/timer.php new file mode 100644 index 000000000..b4650b663 --- /dev/null +++ b/common/framework/timer.php @@ -0,0 +1,109 @@ +assertGreaterThanOrEqual($t1, $started); + $this->assertLessThanOrEqual($t2, $started); + $this->assertGreaterThanOrEqual($t2 - $started, $elapsed); + $this->assertLessThanOrEqual($t3 - $t1, $elapsed); + $this->assertGreaterThan(0, $elapsed); + } + + function testNestedTimers() + { + $t1 = Rhymix\Framework\Timer::start(); + $t2 = Rhymix\Framework\Timer::start(); + $t3 = Rhymix\Framework\Timer::stop(); + $t4 = Rhymix\Framework\Timer::stop(); + + $this->assertGreaterThanOrEqual($t1, $t2); + $this->assertGreaterThan($t4, $t3); + } + + function testMultipleTimers() + { + $t1 = Rhymix\Framework\Timer::start('timer1'); + usleep(5000); + $t2 = Rhymix\Framework\Timer::start('timer2'); + + $t3 = Rhymix\Framework\Timer::stop('timer1'); + usleep(2000); + $t4 = Rhymix\Framework\Timer::stop('timer2'); + + $this->assertGreaterThanOrEqual($t1, $t2); + $this->assertGreaterThanOrEqual($t4, $t3); + } + + function testTimerFormat() + { + $t1 = Rhymix\Framework\Timer::start(); + $t2 = Rhymix\Framework\Timer::stopFormat(); + + $this->assertRegexp('/^[0-9\.]+ms$/', $t2); + } + + function testTimerSinceStartup() + { + $t1 = Rhymix\Framework\Timer::sinceStartup(); + $t2 = Rhymix\Framework\Timer::sinceStartup(); + + $this->assertGreaterThanOrEqual($t1, $t2); + + $t3 = Rhymix\Framework\Timer::sinceStartupFormat(); + $this->assertRegexp('/^[0-9\.]+ms$/', $t3); + } +} From 15a0c591dc0bbfb0dcf0d77b97f91b03d215186b Mon Sep 17 00:00:00 2001 From: Kijin Sung Date: Thu, 17 Mar 2016 16:22:18 +0900 Subject: [PATCH 03/21] Add Calendar class --- common/framework/calendar.php | 132 ++++++++++++++++++++++++++ tests/unit/framework/CalendarTest.php | 52 ++++++++++ 2 files changed, 184 insertions(+) create mode 100644 common/framework/calendar.php create mode 100644 tests/unit/framework/CalendarTest.php diff --git a/common/framework/calendar.php b/common/framework/calendar.php new file mode 100644 index 000000000..43f49a030 --- /dev/null +++ b/common/framework/calendar.php @@ -0,0 +1,132 @@ +assertEquals('January', Rhymix\Framework\Calendar::getMonthName('01')); + $this->assertEquals('October', Rhymix\Framework\Calendar::getMonthName('10')); + $this->assertEquals('Nov', Rhymix\Framework\Calendar::getMonthName(11, false)); + $this->assertEquals('Dec', Rhymix\Framework\Calendar::getMonthName(12, false)); + } + + public function testGetMonthStartDayOfWeek() + { + $this->assertEquals(5, Rhymix\Framework\Calendar::getMonthStartDayOfWeek(1, 2016)); + $this->assertEquals(1, Rhymix\Framework\Calendar::getMonthStartDayOfWeek(2, 2016)); + $this->assertEquals(2, Rhymix\Framework\Calendar::getMonthStartDayOfWeek(3, 2016)); + $this->assertEquals(5, Rhymix\Framework\Calendar::getMonthStartDayOfWeek(4, 2016)); + } + + public function testGetMonthDays() + { + $this->assertEquals(30, Rhymix\Framework\Calendar::getMonthDays(11, 2015)); + $this->assertEquals(31, Rhymix\Framework\Calendar::getMonthDays(12, 2015)); + $this->assertEquals(31, Rhymix\Framework\Calendar::getMonthDays(1, 2016)); + $this->assertEquals(29, Rhymix\Framework\Calendar::getMonthDays(2, 2016)); + } + + public function testGetMonthCalendar() + { + $target_201508 = array( + array(null, null, null, null, null, null, 1), + array(2, 3, 4, 5, 6, 7, 8), + array(9, 10, 11, 12, 13, 14, 15), + array(16, 17, 18, 19, 20, 21, 22), + array(23, 24, 25, 26, 27, 28, 29), + array(30, 31, null, null, null, null, null), + ); + + $target_201603 = array( + array(null, null, 1, 2, 3, 4, 5), + array(6, 7, 8, 9, 10, 11, 12), + array(13, 14, 15, 16, 17, 18, 19), + array(20, 21, 22, 23, 24, 25, 26), + array(27, 28, 29, 30, 31, null, null), + array(null, null, null, null, null, null, null), + ); + + $this->assertEquals($target_201508, Rhymix\Framework\Calendar::getMonthCalendar(8, 2015)); + $this->assertEquals($target_201603, Rhymix\Framework\Calendar::getMonthCalendar(3, 2016)); + } +} From 8b559598803fee0ca8d896819a86fca4995df752 Mon Sep 17 00:00:00 2001 From: Kijin Sung Date: Thu, 17 Mar 2016 16:24:27 +0900 Subject: [PATCH 04/21] Fix unit tests to accommodate exceptionally long-running tests --- tests/unit/framework/TimerTest.php | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/tests/unit/framework/TimerTest.php b/tests/unit/framework/TimerTest.php index d69623563..87dd9bc6d 100644 --- a/tests/unit/framework/TimerTest.php +++ b/tests/unit/framework/TimerTest.php @@ -47,7 +47,7 @@ class TimerTest extends \Codeception\TestCase\Test $t1 = Rhymix\Framework\Timer::start(); $t2 = Rhymix\Framework\Timer::stopFormat(); - $this->assertRegexp('/^[0-9\.]+ms$/', $t2); + $this->assertRegexp('/^[0-9\.,]+ms$/', $t2); } function testTimerSinceStartup() @@ -58,6 +58,6 @@ class TimerTest extends \Codeception\TestCase\Test $this->assertGreaterThanOrEqual($t1, $t2); $t3 = Rhymix\Framework\Timer::sinceStartupFormat(); - $this->assertRegexp('/^[0-9\.]+ms$/', $t3); + $this->assertRegexp('/^[0-9\.,]+ms$/', $t3); } } From 475c8d49a9264c8163420a8c8f4a1b685f9fa844 Mon Sep 17 00:00:00 2001 From: Kijin Sung Date: Thu, 17 Mar 2016 17:33:58 +0900 Subject: [PATCH 05/21] Fix short array syntax --- common/framework/calendar.php | 4 ++-- common/framework/timer.php | 2 +- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/common/framework/calendar.php b/common/framework/calendar.php index 43f49a030..b24e7b379 100644 --- a/common/framework/calendar.php +++ b/common/framework/calendar.php @@ -101,7 +101,7 @@ class Calendar $count = self::getMonthDays($month_number, $year); $initial_blank_cells = (7 + $start - $start_dow) % 7; $final_blank_cells = 42 - $count - $initial_blank_cells; - $temp = []; + $temp = array(); for ($i = 0; $i < $initial_blank_cells; $i++) { @@ -116,7 +116,7 @@ class Calendar $temp[] = null; } - $return = []; + $return = array(); for ($i = 0; $i < 6; $i++) { $week = []; diff --git a/common/framework/timer.php b/common/framework/timer.php index b4650b663..86842158a 100644 --- a/common/framework/timer.php +++ b/common/framework/timer.php @@ -10,7 +10,7 @@ class Timer /** * Timestamps are stored here. */ - protected static $_timestamps = []; + protected static $_timestamps = array(); /** * Start a timer. From 509893e791cf2e7372caed674aa25fae507d7263 Mon Sep 17 00:00:00 2001 From: Kijin Sung Date: Thu, 17 Mar 2016 17:41:03 +0900 Subject: [PATCH 06/21] Fix short array syntax and backwards test --- common/framework/calendar.php | 2 +- tests/unit/framework/TimerTest.php | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/common/framework/calendar.php b/common/framework/calendar.php index b24e7b379..67aaafbe4 100644 --- a/common/framework/calendar.php +++ b/common/framework/calendar.php @@ -119,7 +119,7 @@ class Calendar $return = array(); for ($i = 0; $i < 6; $i++) { - $week = []; + $week = array(); for ($j = 0; $j < 7; $j++) { $week[] = array_shift($temp); diff --git a/tests/unit/framework/TimerTest.php b/tests/unit/framework/TimerTest.php index 87dd9bc6d..b0427d688 100644 --- a/tests/unit/framework/TimerTest.php +++ b/tests/unit/framework/TimerTest.php @@ -25,7 +25,7 @@ class TimerTest extends \Codeception\TestCase\Test $t4 = Rhymix\Framework\Timer::stop(); $this->assertGreaterThanOrEqual($t1, $t2); - $this->assertGreaterThan($t4, $t3); + $this->assertGreaterThan($t3, $t4); } function testMultipleTimers() From 97c10ab2cf4e8a141e95fb47ba7bd6c503ec7df0 Mon Sep 17 00:00:00 2001 From: Kijin Sung Date: Thu, 17 Mar 2016 17:45:15 +0900 Subject: [PATCH 07/21] Add some delays to timer test --- tests/unit/framework/TimerTest.php | 10 +++++++++- 1 file changed, 9 insertions(+), 1 deletion(-) diff --git a/tests/unit/framework/TimerTest.php b/tests/unit/framework/TimerTest.php index b0427d688..1c9783c0d 100644 --- a/tests/unit/framework/TimerTest.php +++ b/tests/unit/framework/TimerTest.php @@ -5,9 +5,13 @@ class TimerTest extends \Codeception\TestCase\Test function testStartStop() { $t1 = microtime(true); + usleep(1000); $started = Rhymix\Framework\Timer::start(); + usleep(1000); $t2 = microtime(true); + usleep(1000); $elapsed = Rhymix\Framework\Timer::stop(); + usleep(1000); $t3 = microtime(true); $this->assertGreaterThanOrEqual($t1, $started); @@ -20,8 +24,11 @@ class TimerTest extends \Codeception\TestCase\Test function testNestedTimers() { $t1 = Rhymix\Framework\Timer::start(); + usleep(1000); $t2 = Rhymix\Framework\Timer::start(); + usleep(1000); $t3 = Rhymix\Framework\Timer::stop(); + usleep(1000); $t4 = Rhymix\Framework\Timer::stop(); $this->assertGreaterThanOrEqual($t1, $t2); @@ -33,7 +40,7 @@ class TimerTest extends \Codeception\TestCase\Test $t1 = Rhymix\Framework\Timer::start('timer1'); usleep(5000); $t2 = Rhymix\Framework\Timer::start('timer2'); - + usleep(1000); $t3 = Rhymix\Framework\Timer::stop('timer1'); usleep(2000); $t4 = Rhymix\Framework\Timer::stop('timer2'); @@ -45,6 +52,7 @@ class TimerTest extends \Codeception\TestCase\Test function testTimerFormat() { $t1 = Rhymix\Framework\Timer::start(); + usleep(1000); $t2 = Rhymix\Framework\Timer::stopFormat(); $this->assertRegexp('/^[0-9\.,]+ms$/', $t2); From 59ee4a7387831e127f245d545eff7aebeb975008 Mon Sep 17 00:00:00 2001 From: Kijin Sung Date: Thu, 17 Mar 2016 19:46:32 +0900 Subject: [PATCH 08/21] Add pagination class --- common/framework/pagination.php | 153 ++++++++++++++++++++++++ tests/unit/framework/PaginationTest.php | 48 ++++++++ 2 files changed, 201 insertions(+) create mode 100644 common/framework/pagination.php create mode 100644 tests/unit/framework/PaginationTest.php diff --git a/common/framework/pagination.php b/common/framework/pagination.php new file mode 100644 index 000000000..cc26906e4 --- /dev/null +++ b/common/framework/pagination.php @@ -0,0 +1,153 @@ + $total_pages) + { + $last_shown = $total_pages; + } + } + else + { + $first_shown = $current_page - floor(($count - 1) / 2); + if ($first_shown < 1) + { + $first_shown = 1; + } + $last_shown = $first_shown + $count - 1; + if ($last_shown > $total_pages) + { + $last_shown = $total_pages; + $first_shown = max(1, $last_shown - $count + 1); + } + } + + // Open the
tag. + $return = array('