/**
  * Validate that an invalid category for a field causes a field
  * to be moved to the Miscellaneous category
  */
 public function test_orphanedcustomfieldsmovedtomiscellaneouscategory()
 {
     global $CFG, $DB;
     $contextlevel = CONTEXT_ELIS_USERSET;
     $misccat = get_string('misc_category', 'local_elisprogram');
     // Set up a custom field with an invalid category.
     $field = new field(array('name' => 'testcustomfieldname', 'datatype' => 'char', 'categoryid' => '99', 'multivalued' => 1));
     $field->save();
     // Set up the default data.
     $defaultparams = array('fieldid' => $field->id, 'contextid' => $contextlevel, 'data' => 'value1');
     $defaultdata = new field_data_char($defaultparams);
     $defaultdata->save();
     // Also create a contextlevel record for the field.
     $fieldcontext = new field_contextlevel();
     $fieldcontext->fieldid = $field->id;
     $fieldcontext->contextlevel = $contextlevel;
     $fieldcontext->save();
     // Call function to check if this is an orphaned field.
     pm_fix_orphaned_fields();
     // Assert that the field exists.
     $result = $DB->get_field(field::TABLE, 'id', array('id' => $field->id));
     $this->assertEquals($field->id, $result);
     // Assert that the field is in the correct context still.
     $sql = "SELECT field.id\n                  FROM {" . field::TABLE . "} field\n                  JOIN {" . field_contextlevel::TABLE . "} ctx\n                    ON ctx.fieldid = field.id\n                 WHERE ctx.contextlevel = ?\n                   AND field.id = ?";
     $params = array($contextlevel, $field->id);
     $result = $DB->get_field_sql($sql, $params);
     $this->assertEquals($field->id, $result);
     // Assert that the Miscellaneous category exists.
     $result = $DB->get_field(field_category::TABLE, 'name', array('name' => $misccat));
     $this->assertEquals($misccat, $result);
     // Assert that the Miscellaneous category is in the correct context.
     $sql = "SELECT category.name\n                  FROM {" . field_category::TABLE . "} category\n                  JOIN {" . field_category_contextlevel::TABLE . "} category_context\n                    ON category.id = category_context.categoryid\n                 WHERE category_context.contextlevel = ?\n                   AND category.name = ?";
     $params = array($contextlevel, $misccat);
     $result = $DB->get_field_sql($sql, $params);
     $this->assertEquals($misccat, $result);
     // Assert that the field is in the Miscellaneous category.
     $sql = "SELECT field.id\n                  FROM {" . field::TABLE . "} field\n                  JOIN {" . field_category::TABLE . "} category\n                    ON category.id = field.categoryid\n                 WHERE field.id = ?\n                   AND category.name = ?";
     $params = array($field->id, $misccat);
     $result = $DB->get_field_sql($sql, $params);
     $this->assertEquals($field->id, $result);
     // Now check that an invalid field will be moved to the existing Miscellaneous category
     // and not create a second Miscellaneous category.
     // Set up a custom field with an invalid category.
     $field = new field(array('name' => 'testcustomfieldname2', 'datatype' => 'char', 'categoryid' => '109'));
     $field->save();
     // Set up the default data.
     $defaultparams = array('fieldid' => $field->id, 'contextid' => $contextlevel, 'data' => 'value2');
     $defaultdata = new field_data_char($defaultparams);
     $defaultdata->save();
     // Also create a contextlevel record for the field.
     $fieldcontext = new field_contextlevel();
     $fieldcontext->fieldid = $field->id;
     $fieldcontext->contextlevel = $contextlevel;
     $fieldcontext->save();
     // Call function to check if this is an orphaned field.
     pm_fix_orphaned_fields();
     // Assert that only one Miscellaneous category exists.
     $result = $DB->get_records(field_category::TABLE, array('name' => $misccat));
     $this->assertEquals(1, count($result));
     // Assert that the field is in the Miscellaneous category.
     $sql = "SELECT field.id\n                  FROM {" . field::TABLE . "} field\n                  JOIN {" . field_category::TABLE . "} category\n                    ON category.id = field.categoryid\n                 WHERE field.id = ?\n                   AND category.name = ?";
     $params = array($field->id, $misccat);
     $result = $DB->get_field_sql($sql, $params);
     $this->assertEquals($field->id, $result);
 }
 /**
  * Create a test ELIS user custom field.
  *
  * @param string $name The name of the custom field.
  * @param int $multivalued The multivalued value to set in the field definition
  * @param bool $setoptions Set to true to set the available menu of choices options
  * @param int $ctx The context level to use for the field
  * @param string $control The type of control to use for the field.
  *
  * @return int The id of the created field
  */
 protected function create_custom_field($name = 'name', $multivalued = 1, $setoptions = true, $ctx = CONTEXT_ELIS_USER, $control = 'menu')
 {
     global $CFG;
     // Field.
     $fieldparams = array('name' => $name, 'datatype' => 'char', 'multivalued' => $multivalued);
     $field = new field($fieldparams);
     $category = new field_category(array('name' => 'testcategoryname'));
     $field = field::ensure_field_exists_for_context_level($field, $ctx, $category);
     // Owner.
     $owneroptions = array('control' => $control);
     if ($setoptions) {
         $owneroptions['options'] = "option1\r\noption2\r\noption3";
     }
     field_owner::ensure_field_owner_exists($field, 'manual', $owneroptions);
     // Default.
     $fielddataparams = array('contextid' => null, 'fieldid' => $field->id, 'data' => 'option3');
     $data = new field_data_char($fielddataparams);
     $data->save();
     return $field->id;
 }
 /**
  * Validate that the field_data "get_for_context_and_field" method always excludes a custom field's default 
  * data when some data exists for the  appropriate context, regardless of the related parameter value.
  *
  * @param boolean $includedefault
  * @dataProvider include_default_provider
  */
 public function test_get_for_context_and_field_excludes_default_value_when_data_exists($includedefault)
 {
     // Set up our custom field data.
     $field = $this->init_custom_field();
     // Set up our data point.
     $fielddatachar = new field_data_char(array('fieldid' => $field->id, 'contextid' => self::CONTEXTID, 'data' => 'value3'));
     $fielddatachar->save();
     // Run get_for_context_and_field to obtain our data set.
     $context = new stdClass();
     $context->id = self::CONTEXTID;
     $data = field_data::get_for_context_and_field($context, $field, $includedefault);
     // Validate number of data records (one for specific context).
     $count = 0;
     $record = null;
     foreach ($data as $datum) {
         $count++;
         $record = $datum;
     }
     $this->assertEquals(1, $count);
     $this->assertEquals($field->id, $record->fieldid);
     $this->assertEquals('value3', $record->data);
 }