Skip to content

Commit 0f1986c

Browse files
committed
Add event selection check for response headers logging
Updated QueryFilterLogger to only add logging headers if the RESPONSE_HEADERS_TO_SEND event is selected in the configuration. Added is_selected_event helper and corresponding unit tests to ensure correct behavior based on configuration.
1 parent 2a77388 commit 0f1986c

File tree

3 files changed

+58
-0
lines changed

3 files changed

+58
-0
lines changed

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

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -166,6 +166,10 @@ public function add_logging_headers( array $headers ): array {
166166
return $headers;
167167
}
168168

169+
if ( ! $this->is_selected_event( Events::RESPONSE_HEADERS_TO_SEND ) ) {
170+
return $headers;
171+
}
172+
169173
$request_id = uniqid( 'wpgql_log_' );
170174
$headers['X-WPGraphQL-Logging-ID'] = $request_id;
171175

plugins/wpgraphql-logging/src/Logger/LoggingHelper.php

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,17 @@ public function should_log_event(string $event, ?string $query = null): bool {
5353
if ( ! $this->is_logging_enabled( $this->config, $query ) ) {
5454
return false;
5555
}
56+
return $this->is_selected_event( $event );
57+
}
58+
59+
/**
60+
* Check if the event is selected in the configuration.
61+
*
62+
* @param string $event The event name to check.
63+
*
64+
* @return bool True if the event is selected, false otherwise.
65+
*/
66+
public function is_selected_event(string $event): bool {
5667
$selected_events = $this->config[ BasicConfigurationTab::EVENT_LOG_SELECTION ] ?? [];
5768
if ( ! is_array( $selected_events ) || empty( $selected_events ) ) {
5869
return false;

plugins/wpgraphql-logging/tests/wpunit/Events/QueryFilterLoggerTest.php

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -260,4 +260,47 @@ public function test_graphql_request_results_log_event_add_context_with_subscrib
260260
$this->assertEquals('This is meta value.', $log_entry->get_context()['meta_data']);
261261
$this->assertEquals(Level::Debug->value, $log_entry->get_level());
262262
}
263+
264+
265+
/**************************************************************
266+
* add_response_headers
267+
**************************************************************/
268+
public function test_graphql_response_headers_logging_disabled(): void {
269+
$instance = $this->create_instance(
270+
[
271+
BasicConfigurationTab::ENABLED => false,
272+
]
273+
);
274+
$headers = ['Content-Type' => 'application/json'];
275+
$result = $instance->add_logging_headers($headers);
276+
$this->assertSame($headers, $result);
277+
}
278+
279+
public function test_graphql_response_headers_logging_not_selected(): void {
280+
$instance = $this->create_instance(
281+
[
282+
BasicConfigurationTab::ENABLED => true,
283+
BasicConfigurationTab::EVENT_LOG_SELECTION => [Events::PRE_REQUEST],
284+
]
285+
);
286+
$headers = ['Content-Type' => 'application/json'];
287+
$result = $instance->add_logging_headers($headers);
288+
$this->assertSame($headers, $result);
289+
}
290+
291+
292+
public function test_graphql_response_headers_logging_add_header(): void {
293+
$instance = $this->create_instance(
294+
[
295+
BasicConfigurationTab::ENABLED => true,
296+
BasicConfigurationTab::EVENT_LOG_SELECTION => [Events::RESPONSE_HEADERS_TO_SEND],
297+
]
298+
);
299+
$headers = ['Content-Type' => 'application/json'];
300+
$result = $instance->add_logging_headers($headers);
301+
$this->assertNotSame($headers, $result);
302+
$this->assertArrayHasKey('X-WPGraphQL-Logging-ID', $result);
303+
$this->assertNotEmpty($result['X-WPGraphQL-Logging-ID']);
304+
$this->assertCount(2, $result);
305+
}
263306
}

0 commit comments

Comments
 (0)