Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /**
- * Test to validate badges_save_backpack_credentials.
- *
- * @dataProvider save_backpack_credentials_provider
- * @param bool $addbackpack True if backpack data has to be created; false otherwise (empty data will be used then).
- * @param string|null $mail Backpack mail address.
- * @param string|null $password Backpack password.
- */
- public function test_save_backpack_credentials(bool $addbackpack = true, ?string $mail = null, ?string $password = null) {
- global $DB;
- $this->resetAfterTest();
- $this->setAdminUser();
- $data = [];
- if ($addbackpack) {
- $data = new \stdClass();
- $data->apiversion = OPEN_BADGES_V2P1;
- $data->backpackapiurl = 'https://dc.imsglobal.org/obchost/ims/ob/v2p1';
- $data->backpackweburl = 'https://dc.imsglobal.org';
- badges_create_site_backpack($data);
- $backpack = $DB->get_record('badge_external_backpack', ['backpackweburl' => $data->backpackweburl]);
- $user = $this->getDataGenerator()->create_user();
- $data = [
- 'externalbackpackid' => $backpack->id,
- 'userid' => $user->id,
- ];
- if (!empty($mail)) {
- $data['backpackemail'] = $mail;
- }
- if (!empty($password)) {
- $data['password'] = $password;
- }
- }
- $return = badges_save_backpack_credentials((object) $data);
- if (array_key_exists('userid', $data)) {
- $record = $DB->get_record('badge_backpack', ['userid' => $user->id]);
- } else {
- $record = $DB->get_records('badge_backpack');
- }
- if (!empty($mail) && !empty($password)) {
- // The backpack credentials are created if the given information is right.
- $this->assertNotEmpty($record);
- $this->assertEquals($data['externalbackpackid'], $return);
- } else if ($addbackpack) {
- // If no email and password are given, no backpack is created/modified.
- $this->assertEmpty($record);
- $this->assertEquals($data['externalbackpackid'], $return);
- } else {
- // There weren't fields to add to the backpack so no DB change is expected.
- $this->assertEmpty($record);
- $this->assertEquals(0, $return);
- }
- // Confirm the existing backpack credential can be updated (if it has been created).
- if (!empty($record)) {
- $data['backpackemail'] = 'modified_' . $mail;
- $data['id'] = $record->id;
- $return = badges_save_backpack_credentials((object) $data);
- $record = $DB->get_record('badge_backpack', ['userid' => $user->id]);
- $this->assertNotEmpty($record);
- $this->assertEquals($data['backpackemail'], $record->email);
- $this->assertEquals($data['externalbackpackid'], $return);
- }
- }
- /**
- * Data provider for test_create_backpack_credentials().
- *
- * @return array
- */
- public function save_backpack_credentials_provider(): array {
- return [
- 'Empty fields' => [
- false,
- ],
- 'No backpack mail or password are defined' => [
- true,
- ],
- 'Both backpack mail and password are defined' => [
- true, 'test@test.com', '1234',
- ],
- 'Only backpack mail is defined (no password is given)' => [
- true, 'test@test.com', null,
- ],
- 'Only backpack password is defined (no mail is given)' => [
- true, null, '1234'
- ],
- ];
- }
Add Comment
Please, Sign In to add comment