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   * Unit tests for the profile manager
  19   *
  20   * @package    tool_moodlenet
  21   * @category   test
  22   * @copyright  2020 Adrian Greeve <adrian@moodle.com>
  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   * Class profile_manager tests
  32   */
  33  class tool_moodlenet_profile_manager_testcase extends advanced_testcase {
  34  
  35      /**
  36       * Test that on this site we use the user table to hold moodle net profile information.
  37       */
  38      public function test_official_profile_exists() {
  39          $this->assertTrue(\tool_moodlenet\profile_manager::official_profile_exists());
  40      }
  41  
  42      /**
  43       * Test a null is returned when the user's mnet profile field is not set.
  44       */
  45      public function test_get_moodlenet_user_profile_no_profile_set() {
  46          $this->resetAfterTest();
  47          $user = $this->getDataGenerator()->create_user();
  48  
  49          $result = \tool_moodlenet\profile_manager::get_moodlenet_user_profile($user->id);
  50          $this->assertNull($result);
  51      }
  52  
  53      /**
  54       * Test a null is returned when the user's mnet profile field is not set.
  55       */
  56      public function test_moodlenet_user_profile_creation_no_profile_set() {
  57          $this->resetAfterTest();
  58          $user = $this->getDataGenerator()->create_user();
  59  
  60          $this->expectException(moodle_exception::class);
  61          $this->expectExceptionMessage(get_string('invalidmoodlenetprofile', 'tool_moodlenet'));
  62          $result = new \tool_moodlenet\moodlenet_user_profile("", $user->id);
  63      }
  64  
  65      /**
  66       * Test the return of a moodle net profile.
  67       */
  68      public function test_get_moodlenet_user_profile() {
  69          $this->resetAfterTest();
  70          $user = $this->getDataGenerator()->create_user(['moodlenetprofile' => '@matt@hq.mnet']);
  71  
  72          $result = \tool_moodlenet\profile_manager::get_moodlenet_user_profile($user->id);
  73          $this->assertEquals($user->moodlenetprofile, $result->get_profile_name());
  74      }
  75  
  76      /**
  77       * Test the creation of a user profile category.
  78       */
  79      public function test_create_user_profile_category() {
  80          global $DB;
  81          $this->resetAfterTest();
  82  
  83          $basecategoryname = get_string('pluginname', 'tool_moodlenet');
  84  
  85          \tool_moodlenet\profile_manager::create_user_profile_category();
  86          $categoryname = \tool_moodlenet\profile_manager::get_category_name();
  87          $this->assertEquals($basecategoryname, $categoryname);
  88          \tool_moodlenet\profile_manager::create_user_profile_category();
  89  
  90          $recordcount = $DB->count_records('user_info_category', ['name' => $basecategoryname]);
  91          $this->assertEquals(1, $recordcount);
  92  
  93          // Test the duplication of categories to ensure a unique name is always used.
  94          $categoryname = \tool_moodlenet\profile_manager::get_category_name();
  95          $this->assertEquals($basecategoryname . 1, $categoryname);
  96          \tool_moodlenet\profile_manager::create_user_profile_category();
  97          $categoryname = \tool_moodlenet\profile_manager::get_category_name();
  98          $this->assertEquals($basecategoryname . 2, $categoryname);
  99      }
 100  
 101      /**
 102       * Test the creating of the custom user profile field to hold the moodle net profile.
 103       */
 104      public function test_create_user_profile_text_field() {
 105          global $DB;
 106          $this->resetAfterTest();
 107  
 108          $shortname = 'mnetprofile';
 109  
 110          $categoryid = \tool_moodlenet\profile_manager::create_user_profile_category();
 111          \tool_moodlenet\profile_manager::create_user_profile_text_field($categoryid);
 112  
 113          $record = $DB->get_record('user_info_field', ['shortname' => $shortname]);
 114          $this->assertEquals($shortname, $record->shortname);
 115          $this->assertEquals($categoryid, $record->categoryid);
 116  
 117          // Test for a unique name if 'mnetprofile' is already in use.
 118          \tool_moodlenet\profile_manager::create_user_profile_text_field($categoryid);
 119          $profilename = \tool_moodlenet\profile_manager::get_profile_field_name();
 120          $this->assertEquals($shortname . 1, $profilename);
 121          \tool_moodlenet\profile_manager::create_user_profile_text_field($categoryid);
 122          $profilename = \tool_moodlenet\profile_manager::get_profile_field_name();
 123          $this->assertEquals($shortname . 2, $profilename);
 124      }
 125  
 126      /**
 127       * Test that the user moodlenet profile is saved.
 128       */
 129      public function test_save_moodlenet_user_profile() {
 130          $this->resetAfterTest();
 131  
 132          $user = $this->getDataGenerator()->create_user();
 133          $profilename = '@matt@hq.mnet';
 134  
 135          $moodlenetprofile = new \tool_moodlenet\moodlenet_user_profile($profilename, $user->id);
 136  
 137          \tool_moodlenet\profile_manager::save_moodlenet_user_profile($moodlenetprofile);
 138  
 139          $userdata = \core_user::get_user($user->id);
 140          $this->assertEquals($profilename, $userdata->moodlenetprofile);
 141      }
 142  }