forked from guncha25/drupal-codeception
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathDrupalWatchdog.php
More file actions
154 lines (140 loc) · 3.64 KB
/
DrupalWatchdog.php
File metadata and controls
154 lines (140 loc) · 3.64 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
<?php
namespace Codeception\Module;
use Codeception\Module;
use Drupal\Core\Logger\RfcLogLevel;
use Drupal\Component\Utility\Xss;
/**
* Class DrupalWatchdog.
*
* ### Example
* #### Example (DrupalWatchdog)
* modules:
* - DrupalWatchdog:
* enabled: true
* level: 'ERROR'
* channels:
* my_module: 'NOTICE'
* php: 'WARNING'
*
* @package Codeception\Module
*/
class DrupalWatchdog extends Module {
/**
* Log messages.
*
* @var array
*/
protected $messages = [];
/**
* Default module configuration.
*
* @var array
*/
protected array $config = [
'channels' => [],
'level' => 'ERROR',
'enabled' => TRUE,
];
/**
* Log levels.
*
* @var array
*/
protected $logLevels = [
'DEBUG' => 7,
'INFO' => 6,
'NOTICE' => 5,
'WARNING' => 4,
'ERROR' => 3,
'CRITICAL' => 2,
'ALERT' => 1,
'EMERGENCY' => 0,
];
/**
* {@inheritdoc}
*/
public function _beforeSuite($settings = []) { // @codingStandardsIgnoreLine
if ($this->_getConfig('enabled')) {
$this->prepareLogWatch();
}
}
/**
* Prepares log.
*/
public function prepareLogWatch() {
if (\Drupal::moduleHandler()->moduleExists('dblog')) {
// Clear log entries from the database log.
\Drupal::database()->truncate('watchdog')->execute();
}
else {
$this->fail('Database logging is not enabled. Please enable dblog module to continue.');
}
}
/**
* {@inheritdoc}
*/
public function _afterSuite() { // @codingStandardsIgnoreLine
if ($this->_getConfig('enabled')) {
$this->checkLogs();
}
}
/**
* Check log.
*
* @param array $settings
* Setting override.
*/
public function checkLogs(array $settings = []) {
$channels = isset($settings['channels']) ? $settings['channels'] : $this->_getConfig('channels');
if (!empty($channels) && is_array($channels)) {
foreach ($this->_getConfig('channels') as $channel => $level) {
if (is_string($level) && isset($this->logLevels[strtoupper($level)])) {
$this->processResult($this->getLogResults($this->logLevels[strtoupper($level)], $channel));
}
}
}
$level = isset($settings['level']) ? $settings['level'] : $this->_getConfig('level');
if (is_string($level) && isset($this->logLevels[strtoupper($level)])) {
$this->processResult($this->getLogResults($this->logLevels[strtoupper($level)]));
}
if (!empty($this->messages)) {
$this->fail(implode(PHP_EOL, $this->messages));
}
}
/**
* Returns query result of log messages.
*
* @param int $level
* Log level.
* @param string $channel
* Log channel.
*
* @return \Drupal\Core\Database\StatementInterface|null
* Query result.
*/
private function getLogResults($level, $channel = NULL) {
$query = \Drupal::database()->select('watchdog', 'w');
$query->fields('w', ['type', 'severity', 'message', 'variables'])
->condition('severity', $level, '<=');
if ($channel) {
$query->condition('type', $channel);
}
return $query->execute();
}
/**
* Process log results.
*
* @param mixed $result
* Query result.
*/
protected function processResult($result) {
foreach ($result as $row) {
// Build a readable message and declare a failure.
$variables = @unserialize($row->variables);
$message = $row->type . ' - ';
$message .= RfcLogLevel::getLevels()[$row->severity] . ': ';
$message .= t(Xss::filterAdmin($row->message), $variables)->render(); // @codingStandardsIgnoreLine
$this->messages[] = $message;
}
}
}