Search moodle.org's
Developer Documentation

See Release Notes

  • Bug fixes for general core bugs in 3.10.x will end 8 November 2021 (12 months).
  • Bug fixes for security issues in 3.10.x will end 9 May 2022 (18 months).
  • PHP version: minimum PHP 7.2.0 Note: minimum PHP version has increased since Moodle 3.8. PHP 7.3.x and 7.4.x are supported too.

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

   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  
  26  defined('MOODLE_INTERNAL') || die();
  27  
  28  global $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   */
  38  class 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')) {
  48              $messagedata = new stdClass();
  49              $messagedata->plain = $mime->getPart($plainpartid)->getContents();
  50              $messagedata->html = '';
  51  
  52              list($message, $format) = test_handler::remove_quoted_text($messagedata);
  53              list ($message, $expectedplain) = preg_replace("#\r\n#", "\n", array($message, $expectedplain));
  54  
  55              // Normalise line endings on both strings.
  56              $this->assertEquals($expectedplain, $message);
  57              $this->assertEquals(FORMAT_PLAIN, $format);
  58          }
  59  
  60          if ($htmlpartid = $mime->findBody('html')) {
  61              $messagedata = new stdClass();
  62              $messagedata->plain = '';
  63              $messagedata->html = $mime->getPart($htmlpartid)->getContents();
  64  
  65              list($message, $format) = test_handler::remove_quoted_text($messagedata);
  66  
  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);
  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.
 112          $content = file_get_contents($file->getRealPath());
 113          $content = preg_replace("#\r\n#", "\n", $content);
 114          $tokens = preg_split('#(?:^|\n*)----([A-Z]+)----\n#', $content,
 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.
 122          );
 123          $section = null;
 124          $data = array();
 125          foreach ($tokens as $i => $token) {
 126              if (null === $section && empty($token)) {
 127                  continue; // Skip leading blank.
 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  
 157  /**
 158   * Class test_handler
 159   */
 160  class test_handler extends \core\message\inbound\handler {
 161  
 162      public static function remove_quoted_text($messagedata) {
 163          return parent::remove_quoted_text($messagedata);
 164      }
 165  
 166      public function get_name() {}
 167  
 168      public function get_description() {}
 169  
 170      public function process_message(stdClass $record, stdClass $messagedata) {}
 171  }