Merge branch 'wip-MDL-62796-master' of git://github.com/marinaglancy/moodle
[moodle.git] / lib / tests / messageinbound_test.php
blob0d4b50f6d5f2389731fb529b96d36288f0eaefb6
1 <?php
2 // This file is part of Moodle - http://moodle.org/
3 //
4 // Moodle is free software: you can redistribute it and/or modify
5 // it under the terms of the GNU General Public License as published by
6 // the Free Software Foundation, either version 3 of the License, or
7 // (at your option) any later version.
8 //
9 // Moodle is distributed in the hope that it will be useful,
10 // but WITHOUT ANY WARRANTY; without even the implied warranty of
11 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 // GNU General Public License for more details.
14 // You should have received a copy of the GNU General Public License
15 // along with Moodle. If not, see <http://www.gnu.org/licenses/>.
17 /**
18 * Test classes for \core\message\inbound.
20 * @package core_message
21 * @category test
22 * @copyright 2015 Andrew Nicols <andrew@nicols.co.uk>
23 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
26 defined('MOODLE_INTERNAL') || die();
28 global $CFG;
30 /**
31 * Test script for message class.
33 * @package core_message
34 * @category test
35 * @copyright 2015 Andrew Nicols <andrew@nicols.co.uk>
36 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
38 class core_messageinbound_testcase extends advanced_testcase {
40 /**
41 * @dataProvider message_inbound_handler_trim_testprovider
43 public function test_messageinbound_handler_trim($file, $source, $expectedplain, $expectedhtml) {
44 $this->resetAfterTest();
46 $mime = Horde_Mime_Part::parseMessage($source);
47 if ($plainpartid = $mime->findBody('plain')) {
48 $messagedata = new stdClass();
49 $messagedata->plain = $mime->getPart($plainpartid)->getContents();
50 $messagedata->html = '';
52 list($message, $format) = test_handler::remove_quoted_text($messagedata);
53 list ($message, $expectedplain) = preg_replace("#\r\n#", "\n", array($message, $expectedplain));
55 // Normalise line endings on both strings.
56 $this->assertEquals($expectedplain, $message);
57 $this->assertEquals(FORMAT_PLAIN, $format);
60 if ($htmlpartid = $mime->findBody('html')) {
61 $messagedata = new stdClass();
62 $messagedata->plain = '';
63 $messagedata->html = $mime->getPart($htmlpartid)->getContents();
65 list($message, $format) = test_handler::remove_quoted_text($messagedata);
67 // Normalise line endings on both strings.
68 list ($message, $expectedhtml) = preg_replace("#\r\n#", "\n", array($message, $expectedhtml));
69 $this->assertEquals($expectedhtml, $message);
70 $this->assertEquals(FORMAT_PLAIN, $format);
74 public function message_inbound_handler_trim_testprovider() {
75 $fixturesdir = realpath(__DIR__ . '/fixtures/messageinbound/');
76 $tests = array();
77 $iterator = new \RecursiveIteratorIterator(
78 new \RecursiveDirectoryIterator($fixturesdir),
79 \RecursiveIteratorIterator::LEAVES_ONLY);
81 foreach ($iterator as $file) {
82 if (!preg_match('/\.test$/', $file)) {
83 continue;
86 try {
87 $testdata = $this->read_test_file($file, $fixturesdir);
88 } catch (\Exception $e) {
89 die($e->getMessage());
92 $test = array(
93 // The filename.
94 basename($file),
96 $testdata['FULLSOURCE'],
98 // The plaintext component of the message.
99 $testdata['EXPECTEDPLAIN'],
101 // The HTML component of the message.
102 $testdata['EXPECTEDHTML'],
105 $tests[basename($file)] = $test;
107 return $tests;
110 protected function read_test_file(\SplFileInfo $file, $fixturesdir) {
111 // Break on the --[TOKEN]-- tags in the file.
112 $content = file_get_contents($file->getRealPath());
113 $content = preg_replace("#\r\n#", "\n", $content);
114 $tokens = preg_split('#(?:^|\n*)----([A-Z]+)----\n#', file_get_contents($file->getRealPath()),
115 null, PREG_SPLIT_DELIM_CAPTURE);
116 $sections = array(
117 // Key => Required.
118 'FULLSOURCE' => true,
119 'EXPECTEDPLAIN' => true,
120 'EXPECTEDHTML' => true,
121 'CLIENT' => true, // Required but not needed for tests, just for documentation.
123 $section = null;
124 $data = array();
125 foreach ($tokens as $i => $token) {
126 if (null === $section && empty($token)) {
127 continue; // Skip leading blank.
129 if (null === $section) {
130 if (!isset($sections[$token])) {
131 throw new coding_exception(sprintf(
132 'The test file "%s" should not contain a section named "%s".',
133 basename($file),
134 $token
137 $section = $token;
138 continue;
140 $sectiondata = $token;
141 $data[$section] = $sectiondata;
142 $section = $sectiondata = null;
144 foreach ($sections as $section => $required) {
145 if ($required && !isset($data[$section])) {
146 throw new coding_exception(sprintf(
147 'The test file "%s" must have a section named "%s".',
148 str_replace($fixturesdir.'/', '', $file),
149 $section
153 return $data;
158 * Class test_handler
160 class test_handler extends \core\message\inbound\handler {
162 public static function remove_quoted_text($messagedata) {
163 return parent::remove_quoted_text($messagedata);
166 public function get_name() {}
168 public function get_description() {}
170 public function process_message(stdClass $record, stdClass $messagedata) {}