1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
|
5
|
|
|
namespace Drupal\Tests\mongodb_watchdog\Kernel; |
6
|
|
|
|
7
|
|
|
use Drupal\Core\Logger\RfcLogLevel; |
|
|
|
|
8
|
|
|
use Drupal\Core\StringTranslation\StringTranslationTrait; |
|
|
|
|
9
|
|
|
use Drupal\mongodb\MongoDb; |
10
|
|
|
use Drupal\mongodb_watchdog\Logger; |
11
|
|
|
use Drupal\Tests\mongodb\Kernel\MongoDbTestBase; |
12
|
|
|
use MongoDB\Model\BSONDocument; |
13
|
|
|
|
14
|
|
|
/** |
15
|
|
|
* Class LoggerTest tests the logging mechanism itself. |
16
|
|
|
* |
17
|
|
|
* @coversDefaultClass \Drupal\mongodb_watchdog\Logger |
18
|
|
|
* |
19
|
|
|
* @group MongoDB |
20
|
|
|
*/ |
21
|
|
|
class LoggerTest extends MongoDbTestBase { |
22
|
|
|
use StringTranslationTrait; |
23
|
|
|
|
24
|
|
|
/** |
25
|
|
|
* The event templates collection. |
26
|
|
|
* |
27
|
|
|
* @var \MongoDB\Collection |
28
|
|
|
*/ |
29
|
|
|
protected $collection; |
30
|
|
|
|
31
|
|
|
/** |
32
|
|
|
* These modules need to be enabled. |
33
|
|
|
* |
34
|
|
|
* @var string[] |
35
|
|
|
*/ |
36
|
|
|
protected static $modules = [ |
37
|
|
|
'system', |
38
|
|
|
MongoDb::MODULE, |
39
|
|
|
Logger::MODULE, |
40
|
|
|
]; |
41
|
|
|
|
42
|
|
|
/** |
43
|
|
|
* {@inheritdoc} |
44
|
|
|
*/ |
45
|
|
|
public function setUp(): void { |
46
|
|
|
parent::setUp(); |
47
|
|
|
$this->installConfig(Logger::MODULE); |
48
|
|
|
} |
49
|
|
|
|
50
|
|
|
/** |
51
|
|
|
* {@inheritdoc} |
52
|
|
|
*/ |
53
|
|
|
protected function getSettingsArray(): array { |
54
|
|
|
$settings = parent::getSettingsArray(); |
55
|
|
|
$settings['databases'][Logger::DB_LOGGER] = [ |
56
|
|
|
static::CLIENT_TEST_ALIAS, |
57
|
|
|
$this->getDatabasePrefix(), |
58
|
|
|
]; |
59
|
|
|
|
60
|
|
|
return $settings; |
61
|
|
|
} |
62
|
|
|
|
63
|
|
|
/** |
64
|
|
|
* Assert that a given entry is present in the watchdog. |
65
|
|
|
* |
66
|
|
|
* @param string $message |
67
|
|
|
* The message is present in the collection. |
68
|
|
|
*/ |
69
|
|
|
public function assertEntry($message): void { |
70
|
|
|
$logged = $this->find($message); |
71
|
|
|
$this->assertNotNull($logged, |
72
|
|
|
(string) $this->t('Event %message is logged', ['%message' => $message])); |
73
|
|
|
$this->assertTrue(isset($logged['message']) && $logged['message'] == $message, |
74
|
|
|
(string) $this->t('Logged message is unchanged')); |
75
|
|
|
} |
76
|
|
|
|
77
|
|
|
/** |
78
|
|
|
* Assert that a given entry is not present in the watchdog. |
79
|
|
|
* |
80
|
|
|
* @param string $message |
81
|
|
|
* The message which must not be present in the collection. |
82
|
|
|
*/ |
83
|
|
|
public function assertNoEntry($message): void { |
84
|
|
|
$logged = $this->find($message); |
85
|
|
|
$this->assertNull($logged, |
86
|
|
|
(string) $this->t('Event %message is not logged', ['%message' => $message])); |
87
|
|
|
} |
88
|
|
|
|
89
|
|
|
/** |
90
|
|
|
* Replaces PSR-3 braces by angle brackets. |
91
|
|
|
* |
92
|
|
|
* Braces in log($l, $message, $c) will be interpreted as PSR-3 placeholders. |
93
|
|
|
* As such they need to be avoid when inserted randomly. |
94
|
|
|
* |
95
|
|
|
* @param string $message |
96
|
|
|
* The raw message. |
97
|
|
|
* |
98
|
|
|
* @return string |
99
|
|
|
* The replacement message. |
100
|
|
|
* |
101
|
|
|
* @see \Drupal\Core\Logger\LogMessageParserInterface::parseMessagePlaceholders() |
102
|
|
|
*/ |
103
|
|
|
public static function debrace(string $message): string { |
104
|
|
|
return str_replace(['{', '}'], ['<', '>'], $message); |
105
|
|
|
} |
106
|
|
|
|
107
|
|
|
/** |
108
|
|
|
* Simplified query to look for a logged message. |
109
|
|
|
* |
110
|
|
|
* @param string $message |
111
|
|
|
* The message to look for. |
112
|
|
|
* |
113
|
|
|
* @return \MongoDB\Model\BSONDocument |
114
|
|
|
* The document containing the message, if any ; NULL otherwise. |
115
|
|
|
*/ |
116
|
|
|
protected function find($message): ?BSONDocument { |
117
|
|
|
$ret = $this->collection->findOne(['message' => $message]); |
118
|
|
|
return $ret; |
|
|
|
|
119
|
|
|
} |
120
|
|
|
|
121
|
|
|
/** |
122
|
|
|
* Ensure logging from a closure does not fail. |
123
|
|
|
* |
124
|
|
|
* @covers ::enhanceLogEntry |
125
|
|
|
* |
126
|
|
|
* @see https://www.drupal.org/project/mongodb/issues/3193195 |
127
|
|
|
*/ |
128
|
|
|
public function testLogClosure(): void { |
129
|
|
|
$logger = $this->container->get(Logger::SERVICE_LOGGER); |
130
|
|
|
$closure = function () use ($logger) { |
131
|
|
|
$logger->notice("This fails on PHP below 7.0, and 5.6 needs to be supported for Drupal 8. The alcaeus adapter passes the version check, but does not address this."); |
132
|
|
|
return 1; |
133
|
|
|
}; |
134
|
|
|
$this->assertEquals(1, $closure()); |
135
|
|
|
} |
136
|
|
|
|
137
|
|
|
/** |
138
|
|
|
* Test the default and non-default mongodb_watchdog insertion behaviours. |
139
|
|
|
* |
140
|
|
|
* Make sure the module applies the watchdog_limit variable, |
141
|
|
|
* |
142
|
|
|
* @covers ::log |
143
|
|
|
*/ |
144
|
|
|
public function testWatchdogLimit(): void { |
145
|
|
|
$config = $this->config(Logger::CONFIG_NAME); |
146
|
|
|
$limit = $config->get(Logger::CONFIG_LIMIT); |
147
|
|
|
$this->assertEquals(RfcLogLevel::DEBUG, $limit, |
148
|
|
|
(string) $this->t('%name defaults to @level', [ |
149
|
|
|
'%name' => Logger::CONFIG_LIMIT, |
150
|
|
|
'@level' => RfcLogLevel::DEBUG, |
151
|
|
|
])); |
152
|
|
|
|
153
|
|
|
$logger = $this->container->get(Logger::SERVICE_LOGGER); |
154
|
|
|
$database = $this->container->get(MongoDb::SERVICE_DB_FACTORY) |
155
|
|
|
->get(Logger::DB_LOGGER); |
156
|
|
|
$this->collection = $database->selectCollection(Logger::TEMPLATE_COLLECTION); |
157
|
|
|
$this->collection->drop(); |
158
|
|
|
|
159
|
|
|
$message = static::debrace($this->randomString(32)); |
160
|
|
|
$logger->log($limit, $message); |
161
|
|
|
$this->assertEntry($message); |
162
|
|
|
|
163
|
|
|
// Now request a higher level: unimportant events should be ignored. For |
164
|
|
|
// this to work, ensure limit is not the maximum level already. |
165
|
|
|
$logger->setLimit(RfcLogLevel::INFO); |
166
|
|
|
$this->collection->drop(); |
167
|
|
|
|
168
|
|
|
$message = $this->randomMachineName(32); |
169
|
|
|
$logger->debug($message); |
170
|
|
|
$this->assertNoEntry($message); |
171
|
|
|
|
172
|
|
|
// ... but events at the limit or more important should be logged. |
173
|
|
|
$message = $this->randomMachineName(32); |
174
|
|
|
$logger->notice($message); |
175
|
|
|
$this->assertEntry($message); |
176
|
|
|
|
177
|
|
|
$message = $this->randomMachineName(32); |
178
|
|
|
$logger->error($message); |
179
|
|
|
$this->assertEntry($message); |
180
|
|
|
} |
181
|
|
|
|
182
|
|
|
} |
183
|
|
|
|
The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g.
excluded_paths: ["lib/*"]
, you can move it to the dependency path list as follows:For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths