Skip to content

Commit

Permalink
MDL-71051 core_user: use user profile generator in all tests
Browse files Browse the repository at this point in the history
  • Loading branch information
marinaglancy committed Apr 28, 2021
1 parent 9a5fd6f commit ef3d6a3
Show file tree
Hide file tree
Showing 14 changed files with 149 additions and 301 deletions.
7 changes: 3 additions & 4 deletions admin/tool/uploaduser/tests/cli_test.php
Original file line number Diff line number Diff line change
Expand Up @@ -126,14 +126,13 @@ public function test_upload_with_applying_defaults() {
* 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';
Expand Down
3 changes: 1 addition & 2 deletions auth/db/tests/db_test.php
Original file line number Diff line number Diff line change
Expand Up @@ -139,8 +139,7 @@ protected function init_auth_database() {
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');
Expand Down
19 changes: 9 additions & 10 deletions auth/email/tests/external_test.php
Original file line number Diff line number Diff line change
Expand Up @@ -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() {
Expand Down Expand Up @@ -109,8 +108,8 @@ public function test_get_signup_settings_with_mathjax_in_profile_fields() {
// 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));

Expand Down
22 changes: 7 additions & 15 deletions availability/condition/profile/tests/condition_test.php
Original file line number Diff line number Diff line change
Expand Up @@ -50,14 +50,10 @@ public function setUp(): void {

$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();
Expand Down Expand Up @@ -333,11 +329,9 @@ public function test_custom_textarea_field() {
$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.
Expand Down Expand Up @@ -465,11 +459,9 @@ public function test_get_user_list_sql() {
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();
Expand Down
6 changes: 3 additions & 3 deletions badges/tests/badgeslib_test.php
Original file line number Diff line number Diff line change
Expand Up @@ -592,9 +592,9 @@ public function test_badges_observer_profile_criteria_review() {
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);
Expand Down
2 changes: 1 addition & 1 deletion lib/testing/generator/data_generator.php
Original file line number Diff line number Diff line change
Expand Up @@ -1289,7 +1289,7 @@ public function create_custom_profile_field(array $data): \stdClass {
'defaultdata' => 0
]
];
foreach ($typedefaults[$data['datatype']] as $field => $value) {
foreach ($typedefaults[$data['datatype']] ?? [] as $field => $value) {
$defaults[$field] = $value;
}

Expand Down
161 changes: 46 additions & 115 deletions lib/tests/event_profile_field_test.php
Original file line number Diff line number Diff line change
Expand Up @@ -50,13 +50,8 @@ public function setUp(): void {
* 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();
Expand All @@ -81,15 +76,8 @@ public function test_user_info_category_updated_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 events.
$sink = $this->redirectEvents();
Expand All @@ -116,18 +104,9 @@ public function test_user_info_category_updated_event() {
* 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();
Expand All @@ -152,10 +131,7 @@ public function test_user_info_field_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']);

// Create a new profile field.
$data = new stdClass();
Expand Down Expand Up @@ -196,27 +172,17 @@ public function test_user_info_field_created_event() {
* 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();
Expand All @@ -241,36 +207,25 @@ public function test_user_info_field_updated_event() {
* 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();
Expand Down Expand Up @@ -302,32 +257,18 @@ public function test_user_info_field_updated_event_move_field() {
* 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();
Expand All @@ -351,27 +292,17 @@ public function test_user_info_field_updated_event_delete_category() {
* 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();
Expand Down
Loading

0 comments on commit ef3d6a3

Please sign in to comment.