private function ApiTest::createApiUser in Lightning API 8.4
Same name and namespace in other branches
- 8.3 tests/src/Functional/ApiTest.php \Drupal\Tests\lightning_api\Functional\ApiTest::createApiUser()
Creates a user account with privileged API access.
Return value
string The user's access token.
See also
::createUser()
2 calls to ApiTest::createApiUser()
- ApiTest::getCreator in tests/
src/ Functional/ ApiTest.php - Creates an API user with all privileges for a single content type.
- ApiTest::testEntities in tests/
src/ Functional/ ApiTest.php - Tests create, read, and update of content entities via the API.
File
- tests/
src/ Functional/ ApiTest.php, line 105
Class
- ApiTest
- Tests that OAuth and JSON:API authenticate and authorize entity operations.
Namespace
Drupal\Tests\lightning_api\FunctionalCode
private function createApiUser(array $permissions = [], $name = NULL, $admin = FALSE) {
$account = $this
->createUser($permissions, $name, $admin);
$roles = $account
->getRoles(TRUE);
$secret = $this
->randomString(32);
$client = Consumer::create([
'label' => 'API Test Client',
'secret' => $secret,
'confidential' => TRUE,
'user_id' => $account
->id(),
'roles' => reset($roles),
]);
$client
->save();
$url = $this
->buildUrl('/oauth/token');
$response = $this->container
->get('http_client')
->post($url, [
'form_params' => [
'grant_type' => 'password',
'client_id' => $client
->uuid(),
'client_secret' => $secret,
'username' => $account
->getAccountName(),
'password' => $account->passRaw,
],
]);
$body = $this
->decodeResponse($response);
// The response should have an access token.
$this
->assertArrayHasKey('access_token', $body);
return $body['access_token'];
}