forked from moodle/moodle
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
MDL-64958 gradeimport_csv: Better unit tests for check_user_exists()
- Loading branch information
1 parent
d45f0f0
commit 9f09d9d
Showing
1 changed file
with
101 additions
and
42 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -242,58 +242,117 @@ public function test_import_new_grade_item() { | |
$this->assertEquals(count($testarray), count($newgradeimportitems)); | ||
} | ||
|
||
/** | ||
* Data provider for \gradeimport_csv_load_data_testcase::test_check_user_exists(). | ||
* | ||
* @return array | ||
*/ | ||
public function check_user_exists_provider() { | ||
return [ | ||
'Fetch by email' => [ | ||
'email', '[email protected]', true | ||
], | ||
'Fetch data using a non-existent email' => [ | ||
'email', '[email protected]', false | ||
], | ||
'Multiple accounts with the same email' => [ | ||
'email', '[email protected]', false, 1 | ||
], | ||
'Fetch data using a valid user ID' => [ | ||
'id', true, true | ||
], | ||
'Fetch data using a non-existent user ID' => [ | ||
'id', false, false | ||
], | ||
'Fetch data using a valid username' => [ | ||
'username', 's1', true | ||
], | ||
'Fetch data using an invalid username' => [ | ||
'username', 's2', false | ||
], | ||
'Fetch data using a valid ID Number' => [ | ||
'idnumber', 's1', true | ||
], | ||
'Fetch data using an invalid ID Number' => [ | ||
'idnumber', 's2', false | ||
], | ||
]; | ||
} | ||
|
||
/** | ||
* Check that the user matches a user in the system. | ||
* | ||
* @dataProvider check_user_exists_provider | ||
* @param string $field The field to use for the query. | ||
* @param string|boolean $value The field value. When fetching by ID, set true to fetch valid user ID, false otherwise. | ||
* @param boolean $successexpected Whether we expect for a user to be found or not. | ||
* @param int $allowaccountssameemail Value for $CFG->allowaccountssameemail | ||
*/ | ||
public function test_check_user_exists() { | ||
public function test_check_user_exists($field, $value, $successexpected, $allowaccountssameemail = 0) { | ||
$this->resetAfterTest(); | ||
|
||
$generator = $this->getDataGenerator(); | ||
|
||
// Need to add one of the users into the system. | ||
$user = new stdClass(); | ||
$user->firstname = 'Anne'; | ||
$user->lastname = 'Able'; | ||
$user->email = '[email protected]'; | ||
$userdetail = $this->getDataGenerator()->create_user($user); | ||
$user = $generator->create_user([ | ||
'firstname' => 'Anne', | ||
'lastname' => 'Able', | ||
'email' => '[email protected]', | ||
'idnumber' => 's1', | ||
'username' => 's1', | ||
]); | ||
|
||
if ($allowaccountssameemail) { | ||
// Create another user with the same email address. | ||
$generator->create_user(['email' => '[email protected]']); | ||
} | ||
|
||
$testobject = new phpunit_gradeimport_csv_load_data(); | ||
// Since the data provider can't know what user ID to use, do a special handling for ID field tests. | ||
if ($field === 'id') { | ||
if ($value) { | ||
// Test for fetching data using a valid user ID. Use the generated user's ID. | ||
$value = $user->id; | ||
} else { | ||
// Test for fetching data using a non-existent user ID. | ||
$value = $user->id + 1; | ||
} | ||
} | ||
|
||
$testarray = $this->csv_load($this->oktext); | ||
$userfields = [ | ||
'field' => $field, | ||
'label' => 'Field label: ' . $field | ||
]; | ||
|
||
$userfields = array('field' => 'email', 'label' => 'Email address'); | ||
// If the user exists then the user id is returned. | ||
$userid = $testobject->test_check_user_exists($testarray[0][5] , $userfields); | ||
// Check that the user id returned matches with the user that we created. | ||
$this->assertEquals($userid, $userdetail->id); | ||
$testobject = new phpunit_gradeimport_csv_load_data(); | ||
|
||
// Check for failure. | ||
// Try for an exception. | ||
$userfields = array('field' => 'id', 'label' => 'userid'); | ||
$userid = $testobject->test_check_user_exists($testarray[0][0], $userfields); | ||
// Check that the userid is null. | ||
$this->assertNull($userid); | ||
|
||
// Expected error message. | ||
$mappingobject = new stdClass(); | ||
$mappingobject->field = $userfields['label']; | ||
$mappingobject->value = $testarray[0][0]; | ||
$expectederrormessage = get_string('usermappingerror', 'grades', $mappingobject); | ||
// Check that expected error message and actual message match. | ||
$gradebookerrors = $testobject->get_gradebookerrors(); | ||
$this->assertEquals($expectederrormessage, $gradebookerrors[0]); | ||
// Check whether the user exists. If so, then the user id is returned. Otherwise, it returns null. | ||
$userid = $testobject->test_check_user_exists($value, $userfields); | ||
|
||
if ($successexpected) { | ||
// Check that the user id returned matches with the user that we created. | ||
$this->assertEquals($user->id, $userid); | ||
|
||
// Check that there are no errors. | ||
$this->assertEmpty($testobject->get_gradebookerrors()); | ||
|
||
} else { | ||
// Check that the userid is null. | ||
$this->assertNull($userid); | ||
|
||
// Check that expected error message and actual message match. | ||
$gradebookerrors = $testobject->get_gradebookerrors(); | ||
$mappingobject = (object)[ | ||
'field' => $userfields['label'], | ||
'value' => $value, | ||
]; | ||
if ($allowaccountssameemail) { | ||
$expectederrormessage = get_string('usermappingerrormultipleusersfound', 'grades', $mappingobject); | ||
} else { | ||
$expectederrormessage = get_string('usermappingerror', 'grades', $mappingobject); | ||
} | ||
|
||
// The field mapping is correct, but the student does not exist. | ||
$userid = $testobject->test_check_user_exists($testarray[1][5], $userfields); | ||
// Check that the userid is null. | ||
$this->assertNull($userid); | ||
|
||
// Expected error message. | ||
$mappingobject = new stdClass(); | ||
$mappingobject->field = $userfields['label']; | ||
$mappingobject->value = $testarray[1][5]; | ||
$expectederrormessage = get_string('usermappingerror', 'grades', $mappingobject); | ||
// Check that expected error message and actual message match. | ||
$gradebookerrors = $testobject->get_gradebookerrors(); | ||
// This is the second error in the array of gradebook errors. | ||
$this->assertEquals($expectederrormessage, $gradebookerrors[1]); | ||
$this->assertEquals($expectederrormessage, $gradebookerrors[0]); | ||
} | ||
} | ||
|
||
/** | ||
|