Skip to content

Commit

Permalink
MDL-78303: Fix unit tests when custom profile fields have been added
Browse files Browse the repository at this point in the history
  • Loading branch information
petersistrom committed Dec 1, 2023
1 parent b1bb567 commit 151f229
Showing 1 changed file with 10 additions and 4 deletions.
14 changes: 10 additions & 4 deletions lib/testing/tests/testing_generator_test.php
Original file line number Diff line number Diff line change
Expand Up @@ -470,20 +470,23 @@ public function test_create_custom_profile_field_category() {
$this->resetAfterTest();
$generator = $this->getDataGenerator();

// MDL-78303: Get the initial sort order in case any custom profile field categories have already been added.
$initialsortorder = (int)$DB->get_field_sql('SELECT MAX(sortorder) FROM {user_info_category}');

// 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);
$this->assertEquals($initialsortorder + 1, $record->sortorder);

// Also check the return value.
$this->assertEquals(1, $result->sortorder);
$this->assertEquals($initialsortorder + 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);
$this->assertEquals($initialsortorder + 2, $record->sortorder);

// Insert category with specified sortorder.
$generator->create_custom_profile_field_category(['name' => 'Toads', 'sortorder' => 9]);
Expand All @@ -502,6 +505,9 @@ public function test_create_custom_profile_field() {
$this->resetAfterTest();
$generator = $this->getDataGenerator();

// MDL-78303: Get the initial sort order in case any custom profile field categories have already been added.
$initialsortorder = (int)$DB->get_field_sql('SELECT MAX(sortorder) FROM {user_info_category}');

// Insert minimal field without specified category.
$field1 = $generator->create_custom_profile_field(
['datatype' => 'text', 'shortname' => 'colour', 'name' => 'Colour']);
Expand Down Expand Up @@ -535,7 +541,7 @@ public function test_create_custom_profile_field() {
// 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);
$this->assertEquals($initialsortorder + 1, $catrecord->sortorder);

// Create another field, this time supplying values for a few of the fields.
$generator->create_custom_profile_field(
Expand Down

0 comments on commit 151f229

Please sign in to comment.