sarjona

test_save_backpack_credentials PHPUnit test sample

Oct 16th, 2020 (edited)
210
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 3.74 KB | None | 0 0
  1.     /**
  2.      * Test to validate badges_save_backpack_credentials.
  3.      *
  4.      * @dataProvider save_backpack_credentials_provider
  5.      * @param  bool $addbackpack True if backpack data has to be created; false otherwise (empty data will be used then).
  6.      * @param  string|null  $mail  Backpack mail address.
  7.      * @param  string|null  $password  Backpack password.
  8.      */
  9.     public function test_save_backpack_credentials(bool $addbackpack = true, ?string $mail = null, ?string $password = null) {
  10.         global $DB;
  11.  
  12.         $this->resetAfterTest();
  13.         $this->setAdminUser();
  14.  
  15.         $data = [];
  16.         if ($addbackpack) {
  17.             $data = new \stdClass();
  18.             $data->apiversion = OPEN_BADGES_V2P1;
  19.             $data->backpackapiurl = 'https://dc.imsglobal.org/obchost/ims/ob/v2p1';
  20.             $data->backpackweburl = 'https://dc.imsglobal.org';
  21.             badges_create_site_backpack($data);
  22.             $backpack = $DB->get_record('badge_external_backpack', ['backpackweburl' => $data->backpackweburl]);
  23.             $user = $this->getDataGenerator()->create_user();
  24.  
  25.             $data = [
  26.                 'externalbackpackid' => $backpack->id,
  27.                 'userid' => $user->id,
  28.             ];
  29.  
  30.             if (!empty($mail)) {
  31.                 $data['backpackemail'] = $mail;
  32.             }
  33.             if (!empty($password)) {
  34.                 $data['password'] = $password;
  35.             }
  36.         }
  37.  
  38.         $return = badges_save_backpack_credentials((object) $data);
  39.         if (array_key_exists('userid', $data)) {
  40.             $record = $DB->get_record('badge_backpack', ['userid' => $user->id]);
  41.         } else {
  42.             $record = $DB->get_records('badge_backpack');
  43.         }
  44.  
  45.         if (!empty($mail) && !empty($password)) {
  46.             // The backpack credentials are created if the given information is right.
  47.             $this->assertNotEmpty($record);
  48.             $this->assertEquals($data['externalbackpackid'], $return);
  49.         } else if ($addbackpack) {
  50.             // If no email and password are given, no backpack is created/modified.
  51.             $this->assertEmpty($record);
  52.             $this->assertEquals($data['externalbackpackid'], $return);
  53.         } else {
  54.             // There weren't fields to add to the backpack so no DB change is expected.
  55.             $this->assertEmpty($record);
  56.             $this->assertEquals(0, $return);
  57.         }
  58.  
  59.         // Confirm the existing backpack credential can be updated (if it has been created).
  60.         if (!empty($record)) {
  61.             $data['backpackemail'] = 'modified_' . $mail;
  62.             $data['id'] = $record->id;
  63.             $return = badges_save_backpack_credentials((object) $data);
  64.             $record = $DB->get_record('badge_backpack', ['userid' => $user->id]);
  65.  
  66.             $this->assertNotEmpty($record);
  67.             $this->assertEquals($data['backpackemail'], $record->email);
  68.             $this->assertEquals($data['externalbackpackid'], $return);
  69.         }
  70.     }
  71.  
  72.     /**
  73.      * Data provider for test_create_backpack_credentials().
  74.      *
  75.      * @return array
  76.      */
  77.     public function save_backpack_credentials_provider(): array {
  78.         return [
  79.             'Empty fields' => [
  80.                 false,
  81.             ],
  82.             'No backpack mail or password are defined' => [
  83.                 true,
  84.             ],
  85.             'Both backpack mail and password are defined' => [
  86.                 true, 'test@test.com', '1234',
  87.             ],
  88.             'Only backpack mail is defined (no password is given)' => [
  89.                 true, 'test@test.com', null,
  90.             ],
  91.             'Only backpack password is defined (no mail is given)' => [
  92.                 true, null, '1234'
  93.             ],
  94.         ];
  95.     }
  96.  
Add Comment
Please, Sign In to add comment