protected function MasqueradeWebTestBase::assertSessionByUid in Masquerade 8.2
Asserts that there is a session for a given user ID.
Parameters
int $uid: The user ID for which to find a session record.
int|null $masquerading: (optional) The expected value of the 'masquerading' session data. Pass NULL to assert that the session data is not set.
1 call to MasqueradeWebTestBase::assertSessionByUid()
- MasqueradeTest::testMasquerade in tests/
src/ Functional/ MasqueradeTest.php - Tests masquerade user links.
File
- tests/
src/ Functional/ MasqueradeWebTestBase.php, line 238
Class
- MasqueradeWebTestBase
- Base test class for Masquerade module web tests.
Namespace
Drupal\Tests\masquerade\FunctionalCode
protected function assertSessionByUid($uid, $masquerading = NULL) {
$result = \Drupal::database()
->query('SELECT * FROM {sessions} WHERE uid = :uid', [
':uid' => $uid,
])
->fetchAll();
if (empty($result)) {
$this
->fail("No session found for uid {$uid}");
}
elseif (count($result) > 1) {
// If there is more than one session, then that must be unexpected.
$this
->fail("Found more than 1 session for uid {$uid}.");
}
else {
$session = reset($result);
// Careful: PHP does not provide a utility function that decodes session
// data only. Using string comparison because rely on default storage.
if ($masquerading) {
$expected = '"masquerading";s:' . strlen($masquerading) . ':"' . $masquerading . '"';
self::assertNotFalse(strpos($session->session, $expected), new FormattableMarkup('Session flag "masquerading" equals @uid.', [
'@uid' => $masquerading,
]));
}
else {
$expected = empty($session->session) || strpos($session->session, 'masquerading') === FALSE;
self::assertTrue($expected, 'Session flag "masquerading" is not set.');
}
}
}