Search moodle.org's
Developer Documentation

See Release Notes
Long Term Support Release

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