Skip to content

Commit 7a59ada

Browse files
committed
Configurable global log level.
1 parent 2957457 commit 7a59ada

File tree

2 files changed

+58
-22
lines changed

2 files changed

+58
-22
lines changed

src/Codeception/Module/DrupalUser.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -72,7 +72,7 @@ public function createUserWithRoles(array $roles = [], $password = FALSE) {
7272
/**
7373
* Log in user by username.
7474
*
75-
* @param string|int $uid
75+
* @param string|int $username
7676
* User id.
7777
*/
7878
public function logInAs($username) {
@@ -151,7 +151,7 @@ public function userCleanup() {
151151
/**
152152
* Delete user created entities.
153153
*
154-
* @param $uid
154+
* @param string|int $uid
155155
* User id.
156156
*
157157
* @throws \Drupal\Core\Entity\EntityStorageException

src/Codeception/Module/DrupalWatchdog.php

Lines changed: 56 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -19,9 +19,8 @@ class DrupalWatchdog extends Module {
1919
* @var array
2020
*/
2121
protected $config = [
22-
'channels' => [
23-
'php' => 'notice',
24-
],
22+
'channels' => [],
23+
'level' => 'ERROR',
2524
];
2625

2726
/**
@@ -48,32 +47,69 @@ public function _beforeSuite($settings = []) { // @codingStandardsIgnoreLine
4847
// Clear log entries from the database log.
4948
\Drupal::database()->truncate('watchdog')->execute();
5049
}
50+
else {
51+
$this->fail('Database loging is not enabled.');
52+
}
5153
}
5254

5355
/**
54-
* Tear down after tests.
56+
* {@inheritdoc}
5557
*/
5658
public function _afterSuite() { // @codingStandardsIgnoreLine
57-
if (\Drupal::moduleHandler()->moduleExists('dblog')) {
58-
foreach ($this->config['channels'] as $channel => $level) {
59+
$channels = $this->_getConfig('channels');
60+
if (!empty($channels) && is_array($channels)) {
61+
foreach ($this->_getConfig('channels') as $channel => $level) {
5962
if (is_string($level) && !empty($this->logLevels[strtoupper($level)])) {
60-
// Load any database log entries of level WARNING or more serious.
61-
$query = \Drupal::database()->select('watchdog', 'w');
62-
$query->fields('w', ['type', 'severity', 'message', 'variables'])
63-
->condition('severity', $this->logLevels[strtoupper($level)], '<=')
64-
->condition('type', $channel);
65-
$result = $query->execute();
66-
foreach ($result as $row) {
67-
// Build a readable message and declare a failure.
68-
$variables = @unserialize($row->variables);
69-
$message = $row->type . ' - ';
70-
$message .= RfcLogLevel::getLevels()[$row->severity] . ': ';
71-
$message .= t(Xss::filterAdmin($row->message), $variables)->render(); // @codingStandardsIgnoreLine
72-
$this->fail($message);
73-
}
63+
$this->processResult($this->getLogResults($level, $channel));
7464
}
7565
}
7666
}
67+
if ($level = $this->_getConfig('level') && is_string($level) && isset($this->logLevels[strtoupper($level)])) {
68+
$this->processResult($this->getLogResults($level));
69+
}
70+
}
71+
72+
/**
73+
* Returns query result of log messages.
74+
*
75+
* @param string $level
76+
* Log level.
77+
* @param string $channel
78+
* Log channel.
79+
*
80+
* @return \Drupal\Core\Database\StatementInterface|null
81+
* Query result.
82+
*/
83+
private function getLogResults($level, $channel = NULL) {
84+
$query = \Drupal::database()->select('watchdog', 'w');
85+
$query->fields('w', ['type', 'severity', 'message', 'variables'])
86+
->condition('severity', $this->logLevels[strtoupper($level)], '<=');
87+
88+
if ($channel) {
89+
$query->condition('type', $channel);
90+
}
91+
return $query->execute();
92+
}
93+
94+
/**
95+
* Process log results.
96+
*
97+
* @param mixed $result
98+
* Query result.
99+
*/
100+
protected function processResult($result) {
101+
$messages = [];
102+
foreach ($result as $row) {
103+
// Build a readable message and declare a failure.
104+
$variables = @unserialize($row->variables);
105+
$message = $row->type . ' - ';
106+
$message .= RfcLogLevel::getLevels()[$row->severity] . ': ';
107+
$message .= t(Xss::filterAdmin($row->message), $variables)->render(); // @codingStandardsIgnoreLine
108+
$messages[] = $message;
109+
}
110+
if (!empty($messages)) {
111+
$this->fail(implode(PHP_EOL, $messages));
112+
}
77113
}
78114

79115
}

0 commit comments

Comments
 (0)