MDL-45242 Testing: Generators for user profile fields
This commit is contained in:
@@ -87,6 +87,18 @@ class behat_core_generator extends behat_generator_base {
|
||||
'required' => ['name', 'category', 'type', 'shortname'],
|
||||
'switchids' => [],
|
||||
],
|
||||
'custom profile field categories' => [
|
||||
'singular' => 'custom profile field category',
|
||||
'datagenerator' => 'custom_profile_field_category',
|
||||
'required' => ['name'],
|
||||
'switchids' => [],
|
||||
],
|
||||
'custom profile fields' => [
|
||||
'singular' => 'custom profile field',
|
||||
'datagenerator' => 'custom_profile_field',
|
||||
'required' => ['datatype', 'shortname', 'name'],
|
||||
'switchids' => [],
|
||||
],
|
||||
'permission overrides' => [
|
||||
'singular' => 'permission override',
|
||||
'datagenerator' => 'permission_override',
|
||||
|
||||
@@ -1190,6 +1190,123 @@ EOD;
|
||||
return $this->get_plugin_generator('core_customfield')->create_field($data);
|
||||
}
|
||||
|
||||
/**
|
||||
* Create a new category for custom profile fields.
|
||||
*
|
||||
* @param array $data Array with 'name' and optionally 'sortorder'
|
||||
* @return \stdClass New category object
|
||||
*/
|
||||
public function create_custom_profile_field_category(array $data): \stdClass {
|
||||
global $DB;
|
||||
|
||||
// Pick next sortorder if not defined.
|
||||
if (!array_key_exists('sortorder', $data)) {
|
||||
$data['sortorder'] = (int)$DB->get_field_sql('SELECT MAX(sortorder) FROM {user_info_category}') + 1;
|
||||
}
|
||||
|
||||
$category = (object)[
|
||||
'name' => $data['name'],
|
||||
'sortorder' => $data['sortorder']
|
||||
];
|
||||
$category->id = $DB->insert_record('user_info_category', $category);
|
||||
|
||||
return $category;
|
||||
}
|
||||
|
||||
/**
|
||||
* Creates a new custom profile field.
|
||||
*
|
||||
* Optional fields are:
|
||||
*
|
||||
* categoryid (or use 'category' to specify by name). If you don't specify
|
||||
* either, it will add the field to a 'Testing' category, which will be created for you if
|
||||
* necessary.
|
||||
*
|
||||
* sortorder (if you don't specify this, it will pick the next one in the category).
|
||||
*
|
||||
* all the other database fields (if you don't specify this, it will pick sensible defaults
|
||||
* based on the data type).
|
||||
*
|
||||
* @param array $data Array with 'datatype', 'shortname', and 'name'
|
||||
* @return \stdClass Database object from the user_info_field table
|
||||
*/
|
||||
public function create_custom_profile_field(array $data): \stdClass {
|
||||
global $DB, $CFG;
|
||||
require_once($CFG->dirroot . '/user/profile/lib.php');
|
||||
|
||||
// Set up category if necessary.
|
||||
if (!array_key_exists('categoryid', $data)) {
|
||||
if (array_key_exists('category', $data)) {
|
||||
$data['categoryid'] = $DB->get_field('user_info_category', 'id',
|
||||
['name' => $data['category']], MUST_EXIST);
|
||||
} else {
|
||||
// Make up a 'Testing' category or use existing.
|
||||
$data['categoryid'] = $DB->get_field('user_info_category', 'id', ['name' => 'Testing']);
|
||||
if (!$data['categoryid']) {
|
||||
$created = $this->create_custom_profile_field_category(['name' => 'Testing']);
|
||||
$data['categoryid'] = $created->id;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// Pick sort order if necessary.
|
||||
if (!array_key_exists('sortorder', $data)) {
|
||||
$data['sortorder'] = (int)$DB->get_field_sql(
|
||||
'SELECT MAX(sortorder) FROM {user_info_field} WHERE categoryid = ?',
|
||||
[$data['categoryid']]) + 1;
|
||||
}
|
||||
|
||||
// Defaults for other values.
|
||||
$defaults = [
|
||||
'description' => '',
|
||||
'descriptionformat' => 0,
|
||||
'required' => 0,
|
||||
'locked' => 0,
|
||||
'visible' => PROFILE_VISIBLE_ALL,
|
||||
'forceunique' => 0,
|
||||
'signup' => 0,
|
||||
'defaultdata' => '',
|
||||
'defaultdataformat' => 0,
|
||||
'param1' => '',
|
||||
'param2' => '',
|
||||
'param3' => '',
|
||||
'param4' => '',
|
||||
'param5' => ''
|
||||
];
|
||||
|
||||
// Type-specific defaults for other values.
|
||||
$typedefaults = [
|
||||
'text' => [
|
||||
'param1' => 30,
|
||||
'param2' => 2048
|
||||
],
|
||||
'menu' => [
|
||||
'param1' => "Yes\nNo",
|
||||
'defaultdata' => 'No'
|
||||
],
|
||||
'datetime' => [
|
||||
'param1' => '2010',
|
||||
'param2' => '2015',
|
||||
'param3' => 1
|
||||
],
|
||||
'checkbox' => [
|
||||
'defaultdata' => 0
|
||||
]
|
||||
];
|
||||
foreach ($typedefaults[$data['datatype']] as $field => $value) {
|
||||
$defaults[$field] = $value;
|
||||
}
|
||||
|
||||
foreach ($defaults as $field => $value) {
|
||||
if (!array_key_exists($field, $data)) {
|
||||
$data[$field] = $value;
|
||||
}
|
||||
}
|
||||
|
||||
$data['id'] = $DB->insert_record('user_info_field', $data);
|
||||
return (object)$data;
|
||||
}
|
||||
|
||||
/**
|
||||
* Create a new user, and enrol them in the specified course as the supplied role.
|
||||
*
|
||||
|
||||
@@ -466,4 +466,119 @@ class core_test_generator_testcase extends advanced_testcase {
|
||||
'parent' => $gradecategory->id));
|
||||
$this->assertEquals($gradecategory->id, $gradecategory2->parent);
|
||||
}
|
||||
|
||||
public function test_create_custom_profile_field_category() {
|
||||
global $DB;
|
||||
|
||||
$this->resetAfterTest();
|
||||
$generator = $this->getDataGenerator();
|
||||
|
||||
// Insert first category without specified sortorder.
|
||||
$result = $generator->create_custom_profile_field_category(['name' => 'Frogs']);
|
||||
$record = $DB->get_record('user_info_category', ['name' => 'Frogs']);
|
||||
$this->assertEquals(1, $record->sortorder);
|
||||
|
||||
// Also check the return value.
|
||||
$this->assertEquals(1, $result->sortorder);
|
||||
$this->assertEquals('Frogs', $result->name);
|
||||
$this->assertEquals($record->id, $result->id);
|
||||
|
||||
// Insert next category without specified sortorder.
|
||||
$generator->create_custom_profile_field_category(['name' => 'Zombies']);
|
||||
$record = $DB->get_record('user_info_category', ['name' => 'Zombies']);
|
||||
$this->assertEquals(2, $record->sortorder);
|
||||
|
||||
// Insert category with specified sortorder.
|
||||
$generator->create_custom_profile_field_category(['name' => 'Toads', 'sortorder' => 9]);
|
||||
$record = $DB->get_record('user_info_category', ['name' => 'Toads']);
|
||||
$this->assertEquals(9, $record->sortorder);
|
||||
|
||||
// Insert another with unspecified sortorder.
|
||||
$generator->create_custom_profile_field_category(['name' => 'Werewolves']);
|
||||
$record = $DB->get_record('user_info_category', ['name' => 'Werewolves']);
|
||||
$this->assertEquals(10, $record->sortorder);
|
||||
}
|
||||
|
||||
public function test_create_custom_profile_field() {
|
||||
global $DB;
|
||||
|
||||
$this->resetAfterTest();
|
||||
$generator = $this->getDataGenerator();
|
||||
|
||||
// Insert minimal field without specified category.
|
||||
$field1 = $generator->create_custom_profile_field(
|
||||
['datatype' => 'text', 'shortname' => 'colour', 'name' => 'Colour']);
|
||||
$record = $DB->get_record('user_info_field', ['shortname' => 'colour']);
|
||||
|
||||
// Check specified values.
|
||||
$this->assertEquals('Colour', $record->name);
|
||||
$this->assertEquals('text', $record->datatype);
|
||||
|
||||
// Check sortorder (first in category).
|
||||
$this->assertEquals(1, $record->sortorder);
|
||||
|
||||
// Check shared defaults for most datatypes.
|
||||
$this->assertEquals('', $record->description);
|
||||
$this->assertEquals(0, $record->descriptionformat);
|
||||
$this->assertEquals(0, $record->required);
|
||||
$this->assertEquals(0, $record->locked);
|
||||
$this->assertEquals(PROFILE_VISIBLE_ALL, $record->visible);
|
||||
$this->assertEquals(0, $record->forceunique);
|
||||
$this->assertEquals(0, $record->signup);
|
||||
$this->assertEquals('', $record->defaultdata);
|
||||
$this->assertEquals(0, $record->defaultdataformat);
|
||||
|
||||
// Check specific defaults for text datatype.
|
||||
$this->assertEquals(30, $record->param1);
|
||||
$this->assertEquals(2048, $record->param2);
|
||||
|
||||
// Check the returned value matches the database data.
|
||||
$this->assertEquals($record, $field1);
|
||||
|
||||
// The category should relate to a new 'testing' category.
|
||||
$catrecord = $DB->get_record('user_info_category', ['id' => $record->categoryid]);
|
||||
$this->assertEquals('Testing', $catrecord->name);
|
||||
$this->assertEquals(1, $catrecord->sortorder);
|
||||
|
||||
// Create another field, this time supplying values for a few of the fields.
|
||||
$generator->create_custom_profile_field(
|
||||
['datatype' => 'text', 'shortname' => 'brightness', 'name' => 'Brightness',
|
||||
'required' => 1, 'forceunique' => 1]);
|
||||
$record = $DB->get_record('user_info_field', ['shortname' => 'brightness']);
|
||||
|
||||
// Same testing category, next sortorder.
|
||||
$this->assertEquals($catrecord->id, $record->categoryid);
|
||||
$this->assertEquals(2, $record->sortorder);
|
||||
|
||||
// Check modified fields.
|
||||
$this->assertEquals(1, $record->required);
|
||||
$this->assertEquals(1, $record->forceunique);
|
||||
|
||||
// Create a field in specified category by id or name...
|
||||
$category = $generator->create_custom_profile_field_category(['name' => 'Amphibians']);
|
||||
$field3 = $generator->create_custom_profile_field(
|
||||
['datatype' => 'text', 'shortname' => 'frog', 'name' => 'Frog',
|
||||
'categoryid' => $category->id]);
|
||||
$this->assertEquals($category->id, $field3->categoryid);
|
||||
$this->assertEquals(1, $field3->sortorder);
|
||||
$field4 = $generator->create_custom_profile_field(
|
||||
['datatype' => 'text', 'shortname' => 'toad', 'name' => 'Toad',
|
||||
'category' => 'Amphibians', 'sortorder' => 4]);
|
||||
$this->assertEquals($category->id, $field4->categoryid);
|
||||
$this->assertEquals(4, $field4->sortorder);
|
||||
|
||||
// Check defaults for menu, datetime, and checkbox.
|
||||
$field5 = $generator->create_custom_profile_field(
|
||||
['datatype' => 'menu', 'shortname' => 'cuisine', 'name' => 'Cuisine']);
|
||||
$this->assertEquals("Yes\nNo", $field5->param1);
|
||||
$this->assertEquals('No', $field5->defaultdata);
|
||||
$field6 = $generator->create_custom_profile_field(
|
||||
['datatype' => 'datetime', 'shortname' => 'epoch', 'name' => 'Epoch']);
|
||||
$this->assertEquals(2010, $field6->param1);
|
||||
$this->assertEquals(2015, $field6->param2);
|
||||
$this->assertEquals(1, $field6->param3);
|
||||
$field7 = $generator->create_custom_profile_field(
|
||||
['datatype' => 'checkbox', 'shortname' => 'areyousure', 'name' => 'Are you sure?']);
|
||||
$this->assertEquals(0, $field7->defaultdata);
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user