From c712250d6b0cfd4bed9484366bfa437ab5fe24b2 Mon Sep 17 00:00:00 2001 From: Andrew Nicols Date: Tue, 28 Apr 2015 09:31:10 +0800 Subject: [PATCH] MDL-48002 inbound Unit tests from fixtures for quoted text --- lib/tests/fixtures/messageinbound/SAMPLE | 3 + lib/tests/messageinbound_test.php | 152 +++++++++++++++++++++++ 2 files changed, 155 insertions(+) create mode 100644 lib/tests/fixtures/messageinbound/SAMPLE create mode 100644 lib/tests/messageinbound_test.php diff --git a/lib/tests/fixtures/messageinbound/SAMPLE b/lib/tests/fixtures/messageinbound/SAMPLE new file mode 100644 index 00000000000..b3231346f9c --- /dev/null +++ b/lib/tests/fixtures/messageinbound/SAMPLE @@ -0,0 +1,3 @@ +----EXPECTEDPLAIN---- +----EXPECTEDHTML---- +----FULLSOURCE---- diff --git a/lib/tests/messageinbound_test.php b/lib/tests/messageinbound_test.php new file mode 100644 index 00000000000..d367cdadd88 --- /dev/null +++ b/lib/tests/messageinbound_test.php @@ -0,0 +1,152 @@ +. + +/** + * Test classes for \core\message\inbound. + * + * @package core_message + * @category test + * @copyright 2015 Andrew Nicols + * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later + */ + +defined('MOODLE_INTERNAL') || die(); + +global $CFG; + +/** + * Test script for message class. + * + * @package core_message + * @category test + * @copyright 2015 Andrew Nicols + * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later + */ +class core_messageinbound_testcase extends advanced_testcase { + + /** + * @dataProvider message_inbound_handler_trim_testprovider + */ + public function test_messageinbound_handler_trim($file, $source, $expectedplain, $expectedhtml) { + $this->resetAfterTest(); + + $mime = Horde_Mime_Part::parseMessage($source); + if ($plainpartid = $mime->findBody('plain')) { + $messagedata = $mime->getPart($plainpartid)->getContents(); + + $linecount = test_handler::get_linecount_to_remove($messagedata); + $actual = test_handler::remove_quoted_text($messagedata, $linecount); + $this->assertEquals($expectedplain, $actual); + } + + if ($htmlpartid = $mime->findBody('html')) { + $messagedata = html_to_text($mime->getPart($htmlpartid)->getContents()); + + $linecount = test_handler::get_linecount_to_remove($messagedata); + $actual = test_handler::remove_quoted_text($messagedata, $linecount); + $this->assertEquals($expectedhtml, $actual); + } + } + + public function message_inbound_handler_trim_testprovider() { + $fixturesdir = realpath(__DIR__ . '/fixtures/messageinbound/'); + $tests = array(); + $iterator = new \RecursiveIteratorIterator( + new \RecursiveDirectoryIterator($fixturesdir), + \RecursiveIteratorIterator::LEAVES_ONLY); + + foreach ($iterator as $file) { + if (!preg_match('/\.test$/', $file)) { + continue; + } + + try { + $testdata = $this->read_test_file($file, $fixturesdir); + } catch (\Exception $e) { + die($e->getMessage()); + } + + $test = array( + // The filename. + basename($file), + + $testdata['FULLSOURCE'], + + // The plaintext component of the message. + $testdata['EXPECTEDPLAIN'], + + // The HTML component of the message. + $testdata['EXPECTEDHTML'], + ); + + $tests[basename($file)] = $test; + } + return $tests; + } + + protected function read_test_file(\SplFileInfo $file, $fixturesdir) { + // Break on the --[TOKEN]-- tags in the file. + $tokens = preg_split('#(?:^|\n*)----([A-Z-]+)----\n#', file_get_contents($file->getRealPath()), + null, PREG_SPLIT_DELIM_CAPTURE); + $sections = array( + // Key => Required. + 'FULLSOURCE' => true, + 'EXPECTEDPLAIN' => true, + 'EXPECTEDHTML' => true, + ); + $section = null; + foreach ($tokens as $i => $token) { + if (null === $section && empty($token)) { + continue; // skip leading blank + } + if (null === $section) { + if (!isset($sections[$token])) { + throw new coding_exception(sprintf( + 'The test file "%s" should not contain a section named "%s".', + basename($file), + $token + )); + } + $section = $token; + continue; + } + $sectiondata = $token; + $data[$section] = $sectiondata; + $section = $sectiondata = null; + } + foreach ($sections as $section => $required) { + if ($required && !isset($data[$section])) { + throw new coding_exception(sprintf( + 'The test file "%s" must have a section named "%s".', + str_replace($fixturesdir.'/', '', $file), + $section + )); + } + } + return $data; + } +} + +class test_handler extends \mod_forum\message\inbound\reply_handler { + + public static function remove_quoted_text($text, $linecount = 1) { + return parent::remove_quoted_text($text, $linecount); + } + + public static function get_linecount_to_remove($messagedata) { + return parent::get_linecount_to_remove($messagedata); + } +}