MDL-50790 inbound: Stop incorrect amount of lines getting removed.
[moodle.git] / lib / tests / messageinbound_test.php
CommitLineData
a8a742cb
AN
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.
13//
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/>.
16
17/**
18 * Test classes for \core\message\inbound.
19 *
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
24 */
25
26defined('MOODLE_INTERNAL') || die();
27
28global $CFG;
29
30/**
31 * Test script for message class.
32 *
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
37 */
38class core_messageinbound_testcase extends advanced_testcase {
39
40 /**
41 * @dataProvider message_inbound_handler_trim_testprovider
42 */
43 public function test_messageinbound_handler_trim($file, $source, $expectedplain, $expectedhtml) {
44 $this->resetAfterTest();
45
46 $mime = Horde_Mime_Part::parseMessage($source);
47 if ($plainpartid = $mime->findBody('plain')) {
0174fa9b
AA
48 $messagedata = new stdClass();
49 $messagedata->plain = $mime->getPart($plainpartid)->getContents();
50 $messagedata->html = '';
a8a742cb 51
0174fa9b 52 list($message, $format) = test_handler::remove_quoted_text($messagedata);
802dbbf8 53 list ($message, $expectedplain) = preg_replace("#\r\n#", "\n", array($message, $expectedplain));
2d07240f 54
802dbbf8
AN
55 // Normalise line endings on both strings.
56 $this->assertEquals($expectedplain, $message);
0174fa9b 57 $this->assertEquals(FORMAT_PLAIN, $format);
a8a742cb
AN
58 }
59
60 if ($htmlpartid = $mime->findBody('html')) {
0174fa9b
AA
61 $messagedata = new stdClass();
62 $messagedata->plain = '';
63 $messagedata->html = $mime->getPart($htmlpartid)->getContents();
a8a742cb 64
0174fa9b 65 list($message, $format) = test_handler::remove_quoted_text($messagedata);
2d07240f 66
802dbbf8
AN
67 // Normalise line endings on both strings.
68 list ($message, $expectedhtml) = preg_replace("#\r\n#", "\n", array($message, $expectedhtml));
69 $this->assertEquals($expectedhtml, $message);
0174fa9b 70 $this->assertEquals(FORMAT_PLAIN, $format);
a8a742cb
AN
71 }
72 }
73
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);
80
81 foreach ($iterator as $file) {
82 if (!preg_match('/\.test$/', $file)) {
83 continue;
84 }
85
86 try {
87 $testdata = $this->read_test_file($file, $fixturesdir);
88 } catch (\Exception $e) {
89 die($e->getMessage());
90 }
91
92 $test = array(
93 // The filename.
94 basename($file),
95
96 $testdata['FULLSOURCE'],
97
98 // The plaintext component of the message.
99 $testdata['EXPECTEDPLAIN'],
100
101 // The HTML component of the message.
102 $testdata['EXPECTEDHTML'],
103 );
104
105 $tests[basename($file)] = $test;
106 }
107 return $tests;
108 }
109
110 protected function read_test_file(\SplFileInfo $file, $fixturesdir) {
111 // Break on the --[TOKEN]-- tags in the file.
802dbbf8
AN
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()),
a8a742cb
AN
115 null, PREG_SPLIT_DELIM_CAPTURE);
116 $sections = array(
117 // Key => Required.
118 'FULLSOURCE' => true,
119 'EXPECTEDPLAIN' => true,
120 'EXPECTEDHTML' => true,
5e4438b5 121 'CLIENT' => true, // Required but not needed for tests, just for documentation.
a8a742cb
AN
122 );
123 $section = null;
0174fa9b 124 $data = array();
a8a742cb
AN
125 foreach ($tokens as $i => $token) {
126 if (null === $section && empty($token)) {
0174fa9b 127 continue; // Skip leading blank.
a8a742cb
AN
128 }
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
135 ));
136 }
137 $section = $token;
138 continue;
139 }
140 $sectiondata = $token;
141 $data[$section] = $sectiondata;
142 $section = $sectiondata = null;
143 }
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
150 ));
151 }
152 }
153 return $data;
154 }
155}
156
0174fa9b
AA
157/**
158 * Class test_handler
159 */
160class test_handler extends \core\message\inbound\handler {
a8a742cb 161
0174fa9b
AA
162 public static function remove_quoted_text($messagedata) {
163 return parent::remove_quoted_text($messagedata);
a8a742cb
AN
164 }
165
0174fa9b
AA
166 public function get_name() {}
167
168 public function get_description() {}
169
170 public function process_message(stdClass $record, stdClass $messagedata) {}
a8a742cb 171}