public function FileLogTokenTest::testTokens in File Log 8
Same name and namespace in other branches
- 2.0.x tests/src/Unit/FileLogTokenTest.php \Drupal\Tests\filelog\Unit\FileLogTokenTest::testTokens()
Test the tokens of the log message.
@dataProvider providerMessages
Parameters
mixed $level: Severity level of the log message.
string $message: Content of the log message.
array $context: Context of the log message.
File
- tests/
src/ Unit/ FileLogTokenTest.php, line 119
Class
- FileLogTokenTest
- Test the filelog message token integration.
Namespace
Drupal\Tests\filelog\UnitCode
public function testTokens($level, $message, array $context) : void {
$variables = $this->logMessageParser
->parseMessagePlaceholders($message, $context);
$logMessage = new LogMessage($level, $message, $variables, $context);
$levels = LogMessage::getLevels();
$expectedTokens = [
'type' => $context['channel'],
'level' => $levels[$level],
'message' => strtr($message, $variables),
'location' => $context['request_uri'],
'referrer' => $context['referer'] ?? NULL,
'ip' => $context['ip'] ?? '0.0.0.0',
'created' => 'date:default:' . $context['timestamp'],
'created:format' => 'date:format:' . $context['timestamp'],
'user' => 'user:' . $context['uid'],
'user:token' => 'user:token:' . $context['uid'],
];
foreach ($variables as $name => $value) {
$expectedTokens['variable:' . substr($name, 1)] = $value;
}
foreach ($context as $name => $value) {
$expectedTokens['context:' . $name] = $value;
}
$tokens = [];
foreach ($expectedTokens as $name => $value) {
$tokens[$name] = 'log:' . $name;
}
$user = $this
->getMockBuilder(User::class)
->disableOriginalConstructor()
->getMock();
$this->userStorage
->expects($this
->once())
->method('load')
->with($context['uid'])
->willReturn($user);
$user
->expects($this
->once())
->method('label')
->willReturn('user:' . $context['uid']);
$this->dateFormatter
->expects($this
->once())
->method('format')
->with($context['timestamp'])
->willReturn('date:default:' . $context['timestamp']);
$options = [];
/** @var \Drupal\Core\Render\BubbleableMetadata $metadata */
$metadata = $this
->createMock(BubbleableMetadata::class);
// Mock the token service calls.
$this->token
->expects($this
->exactly(2))
->method('generate')
->withConsecutive([
'user',
[
'token' => 'log:user:token',
],
[
'user' => $user,
],
$options,
$metadata,
], [
'date',
[
'format' => 'log:created:format',
],
[
'date' => $context['timestamp'],
],
$options,
$metadata,
])
->willReturnOnConsecutiveCalls([
'log:user:token' => 'user:token:' . $context['uid'],
], [
'log:created:format' => 'date:format:' . $context['timestamp'],
]);
$values = filelog_tokens('log', $tokens, [
'log' => $logMessage,
], $options, $metadata);
// Check that each token was replaced by the correct value.
foreach ($tokens as $name => $original) {
$this
->assertEquals($expectedTokens[$name], $values[$original]);
}
// Make sure that nothing else was replaced.
$this
->assertCount(count($expectedTokens), $values);
}