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  namespace core_message;
  18  
  19  defined('MOODLE_INTERNAL') || die();
  20  
  21  global $CFG;
  22  
  23  require_once($CFG->dirroot . '/message/tests/messagelib_test.php');
  24  
  25  /**
  26   * Tests for the message helper class.
  27   *
  28   * @package core_message
  29   * @category test
  30   * @copyright 2018 Jake Dallimore <jrhdallimore@gmail.com>
  31   * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  32   */
  33  class helper_test extends \advanced_testcase {
  34  
  35      public function setUp(): void {
  36          $this->resetAfterTest(true);
  37      }
  38  
  39      public function test_get_member_info_ordering() {
  40          // Create a conversation with several users.
  41          $user1 = self::getDataGenerator()->create_user();
  42          $user2 = self::getDataGenerator()->create_user();
  43          $user3 = self::getDataGenerator()->create_user();
  44          $user4 = self::getDataGenerator()->create_user();
  45  
  46          \core_message\api::create_conversation(
  47              \core_message\api::MESSAGE_CONVERSATION_TYPE_GROUP,
  48              [
  49                  $user1->id,
  50                  $user2->id,
  51                  $user3->id,
  52                  $user4->id,
  53              ],
  54              'Group conversation'
  55          );
  56  
  57          // Verify that the member information comes back in the same order that we specified in the input array.
  58          $memberinfo = \core_message\helper::get_member_info($user1->id, [$user3->id, $user4->id, $user2->id]);
  59          $this->assertEquals($user3->id, array_shift($memberinfo)->id);
  60          $this->assertEquals($user4->id, array_shift($memberinfo)->id);
  61          $this->assertEquals($user2->id, array_shift($memberinfo)->id);
  62      }
  63  
  64      /**
  65       * Test search_get_user_details returns the correct profile data when $CFG->messagingallusers is disabled.
  66       */
  67      public function test_search_get_user_details_sitewide_disabled() {
  68          global $DB;
  69          set_config('messagingallusers', false);
  70  
  71          // Two students sharing course 1, visible profile within course (no groups).
  72          $user1 = $this->getDataGenerator()->create_user();
  73          $user2 = $this->getDataGenerator()->create_user();
  74          $course1 = $this->getDataGenerator()->create_course((object) ['groupmode' => 0]);
  75          $this->getDataGenerator()->enrol_user($user1->id, $course1->id);
  76          $this->getDataGenerator()->enrol_user($user2->id, $course1->id);
  77  
  78          // A teacher in course 1.
  79          $user3 = $this->getDataGenerator()->create_user();
  80          $this->getDataGenerator()->enrol_user($user3->id, $course1->id, 'editingteacher');
  81  
  82          // Two students sharing course 2, separate groups (profiles not visible to one another).
  83          // Note: no groups are created here, but separate groups mode alone is enough to restrict profile access.
  84          $user4 = $this->getDataGenerator()->create_user();
  85          $user5 = $this->getDataGenerator()->create_user();
  86          $course2 = $this->getDataGenerator()->create_course((object) ['groupmode' => 1]);
  87          $this->getDataGenerator()->enrol_user($user4->id, $course2->id);
  88          $this->getDataGenerator()->enrol_user($user5->id, $course2->id);
  89  
  90          // A teacher in course 2.
  91          $user6 = $this->getDataGenerator()->create_user();
  92          $this->getDataGenerator()->enrol_user($user6->id, $course2->id, 'editingteacher');
  93  
  94          // Teacher and course contact in course 3.
  95          $user7 = $this->getDataGenerator()->create_user();
  96          $course3 = $this->getDataGenerator()->create_course();
  97          $this->getDataGenerator()->enrol_user($user7->id, $course3->id, 'editingteacher');
  98          $teacherrole = $DB->get_record('role', array('shortname' => 'editingteacher'));
  99  
 100          // Make teachers course contacts.
 101          set_config('coursecontact', $teacherrole->id);
 102  
 103          // User 1 should be able to see users within their course, but not course contacts or students in other courses.
 104          $this->setUser($user1);
 105          $this->assertNotEmpty(\core_message\helper::search_get_user_details($user2)); // Student in same course.
 106          $this->assertEmpty(\core_message\helper::search_get_user_details($user4)); // Student in another course.
 107          $this->assertNotEmpty(\core_message\helper::search_get_user_details($user3)); // Teacher in same course.
 108          $this->assertEmpty(\core_message\helper::search_get_user_details($user7)); // Teacher (course contact) in another course.
 109  
 110          // User 3 should be able to see the teacher in their own course, but not other students in that course nor course contacts
 111          // or students in other courses.
 112          $this->setUser($user4);
 113          $this->assertEmpty(\core_message\helper::search_get_user_details($user5)); // Student in same course.
 114          $this->assertEmpty(\core_message\helper::search_get_user_details($user1)); // Student in another course.
 115          $this->assertNotEmpty(\core_message\helper::search_get_user_details($user6)); // Teacher in same course.
 116          $this->assertEmpty(\core_message\helper::search_get_user_details($user7)); // Teacher (course contact) in another course.
 117      }
 118  
 119      /**
 120       * Test search_get_user_details returns the correct profile data when $CFG->messagingallusers is enabled.
 121       */
 122      public function test_search_get_user_details_sitewide_enabled() {
 123          global $DB;
 124          set_config('messagingallusers', true);
 125  
 126          // Two students sharing course 1, visible profile within course (no groups).
 127          $user1 = $this->getDataGenerator()->create_user();
 128          $user2 = $this->getDataGenerator()->create_user();
 129          $course1 = $this->getDataGenerator()->create_course((object) ['groupmode' => 0]);
 130          $this->getDataGenerator()->enrol_user($user1->id, $course1->id);
 131          $this->getDataGenerator()->enrol_user($user2->id, $course1->id);
 132  
 133          // A teacher in course 1.
 134          $user3 = $this->getDataGenerator()->create_user();
 135          $this->getDataGenerator()->enrol_user($user3->id, $course1->id, 'editingteacher');
 136  
 137          // Two students sharing course 2, separate groups (profiles not visible to one another).
 138          // Note: no groups are created here, but separate groups mode alone is enough to restrict profile access.
 139          $user4 = $this->getDataGenerator()->create_user();
 140          $user5 = $this->getDataGenerator()->create_user();
 141          $course2 = $this->getDataGenerator()->create_course((object) ['groupmode' => 1]);
 142          $this->getDataGenerator()->enrol_user($user4->id, $course2->id);
 143          $this->getDataGenerator()->enrol_user($user5->id, $course2->id);
 144  
 145          // A teacher in course 2.
 146          $user6 = $this->getDataGenerator()->create_user();
 147          $this->getDataGenerator()->enrol_user($user6->id, $course2->id, 'editingteacher');
 148  
 149          // Teacher and course contact in course 3.
 150          $user7 = $this->getDataGenerator()->create_user();
 151          $course3 = $this->getDataGenerator()->create_course();
 152          $this->getDataGenerator()->enrol_user($user7->id, $course3->id, 'editingteacher');
 153          $teacherrole = $DB->get_record('role', array('shortname' => 'editingteacher'));
 154  
 155          // Make teachers course contacts.
 156          set_config('coursecontact', $teacherrole->id);
 157  
 158          // User 1 should be able to see users within their course and course contacts, but not students in other courses.
 159          $this->setUser($user1);
 160          $this->assertNotEmpty(\core_message\helper::search_get_user_details($user2)); // Student in same course.
 161          $this->assertEmpty(\core_message\helper::search_get_user_details($user4)); // Student in another course.
 162          $this->assertNotEmpty(\core_message\helper::search_get_user_details($user3)); // Teacher in same course.
 163          $this->assertNotEmpty(\core_message\helper::search_get_user_details($user7)); // Teacher (course contact) in another course.
 164  
 165          // User 3 should be able to see the teacher in their own course, but not other students in that course nor course contacts
 166          // or students in other courses.
 167          $this->setUser($user4);
 168          $this->assertEmpty(\core_message\helper::search_get_user_details($user5)); // Student in same course.
 169          $this->assertEmpty(\core_message\helper::search_get_user_details($user1)); // Student in another course.
 170          $this->assertNotEmpty(\core_message\helper::search_get_user_details($user6)); // Teacher in same course.
 171          $this->assertNotEmpty(\core_message\helper::search_get_user_details($user7)); // Teacher (course contact) in another course.
 172      }
 173  
 174      /**
 175       * Test prevent_unclosed_html_tags returns the correct html.
 176       *
 177       * @dataProvider prevent_unclosed_html_tags_data
 178       * @param string $text text to preview unclosed html tags.
 179       * @param string $goodhtml html good structured.
 180       * @param bool $removebody true if we want to remove tag body.
 181       */
 182      public function test_prevent_unclosed_html_tags(string $message, string $goodhtml, bool $removebody) {
 183          $this->setAdminUser();
 184  
 185          $html = \core_message\helper::prevent_unclosed_html_tags($message, $removebody);
 186          $this->assertSame($goodhtml, $html);
 187      }
 188  
 189      /**
 190       * Data provider for the test_prevent_unclosed_html_tags_data tests.
 191       *
 192       * @return  array
 193       */
 194      public function prevent_unclosed_html_tags_data(): array {
 195          return [
 196              'Prevent unclosed html elements' => [
 197                  '<h1>Title</h1><p>Paragraph</p><b>Bold', '<h1>Title</h1><p>Paragraph</p><b>Bold</b>', true
 198              ],
 199              'Prevent unclosed html elements including comments' => [
 200                  '<h1>Title</h1><p>Paragraph</p><!-- Comments //--><b>Bold', '<h1>Title</h1><p>Paragraph</p><!-- Comments //--><b>Bold</b>', true
 201              ],
 202              'Prevent unclosed comments' => ['<h1>Title</h1><p>Paragraph</p><!-- Comments', '<h1>Title</h1><p>Paragraph</p>', true
 203              ],
 204              'Prevent unclosed html elements without removing tag body' => [
 205                  '<body><h2>Title 2</h2><p>Paragraph</p><b>Bold</body>', '<body><h2>Title 2</h2><p>Paragraph</p><b>Bold</b></body>', false
 206              ],
 207              'Empty html' => [
 208                  '', '', false
 209              ],
 210              'Check encoding UTF-8 is working' => [
 211                  '<body><h1>Title</h1><p>السلام عليكم</p></body>', '<body><h1>Title</h1><p>السلام عليكم</p></body>', false
 212              ],
 213          ];
 214      }
 215  }