Search moodle.org's
Developer Documentation


  • Bug fixes for general core bugs in 2.8.x ended 9 November 2015 (12 months).
  • Bug fixes for security issues in 2.8.x ended 9 May 2016 (18 months).
  • minimum PHP 5.4.4 (always use latest PHP 5.4.x or 5.5.x on Windows - http://windows.php.net/download/), PHP 7 is NOT supported
  • Differences Between: [Versions 28 and 29] [Versions 28 and 30] [Versions 28 and 31] [Versions 28 and 32] [Versions 28 and 33] [Versions 28 and 34] [Versions 28 and 35] [Versions 28 and 36] [Versions 28 and 37]

       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 user/profile/lib.php.
      19   *
      20   * @package core_user
      21   * @copyright 2014 The Open University
      22   * @licensehttp://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
      23   */
      24  
      25  defined('MOODLE_INTERNAL') || die();
      26  
      27  global $CFG;
      28  
      29  /**
      30   * Unit tests for user/profile/lib.php.
      31   *
      32   * @package core_user
      33   * @copyright 2014 The Open University
      34   * @licensehttp://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
      35   */
      36  class core_user_profilelib_testcase extends advanced_testcase {
      37      /**
      38       * Tests profile_get_custom_fields function and checks it is consistent
      39       * with profile_user_record.
      40       */
      41      public function test_get_custom_fields() {
      42          global $DB, $CFG;
      43          require_once($CFG->dirroot . '/user/profile/lib.php');
      44  
      45          $this->resetAfterTest();
      46          $user = $this->getDataGenerator()->create_user();
      47  
      48          // Add a custom field of textarea type.
      49          $id1 = $DB->insert_record('user_info_field', array(
      50                  'shortname' => 'frogdesc', 'name' => 'Description of frog', 'categoryid' => 1,
      51                  'datatype' => 'textarea'));
      52  
      53          // Check the field is returned.
      54          $result = profile_get_custom_fields();
      55          $this->assertArrayHasKey($id1, $result);
      56          $this->assertEquals('frogdesc', $result[$id1]->shortname);
      57  
      58          // Textarea types are not included in user data though, so if we
      59          // use the 'only in user data' parameter, there is still nothing.
      60          $this->assertArrayNotHasKey($id1, profile_get_custom_fields(true));
      61  
      62          // Check that profile_user_record returns same (no) fields.
      63          $this->assertObjectNotHasAttribute('frogdesc', profile_user_record($user->id));
      64  
      65          // Check that profile_user_record returns all the fields when requested.
      66          $this->assertObjectHasAttribute('frogdesc', profile_user_record($user->id, false));
      67  
      68          // Add another custom field, this time of normal text type.
      69          $id2 = $DB->insert_record('user_info_field', array(
      70                  'shortname' => 'frogname', 'name' => 'Name of frog', 'categoryid' => 1,
      71                  'datatype' => 'text'));
      72  
      73          // Check both are returned using normal option.
      74          $result = profile_get_custom_fields();
      75          $this->assertArrayHasKey($id2, $result);
      76          $this->assertEquals('frogname', $result[$id2]->shortname);
      77  
      78          // And check that only the one is returned the other way.
      79          $this->assertArrayHasKey($id2, profile_get_custom_fields(true));
      80  
      81          // Check profile_user_record returns same field.
      82          $this->assertObjectHasAttribute('frogname', profile_user_record($user->id));
      83  
      84          // Check that profile_user_record returns all the fields when requested.
      85          $this->assertObjectHasAttribute('frogname', profile_user_record($user->id, false));
      86      }
      87  
      88      /**
      89       * Make sure that all profile fields can be initialised without arguments.
      90       */
      91      public function test_default_constructor() {
      92          global $DB, $CFG;
      93          require_once($CFG->dirroot . '/user/profile/definelib.php');
      94          $datatypes = profile_list_datatypes();
      95          foreach ($datatypes as $datatype => $datatypename) {
      96              require_once($CFG->dirroot . '/user/profile/field/' .
      97                  $datatype . '/field.class.php');
      98              $newfield = 'profile_field_' . $datatype;
      99              $formfield = new $newfield();
     100              $this->assertNotNull($formfield);
     101          }
     102      }
     103  }
    

    Search This Site: