Merge branch 'MDL-37792-master' of git://github.com/sammarshallou/moodle
authorDamyon Wiese <damyon@moodle.com>
Tue, 26 Feb 2013 02:45:21 +0000 (10:45 +0800)
committerDamyon Wiese <damyon@moodle.com>
Tue, 26 Feb 2013 02:45:21 +0000 (10:45 +0800)
1  2 
lib/tests/conditionlib_test.php

@@@ -181,14 -181,13 +181,14 @@@ class conditionlib_testcase extends adv
      }
  
      private function make_course() {
 -        global $DB;
 -        $categoryid = $DB->insert_record('course_categories', (object)array('name'=>'conditionlibtest'));
 -        $courseid = $DB->insert_record('course', (object)array(
 -            'fullname'=>'Condition test','shortname'=>'CT1',
 -            'category'=>$categoryid,'enablecompletion'=>1));
 -        context_course::instance($courseid);
 -        return $courseid;
 +        $category = $this->getDataGenerator()->create_category(array('name' => 'conditionlibtest'));
 +        $course = $this->getDataGenerator()->create_course(
 +                array('fullname' => 'Condition test',
 +                    'shortname' => 'CT1',
 +                    'category' => $category->id,
 +                    'enablecompletion' => 1));
 +        context_course::instance($course->id);
 +        return $course->id;
      }
  
      private function make_course_module($courseid,$params=array()) {
              'completion'=>COMPLETION_TRACKING_MANUAL));
          $cmid2=$this->make_course_module($courseid,array(
              'showavailability'=>0,'availablefrom'=>0,'availableuntil'=>0));
 -        $this->make_section($courseid,array($cmid1,$cmid2));
 +        $this->make_section($courseid, array($cmid1, $cmid2), 1);
  
          // Add a fake grade item
          $gradeitemid=$DB->insert_record('grade_items',(object)array(
          $courseid=$this->make_course();
          $cmid=$this->make_course_module($courseid,array(
              'showavailability'=>0,'availablefrom'=>0,'availableuntil'=>0));
 -        $this->make_section($courseid,array($cmid));
 +        $this->make_section($courseid, array($cmid), 1);
  
          // Check it has no conditions
          $test1=new condition_info((object)array('id'=>$cmid),
          // Make course and module
          $courseid = $this->make_course();
          $cmid = $this->make_course_module($courseid);
 -        $sectionid = $this->make_section($courseid, array($cmid));
 +        $sectionid = $this->make_section($courseid, array($cmid), 1);
  
          // Check it has no conditions
          $test1 = new condition_info_section((object)array('id'=>$sectionid),
          // Completion
          $oldid=$cmid;
          $cmid=$this->make_course_module($courseid);
 -        $this->make_section($courseid,array($oldid,$cmid));
 +        $this->make_section($courseid, array($oldid, $cmid), 1);
          $oldcm=$DB->get_record('course_modules',array('id'=>$oldid));
          $oldcm->completion=COMPLETION_TRACKING_MANUAL;
          $DB->update_record('course_modules',$oldcm);
          $this->assertFalse($ci->is_available($text, false, $USER->id + 1));
          $this->assertFalse($ci->is_available($text, true, $USER->id + 1));
      }
+     /**
+      * Tests user fields to ensure that the list of provided fields includes only
+      * fields which the equivalent function can be used to obtain the value of.
+      */
+     public function test_condition_user_fields() {
+         global $CFG, $DB, $USER;
+         // Set up basic data.
+         $courseid = $this->make_course();
+         $cmid = $this->make_course_module($courseid);
+         $ci = new condition_info_testwrapper(
+                 (object)array('id' => $cmid), CONDITION_MISSING_EVERYTHING);
+         // Add a custom user profile field. Unfortunately there is no back-end
+         // API for adding profile fields without having an actual form and doing
+         // redirects and stuff! These are the default text field parameters.
+         require_once($CFG->dirroot . '/user/profile/lib.php');
+         $field = (object)array(
+                 'shortname' => 'myfield', 'name' => 'My field', 'required' => 0,
+                 'locked' => 0, 'forceunique' => 0, 'signup' => 0,
+                 'visible' => PROFILE_VISIBLE_ALL,
+                 'datatype' => 'text', 'description' => 'A field of mine',
+                 'descriptionformat' => FORMAT_HTML, 'defaultdata' => '',
+                 'defaultdataformat' => FORMAT_HTML, 'param1' => 30, 'param2' => 2048,
+                 'param3' => 0, 'param4' => '', 'param5' => '');
+         $customfieldid = $DB->insert_record('user_info_field', $field);
+         // Get list of condition user fields.
+         $fields = condition_info::get_condition_user_fields();
+         // Check custom field is included.
+         $this->assertEquals('My field', $fields[$customfieldid]);
+         // For all other fields, check it actually works to get data from them.
+         foreach ($fields as $fieldid => $name) {
+             // Not checking the result, just that it's possible to get it
+             // without error.
+             $ci->get_cached_user_profile_field($USER->id, $fieldid);
+         }
+     }
  }
  
+ /**
+  * Test wrapper used only to make protected functions public so they can be
+  * tested.
+  */
+ class condition_info_testwrapper extends condition_info {
+     public function get_cached_user_profile_field($userid, $fieldid) {
+         return parent::get_cached_user_profile_field($userid, $fieldid);
+     }
+ }