-
-
Notifications
You must be signed in to change notification settings - Fork 42
Expand file tree
/
Copy pathCSVExportTest.php
More file actions
221 lines (184 loc) · 5.61 KB
/
Copy pathCSVExportTest.php
File metadata and controls
221 lines (184 loc) · 5.61 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
<?php declare(strict_types = 1);
namespace Crontrol\Tests;
use Crontrol;
class CSVExportTest extends Test {
/**
* Export events to CSV and return all rows
*
* @param string $type Event type to export
* @return list<list<string|null>> Array of CSV rows
*/
private function exportEventsToArray( string $type = 'all' ): array {
$stream = fopen( 'php://memory', 'w+' );
if ( ! is_resource( $stream ) ) {
self::fail( 'Failed to open memory stream for CSV export' );
}
Crontrol\export_events_csv( $type, $stream );
rewind( $stream );
$rows = array();
while ( ( $row = fgetcsv( $stream ) ) !== false ) {
if ( $row !== null ) {
$rows[] = $row;
}
}
fclose( $stream );
return $rows;
}
/**
* Export events to CSV and return only the header row
*
* @param string $type Event type to export
* @return list<string|null> Header row
*/
private function exportEventsHeaders( string $type = 'all' ): array {
$rows = $this->exportEventsToArray( $type );
return $rows[0];
}
/**
* Export events to CSV and return only the data rows (excluding headers)
*
* @param string $type Event type to export
* @return list<list<string|null>> Data rows without headers
*/
private function exportEventsDataRows( string $type = 'all' ): array {
$rows = $this->exportEventsToArray( $type );
return array_slice( $rows, 1 );
}
/**
* Find a specific event row by hook name
*
* @param list<list<string|null>> $rows CSV rows to search
* @param string $hook Hook name to find
* @return list<string|null>|null The matching row or null if not found
*/
private function findEventRow( array $rows, string $hook ): ?array {
foreach ( $rows as $row ) {
if ( $row[0] === $hook ) {
return $row;
}
}
return null;
}
/**
* Get and assert an event row exists
*
* @param string $hook Hook name to find
* @param string $type Event type to export
* @return list<string|null> The event row
*/
private function getEventRow( string $hook, string $type = 'all' ): array {
$data_rows = $this->exportEventsDataRows( $type );
$event_row = $this->findEventRow( $data_rows, $hook );
if ( $event_row === null ) {
self::fail( "Event with hook '$hook' not found in CSV export" );
}
return $event_row;
}
/**
* Test that CSV export produces correct headers
*/
public function testCSVExportHeaders(): void {
$headers = $this->exportEventsHeaders( 'all' );
$expected_headers = array(
'hook',
'arguments',
'next_run',
'next_run_gmt',
'action',
'schedule',
'interval',
);
self::assertSame( $expected_headers, $headers );
}
/**
* Test that CSV export includes scheduled events
*/
public function testCSVExportIncludesScheduledEvents(): void {
// Schedule a test event
$timestamp = time() + 123;
$hook = 'test_csv_export_hook';
$args = array( 'test_arg' => 'test_value' );
wp_schedule_single_event( $timestamp, $hook, array( $args ) );
$test_event_row = $this->getEventRow( $hook, 'all' );
// Arguments
self::assertSame( '[{"test_arg":"test_value"}]', $test_event_row[1] );
// Schedule
self::assertSame( 'Non-repeating', $test_event_row[5] );
// Interval
self::assertSame( '0', $test_event_row[6] );
}
/**
* Test that CSV export includes recurring events
*/
public function testCSVExportIncludesRecurringEvents(): void {
// Schedule a recurring test event
$timestamp = time() + 123;
$hook = 'test_csv_export_recurring_hook';
$args = array();
$recurrence = 'hourly';
wp_schedule_event( $timestamp, $recurrence, $hook, $args );
$test_event_row = $this->getEventRow( $hook, 'all' );
// Arguments
self::assertSame( '', $test_event_row[1] );
// Schedule
self::assertSame( 'Once Hourly', $test_event_row[5] );
// Interval
self::assertSame( '3600', $test_event_row[6] );
}
/**
* Test that CSV export handles PHP cron jobs correctly
*/
public function testCSVExportHandlesPHPCronJobs(): void {
// Schedule a PHP cron job
$timestamp = time() + 123;
$hook = 'crontrol_cron_job';
$php = 'echo "test";';
$args = array(
array(
'code' => $php,
'name' => 'Test PHP Job',
'hash' => wp_hash( $php ),
),
);
wp_schedule_single_event( $timestamp, $hook, $args );
$php_job_row = $this->getEventRow( $hook, 'all' );
// Arguments
self::assertSame( 'PHP Code', $php_job_row[1] );
// Action
self::assertSame( 'WP Crontrol', $php_job_row[4] );
}
/**
* Test that CSV export handles events with no arguments
*/
public function testCSVExportHandlesEventsWithNoArguments(): void {
// Schedule an event with no arguments
$timestamp = time() + 123;
$hook = 'test_csv_no_args_hook';
wp_schedule_single_event( $timestamp, $hook );
$test_event_row = $this->getEventRow( $hook, 'all' );
// Arguments
self::assertSame( '', $test_event_row[1] );
}
/**
* Test that CSV export handles invalid schedule names
*/
public function testCSVExportHandlesInvalidScheduleNames(): void {
// Create an event with a custom/invalid schedule
$timestamp = time() + 123;
$hook = 'test_csv_invalid_schedule';
$key = md5( serialize( array() ) );
// Manually add an event with an invalid schedule using the proper structure
$crons = _get_cron_array();
$crons[ $timestamp ][ $hook ][ $key ] = array(
'schedule' => 'non_existent_schedule',
'args' => array(),
'interval' => 9999,
);
_set_cron_array( $crons );
$test_event_row = $this->getEventRow( $hook, 'all' );
// Schedule name should show the error message for invalid schedule
self::assertSame( 'Unknown (non_existent_schedule)', $test_event_row[5] );
// Interval should still be correct
self::assertSame( '9999', $test_event_row[6] );
}
}