|
| 1 | +<?php |
| 2 | + |
| 3 | +declare(strict_types=1); |
| 4 | + |
| 5 | +namespace WPGraphQL\Logging\Tests\Logger\Database; |
| 6 | + |
| 7 | +use lucatume\WPBrowser\TestCase\WPTestCase; |
| 8 | +use DateTimeImmutable; |
| 9 | +use ReflectionClass; |
| 10 | +use WPGraphQL\Logging\Logger\Database\DatabaseEntity; |
| 11 | +use WPGraphQL\Logging\Logger\Database\LogsRepository; |
| 12 | +use Mockery; |
| 13 | + |
| 14 | +/** |
| 15 | + * Test for the LogsRepository |
| 16 | + * |
| 17 | + * @package WPGraphQL\Logging |
| 18 | + * @since 0.0.1 |
| 19 | + */ |
| 20 | +class LogsRepositoryTest extends WPTestCase |
| 21 | +{ |
| 22 | + private LogsRepository $logs_repository; |
| 23 | + |
| 24 | + protected function setUp(): void |
| 25 | + { |
| 26 | + parent::setUp(); |
| 27 | + $this->logs_repository = new LogsRepository(); |
| 28 | + |
| 29 | + // Create the database table for testing |
| 30 | + DatabaseEntity::create_table(); |
| 31 | + } |
| 32 | + |
| 33 | + protected function tearDown(): void |
| 34 | + { |
| 35 | + // Clean up the database table |
| 36 | + $this->logs_repository->delete_all(); |
| 37 | + parent::tearDown(); |
| 38 | + } |
| 39 | + |
| 40 | + public function insert_mock_data(): void |
| 41 | + { |
| 42 | + $log_data = [ |
| 43 | + 'channel' => 'wpgraphql_logging', |
| 44 | + 'level' => 200, |
| 45 | + 'level_name' => 'INFO', |
| 46 | + 'message' => 'WPGraphQL Outgoing Response', |
| 47 | + 'context' => [ |
| 48 | + 'site_url' => 'http://test.local', |
| 49 | + 'wp_version' => '6.8.2', |
| 50 | + 'wp_debug_mode' => true, |
| 51 | + 'plugin_version'=> '0.0.1' |
| 52 | + ], |
| 53 | + 'extra' => [ |
| 54 | + 'ip' => '127.0.0.1', |
| 55 | + 'url' => '/index.php?graphql', |
| 56 | + 'server' => 'test.local', |
| 57 | + 'referrer' => 'http://test.local/wp-admin/admin.php?page=graphiql-ide', |
| 58 | + 'process_id' => 5819, |
| 59 | + 'http_method' => 'POST', |
| 60 | + 'memory_usage' => '14 MB', |
| 61 | + 'wpgraphql_query' => 'query GetPost($uri: ID!) { post(id: $uri, idType: URI) { title content } }', |
| 62 | + 'memory_peak_usage' => '14 MB', |
| 63 | + 'wpgraphql_variables' => [ |
| 64 | + 'uri' => 'hello-world' |
| 65 | + ], |
| 66 | + 'wpgraphql_operation_name' => 'GetPost' |
| 67 | + ] |
| 68 | + ]; |
| 69 | + |
| 70 | + $log_entry = DatabaseEntity::create(...$log_data); |
| 71 | + $log_entry->save(); |
| 72 | + $log_data['level'] = 400; |
| 73 | + $log_data['level_name'] = 'ERROR'; |
| 74 | + $log_data['message'] = 'WPGraphQL Error'; |
| 75 | + sleep(1); // Ensure different timestamps |
| 76 | + $log_entry = DatabaseEntity::create(...$log_data); |
| 77 | + $log_entry->save(); |
| 78 | + } |
| 79 | + |
| 80 | + public function test_get_logs_with_default_args(): void |
| 81 | + { |
| 82 | + $this->insert_mock_data(); |
| 83 | + $logs = $this->logs_repository->get_logs(); |
| 84 | + $this->assertIsArray($logs); |
| 85 | + $this->assertCount(2, $logs); |
| 86 | + $this->assertInstanceOf(DatabaseEntity::class, $logs[0]); |
| 87 | + $this->assertInstanceOf(DatabaseEntity::class, $logs[1]); |
| 88 | + } |
| 89 | + |
| 90 | + public function test_get_logs_with_custom_args(): void |
| 91 | + { |
| 92 | + $this->insert_mock_data(); |
| 93 | + $args = [ |
| 94 | + 'number' => 50, |
| 95 | + 'offset' => 0, |
| 96 | + 'orderby' => 'datetime', |
| 97 | + 'order' => 'DESC' |
| 98 | + ]; |
| 99 | + $logs = $this->logs_repository->get_logs($args); |
| 100 | + $this->assertIsArray($logs); |
| 101 | + $this->assertCount(2, $logs); |
| 102 | + |
| 103 | + // Should get the last inserted log first |
| 104 | + $this->assertEquals('WPGraphQL Error', $logs[0]->get_message()); |
| 105 | + |
| 106 | + /** |
| 107 | + * Test default orderby |
| 108 | + */ |
| 109 | + $args['orderby'] = ''; |
| 110 | + $logs = $this->logs_repository->get_logs($args); |
| 111 | + $this->assertIsArray($logs); |
| 112 | + |
| 113 | + // Should be last as default is DESC |
| 114 | + $this->assertEquals('WPGraphQL Error', $logs[0]->get_message()); |
| 115 | + |
| 116 | + /** |
| 117 | + * Test where is string should not work |
| 118 | + */ |
| 119 | + $args['where'] = 'level = 200'; |
| 120 | + $logs = $this->logs_repository->get_logs($args); |
| 121 | + $this->assertIsArray($logs); |
| 122 | + |
| 123 | + /** |
| 124 | + * Test invalid order |
| 125 | + */ |
| 126 | + $args['order'] = ''; |
| 127 | + $logs = $this->logs_repository->get_logs($args); |
| 128 | + $this->assertIsArray($logs); |
| 129 | + $this->assertCount(2, $logs); |
| 130 | + |
| 131 | + // Should be last one as where clause is ignored |
| 132 | + $this->assertEquals('WPGraphQL Error', $logs[0]->get_message()); |
| 133 | + |
| 134 | + // Check log count |
| 135 | + $this->assertEquals(2, $this->logs_repository->get_log_count([])); |
| 136 | + $this->assertEquals(1, $this->logs_repository->get_log_count(["level = 400", "channel = 'wpgraphql_logging'"])); |
| 137 | + } |
| 138 | + |
| 139 | + public function test_delete_logs(): void |
| 140 | + { |
| 141 | + $this->insert_mock_data(); |
| 142 | + $logs = $this->logs_repository->get_logs(); |
| 143 | + $this->assertCount(2, $logs); |
| 144 | + |
| 145 | + // Delete one log |
| 146 | + $result = $this->logs_repository->delete($logs[0]->get_id()); |
| 147 | + $this->assertTrue($result); |
| 148 | + |
| 149 | + // Delete invalid logs |
| 150 | + $result = $this->logs_repository->delete(0); |
| 151 | + $this->assertFalse($result); |
| 152 | + |
| 153 | + // Check remaining logs |
| 154 | + $logs = $this->logs_repository->get_logs(); |
| 155 | + $this->assertCount(1, $logs); |
| 156 | + |
| 157 | + // Delete all logs |
| 158 | + $this->logs_repository->delete_all(); |
| 159 | + $logs = $this->logs_repository->get_logs(); |
| 160 | + $this->assertCount(0, $logs); |
| 161 | + } |
| 162 | + |
| 163 | + public function test_delete_log_older_than(): void |
| 164 | + { |
| 165 | + $this->insert_mock_data(); |
| 166 | + $logs = $this->logs_repository->get_logs(); |
| 167 | + $this->assertCount(2, $logs); |
| 168 | + $date = $logs[0]->get_datetime(); |
| 169 | + $dateTime = new \DateTime($date); |
| 170 | + |
| 171 | + $result = $this->logs_repository->delete_log_older_than($dateTime); |
| 172 | + $this->assertTrue($result); |
| 173 | + $logs = $this->logs_repository->get_logs(); |
| 174 | + $this->assertCount(1, $logs); |
| 175 | + |
| 176 | + // Delete last log |
| 177 | + $dateTime->modify('+1 second'); |
| 178 | + $result = $this->logs_repository->delete_log_older_than($dateTime); |
| 179 | + $this->assertTrue($result); |
| 180 | + $logs = $this->logs_repository->get_logs(); |
| 181 | + $this->assertCount(0, $logs); |
| 182 | + } |
| 183 | +} |
0 commit comments