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 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  namespace mod_chat;
  18  
  19  defined('MOODLE_INTERNAL') || die();
  20  
  21  global $CFG;
  22  require_once($CFG->dirroot . '/mod/chat/lib.php');
  23  
  24  /**
  25   * Tests for format_message.
  26   *
  27   * @package    mod_chat
  28   * @copyright  2016 Andrew NIcols
  29   * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  30   */
  31  class format_message_test extends \advanced_testcase {
  32  
  33      const USER_CURRENT = 1;
  34      const USER_OTHER = 2;
  35  
  36      public function chat_format_message_manually_provider() {
  37          $dateregexp = '\d{2}:\d{2}';
  38          return [
  39              'Beep everyone' => [
  40                  'message'       => 'beep all',
  41                  'issystem'      => false,
  42                  'willreturn'    => true,
  43                  'expecttext'    => "/^{$dateregexp}: " . get_string('messagebeepseveryone', 'chat', '__CURRENTUSER__') . ': /',
  44                  'refreshusers'  => false,
  45                  'beep'          => true,
  46              ],
  47              'Beep the current user' => [
  48                  'message'       => 'beep __CURRENTUSER__',
  49                  'issystem'      => false,
  50                  'willreturn'    => true,
  51                  'expecttext'    => "/^{$dateregexp}: " . get_string('messagebeepsyou', 'chat', '__CURRENTUSER__') . ': /',
  52                  'refreshusers'  => false,
  53                  'beep'          => true,
  54              ],
  55              'Beep another user' => [
  56                  'message'       => 'beep __OTHERUSER__',
  57                  'issystem'      => false,
  58                  'willreturn'    => false,
  59                  'expecttext'    => null,
  60                  'refreshusers'  => null,
  61                  'beep'          => null,
  62              ],
  63              'Malformed beep' => [
  64                  'message'       => 'beep',
  65                  'issystem'      => false,
  66                  'willreturn'    => true,
  67                  'expecttext'    => "/^{$dateregexp} __CURRENTUSER_FIRST__: beep$/",
  68                  'refreshusers'  => false,
  69                  'beep'          => false,
  70              ],
  71              '/me says' => [
  72                  'message'       => '/me writes a test',
  73                  'issystem'      => false,
  74                  'willreturn'    => true,
  75                  'expecttext'    => "/^{$dateregexp}: \*\*\* __CURRENTUSER_FIRST__ writes a test$/",
  76                  'refreshusers'  => false,
  77                  'beep'          => false,
  78              ],
  79              'Invalid command' => [
  80                  'message'       => '/help',
  81                  'issystem'      => false,
  82                  'willreturn'    => true,
  83                  'expecttext'    => "/^{$dateregexp} __CURRENTUSER_FIRST__: \/help$/",
  84                  'refreshusers'  => false,
  85                  'beep'          => false,
  86              ],
  87              'To user' => [
  88                  'message'       => 'To Bernard:I love tests',
  89                  'issystem'      => false,
  90                  'willreturn'    => true,
  91                  'expecttext'    => "/^{$dateregexp}: __CURRENTUSER_FIRST__ " . get_string('saidto', 'chat') . " Bernard: I love tests$/",
  92                  'refreshusers'  => false,
  93                  'beep'          => false,
  94              ],
  95              'To user trimmed' => [
  96                  'message'       => 'To Bernard: I love tests',
  97                  'issystem'      => false,
  98                  'willreturn'    => true,
  99                  'expecttext'    => "/^{$dateregexp}: __CURRENTUSER_FIRST__ " . get_string('saidto', 'chat') . " Bernard: I love tests$/",
 100                  'refreshusers'  => false,
 101                  'beep'          => false,
 102              ],
 103              'System: enter' => [
 104                  'message'       => 'enter',
 105                  'issystem'      => true,
 106                  'willreturn'    => true,
 107                  'expecttext'    => "/^{$dateregexp}: " . get_string('messageenter', 'chat', '__CURRENTUSER__') . "$/",
 108                  'refreshusers'  => true,
 109                  'beep'          => false,
 110              ],
 111              'System: exit' => [
 112                  'message'       => 'exit',
 113                  'issystem'      => true,
 114                  'willreturn'    => true,
 115                  'expecttext'    => "/^{$dateregexp}: " . get_string('messageexit', 'chat', '__CURRENTUSER__') . "$/",
 116                  'refreshusers'  => true,
 117                  'beep'          => false,
 118              ],
 119          ];
 120      }
 121  
 122      /**
 123       * @dataProvider chat_format_message_manually_provider
 124       */
 125      public function test_chat_format_message_manually($messagetext, $issystem, $willreturn,
 126              $expecttext, $refreshusers, $expectbeep) {
 127  
 128          $this->resetAfterTest();
 129  
 130          $course = $this->getDataGenerator()->create_course();
 131          $currentuser = $this->getDataGenerator()->create_user();
 132          $this->setUser($currentuser);
 133          $otheruser = $this->getDataGenerator()->create_user();
 134  
 135          // Replace the message texts.
 136          // These can't be done in the provider because it runs before the
 137          // test starts.
 138          $messagetext = str_replace('__CURRENTUSER__', $currentuser->id, $messagetext);
 139          $messagetext = str_replace('__OTHERUSER__', $otheruser->id, $messagetext);
 140  
 141          $message = (object) [
 142              'message'   => $messagetext,
 143              'timestamp' => time(),
 144              'issystem'  => $issystem,
 145          ];
 146  
 147          $result = chat_format_message_manually($message, $course->id, $currentuser, $currentuser);
 148  
 149          if (!$willreturn) {
 150              $this->assertFalse($result);
 151          } else {
 152              $this->assertNotFalse($result);
 153              if (!empty($expecttext)) {
 154                  $expecttext = str_replace('__CURRENTUSER__', fullname($currentuser), $expecttext);
 155                  $expecttext = str_replace('__CURRENTUSER_FIRST__', $currentuser->firstname, $expecttext);
 156                  $this->assertMatchesRegularExpression($expecttext, $result->text);
 157              }
 158  
 159              $this->assertEquals($refreshusers, $result->refreshusers);
 160              $this->assertEquals($expectbeep, $result->beep);
 161          }
 162      }
 163  }