From ef3d6a3d705382385d7a712573f8f1844728816c Mon Sep 17 00:00:00 2001 From: Marina Glancy Date: Fri, 19 Mar 2021 21:40:41 +0100 Subject: [PATCH] MDL-71051 core_user: use user profile generator in all tests --- admin/tool/uploaduser/tests/cli_test.php | 7 +- auth/db/tests/db_test.php | 3 +- auth/email/tests/external_test.php | 19 +-- .../profile/tests/condition_test.php | 22 +-- badges/tests/badgeslib_test.php | 6 +- lib/testing/generator/data_generator.php | 2 +- lib/tests/event_profile_field_test.php | 161 +++++------------- .../field/checkbox/tests/privacy_test.php | 32 ++-- .../field/datetime/tests/privacy_test.php | 32 ++-- .../profile/field/menu/tests/privacy_test.php | 32 ++-- .../field/social/tests/privacy_test.php | 32 ++-- .../profile/field/text/tests/privacy_test.php | 32 ++-- .../field/textarea/tests/privacy_test.php | 32 ++-- user/tests/profilelib_test.php | 38 ++--- 14 files changed, 149 insertions(+), 301 deletions(-) diff --git a/admin/tool/uploaduser/tests/cli_test.php b/admin/tool/uploaduser/tests/cli_test.php index a13c047a841..d2af41ce849 100644 --- a/admin/tool/uploaduser/tests/cli_test.php +++ b/admin/tool/uploaduser/tests/cli_test.php @@ -126,14 +126,13 @@ class tool_uploaduser_cli_testcase extends advanced_testcase { * User upload with user profile fields */ public function test_upload_with_profile_fields() { - global $DB, $CFG; + global $CFG; $this->resetAfterTest(); set_config('passwordpolicy', 0); $this->setAdminUser(); - $categoryid = $DB->insert_record('user_info_category', ['name' => 'Cat 1', 'sortorder' => 1]); - $this->field1 = $DB->insert_record('user_info_field', [ - 'shortname' => 'superfield', 'name' => 'Super field', 'categoryid' => $categoryid, + $this->field1 = $this->getDataGenerator()->create_custom_profile_field([ + 'shortname' => 'superfield', 'name' => 'Super field', 'datatype' => 'text', 'signup' => 1, 'visible' => 1, 'required' => 1, 'sortorder' => 1]); $filepath = $CFG->dirroot.'/lib/tests/fixtures/upload_users_profile.csv'; diff --git a/auth/db/tests/db_test.php b/auth/db/tests/db_test.php index 1de432c3f17..ce163583dc2 100644 --- a/auth/db/tests/db_test.php +++ b/auth/db/tests/db_test.php @@ -139,8 +139,7 @@ class auth_db_testcase extends advanced_testcase { set_config('field_lock_email', 'unlocked', 'auth_db'); // Create a user profile field and add mapping to it. - $DB->insert_record('user_info_field', ['shortname' => 'pet', 'name' => 'Pet', 'required' => 0, - 'visible' => 1, 'locked' => 0, 'categoryid' => 1, 'datatype' => 'text']); + $this->getDataGenerator()->create_custom_profile_field(['shortname' => 'pet', 'name' => 'Pet', 'datatype' => 'text']); set_config('field_map_profile_field_pet', 'animal', 'auth_db'); set_config('field_updatelocal_profile_field_pet', 'oncreate', 'auth_db'); diff --git a/auth/email/tests/external_test.php b/auth/email/tests/external_test.php index ed1900a6dfd..c04a76a8032 100644 --- a/auth/email/tests/external_test.php +++ b/auth/email/tests/external_test.php @@ -44,18 +44,17 @@ class auth_email_external_testcase extends externallib_advanced_testcase { * Set up for every test */ public function setUp(): void { - global $CFG, $DB; + global $CFG; $this->resetAfterTest(true); $CFG->registerauth = 'email'; - $categoryid = $DB->insert_record('user_info_category', array('name' => 'Cat 1', 'sortorder' => 1)); - $this->field1 = $DB->insert_record('user_info_field', array( - 'shortname' => 'frogname', 'name' => 'Name of frog', 'categoryid' => $categoryid, - 'datatype' => 'text', 'signup' => 1, 'visible' => 1, 'required' => 1, 'sortorder' => 1)); - $this->field2 = $DB->insert_record('user_info_field', array( - 'shortname' => 'sometext', 'name' => 'Some text in textarea', 'categoryid' => $categoryid, - 'datatype' => 'textarea', 'signup' => 1, 'visible' => 1, 'required' => 1, 'sortorder' => 2)); + $this->field1 = $this->getDataGenerator()->create_custom_profile_field(array( + 'shortname' => 'frogname', 'name' => 'Name of frog', + 'datatype' => 'text', 'signup' => 1, 'visible' => 1, 'required' => 1, 'sortorder' => 1))->id; + $this->field2 = $this->getDataGenerator()->create_custom_profile_field(array( + 'shortname' => 'sometext', 'name' => 'Some text in textarea', + 'datatype' => 'textarea', 'signup' => 1, 'visible' => 1, 'required' => 1, 'sortorder' => 2))->id; } public function test_get_signup_settings() { @@ -109,8 +108,8 @@ class auth_email_external_testcase extends externallib_advanced_testcase { // Create category with MathJax and a new field with MathJax. $categoryname = 'Cat $$(a+b)=2$$'; $fieldname = 'Some text $$(a+b)=2$$'; - $categoryid = $DB->insert_record('user_info_category', array('name' => $categoryname, 'sortorder' => 1)); - $field3 = $DB->insert_record('user_info_field', array( + $categoryid = $this->getDataGenerator()->create_custom_profile_field_category(['name' => $categoryname])->id; + $this->getDataGenerator()->create_custom_profile_field(array( 'shortname' => 'mathjaxname', 'name' => $fieldname, 'categoryid' => $categoryid, 'datatype' => 'textarea', 'signup' => 1, 'visible' => 1, 'required' => 1, 'sortorder' => 2)); diff --git a/availability/condition/profile/tests/condition_test.php b/availability/condition/profile/tests/condition_test.php index 7e3074c7906..12d44772e57 100644 --- a/availability/condition/profile/tests/condition_test.php +++ b/availability/condition/profile/tests/condition_test.php @@ -50,14 +50,10 @@ class availability_profile_condition_testcase extends advanced_testcase { $this->resetAfterTest(); - // Add a custom profile field type. The API for doing this is indescribably - // horrid and tightly intertwined with the form UI, so it's best to add - // it directly in database. - $DB->insert_record('user_info_field', array( - 'shortname' => 'frogtype', 'name' => 'Type of frog', 'categoryid' => 1, + // Add a custom profile field type. + $this->profilefield = $this->getDataGenerator()->create_custom_profile_field(array( + 'shortname' => 'frogtype', 'name' => 'Type of frog', 'datatype' => 'text')); - $this->profilefield = $DB->get_record('user_info_field', - array('shortname' => 'frogtype')); // Clear static cache. \availability_profile\condition::wipe_static_cache(); @@ -333,11 +329,9 @@ class availability_profile_condition_testcase extends advanced_testcase { $info = new \core_availability\mock_info(); // Add custom textarea type. - $DB->insert_record('user_info_field', array( - 'shortname' => 'longtext', 'name' => 'Long text', 'categoryid' => 1, + $customfield = $this->getDataGenerator()->create_custom_profile_field(array( + 'shortname' => 'longtext', 'name' => 'Long text', 'datatype' => 'textarea')); - $customfield = $DB->get_record('user_info_field', - array('shortname' => 'longtext')); // The list of fields should include the text field added in setUp(), // but should not include the textarea field added just now. @@ -465,11 +459,9 @@ class availability_profile_condition_testcase extends advanced_testcase { condition::wipe_static_cache(); // For testing, make another info field with default value. - $DB->insert_record('user_info_field', array( - 'shortname' => 'tonguestyle', 'name' => 'Tongue style', 'categoryid' => 1, + $otherprofilefield = $this->getDataGenerator()->create_custom_profile_field(array( + 'shortname' => 'tonguestyle', 'name' => 'Tongue style', 'datatype' => 'text', 'defaultdata' => 'Slimy')); - $otherprofilefield = $DB->get_record('user_info_field', - array('shortname' => 'tonguestyle')); // Make a test course and some users. $generator = $this->getDataGenerator(); diff --git a/badges/tests/badgeslib_test.php b/badges/tests/badgeslib_test.php index b44d2f59a52..2be62c01178 100644 --- a/badges/tests/badgeslib_test.php +++ b/badges/tests/badgeslib_test.php @@ -592,9 +592,9 @@ class badgeslib_test extends advanced_testcase { require_once($CFG->dirroot.'/user/profile/lib.php'); // Add a custom field of textarea type. - $customprofileid = $DB->insert_record('user_info_field', array( - 'shortname' => 'newfield', 'name' => 'Description of new field', 'categoryid' => 1, - 'datatype' => 'textarea')); + $customprofileid = $this->getDataGenerator()->create_custom_profile_field(array( + 'shortname' => 'newfield', 'name' => 'Description of new field', + 'datatype' => 'textarea'))->id; $this->preventResetByRollback(); // Messaging is not compatible with transactions. $badge = new badge($this->coursebadge); diff --git a/lib/testing/generator/data_generator.php b/lib/testing/generator/data_generator.php index 29c81681464..9eb9914009c 100644 --- a/lib/testing/generator/data_generator.php +++ b/lib/testing/generator/data_generator.php @@ -1289,7 +1289,7 @@ EOD; 'defaultdata' => 0 ] ]; - foreach ($typedefaults[$data['datatype']] as $field => $value) { + foreach ($typedefaults[$data['datatype']] ?? [] as $field => $value) { $defaults[$field] = $value; } diff --git a/lib/tests/event_profile_field_test.php b/lib/tests/event_profile_field_test.php index 82154e7dcae..d2b5b42b6c8 100644 --- a/lib/tests/event_profile_field_test.php +++ b/lib/tests/event_profile_field_test.php @@ -50,13 +50,8 @@ class core_event_profile_field_testcase extends advanced_testcase { * Test that triggering the user_info_category_created event works as expected. */ public function test_user_info_category_created_event() { - global $DB; - // Create a new profile category. - $cat1 = new stdClass(); - $cat1->name = 'Example category'; - $cat1->sortorder = $DB->count_records('user_info_category') + 1; - $cat1->id = $DB->insert_record('user_info_category', $cat1); + $cat1 = $this->getDataGenerator()->create_custom_profile_field_category(['name' => 'Example category']); // Trigger the event. $sink = $this->redirectEvents(); @@ -81,15 +76,8 @@ class core_event_profile_field_testcase extends advanced_testcase { global $DB; // Create new profile categories. - $cat1 = new stdClass(); - $cat1->name = 'Example category'; - $cat1->sortorder = $DB->count_records('user_info_category') + 1; - $cat1->id = $DB->insert_record('user_info_category', $cat1); - - $cat2 = new stdClass(); - $cat2->name = 'Example category 2'; - $cat2->sortorder = $DB->count_records('user_info_category') + 1; - $cat2->id = $DB->insert_record('user_info_category', $cat2); + $cat1 = $this->getDataGenerator()->create_custom_profile_field_category(['name' => 'Example category']); + $cat2 = $this->getDataGenerator()->create_custom_profile_field_category(['name' => 'Example category 2']); // Trigger the events. $sink = $this->redirectEvents(); @@ -116,18 +104,9 @@ class core_event_profile_field_testcase extends advanced_testcase { * Test that deleting a user info category triggers a delete event. */ public function test_user_info_category_deleted_event() { - global $DB; - // Create new profile categories. - $cat1 = new stdClass(); - $cat1->name = 'Example category'; - $cat1->sortorder = $DB->count_records('user_info_category') + 1; - $cat1->id = $DB->insert_record('user_info_category', $cat1); - - $cat2 = new stdClass(); - $cat2->name = 'Example category 2'; - $cat2->sortorder = $DB->count_records('user_info_category') + 1; - $cat2->id = $DB->insert_record('user_info_category', $cat2); + $cat1 = $this->getDataGenerator()->create_custom_profile_field_category(['name' => 'Example category']); + $cat2 = $this->getDataGenerator()->create_custom_profile_field_category(['name' => 'Example category 2']); // Trigger the event. $sink = $this->redirectEvents(); @@ -152,10 +131,7 @@ class core_event_profile_field_testcase extends advanced_testcase { global $DB; // Create a new profile category. - $cat1 = new stdClass(); - $cat1->name = 'Example category'; - $cat1->sortorder = $DB->count_records('user_info_category') + 1; - $cat1->id = $DB->insert_record('user_info_category', $cat1); + $cat1 = $this->getDataGenerator()->create_custom_profile_field_category(['name' => 'Example category']); // Create a new profile field. $data = new stdClass(); @@ -196,27 +172,17 @@ class core_event_profile_field_testcase extends advanced_testcase { * Test that updating a user info field triggers an update event. */ public function test_user_info_field_updated_event() { - global $DB; - // Create a new profile category. - $cat1 = new stdClass(); - $cat1->name = 'Example category'; - $cat1->sortorder = $DB->count_records('user_info_category') + 1; - $cat1->id = $DB->insert_record('user_info_category', $cat1); + $cat1 = $this->getDataGenerator()->create_custom_profile_field_category(['name' => 'Example category']); // Create a new profile field. - $data = new stdClass(); - $data->datatype = 'text'; - $data->shortname = 'example'; - $data->name = 'Example field'; - $data->description = 'Hello this is an example.'; - $data->required = false; - $data->locked = false; - $data->forceunique = false; - $data->signup = false; - $data->visible = '0'; - $data->categoryid = $cat1->id; - $data->id = $DB->insert_record('user_info_field', $data); + $data = $this->getDataGenerator()->create_custom_profile_field([ + 'datatype' => 'text', + 'shortname' => 'example', + 'name' => 'Example field', + 'description' => 'Hello this is an example.', + 'categoryid' => $cat1->id, + ]); // Trigger the event. $sink = $this->redirectEvents(); @@ -241,36 +207,25 @@ class core_event_profile_field_testcase extends advanced_testcase { * Test that moving a field triggers update events. */ public function test_user_info_field_updated_event_move_field() { - global $DB; - // Create a new profile category. - $cat1 = new stdClass(); - $cat1->name = 'Example category'; - $cat1->sortorder = $DB->count_records('user_info_category') + 1; - $cat1->id = $DB->insert_record('user_info_category', $cat1); + $cat1 = $this->getDataGenerator()->create_custom_profile_field_category(['name' => 'Example category']); // Create a new profile field. - $field1 = new stdClass(); - $field1->datatype = 'text'; - $field1->shortname = 'example'; - $field1->name = 'Example field'; - $field1->description = 'Hello this is an example.'; - $field1->required = false; - $field1->locked = false; - $field1->forceunique = false; - $field1->signup = false; - $field1->visible = '0'; - $field1->categoryid = $cat1->id; - $field1->sortorder = $DB->count_records('user_info_field') + 1; - $field1->id = $DB->insert_record('user_info_field', $field1); + $field1 = $this->getDataGenerator()->create_custom_profile_field([ + 'datatype' => 'text', + 'shortname' => 'example', + 'name' => 'Example field', + 'description' => 'Hello this is an example.', + 'categoryid' => $cat1->id, + ]); // Create another that we will be moving. - $field2 = clone $field1; - $field2->datatype = 'text'; - $field2->shortname = 'example2'; - $field2->name = 'Example field 2'; - $field2->sortorder = $DB->count_records('user_info_field') + 1; - $field2->id = $DB->insert_record('user_info_field', $field2); + $field2 = $this->getDataGenerator()->create_custom_profile_field([ + 'datatype' => 'text', + 'shortname' => 'example2', + 'name' => 'Example field 2', + 'categoryid' => $cat1->id, + ]); // Trigger the events. $sink = $this->redirectEvents(); @@ -302,32 +257,18 @@ class core_event_profile_field_testcase extends advanced_testcase { * another category triggers an update event. */ public function test_user_info_field_updated_event_delete_category() { - global $DB; - - // Create a new profile category. - $cat1 = new stdClass(); - $cat1->name = 'Example category'; - $cat1->sortorder = $DB->count_records('user_info_category') + 1; - $cat1->id = $DB->insert_record('user_info_category', $cat1); - - $cat2 = new stdClass(); - $cat2->name = 'Example category'; - $cat2->sortorder = $DB->count_records('user_info_category') + 1; - $cat2->id = $DB->insert_record('user_info_category', $cat2); + // Create profile categories. + $cat1 = $this->getDataGenerator()->create_custom_profile_field_category(['name' => 'Example category']); + $cat2 = $this->getDataGenerator()->create_custom_profile_field_category(['name' => 'Example category']); // Create a new profile field. - $field = new stdClass(); - $field->datatype = 'text'; - $field->shortname = 'example'; - $field->name = 'Example field'; - $field->description = 'Hello this is an example.'; - $field->required = false; - $field->locked = false; - $field->forceunique = false; - $field->signup = false; - $field->visible = '0'; - $field->categoryid = $cat1->id; - $field->id = $DB->insert_record('user_info_field', $field); + $field = $this->getDataGenerator()->create_custom_profile_field([ + 'datatype' => 'text', + 'shortname' => 'example', + 'name' => 'Example field', + 'description' => 'Hello this is an example.', + 'categoryid' => $cat1->id, + ]); // Trigger the event. $sink = $this->redirectEvents(); @@ -351,27 +292,17 @@ class core_event_profile_field_testcase extends advanced_testcase { * Test that deleting a user info field triggers a delete event. */ public function test_user_info_field_deleted_event() { - global $DB; - // Create a new profile category. - $cat1 = new stdClass(); - $cat1->name = 'Example category'; - $cat1->sortorder = $DB->count_records('user_info_category') + 1; - $cat1->id = $DB->insert_record('user_info_category', $cat1); + $cat1 = $this->getDataGenerator()->create_custom_profile_field_category(['name' => 'Example category']); // Create a new profile field. - $data = new stdClass(); - $data->datatype = 'text'; - $data->shortname = 'delete'; - $data->name = 'Example field for delete'; - $data->description = 'Hello this is an example.'; - $data->required = false; - $data->locked = false; - $data->forceunique = false; - $data->signup = false; - $data->visible = '0'; - $data->categoryid = $cat1->id; - $data->id = $DB->insert_record('user_info_field', $data, true); + $data = $this->getDataGenerator()->create_custom_profile_field([ + 'datatype' => 'text', + 'shortname' => 'delete', + 'name' => 'Example field for delete', + 'description' => 'Hello this is an example.', + 'categoryid' => $cat1->id, + ]); // Trigger the event. $sink = $this->redirectEvents(); diff --git a/user/profile/field/checkbox/tests/privacy_test.php b/user/profile/field/checkbox/tests/privacy_test.php index 9f4862e84bb..17d618c9767 100644 --- a/user/profile/field/checkbox/tests/privacy_test.php +++ b/user/profile/field/checkbox/tests/privacy_test.php @@ -276,13 +276,8 @@ class profilefield_checkbox_testcase extends provider_testcase { * @return int The ID of the profile category */ private function add_profile_category() { - global $DB; - // Create a new profile category. - $cat = new stdClass(); - $cat->name = 'Test category'; - $cat->sortorder = 1; - - return $DB->insert_record('user_info_category', $cat); + $cat = $this->getDataGenerator()->create_custom_profile_field_category(['name' => 'Test category']); + return $cat->id; } /** @@ -293,20 +288,13 @@ class profilefield_checkbox_testcase extends provider_testcase { * @return int The ID of the profile field */ private function add_profile_field($categoryid, $datatype) { - global $DB; - // Create a new profile field. - $data = new stdClass(); - $data->datatype = $datatype; - $data->shortname = 'tstField'; - $data->name = 'Test field'; - $data->description = 'This is a test.'; - $data->required = false; - $data->locked = false; - $data->forceunique = false; - $data->signup = false; - $data->visible = '0'; - $data->categoryid = $categoryid; - - return $DB->insert_record('user_info_field', $data); + $data = $this->getDataGenerator()->create_custom_profile_field([ + 'datatype' => $datatype, + 'shortname' => 'tstField', + 'name' => 'Test field', + 'description' => 'This is a test.', + 'categoryid' => $categoryid, + ]); + return $data->id; } } diff --git a/user/profile/field/datetime/tests/privacy_test.php b/user/profile/field/datetime/tests/privacy_test.php index d18026e044a..eaa1921495a 100644 --- a/user/profile/field/datetime/tests/privacy_test.php +++ b/user/profile/field/datetime/tests/privacy_test.php @@ -280,13 +280,8 @@ class profilefield_datetime_testcase extends provider_testcase { * @return int The ID of the profile category */ private function add_profile_category() { - global $DB; - // Create a new profile category. - $cat = new stdClass(); - $cat->name = 'Test category'; - $cat->sortorder = 1; - - return $DB->insert_record('user_info_category', $cat); + $cat = $this->getDataGenerator()->create_custom_profile_field_category(['name' => 'Test category']); + return $cat->id; } /** @@ -297,20 +292,13 @@ class profilefield_datetime_testcase extends provider_testcase { * @return int The ID of the profile field */ private function add_profile_field($categoryid, $datatype) { - global $DB; - // Create a new profile field. - $data = new stdClass(); - $data->datatype = $datatype; - $data->shortname = 'tstField'; - $data->name = 'Test field'; - $data->description = 'This is a test.'; - $data->required = false; - $data->locked = false; - $data->forceunique = false; - $data->signup = false; - $data->visible = '0'; - $data->categoryid = $categoryid; - - return $DB->insert_record('user_info_field', $data); + $data = $this->getDataGenerator()->create_custom_profile_field([ + 'datatype' => $datatype, + 'shortname' => 'tstField', + 'name' => 'Test field', + 'description' => 'This is a test.', + 'categoryid' => $categoryid, + ]); + return $data->id; } } diff --git a/user/profile/field/menu/tests/privacy_test.php b/user/profile/field/menu/tests/privacy_test.php index ff399ebb7d6..647f40fe13f 100644 --- a/user/profile/field/menu/tests/privacy_test.php +++ b/user/profile/field/menu/tests/privacy_test.php @@ -277,13 +277,8 @@ class profilefield_menu_testcase extends provider_testcase { * @return int The ID of the profile category */ private function add_profile_category() { - global $DB; - // Create a new profile category. - $cat = new stdClass(); - $cat->name = 'Test category'; - $cat->sortorder = 1; - - return $DB->insert_record('user_info_category', $cat); + $cat = $this->getDataGenerator()->create_custom_profile_field_category(['name' => 'Test category']); + return $cat->id; } /** @@ -294,20 +289,13 @@ class profilefield_menu_testcase extends provider_testcase { * @return int The ID of the profile field */ private function add_profile_field($categoryid, $datatype) { - global $DB; - // Create a new profile field. - $data = new stdClass(); - $data->datatype = $datatype; - $data->shortname = 'tstField'; - $data->name = 'Test field'; - $data->description = 'This is a test.'; - $data->required = false; - $data->locked = false; - $data->forceunique = false; - $data->signup = false; - $data->visible = '0'; - $data->categoryid = $categoryid; - - return $DB->insert_record('user_info_field', $data); + $data = $this->getDataGenerator()->create_custom_profile_field([ + 'datatype' => $datatype, + 'shortname' => 'tstField', + 'name' => 'Test field', + 'description' => 'This is a test.', + 'categoryid' => $categoryid, + ]); + return $data->id; } } diff --git a/user/profile/field/social/tests/privacy_test.php b/user/profile/field/social/tests/privacy_test.php index 4c336f04b32..bc6707cd2c6 100644 --- a/user/profile/field/social/tests/privacy_test.php +++ b/user/profile/field/social/tests/privacy_test.php @@ -276,13 +276,8 @@ class profilefield_social_testcase extends provider_testcase { * @return int The ID of the profile category */ private function add_profile_category() { - global $DB; - // Create a new profile category. - $cat = new stdClass(); - $cat->name = 'Test category'; - $cat->sortorder = 1; - - return $DB->insert_record('user_info_category', $cat); + $cat = $this->getDataGenerator()->create_custom_profile_field_category(['name' => 'Test category']); + return $cat->id; } /** @@ -293,20 +288,13 @@ class profilefield_social_testcase extends provider_testcase { * @return int The ID of the profile field */ private function add_profile_field($categoryid, $datatype) { - global $DB; - // Create a new profile field. - $data = new stdClass(); - $data->datatype = $datatype; - $data->shortname = 'icq'; - $data->name = 'icq'; - $data->description = ''; - $data->required = false; - $data->locked = false; - $data->forceunique = false; - $data->signup = false; - $data->visible = '0'; - $data->categoryid = $categoryid; - - return $DB->insert_record('user_info_field', $data); + $data = $this->getDataGenerator()->create_custom_profile_field([ + 'datatype' => $datatype, + 'shortname' => 'tstField', + 'name' => 'Test field', + 'description' => 'This is a test.', + 'categoryid' => $categoryid, + ]); + return $data->id; } } diff --git a/user/profile/field/text/tests/privacy_test.php b/user/profile/field/text/tests/privacy_test.php index a5c35d5ff1c..a3e2bcc60be 100644 --- a/user/profile/field/text/tests/privacy_test.php +++ b/user/profile/field/text/tests/privacy_test.php @@ -277,13 +277,8 @@ class profilefield_text_testcase extends provider_testcase { * @return int The ID of the profile category */ private function add_profile_category() { - global $DB; - // Create a new profile category. - $cat = new stdClass(); - $cat->name = 'Test category'; - $cat->sortorder = 1; - - return $DB->insert_record('user_info_category', $cat); + $cat = $this->getDataGenerator()->create_custom_profile_field_category(['name' => 'Test category']); + return $cat->id; } /** @@ -294,20 +289,13 @@ class profilefield_text_testcase extends provider_testcase { * @return int The ID of the profile field */ private function add_profile_field($categoryid, $datatype) { - global $DB; - // Create a new profile field. - $data = new stdClass(); - $data->datatype = $datatype; - $data->shortname = 'tstField'; - $data->name = 'Test field'; - $data->description = 'This is a test.'; - $data->required = false; - $data->locked = false; - $data->forceunique = false; - $data->signup = false; - $data->visible = '0'; - $data->categoryid = $categoryid; - - return $DB->insert_record('user_info_field', $data); + $data = $this->getDataGenerator()->create_custom_profile_field([ + 'datatype' => $datatype, + 'shortname' => 'tstField', + 'name' => 'Test field', + 'description' => 'This is a test.', + 'categoryid' => $categoryid, + ]); + return $data->id; } } diff --git a/user/profile/field/textarea/tests/privacy_test.php b/user/profile/field/textarea/tests/privacy_test.php index 5ed8bb49b61..1e47d57cb3e 100644 --- a/user/profile/field/textarea/tests/privacy_test.php +++ b/user/profile/field/textarea/tests/privacy_test.php @@ -276,13 +276,8 @@ class profilefield_textarea_testcase extends provider_testcase { * @return int The ID of the profile category */ private function add_profile_category() { - global $DB; - // Create a new profile category. - $cat = new stdClass(); - $cat->name = 'Test category'; - $cat->sortorder = 1; - - return $DB->insert_record('user_info_category', $cat); + $cat = $this->getDataGenerator()->create_custom_profile_field_category(['name' => 'Test category']); + return $cat->id; } /** @@ -293,20 +288,13 @@ class profilefield_textarea_testcase extends provider_testcase { * @return int The ID of the profile field */ private function add_profile_field($categoryid, $datatype) { - global $DB; - // Create a new profile field. - $data = new stdClass(); - $data->datatype = $datatype; - $data->shortname = 'tstField'; - $data->name = 'Test field'; - $data->description = 'This is a test.'; - $data->required = false; - $data->locked = false; - $data->forceunique = false; - $data->signup = false; - $data->visible = '0'; - $data->categoryid = $categoryid; - - return $DB->insert_record('user_info_field', $data); + $data = $this->getDataGenerator()->create_custom_profile_field([ + 'datatype' => $datatype, + 'shortname' => 'tstField', + 'name' => 'Test field', + 'description' => 'This is a test.', + 'categoryid' => $categoryid, + ]); + return $data->id; } } diff --git a/user/tests/profilelib_test.php b/user/tests/profilelib_test.php index 98c0c034cb7..cbfea7cb0ae 100644 --- a/user/tests/profilelib_test.php +++ b/user/tests/profilelib_test.php @@ -39,16 +39,16 @@ class core_user_profilelib_testcase extends advanced_testcase { * with profile_user_record. */ public function test_get_custom_fields() { - global $DB, $CFG; + global $CFG; require_once($CFG->dirroot . '/user/profile/lib.php'); $this->resetAfterTest(); $user = $this->getDataGenerator()->create_user(); // Add a custom field of textarea type. - $id1 = $DB->insert_record('user_info_field', array( - 'shortname' => 'frogdesc', 'name' => 'Description of frog', 'categoryid' => 1, - 'datatype' => 'textarea')); + $id1 = $this->getDataGenerator()->create_custom_profile_field([ + 'shortname' => 'frogdesc', 'name' => 'Description of frog', + 'datatype' => 'textarea'])->id; // Check the field is returned. $result = profile_get_custom_fields(); @@ -66,9 +66,9 @@ class core_user_profilelib_testcase extends advanced_testcase { $this->assertObjectHasAttribute('frogdesc', profile_user_record($user->id, false)); // Add another custom field, this time of normal text type. - $id2 = $DB->insert_record('user_info_field', array( - 'shortname' => 'frogname', 'name' => 'Name of frog', 'categoryid' => 1, - 'datatype' => 'text')); + $id2 = $this->getDataGenerator()->create_custom_profile_field(array( + 'shortname' => 'frogname', 'name' => 'Name of frog', + 'datatype' => 'text'))->id; // Check both are returned using normal option. $result = profile_get_custom_fields(); @@ -147,26 +147,26 @@ class core_user_profilelib_testcase extends advanced_testcase { * Test that {@link user_not_fully_set_up()} takes required custom fields into account. */ public function test_profile_has_required_custom_fields_set() { - global $CFG, $DB; + global $CFG; require_once($CFG->dirroot.'/mnet/lib.php'); $this->resetAfterTest(); // Add a required, visible, unlocked custom field. - $DB->insert_record('user_info_field', ['shortname' => 'house', 'name' => 'House', 'required' => 1, - 'visible' => 1, 'locked' => 0, 'categoryid' => 1, 'datatype' => 'text']); + $this->getDataGenerator()->create_custom_profile_field(['shortname' => 'house', 'name' => 'House', 'required' => 1, + 'visible' => 1, 'locked' => 0, 'datatype' => 'text']); // Add an optional, visible, unlocked custom field. - $DB->insert_record('user_info_field', ['shortname' => 'pet', 'name' => 'Pet', 'required' => 0, - 'visible' => 1, 'locked' => 0, 'categoryid' => 1, 'datatype' => 'text']); + $this->getDataGenerator()->create_custom_profile_field(['shortname' => 'pet', 'name' => 'Pet', 'required' => 0, + 'visible' => 1, 'locked' => 0, 'datatype' => 'text']); // Add required but invisible custom field. - $DB->insert_record('user_info_field', ['shortname' => 'secretid', 'name' => 'Secret ID', 'required' => 1, - 'visible' => 0, 'locked' => 0, 'categoryid' => 1, 'datatype' => 'text']); + $this->getDataGenerator()->create_custom_profile_field(['shortname' => 'secretid', 'name' => 'Secret ID', + 'required' => 1, 'visible' => 0, 'locked' => 0, 'datatype' => 'text']); // Add required but locked custom field. - $DB->insert_record('user_info_field', ['shortname' => 'muggleborn', 'name' => 'Muggle-born', 'required' => 1, - 'visible' => 1, 'locked' => 1, 'categoryid' => 1, 'datatype' => 'checkbox']); + $this->getDataGenerator()->create_custom_profile_field(['shortname' => 'muggleborn', 'name' => 'Muggle-born', + 'required' => 1, 'visible' => 1, 'locked' => 1, 'datatype' => 'checkbox']); // Create some student accounts. $hermione = $this->getDataGenerator()->create_user(); @@ -215,14 +215,14 @@ class core_user_profilelib_testcase extends advanced_testcase { * Test that user generator sets the custom profile fields */ public function test_profile_fields_in_generator() { - global $CFG, $DB; + global $CFG; require_once($CFG->dirroot.'/mnet/lib.php'); $this->resetAfterTest(); // Add a required, visible, unlocked custom field. - $DB->insert_record('user_info_field', ['shortname' => 'house', 'name' => 'House', 'required' => 1, - 'visible' => 1, 'locked' => 0, 'categoryid' => 1, 'datatype' => 'text']); + $this->getDataGenerator()->create_custom_profile_field(['shortname' => 'house', 'name' => 'House', 'required' => 1, + 'visible' => 1, 'locked' => 0, 'datatype' => 'text', 'default' => null]); // Create some student accounts. $hermione = $this->getDataGenerator()->create_user(['profile_field_house' => 'Gryffindor']);