Skip to content

Commit d8b7366

Browse files
committed
Added tests for Data Deletion Scheduler.
1 parent a1580ad commit d8b7366

File tree

2 files changed

+167
-1
lines changed

2 files changed

+167
-1
lines changed

plugins/wpgraphql-logging/src/Logger/Scheduler/DataDeletionScheduler.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -90,7 +90,7 @@ public function perform_deletion(): void {
9090
}
9191

9292
try {
93-
self::delete_old_logs( $retention_days );
93+
$this->delete_old_logs( $retention_days );
9494
} catch ( \Throwable $e ) {
9595
do_action('wpgraphql_logging_cleanup_error', [
9696
'error_message' => $e->getMessage(),
Lines changed: 166 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,166 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace WPGraphQL\Logging\Tests\Logger\Scheduler;
6+
7+
use WPGraphQL\Logging\Admin\Settings\ConfigurationHelper;
8+
use WPGraphQL\Logging\Admin\Settings\Fields\Tab\DataManagementTab;
9+
use WPGraphQL\Logging\Logger\Database\LogsRepository;
10+
use WPGraphQL\Logging\Logger\Scheduler\DataDeletionScheduler;
11+
use WPGraphQL\Logging\Logger\Database\DatabaseEntity;
12+
use Monolog\Level;
13+
use lucatume\WPBrowser\TestCase\WPTestCase;
14+
use Mockery;
15+
16+
17+
class DataDeletionSchedulerTest extends WPTestCase {
18+
19+
20+
protected $initial_log_count = 0;
21+
22+
protected LogsRepository $repository;
23+
24+
protected function setUp(): void {
25+
parent::setUp();
26+
$this->repository = new LogsRepository();
27+
$this->generate_logs();
28+
$this->initial_log_count = $this->get_total_log_count();
29+
// wp_clear_scheduled_hook(DataDeletionScheduler::DELETION_HOOK);
30+
}
31+
32+
protected function tearDown(): void {
33+
$this->delete_logs();
34+
// wp_clear_scheduled_hook(DataDeletionScheduler::DELETION_HOOK);
35+
parent::tearDown();
36+
}
37+
38+
39+
public function generate_logs() : void {
40+
41+
global $wpdb;
42+
$table_name = DatabaseEntity::get_table_name();
43+
$repository = new LogsRepository();
44+
$now = new \DateTime();
45+
for ($i = 0; $i < 10; $i++) {
46+
$entity = DatabaseEntity::create(
47+
'wpgraphql_logging',
48+
200,
49+
'info',
50+
'Test log ' . $i,
51+
['query' => 'query { posts { id title } }'],
52+
[]
53+
);
54+
55+
56+
$id = $entity->save();
57+
58+
// Manually set the datetime to simulate old logs
59+
$log_date = (clone $now)->modify("-" . $i . " days");
60+
$wpdb->update(
61+
$table_name,
62+
['datetime' => $log_date->format('Y-m-d H:i:s')],
63+
['id' => $id],
64+
['%s'],
65+
['%d']
66+
);
67+
}
68+
}
69+
70+
public function delete_logs() : void {
71+
$this->repository->delete_all();
72+
}
73+
74+
public function get_total_log_count() : int {
75+
return $this->repository->get_log_count([]);
76+
}
77+
78+
public function test_init_creates_singleton_instance(): void {
79+
$reflection = new \ReflectionClass(DataDeletionScheduler::class);
80+
$instanceProperty = $reflection->getProperty('instance');
81+
$instanceProperty->setAccessible(true);
82+
$instanceProperty->setValue(null, null);
83+
84+
$instance1 = DataDeletionScheduler::init();
85+
$instance2 = DataDeletionScheduler::init();
86+
$this->assertSame($instance1, $instance2);
87+
$this->assertInstanceOf(DataDeletionScheduler::class, $instance1);
88+
$this->assertInstanceOf(DataDeletionScheduler::class, $instance2);
89+
}
90+
91+
92+
public function create_mock_scheduler(array $config) : DataDeletionScheduler {
93+
$scheduler = DataDeletionScheduler::init();
94+
$reflection = new \ReflectionClass($scheduler);
95+
$configProperty = $reflection->getProperty('config');
96+
$configProperty->setAccessible(true);
97+
$configProperty->setValue($scheduler, $config);
98+
return $scheduler;
99+
}
100+
101+
102+
public function test_data_deletion_scheduler_no_deletion_when_disabled(): void {
103+
104+
$scheduler = $this->create_mock_scheduler(
105+
[
106+
DataManagementTab::DATA_DELETION_ENABLED => false,
107+
DataManagementTab::DATA_RETENTION_DAYS => 30
108+
]
109+
);
110+
111+
$scheduler->perform_deletion();
112+
$this->assertEquals($this->initial_log_count, $this->get_total_log_count());
113+
}
114+
115+
public function test_data_deletion_scheduler_no_deletion_invalid_retention_days(): void {
116+
117+
$scheduler = $this->create_mock_scheduler(
118+
[
119+
DataManagementTab::DATA_DELETION_ENABLED => true,
120+
DataManagementTab::DATA_RETENTION_DAYS => 'invalid_integer'
121+
]
122+
);
123+
124+
$scheduler->perform_deletion();
125+
$this->assertEquals($this->initial_log_count, $this->get_total_log_count());
126+
}
127+
128+
public function test_data_deletion_scheduler_no_deletion_zero_retention_days(): void {
129+
130+
$scheduler = $this->create_mock_scheduler(
131+
[
132+
DataManagementTab::DATA_DELETION_ENABLED => true,
133+
DataManagementTab::DATA_RETENTION_DAYS => 0
134+
]
135+
);
136+
137+
$scheduler->perform_deletion();
138+
$this->assertEquals($this->initial_log_count, $this->get_total_log_count());
139+
}
140+
141+
public function test_data_deletion_scheduler_valid_config(): void {
142+
143+
$scheduler = $this->create_mock_scheduler(
144+
[
145+
DataManagementTab::DATA_DELETION_ENABLED => true,
146+
DataManagementTab::DATA_RETENTION_DAYS => 3
147+
]
148+
);
149+
150+
$expected_count = $this->repository->get_log_count(['datetime >= NOW() - INTERVAL 3 DAY']);
151+
// Delete logs
152+
$scheduler->perform_deletion();
153+
$total_count = $this->get_total_log_count();
154+
$this->assertLessThan($this->initial_log_count, $total_count);
155+
$this->assertGreaterThanOrEqual(0, $total_count);
156+
$this->assertEquals($expected_count, $total_count);
157+
}
158+
159+
public function test_schedule_deletion_schedules_event(): void {
160+
wp_clear_scheduled_hook(DataDeletionScheduler::DELETION_HOOK);
161+
$scheduler = DataDeletionScheduler::init();
162+
$this->assertFalse(wp_next_scheduled(DataDeletionScheduler::DELETION_HOOK));
163+
$scheduler->schedule_deletion();
164+
$this->assertNotFalse(wp_next_scheduled(DataDeletionScheduler::DELETION_HOOK));
165+
}
166+
}

0 commit comments

Comments
 (0)