Skip to content

Commit 9559fbe

Browse files
committed
Added tests for Events Action Logger. Added fixes to the action logger after finding a few bugs.
1 parent d8b7366 commit 9559fbe

File tree

5 files changed

+547
-31
lines changed

5 files changed

+547
-31
lines changed

plugins/wpgraphql-logging/src/Events/QueryActionLogger.php

Lines changed: 33 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -61,16 +61,10 @@ public function __construct( LoggerService $logger, array $config ) {
6161
*/
6262
public function log_pre_request( ?string $query, ?string $operation_name, ?array $variables ): void {
6363
try {
64-
if ( ! $this->is_logging_enabled( $this->config, $query ) ) {
65-
return;
66-
}
67-
$selected_events = $this->config[ BasicConfigurationTab::EVENT_LOG_SELECTION ] ?? [];
68-
if ( ! is_array( $selected_events ) || empty( $selected_events ) ) {
69-
return;
70-
}
71-
if ( ! in_array( Events::PRE_REQUEST, $selected_events, true ) ) {
64+
if ( ! $this->should_log_event( Events::PRE_REQUEST, $query ) ) {
7265
return;
7366
}
67+
7468
$context = [
7569
'query' => $query,
7670
'variables' => $variables,
@@ -97,23 +91,21 @@ public function log_pre_request( ?string $query, ?string $operation_name, ?array
9791
public function log_graphql_before_execute( Request $request ): void {
9892
try {
9993
/** @var \GraphQL\Server\OperationParams $params */
100-
$params = $request->params;
94+
$params = $request->params;
95+
if ( null === $params || ! \is_object( $params ) ) {
96+
return;
97+
}
98+
$query = $params->query;
99+
if ( ! $this->should_log_event( Events::BEFORE_GRAPHQL_EXECUTION, $query ) ) {
100+
return;
101+
}
102+
101103
$context = [
102-
'query' => $params->query,
104+
'query' => $query,
103105
'operation_name' => $params->operation,
104106
'variables' => $params->variables,
105107
'params' => $params,
106108
];
107-
if ( ! $this->is_logging_enabled( $this->config, $params->query ) ) {
108-
return;
109-
}
110-
$selected_events = $this->config[ BasicConfigurationTab::EVENT_LOG_SELECTION ] ?? [];
111-
if ( ! is_array( $selected_events ) || empty( $selected_events ) ) {
112-
return;
113-
}
114-
if ( ! in_array( Events::BEFORE_GRAPHQL_EXECUTION, $selected_events, true ) ) {
115-
return;
116-
}
117109

118110
$payload = EventManager::transform( Events::BEFORE_GRAPHQL_EXECUTION, [
119111
'context' => $context,
@@ -154,16 +146,10 @@ public function log_before_response_returned(
154146
?string $query_id
155147
): void {
156148
try {
157-
if ( ! $this->is_logging_enabled( $this->config, $query ) ) {
158-
return;
159-
}
160-
$selected_events = $this->config[ BasicConfigurationTab::EVENT_LOG_SELECTION ] ?? [];
161-
if ( ! is_array( $selected_events ) || empty( $selected_events ) ) {
162-
return;
163-
}
164-
if ( ! in_array( Events::BEFORE_RESPONSE_RETURNED, $selected_events, true ) ) {
149+
if ( ! $this->should_log_event( Events::BEFORE_RESPONSE_RETURNED, $query ) ) {
165150
return;
166151
}
152+
167153
$encoded_request = wp_json_encode( $request );
168154
$context = [
169155
'response' => $response,
@@ -196,6 +182,25 @@ public function log_before_response_returned(
196182
}
197183
}
198184

185+
/**
186+
* Determine if the event should be logged based on the configuration and selected events.
187+
*
188+
* @param string $event The event name.
189+
* @param string|null $query The GraphQL query (optional).
190+
*
191+
* @return bool True if the event should be logged, false otherwise.
192+
*/
193+
public function should_log_event(string $event, ?string $query = null): bool {
194+
if ( ! $this->is_logging_enabled( $this->config, $query ) ) {
195+
return false;
196+
}
197+
$selected_events = $this->config[ BasicConfigurationTab::EVENT_LOG_SELECTION ] ?? [];
198+
if ( ! is_array( $selected_events ) || empty( $selected_events ) ) {
199+
return false;
200+
}
201+
return in_array( $event, $selected_events, true );
202+
}
203+
199204
/**
200205
* Get the context for the response.
201206
*

plugins/wpgraphql-logging/src/Logger/Rules/ExcludeQueryRule.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ class ExcludeQueryRule implements LoggingRuleInterface {
2424
*/
2525
public function passes(array $config, ?string $query_string = null): bool {
2626
$queries = $config[ BasicConfigurationTab::EXCLUDE_QUERY ] ?? '';
27-
if ( null === $query_string || '' === trim( $queries ) ) {
27+
if ( null === $query_string || ! is_string( $queries ) || '' === trim( $queries ) ) {
2828
return true;
2929
}
3030

0 commit comments

Comments
 (0)