Search moodle.org's
Developer Documentation

See Release Notes

  • Bug fixes for general core bugs in 3.11.x will end 14 Nov 2022 (12 months plus 6 months extension).
  • Bug fixes for security issues in 3.11.x will end 13 Nov 2023 (18 months plus 12 months extension).
  • PHP version: minimum PHP 7.3.0 Note: minimum PHP version has increased since Moodle 3.10. PHP 7.4.x is supported too.

Differences Between: [Versions 310 and 311] [Versions 311 and 401] [Versions 311 and 402] [Versions 311 and 403] [Versions 39 and 311]

   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 script for message class.
  19   *
  20   * Test classes for \core\message\inbound.
  21   *
  22   * @package core
  23   * @category test
  24   * @copyright 2015 Andrew Nicols <andrew@nicols.co.uk>
  25   * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  26   */
  27  
  28  namespace core;
  29  
  30  defined('MOODLE_INTERNAL') || die();
  31  
  32  /**
  33   * Test script for message class.
  34   *
  35   * Test classes for \core\message\inbound.
  36   *
  37   * @package core
  38   * @category test
  39   * @copyright 2015 Andrew Nicols <andrew@nicols.co.uk>
  40   * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  41   */
  42  class messageinbound_test extends \advanced_testcase {
  43  
  44      /**
  45       * @dataProvider message_inbound_handler_trim_testprovider
  46       */
  47      public function test_messageinbound_handler_trim($file, $source, $expectedplain, $expectedhtml) {
  48          $this->resetAfterTest();
  49  
  50          $mime = \Horde_Mime_Part::parseMessage($source);
  51          if ($plainpartid = $mime->findBody('plain')) {
  52              $messagedata = new \stdClass();
  53              $messagedata->plain = $mime->getPart($plainpartid)->getContents();
  54              $messagedata->html = '';
  55  
  56              list($message, $format) = test_handler::remove_quoted_text($messagedata);
  57              list ($message, $expectedplain) = preg_replace("#\r\n#", "\n", array($message, $expectedplain));
  58  
  59              // Normalise line endings on both strings.
  60              $this->assertEquals($expectedplain, $message);
  61              $this->assertEquals(FORMAT_PLAIN, $format);
  62          }
  63  
  64          if ($htmlpartid = $mime->findBody('html')) {
  65              $messagedata = new \stdClass();
  66              $messagedata->plain = '';
  67              $messagedata->html = $mime->getPart($htmlpartid)->getContents();
  68  
  69              list($message, $format) = test_handler::remove_quoted_text($messagedata);
  70  
  71              // Normalise line endings on both strings.
  72              list ($message, $expectedhtml) = preg_replace("#\r\n#", "\n", array($message, $expectedhtml));
  73              $this->assertEquals($expectedhtml, $message);
  74              $this->assertEquals(FORMAT_PLAIN, $format);
  75          }
  76      }
  77  
  78      public function message_inbound_handler_trim_testprovider() {
  79          $fixturesdir = realpath(__DIR__ . '/fixtures/messageinbound/');
  80          $tests = array();
  81          $iterator = new \RecursiveIteratorIterator(
  82                  new \RecursiveDirectoryIterator($fixturesdir),
  83                  \RecursiveIteratorIterator::LEAVES_ONLY);
  84  
  85          foreach ($iterator as $file) {
  86              if (!preg_match('/\.test$/', $file)) {
  87                  continue;
  88              }
  89  
  90              try {
  91                  $testdata = $this->read_test_file($file, $fixturesdir);
  92              } catch (\Exception $e) {
  93                  die($e->getMessage());
  94              }
  95  
  96              $test = array(
  97                      // The filename.
  98                      basename($file),
  99  
 100                      $testdata['FULLSOURCE'],
 101  
 102                      // The plaintext component of the message.
 103                      $testdata['EXPECTEDPLAIN'],
 104  
 105                      // The HTML component of the message.
 106                      $testdata['EXPECTEDHTML'],
 107                  );
 108  
 109              $tests[basename($file)] = $test;
 110          }
 111          return $tests;
 112      }
 113  
 114      protected function read_test_file(\SplFileInfo $file, $fixturesdir) {
 115          // Break on the --[TOKEN]-- tags in the file.
 116          $content = file_get_contents($file->getRealPath());
 117          $content = preg_replace("#\r\n#", "\n", $content);
 118          $tokens = preg_split('#(?:^|\n*)----([A-Z]+)----\n#', $content,
 119                  null, PREG_SPLIT_DELIM_CAPTURE);
 120          $sections = array(
 121              // Key              => Required.
 122              'FULLSOURCE'        => true,
 123              'EXPECTEDPLAIN'     => true,
 124              'EXPECTEDHTML'      => true,
 125              'CLIENT'            => true, // Required but not needed for tests, just for documentation.
 126          );
 127          $section = null;
 128          $data = array();
 129          foreach ($tokens as $i => $token) {
 130              if (null === $section && empty($token)) {
 131                  continue; // Skip leading blank.
 132              }
 133              if (null === $section) {
 134                  if (!isset($sections[$token])) {
 135                      throw new \coding_exception(sprintf(
 136                          'The test file "%s" should not contain a section named "%s".',
 137                          basename($file),
 138                          $token
 139                      ));
 140                  }
 141                  $section = $token;
 142                  continue;
 143              }
 144              $sectiondata = $token;
 145              $data[$section] = $sectiondata;
 146              $section = $sectiondata = null;
 147          }
 148          foreach ($sections as $section => $required) {
 149              if ($required && !isset($data[$section])) {
 150                  throw new \coding_exception(sprintf(
 151                      'The test file "%s" must have a section named "%s".',
 152                      str_replace($fixturesdir.'/', '', $file),
 153                      $section
 154                  ));
 155              }
 156          }
 157          return $data;
 158      }
 159  }
 160  
 161  /**
 162   * Class test_handler
 163   */
 164  class test_handler extends \core\message\inbound\handler {
 165  
 166      public static function remove_quoted_text($messagedata) {
 167          return parent::remove_quoted_text($messagedata);
 168      }
 169  
 170      public function get_name() {}
 171  
 172      public function get_description() {}
 173  
 174      public function process_message(\stdClass $record, \stdClass $messagedata) {}
 175  }