View source
<?php
namespace Drupal\Tests\externalauth\Kernel;
use Drupal\KernelTests\KernelTestBase;
use Drupal\user\Entity\User;
class AuthmapTest extends KernelTestBase {
public static $modules = [
'system',
'user',
'field',
'externalauth',
];
protected $authmap;
protected function setUp() {
parent::setUp();
$this->authmap = \Drupal::service('externalauth.authmap');
$this
->installSchema('externalauth', [
'authmap',
]);
$this
->installEntitySchema('user');
}
public function testAuthmap() {
$values = [
'uid' => 2,
'name' => $this
->randomMachineName(),
];
$account = User::create($values);
$account
->save();
$external_ids = [
'provider1' => [
'authname' => $this
->randomMachineName(),
'data' => $this
->randomMachineName(),
],
'provider2' => [
'authname' => $this
->randomMachineName(),
'data' => $this
->randomMachineName(),
],
];
foreach ($external_ids as $provider => $auth_data) {
$this->authmap
->save($account, $provider, $auth_data['authname'], $auth_data['data']);
}
$count = \Drupal::database()
->query('SELECT COUNT(*) FROM {authmap}')
->fetchField();
$this
->assertEquals($count, 2, 'Number of authmap entries is correct.');
$this
->assertEquals($this->authmap
->get($account
->id(), 'provider1'), $external_ids['provider1']['authname'], 'Authname can be retrieved for user via get().');
$this
->assertEquals($this->authmap
->get($account
->id(), 'provider2'), $external_ids['provider2']['authname'], 'Authname can be retrieved for user via get().');
$provider1_authdata = $this->authmap
->getAuthData($account
->id(), 'provider1');
$this
->assertEquals($provider1_authdata['authname'], $external_ids['provider1']['authname'], 'Authname can be retrieved via getAuthData().');
$this
->assertEquals($provider1_authdata['data'], $external_ids['provider1']['data'], 'Auth data can be retrieved via getAuthData().');
$provider2_authdata = $this->authmap
->getAuthData($account
->id(), 'provider2');
$this
->assertEquals($provider2_authdata['authname'], $external_ids['provider2']['authname'], 'Authname can be retrieved via getAuthData().');
$this
->assertEquals($provider2_authdata['data'], $external_ids['provider2']['data'], 'Auth data can be retrieved via getAuthData().');
$all_authnames = $this->authmap
->getAll($account
->id());
$expected_authnames = [
'provider1' => $external_ids['provider1']['authname'],
'provider2' => $external_ids['provider2']['authname'],
];
$this
->assertEquals($all_authnames, $expected_authnames, 'All authnames for user can be retrieved.');
$uid = $this->authmap
->getUid($external_ids['provider1']['authname'], 'provider1');
$this
->assertEquals($uid, $account
->id(), 'User ID can be retrieved based on authname & provider.');
$this->authmap
->deleteProvider('provider1');
$count = \Drupal::database()
->query('SELECT COUNT(*) FROM {authmap}')
->fetchField();
$this
->assertEquals($count, 1, 'Provider data deleted successfully.');
$this->authmap
->delete($account
->id());
$count = \Drupal::database()
->query('SELECT COUNT(*) FROM {authmap}')
->fetchField();
$this
->assertEquals($count, 0, 'User authnames deleted successfully.');
}
}