|
| 1 | +<?php |
| 2 | + |
| 3 | +namespace WPGraphQL\Logging\Tests\Logger\Processors; |
| 4 | + |
| 5 | +use WPGraphQL\Logging\Logger\Processors\DataSanitizationProcessor; |
| 6 | +use WPGraphQL\Logging\Admin\Settings\ConfigurationHelper; |
| 7 | +use lucatume\WPBrowser\TestCase\WPTestCase; |
| 8 | +use WPGraphQL\Logging\Admin\Settings\Fields\Tab\DataManagementTab; |
| 9 | +use Monolog\LogRecord; |
| 10 | +use Monolog\Level; |
| 11 | +use DateTimeImmutable; |
| 12 | + |
| 13 | +/** |
| 14 | + * Test cases for the DataSanitizationProcessor |
| 15 | + * |
| 16 | + * @package WPGraphQL\Logging |
| 17 | + * |
| 18 | + * @since 0.0.1 |
| 19 | + */ |
| 20 | +class DataSanitizationProcessorTest extends WPTestCase { |
| 21 | + |
| 22 | + |
| 23 | + public function create_mock_processor(array $config) : DataSanitizationProcessor { |
| 24 | + $processor = new DataSanitizationProcessor(); |
| 25 | + $reflection = new \ReflectionClass($processor); |
| 26 | + $configProperty = $reflection->getProperty('config'); |
| 27 | + $configProperty->setAccessible(true); |
| 28 | + $configProperty->setValue($processor, $config); |
| 29 | + return $processor; |
| 30 | + } |
| 31 | + |
| 32 | + public function test_process_record_not_enabled(): void |
| 33 | + { |
| 34 | + $processor = $this->create_mock_processor( |
| 35 | + [ |
| 36 | + DataManagementTab::DATA_SANITIZATION_ENABLED => false, |
| 37 | + DataManagementTab::DATA_SANITIZATION_METHOD => 'recommended', |
| 38 | + ] |
| 39 | + ); |
| 40 | + |
| 41 | + $record = new LogRecord( |
| 42 | + new DateTimeImmutable('now'), |
| 43 | + 'wpgraphql_logging', |
| 44 | + Level::Info, |
| 45 | + 'Test log message', |
| 46 | + ['test_field' => 'test_value'], |
| 47 | + [] |
| 48 | + ); |
| 49 | + $result = $processor->__invoke($record); |
| 50 | + $this->assertSame($record, $result); |
| 51 | + } |
| 52 | + |
| 53 | + |
| 54 | + public function test_process_record_empty_custom_rules_no_processing(): void { |
| 55 | + |
| 56 | + $processor = $this->create_mock_processor( |
| 57 | + [ |
| 58 | + DataManagementTab::DATA_SANITIZATION_ENABLED => true, |
| 59 | + DataManagementTab::DATA_SANITIZATION_METHOD => 'custom', |
| 60 | + DataManagementTab::DATA_SANITIZATION_CUSTOM_FIELD_ANONYMIZE => '', |
| 61 | + DataManagementTab::DATA_SANITIZATION_CUSTOM_FIELD_REMOVE => '', |
| 62 | + DataManagementTab::DATA_SANITIZATION_CUSTOM_FIELD_TRUNCATE => '' |
| 63 | + ] |
| 64 | + ); |
| 65 | + |
| 66 | + $record = new LogRecord( |
| 67 | + new DateTimeImmutable('now'), |
| 68 | + 'wpgraphql_logging', |
| 69 | + Level::Info, |
| 70 | + 'Test log message', |
| 71 | + ['test_field' => 'test_value'], |
| 72 | + [] |
| 73 | + ); |
| 74 | + $result = $processor->__invoke($record); |
| 75 | + $this->assertSame($record, $result); |
| 76 | + } |
| 77 | + |
| 78 | + |
| 79 | + public function test_process_record_process_recommended_rules(): void |
| 80 | + { |
| 81 | + $processor = $this->create_mock_processor( |
| 82 | + [ |
| 83 | + DataManagementTab::DATA_SANITIZATION_ENABLED => true, |
| 84 | + DataManagementTab::DATA_SANITIZATION_METHOD => 'recommended', |
| 85 | + ] |
| 86 | + ); |
| 87 | + |
| 88 | + $record = new LogRecord( |
| 89 | + new DateTimeImmutable('now'), |
| 90 | + 'wpgraphql_logging', |
| 91 | + Level::Info, |
| 92 | + 'Test log message', |
| 93 | + [ |
| 94 | + 'request' => [ |
| 95 | + 'app_context' => [ |
| 96 | + 'viewer' => [ |
| 97 | + 'data' => [ |
| 98 | + 'user_email' => 'sensitive_data', |
| 99 | + ], |
| 100 | + 'allcaps' => 'administrator', |
| 101 | + 'cap_key' => 'sensitive_data', |
| 102 | + 'caps' => 'sensitive_data', |
| 103 | + 'safe_field' => 'safe_data' |
| 104 | + ] |
| 105 | + ] |
| 106 | + ], |
| 107 | + 'query' => 'query { posts { id } }' |
| 108 | + ], |
| 109 | + [] |
| 110 | + ); |
| 111 | + $result = $processor->__invoke($record); |
| 112 | + $this->assertNotSame($record, $result); |
| 113 | + |
| 114 | + $data = $result->toArray(); |
| 115 | + |
| 116 | + $this->assertSame($data['context'], [ |
| 117 | + 'request' => [ |
| 118 | + 'app_context' => [ |
| 119 | + 'viewer' => [ |
| 120 | + 'safe_field' => 'safe_data' |
| 121 | + ] |
| 122 | + ] |
| 123 | + ], |
| 124 | + 'query' => 'query { posts { id } }' |
| 125 | + ]); |
| 126 | + } |
| 127 | + |
| 128 | + |
| 129 | + public function test_process_record_process_custom_rules(): void |
| 130 | + { |
| 131 | + $processor = $this->create_mock_processor( |
| 132 | + [ |
| 133 | + DataManagementTab::DATA_SANITIZATION_ENABLED => true, |
| 134 | + DataManagementTab::DATA_SANITIZATION_METHOD => 'custom', |
| 135 | + DataManagementTab::DATA_SANITIZATION_CUSTOM_FIELD_ANONYMIZE => 'request.app_context.viewer.user_email, request.app_context.viewer.display_name', |
| 136 | + DataManagementTab::DATA_SANITIZATION_CUSTOM_FIELD_REMOVE => 'request.app_context.viewer.allcaps, request.app_context.viewer.cap_key', |
| 137 | + DataManagementTab::DATA_SANITIZATION_CUSTOM_FIELD_TRUNCATE => 'request.app_context.viewer.caps' |
| 138 | + ] |
| 139 | + ); |
| 140 | + |
| 141 | + $record = new LogRecord( |
| 142 | + new DateTimeImmutable('now'), |
| 143 | + 'wpgraphql_logging', |
| 144 | + Level::Info, |
| 145 | + 'Test log message', |
| 146 | + [ |
| 147 | + 'request' => [ |
| 148 | + 'app_context' => [ |
| 149 | + 'viewer' => [ |
| 150 | + 'display_name' => 'Sensitive Name', |
| 151 | + 'user_email' => 'sensitive_data', |
| 152 | + 'allcaps' => 'administrator', |
| 153 | + 'cap_key' => 'sensitive_data', |
| 154 | + 'caps' => 'This is a really long string that should be truncated', |
| 155 | + 'safe_field' => 'safe_data' |
| 156 | + ] |
| 157 | + ] |
| 158 | + ], |
| 159 | + 'query' => 'query { posts { id } }' |
| 160 | + ], |
| 161 | + [] |
| 162 | + ); |
| 163 | + $result = $processor->__invoke($record); |
| 164 | + $this->assertNotSame($record, $result); |
| 165 | + |
| 166 | + $data = $result->toArray(); |
| 167 | + |
| 168 | + $this->assertSame($data['context'], [ |
| 169 | + 'request' => [ |
| 170 | + 'app_context' => [ |
| 171 | + 'viewer' => [ |
| 172 | + 'display_name' => '***', |
| 173 | + 'user_email' => '***', |
| 174 | + 'caps' => 'This is a really long string that should be tru...', |
| 175 | + 'safe_field' => 'safe_data', |
| 176 | + ] |
| 177 | + ] |
| 178 | + ], |
| 179 | + 'query' => 'query { posts { id } }' |
| 180 | + ]); |
| 181 | + } |
| 182 | +} |
0 commit comments