Skip to content

Commit

Permalink
Fix tests.
Browse files Browse the repository at this point in the history
  • Loading branch information
kagg-design committed Feb 23, 2025
1 parent d777aac commit ab5d1de
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 9 deletions.
2 changes: 1 addition & 1 deletion .tests/php/unit/Settings/EventsPageTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -781,6 +781,6 @@ static function ( $ids ) {
$subject = Mockery::mock( EventsPage::class )->makePartial();
$subject->shouldAllowMockingProtectedMethods();

self::assertTrue( $subject->delete_events( $ids ) );
self::assertTrue( $subject->delete_events( [ 'ids' => $ids ] ) );
}
}
18 changes: 14 additions & 4 deletions .tests/php/unit/Settings/FormsPageTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -621,24 +621,34 @@ public function test_delete_events(): void {
'formId' => '177',
],
];
$where_clause = '(source = %s AND form_id = %s) OR (source = %s AND form_id = %s)';
$dates = [
'2025-01-24',
'2025-02-23',
];
$args = [
'ids' => $ids,
'dates' => $dates,
];
$where_clause = '((source = %s AND form_id = %s) OR (source = %s AND form_id = %s)) AND date_gmt BETWEEN %s AND %s';
$prefix = 'wp_';
$table_name = 'hcaptcha_events';
$sql = "DELETE FROM $prefix$table_name WHERE $where_clause";
$prepared = "DELETE FROM $prefix$table_name WHERE (source = '[\"WordPress\"]' AND form_id = 'login') OR (source = '[\"Contact Form 7\"]' AND form_id = '177')";
$prepared = "DELETE FROM $prefix$table_name WHERE ((source = '[\"WordPress\"]' AND form_id = 'login') OR (source = '[\"Contact Form 7\"]' AND form_id = '177')) AND date_gmt BETWEEN '2025-01-24 00:00:00' AND '2025-02-23 23:59:59'";
$result = count( $ids );

WP_Mock::passthruFunction( 'get_gmt_from_date' );

// phpcs:ignore WordPress.WP.GlobalVariablesOverride.Prohibited
$wpdb = Mockery::mock( 'WPDB' );
$wpdb->prefix = $prefix;
$wpdb->shouldReceive( 'prepare' )
->with( $sql, 'WordPress', 'login', 'Contact Form 7', '177' )
->with( $sql, 'WordPress', 'login', 'Contact Form 7', '177', '2025-01-24 00:00:00', '2025-02-23 23:59:59' )
->andReturn( $prepared );
$wpdb->shouldReceive( 'query' )->with( $prepared )->andReturn( $result );

$subject = Mockery::mock( FormsPage::class )->makePartial();
$subject->shouldAllowMockingProtectedMethods();

self::assertTrue( $subject->delete_events( $ids ) );
self::assertTrue( $subject->delete_events( $args ) );
}
}
18 changes: 14 additions & 4 deletions .tests/php/unit/Settings/ListPageBaseTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -124,7 +124,11 @@ public function test_date_picker_display_when_not_allowed(): void {
* @return void
*/
public function test_bulk_action(): void {
$ids = [ 1, 2, 3 ];
$ids = [ 1, 2, 3 ];
$args = [
'ids' => $ids,
'dates' => [],
];

// phpcs:ignore WordPress.WP.AlternativeFunctions.json_encode_json_encode
$ids_encoded = json_encode( $ids );
Expand All @@ -135,11 +139,12 @@ public function test_bulk_action(): void {
$subject = Mockery::mock( ListPageBase::class )->makePartial();
$subject->shouldAllowMockingProtectedMethods();
$subject->shouldReceive( 'run_checks' );
$subject->shouldReceive( 'delete_events' )->with( $ids )->andReturn( true );
$subject->shouldReceive( 'delete_events' )->with( $args )->andReturn( true );

WP_Mock::passthruFunction( 'wp_unslash' );
WP_Mock::passthruFunction( 'sanitize_text_field' );
WP_Mock::userFunction( 'set_transient' )->once()->with( 'hcaptcha_page_base', 'Selected items have been successfully deleted.' );
WP_Mock::userFunction( 'set_transient' )->once()
->with( 'hcaptcha_page_base', 'Selected items have been successfully deleted.' );
WP_Mock::userFunction( 'wp_send_json_success' )->once()->with();

$subject->bulk_action();
Expand All @@ -166,12 +171,17 @@ public function test_bulk_action_with_invalid_action(): void {
* @return void
*/
public function test_bulk_action_with_delete_error(): void {
$args = [
'ids' => [],
'dates' => [],
];

$_POST['bulk'] = 'trash';

$subject = Mockery::mock( ListPageBase::class )->makePartial();
$subject->shouldAllowMockingProtectedMethods();
$subject->shouldReceive( 'run_checks' );
$subject->shouldReceive( 'delete_events' )->with( [] )->andReturn( false );
$subject->shouldReceive( 'delete_events' )->with( $args )->andReturn( false );

WP_Mock::passthruFunction( 'wp_unslash' );
WP_Mock::passthruFunction( 'sanitize_text_field' );
Expand Down

0 comments on commit ab5d1de

Please sign in to comment.