You are here

public function LTIAuthTest::testSuccessfulAuthenticationExistingUser in LTI Tool Provider 8

Same name and namespace in other branches
  1. 2.x tests/src/Functional/LTIAuthTest.php \Drupal\Tests\lti_tool_provider\Functional\LTIAuthTest::testSuccessfulAuthenticationExistingUser()

Test successful authentication with existing user.

Throws

OAuthException

EntityStorageException

Exception

File

tests/src/Functional/LTIAuthTest.php, line 323

Class

LTIAuthTest
Functional tests for LTI authentication.

Namespace

Drupal\Tests\lti_tool_provider\Functional

Code

public function testSuccessfulAuthenticationExistingUser() {
  $oauth = new OAuth($this->consumer
    ->get('consumer_key')->value, $this->consumer
    ->get('consumer_secret')->value, OAUTH_SIG_METHOD_HMACSHA1, OAUTH_AUTH_TYPE_URI);
  $timestamp = time();
  $nonce = md5($timestamp);
  $oauth
    ->setTimestamp($timestamp);
  $oauth
    ->setNonce($nonce);
  $mail = 'user@lms.edu';
  $user = User::create();
  if ($user instanceof User) {
    $user
      ->setUsername($mail);
    $user
      ->setEmail($mail);
    $user
      ->setPassword(user_password());
    $user
      ->enforceIsNew();
    $user
      ->activate();
  }
  $user
    ->save();
  $url = Url::fromRoute('lti_tool_provider.lti');
  $params = [
    'oauth_version' => '1.0',
    'oauth_signature_method' => 'HMAC-SHA1',
    'oauth_consumer_key' => 'consumer_key',
    'oauth_timestamp' => $timestamp,
    'oauth_nonce' => $nonce,
    'lti_message_type' => 'basic-lti-launch-request',
    'lti_version' => 'LTI-1p0',
    'resource_link_id' => 'resource_link_id',
    'lis_person_contact_email_primary' => $mail,
  ];
  $signature = $oauth
    ->generateSignature('POST', $url
    ->setAbsolute()
    ->toString(), $params);
  $params['oauth_signature'] = $signature;
  $response = $this
    ->request('POST', $url, [
    'form_params' => $params,
  ]);
  $ids = $this->userStorage
    ->getQuery()
    ->condition('name', $mail, '=')
    ->condition('mail', $mail, '=')
    ->execute();
  $this
    ->assertEquals(200, $response
    ->getStatusCode());
  $this
    ->assertCount(1, $ids);
}