From c9e213d26c5253fd89699234d804b3d0a4d40ac4 Mon Sep 17 00:00:00 2001 From: Tosin Oguntuyi Date: Wed, 4 Dec 2024 14:51:19 +0100 Subject: [PATCH 01/19] Set up for event form handler test --- tests/event/event-form-handler.php | 23 +++++++++++++++++++++++ 1 file changed, 23 insertions(+) create mode 100644 tests/event/event-form-handler.php diff --git a/tests/event/event-form-handler.php b/tests/event/event-form-handler.php new file mode 100644 index 00000000..b81386c2 --- /dev/null +++ b/tests/event/event-form-handler.php @@ -0,0 +1,23 @@ +attendee_repository = new Attendee_Repository(); + $this->event_repository = new Event_Repository( $this->now, $this->attendee_repository ); + $this->event_form_handler = new Event_Form_Handler( $this->now, $this->event_repository ); + } +} From 35bc39d026c1fd7ada1a52af57b1d1fbf3b055f2 Mon Sep 17 00:00:00 2001 From: Tosin Oguntuyi Date: Mon, 9 Dec 2024 12:01:10 +0100 Subject: [PATCH 02/19] Add event form handler factory --- tests/lib/event-form-handler-factory.php | 31 ++++++++++++++++++++++++ 1 file changed, 31 insertions(+) create mode 100644 tests/lib/event-form-handler-factory.php diff --git a/tests/lib/event-form-handler-factory.php b/tests/lib/event-form-handler-factory.php new file mode 100644 index 00000000..bf857ded --- /dev/null +++ b/tests/lib/event-form-handler-factory.php @@ -0,0 +1,31 @@ + 'submit_event_ajax', + 'form_name' => $form_name, + 'event_id' => $_event_id, + 'event_form_action' => 'publish', + 'event_title' => $event_title, + 'event_description' => $event_description, + 'event_start' => $now->modify( '+1 month' ), + 'event_end' => $now->modify( '+2 month' ), + 'event_timezone' => $timezone, + 'event_attendance_mode' => $event_attendance_mode, + ); + + } +} From 94c8a5258c72be6b66eac41e342d56e9b1271c58 Mon Sep 17 00:00:00 2001 From: Tosin Oguntuyi Date: Wed, 11 Dec 2024 01:55:13 +0100 Subject: [PATCH 03/19] Add test for non-logged in user --- tests/event/event-form-handler.php | 26 ++++++++++++++++++++------ 1 file changed, 20 insertions(+), 6 deletions(-) diff --git a/tests/event/event-form-handler.php b/tests/event/event-form-handler.php index b81386c2..2bc67fa9 100644 --- a/tests/event/event-form-handler.php +++ b/tests/event/event-form-handler.php @@ -6,18 +6,32 @@ use Wporg\TranslationEvents\Attendee\Attendee_Repository; use Wporg\TranslationEvents\Event\Event_Repository; use Wporg\TranslationEvents\Event\Event_Form_Handler; - - +use Wporg\TranslationEvents\Tests\Event_Form_Handler_Factory; class Event_Form_Handler_Test extends Base_Test { private Event_Repository $event_repository; private Attendee_Repository $attendee_repository; - private $event_form_handler; + private Event_Form_Handler $event_form_handler; + private Event_Form_Handler_Factory $event_form_handler_factory; public function setUp(): void { parent::setUp(); - $this->attendee_repository = new Attendee_Repository(); - $this->event_repository = new Event_Repository( $this->now, $this->attendee_repository ); - $this->event_form_handler = new Event_Form_Handler( $this->now, $this->event_repository ); + $this->attendee_repository = new Attendee_Repository(); + $this->event_repository = new Event_Repository( $this->now, $this->attendee_repository ); + $this->event_form_handler = new Event_Form_Handler( $this->now, $this->event_repository ); + $this->event_form_handler_factory = new Event_Form_Handler_Factory(); + } + + /** + * Test that the user must be logged in to create an event. + * + * @return void + */ + public function test_user_is_not_logged_in() { + wp_set_current_user( 0 ); + $form_data = $this->event_form_handler_factory->future_inactive_event_form_data( 'create_event', $this->now ); + $response = $this->event_form_handler->handle( $form_data ); + + $this->assertEquals( '{"success":false,"data":"The user must be logged in."}', $response ); } } From f5076ca1a450976615166a8abdab534f5ab6c2da Mon Sep 17 00:00:00 2001 From: Tosin Oguntuyi Date: Wed, 11 Dec 2024 13:32:20 +0100 Subject: [PATCH 04/19] Refactor code to make it testable --- includes/event/event-form-handler.php | 58 ++++++++++++++------------- 1 file changed, 31 insertions(+), 27 deletions(-) diff --git a/includes/event/event-form-handler.php b/includes/event/event-form-handler.php index 9e1aad09..f91c24c7 100644 --- a/includes/event/event-form-handler.php +++ b/includes/event/event-form-handler.php @@ -21,26 +21,35 @@ public function __construct( DateTimeImmutable $now, Event_Repository $event_rep } public function handle( array $form_data ): void { + $response = $this->process_form( $form_data ); + if ( is_wp_error( $response ) ) { + $status_code = $response->get_error_data( 'status' ); + wp_send_json_error( $response->get_error_message(), $status_code ); + } + wp_send_json_success( $response ); + } + + public function process_form( array $form_data ) { if ( ! is_user_logged_in() ) { - wp_send_json_error( esc_html__( 'The user must be logged in.', 'gp-translation-events' ), 403 ); + return new WP_Error( 'not_logged_in', esc_html__( 'The user must be logged in.', 'gp-translation-events' ), array( 'status' => 403 ) ); } $action = isset( $form_data['form_name'] ) ? sanitize_text_field( wp_unslash( $form_data['form_name'] ) ) : ''; if ( ! in_array( $action, array( 'create_event', 'edit_event', 'trash_event' ), true ) ) { - wp_send_json_error( esc_html__( 'Invalid form name.', 'gp-translation-events' ), 403 ); + return new WP_Error( '', esc_html__( 'Invalid form name.', 'gp-translation-events' ), array( 'status' => 403 ) ); } $event_id = isset( $form_data['event_id'] ) ? intval( sanitize_text_field( wp_unslash( $form_data['event_id'] ) ) ) : 0; $event = null; if ( 'create_event' === $action && ( ! current_user_can( 'create_translation_event' ) ) ) { - wp_send_json_error( esc_html__( 'You do not have permissions to create events.', 'gp-translation-events' ), 403 ); + return new WP_Error( '', esc_html__( 'You do not have permissions to create events.', 'gp-translation-events' ), array( 'status' => 403 ) ); } if ( 'edit_event' === $action && ( ! current_user_can( 'edit_translation_event', $event_id ) ) ) { - wp_send_json_error( esc_html__( 'You do not have permissions to edit this event.', 'gp-translation-events' ), 403 ); + return new WP_Error( '', esc_html__( 'You do not have permissions to edit this event.', 'gp-translation-events' ), array( 'status' => 403 ) ); } if ( 'trash_event' === $action && ( ! current_user_can( 'trash_translation_event', $event_id ) ) ) { - wp_send_json_error( esc_html__( 'You do not have permissions to delete this event.', 'gp-translation-events' ), 403 ); + return new WP_Error( '', esc_html__( 'You do not have permissions to delete this event.', 'gp-translation-events' ), array( 'status' => 403 ) ); } $is_nonce_valid = false; @@ -52,7 +61,7 @@ public function handle( array $form_data ): void { } } if ( ! $is_nonce_valid ) { - wp_send_json_error( esc_html__( 'Nonce verification failed.', 'gp-translation-events' ), 403 ); + return new WP_Error( '', esc_html__( 'Nonce verification failed.', 'gp-translation-events' ), array( 'status' => 403 ) ); } $response_message = ''; @@ -63,17 +72,17 @@ public function handle( array $form_data ): void { if ( 'trash_event' === $action ) { // Trash event. if ( ! $event ) { - wp_send_json_error( esc_html__( 'Event does not exist.', 'gp-translation-events' ), 404 ); + return new WP_Error( '', esc_html__( 'Event does not exist.', 'gp-translation-events' ), array( 'status' => 404 ) ); } $stats_calculator = new Stats_Calculator(); try { $event_stats = $stats_calculator->for_event( $event->id() ); } catch ( Exception $e ) { - wp_send_json_error( esc_html__( 'Failed to calculate event stats.', 'gp-translation-events' ), 500 ); + return new WP_Error( '', esc_html__( 'Failed to calculate event stats.', 'gp-translation-events' ), array( 'status' => 500 ) ); } if ( ! empty( $event_stats->rows() ) ) { - wp_send_json_error( esc_html__( 'Event has stats so it cannot be deleted.', 'gp-translation-events' ), 422 ); + return new WP_Error( '', esc_html__( 'Event has stats so it cannot be deleted.', 'gp-translation-events' ), array( 'status' => 422 ) ); } if ( false === $this->event_repository->trash_event( $event ) ) { @@ -90,35 +99,30 @@ public function handle( array $form_data ): void { try { $new_event = $this->parse_form_data( $form_data ); } catch ( InvalidTimeZone $e ) { - wp_send_json_error( esc_html__( 'Invalid time zone.', 'gp-translation-events' ), 422 ); - return; + return new WP_Error( '', esc_html__( 'Invalid time zone.', 'gp-translation-events' ), array( 'status' => 422 ) ); } catch ( InvalidStart $e ) { - wp_send_json_error( esc_html__( 'Invalid start date.', 'gp-translation-events' ), 422 ); - return; + return new WP_Error( '', esc_html__( 'Invalid start date.', 'gp-translation-events' ), array( 'status' => 422 ) ); } catch ( InvalidEnd $e ) { - wp_send_json_error( esc_html__( 'Invalid end date.', 'gp-translation-events' ), 422 ); - return; + return new WP_Error( '', esc_html__( 'Invalid end date.', 'gp-translation-events' ), array( 'status' => 422 ) ); } catch ( InvalidStatus $e ) { - wp_send_json_error( esc_html__( 'Invalid status.', 'gp-translation-events' ), 422 ); - return; + return new WP_Error( '', esc_html__( 'Invalid status.', 'gp-translation-events' ), array( 'status' => 422 ) ); } if ( empty( $new_event->title() ) ) { - wp_send_json_error( esc_html__( 'Invalid title.', 'gp-translation-events' ), 422 ); - return; + return new WP_Error( '', esc_html__( 'Invalid title.', 'gp-translation-events' ), array( 'status' => 422 ) ); } // This is a list of slugs that are not allowed, as they conflict with the event URLs. $invalid_slugs = array( 'new', 'edit', 'attend', 'my-events' ); if ( in_array( sanitize_title( $new_event->title() ), $invalid_slugs, true ) ) { - wp_send_json_error( esc_html__( 'Invalid slug.', 'gp-translation-events' ), 422 ); + return new WP_Error( '', esc_html__( 'Invalid slug.', 'gp-translation-events' ), array( 'status' => 422 ) ); } if ( 'create_event' === $action ) { $result = $this->event_repository->insert_event( $new_event ); if ( $result instanceof WP_Error ) { - wp_send_json_error( esc_html__( 'Failed to create event.', 'gp-translation-events' ), 422 ); - return; + return new WP_Error( '', esc_html__( 'Failed to create event.', 'gp-translation-events' ), array( 'status' => 422 ) ); + } $response_message = esc_html__( 'Event created successfully.', 'gp-translation-events' ); $this->notifications_schedule->schedule_emails( $result ); @@ -126,7 +130,7 @@ public function handle( array $form_data ): void { if ( 'edit_event' === $action ) { $event = $this->event_repository->get_event( $new_event->id() ); if ( ! $event ) { - wp_send_json_error( esc_html__( 'Event does not exist.', 'gp-translation-events' ), 404 ); + return new WP_Error( '', esc_html__( 'Event does not exist.', 'gp-translation-events' ), array( 'status' => 404 ) ); } try { @@ -153,14 +157,14 @@ public function handle( array $form_data ): void { $event->set_attendance_mode( $new_event->attendance_mode() ); } } catch ( Exception $e ) { - wp_send_json_error( esc_html__( 'Failed to update event.', 'gp-translation-events' ), 422 ); - return; + return new WP_Error( '', esc_html__( 'Failed to update event.', 'gp-translation-events' ), array( 'status' => 422 ) ); + } $result = $this->event_repository->update_event( $event ); if ( $result instanceof WP_Error ) { - wp_send_json_error( esc_html__( 'Failed to update event.', 'gp-translation-events' ), 422 ); - return; + return new WP_Error( '', esc_html__( 'Failed to update event.', 'gp-translation-events' ), array( 'status' => 422 ) ); + } $response_message = esc_html__( 'Event updated successfully', 'gp-translation-events' ); $this->notifications_schedule->schedule_emails( $result ); From ebf39aed3ca000dc5286f88cbda0ddc32640f5e5 Mon Sep 17 00:00:00 2001 From: Tosin Oguntuyi Date: Wed, 11 Dec 2024 13:34:20 +0100 Subject: [PATCH 05/19] Update expected output --- tests/event/event-form-handler.php | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/tests/event/event-form-handler.php b/tests/event/event-form-handler.php index 2bc67fa9..f154f3c2 100644 --- a/tests/event/event-form-handler.php +++ b/tests/event/event-form-handler.php @@ -30,8 +30,8 @@ public function setUp(): void { public function test_user_is_not_logged_in() { wp_set_current_user( 0 ); $form_data = $this->event_form_handler_factory->future_inactive_event_form_data( 'create_event', $this->now ); - $response = $this->event_form_handler->handle( $form_data ); + $response = $this->event_form_handler->process_form( $form_data ); - $this->assertEquals( '{"success":false,"data":"The user must be logged in."}', $response ); + $this->assertEquals( 'The user must be logged in.', $response->get_error_message() ); } } From 11f9855fca43334db018c94dbeea7408d52ef2ee Mon Sep 17 00:00:00 2001 From: Tosin Oguntuyi Date: Wed, 11 Dec 2024 13:44:29 +0100 Subject: [PATCH 06/19] Fix lint error --- tests/lib/event-form-handler-factory.php | 1 - 1 file changed, 1 deletion(-) diff --git a/tests/lib/event-form-handler-factory.php b/tests/lib/event-form-handler-factory.php index bf857ded..7713f702 100644 --- a/tests/lib/event-form-handler-factory.php +++ b/tests/lib/event-form-handler-factory.php @@ -26,6 +26,5 @@ public function future_inactive_event_form_data( $form_name, DateTimeImmutable $ 'event_timezone' => $timezone, 'event_attendance_mode' => $event_attendance_mode, ); - } } From 4343121e9c203ca78247e2b76aa182b3b4e36186 Mon Sep 17 00:00:00 2001 From: Tosin Oguntuyi Date: Wed, 11 Dec 2024 20:03:29 +0100 Subject: [PATCH 07/19] Add error code for invalid form name --- includes/event/event-form-handler.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/includes/event/event-form-handler.php b/includes/event/event-form-handler.php index f91c24c7..bd523de2 100644 --- a/includes/event/event-form-handler.php +++ b/includes/event/event-form-handler.php @@ -36,7 +36,7 @@ public function process_form( array $form_data ) { $action = isset( $form_data['form_name'] ) ? sanitize_text_field( wp_unslash( $form_data['form_name'] ) ) : ''; if ( ! in_array( $action, array( 'create_event', 'edit_event', 'trash_event' ), true ) ) { - return new WP_Error( '', esc_html__( 'Invalid form name.', 'gp-translation-events' ), array( 'status' => 403 ) ); + return new WP_Error( 'form_name_error', esc_html__( 'Invalid form name.', 'gp-translation-events' ), array( 'status' => 403 ) ); } $event_id = isset( $form_data['event_id'] ) ? intval( sanitize_text_field( wp_unslash( $form_data['event_id'] ) ) ) : 0; From ca4b5a6d6dce2dbd97790e865d9fa4b49c830601 Mon Sep 17 00:00:00 2001 From: Tosin Oguntuyi Date: Wed, 11 Dec 2024 20:04:05 +0100 Subject: [PATCH 08/19] Test for invalid form name --- tests/event/event-form-handler.php | 8 ++++++++ 1 file changed, 8 insertions(+) diff --git a/tests/event/event-form-handler.php b/tests/event/event-form-handler.php index f154f3c2..790ea7f1 100644 --- a/tests/event/event-form-handler.php +++ b/tests/event/event-form-handler.php @@ -20,6 +20,7 @@ public function setUp(): void { $this->event_repository = new Event_Repository( $this->now, $this->attendee_repository ); $this->event_form_handler = new Event_Form_Handler( $this->now, $this->event_repository ); $this->event_form_handler_factory = new Event_Form_Handler_Factory(); + $this->set_normal_user_as_current(); } /** @@ -34,4 +35,11 @@ public function test_user_is_not_logged_in() { $this->assertEquals( 'The user must be logged in.', $response->get_error_message() ); } + + public function test_form_name_is_invalid() { + $form_data = $this->event_form_handler_factory->future_inactive_event_form_data( 'invalid_form_name', $this->now ); + $response = $this->event_form_handler->process_form( $form_data ); + + $this->assertEquals( 'Invalid form name.', $response->get_error_message() ); + } } From 5717ff6bdab6861677123151ed411960ea469a47 Mon Sep 17 00:00:00 2001 From: Tosin Oguntuyi Date: Fri, 13 Dec 2024 15:00:34 +0100 Subject: [PATCH 09/19] Add error code for all errors --- includes/event/event-form-handler.php | 34 +++++++++++++-------------- 1 file changed, 17 insertions(+), 17 deletions(-) diff --git a/includes/event/event-form-handler.php b/includes/event/event-form-handler.php index bd523de2..f85111c6 100644 --- a/includes/event/event-form-handler.php +++ b/includes/event/event-form-handler.php @@ -43,13 +43,13 @@ public function process_form( array $form_data ) { $event = null; if ( 'create_event' === $action && ( ! current_user_can( 'create_translation_event' ) ) ) { - return new WP_Error( '', esc_html__( 'You do not have permissions to create events.', 'gp-translation-events' ), array( 'status' => 403 ) ); + return new WP_Error( 'permission_error', esc_html__( 'You do not have permissions to create events.', 'gp-translation-events' ), array( 'status' => 403 ) ); } if ( 'edit_event' === $action && ( ! current_user_can( 'edit_translation_event', $event_id ) ) ) { - return new WP_Error( '', esc_html__( 'You do not have permissions to edit this event.', 'gp-translation-events' ), array( 'status' => 403 ) ); + return new WP_Error( 'permission_error', esc_html__( 'You do not have permissions to edit this event.', 'gp-translation-events' ), array( 'status' => 403 ) ); } if ( 'trash_event' === $action && ( ! current_user_can( 'trash_translation_event', $event_id ) ) ) { - return new WP_Error( '', esc_html__( 'You do not have permissions to delete this event.', 'gp-translation-events' ), array( 'status' => 403 ) ); + return new WP_Error( 'permission_error', esc_html__( 'You do not have permissions to delete this event.', 'gp-translation-events' ), array( 'status' => 403 ) ); } $is_nonce_valid = false; @@ -61,7 +61,7 @@ public function process_form( array $form_data ) { } } if ( ! $is_nonce_valid ) { - return new WP_Error( '', esc_html__( 'Nonce verification failed.', 'gp-translation-events' ), array( 'status' => 403 ) ); + return new WP_Error( 'invalid_nonce', esc_html__( 'Nonce verification failed.', 'gp-translation-events' ), array( 'status' => 403 ) ); } $response_message = ''; @@ -72,17 +72,17 @@ public function process_form( array $form_data ) { if ( 'trash_event' === $action ) { // Trash event. if ( ! $event ) { - return new WP_Error( '', esc_html__( 'Event does not exist.', 'gp-translation-events' ), array( 'status' => 404 ) ); + return new WP_Error( 'inexistent_event', esc_html__( 'Event does not exist.', 'gp-translation-events' ), array( 'status' => 404 ) ); } $stats_calculator = new Stats_Calculator(); try { $event_stats = $stats_calculator->for_event( $event->id() ); } catch ( Exception $e ) { - return new WP_Error( '', esc_html__( 'Failed to calculate event stats.', 'gp-translation-events' ), array( 'status' => 500 ) ); + return new WP_Error( 'stats_calculation_error', esc_html__( 'Failed to calculate event stats.', 'gp-translation-events' ), array( 'status' => 500 ) ); } if ( ! empty( $event_stats->rows() ) ) { - return new WP_Error( '', esc_html__( 'Event has stats so it cannot be deleted.', 'gp-translation-events' ), array( 'status' => 422 ) ); + return new WP_Error( 'cannot_delete_event', esc_html__( 'Event has stats so it cannot be deleted.', 'gp-translation-events' ), array( 'status' => 422 ) ); } if ( false === $this->event_repository->trash_event( $event ) ) { @@ -99,29 +99,29 @@ public function process_form( array $form_data ) { try { $new_event = $this->parse_form_data( $form_data ); } catch ( InvalidTimeZone $e ) { - return new WP_Error( '', esc_html__( 'Invalid time zone.', 'gp-translation-events' ), array( 'status' => 422 ) ); + return new WP_Error( 'invalid_timezone', esc_html__( 'Invalid time zone.', 'gp-translation-events' ), array( 'status' => 422 ) ); } catch ( InvalidStart $e ) { - return new WP_Error( '', esc_html__( 'Invalid start date.', 'gp-translation-events' ), array( 'status' => 422 ) ); + return new WP_Error( 'invalid_start_date', esc_html__( 'Invalid start date.', 'gp-translation-events' ), array( 'status' => 422 ) ); } catch ( InvalidEnd $e ) { - return new WP_Error( '', esc_html__( 'Invalid end date.', 'gp-translation-events' ), array( 'status' => 422 ) ); + return new WP_Error( 'invalid_end_date', esc_html__( 'Invalid end date.', 'gp-translation-events' ), array( 'status' => 422 ) ); } catch ( InvalidStatus $e ) { - return new WP_Error( '', esc_html__( 'Invalid status.', 'gp-translation-events' ), array( 'status' => 422 ) ); + return new WP_Error( 'invalid_status', esc_html__( 'Invalid status.', 'gp-translation-events' ), array( 'status' => 422 ) ); } if ( empty( $new_event->title() ) ) { - return new WP_Error( '', esc_html__( 'Invalid title.', 'gp-translation-events' ), array( 'status' => 422 ) ); + return new WP_Error( 'invalid_title', esc_html__( 'Invalid title.', 'gp-translation-events' ), array( 'status' => 422 ) ); } // This is a list of slugs that are not allowed, as they conflict with the event URLs. $invalid_slugs = array( 'new', 'edit', 'attend', 'my-events' ); if ( in_array( sanitize_title( $new_event->title() ), $invalid_slugs, true ) ) { - return new WP_Error( '', esc_html__( 'Invalid slug.', 'gp-translation-events' ), array( 'status' => 422 ) ); + return new WP_Error( 'invalid_slug', esc_html__( 'Invalid slug.', 'gp-translation-events' ), array( 'status' => 422 ) ); } if ( 'create_event' === $action ) { $result = $this->event_repository->insert_event( $new_event ); if ( $result instanceof WP_Error ) { - return new WP_Error( '', esc_html__( 'Failed to create event.', 'gp-translation-events' ), array( 'status' => 422 ) ); + return new WP_Error( 'cannot_create_event', esc_html__( 'Failed to create event.', 'gp-translation-events' ), array( 'status' => 422 ) ); } $response_message = esc_html__( 'Event created successfully.', 'gp-translation-events' ); @@ -130,7 +130,7 @@ public function process_form( array $form_data ) { if ( 'edit_event' === $action ) { $event = $this->event_repository->get_event( $new_event->id() ); if ( ! $event ) { - return new WP_Error( '', esc_html__( 'Event does not exist.', 'gp-translation-events' ), array( 'status' => 404 ) ); + return new WP_Error( 'inexistent_event', esc_html__( 'Event does not exist.', 'gp-translation-events' ), array( 'status' => 404 ) ); } try { @@ -157,13 +157,13 @@ public function process_form( array $form_data ) { $event->set_attendance_mode( $new_event->attendance_mode() ); } } catch ( Exception $e ) { - return new WP_Error( '', esc_html__( 'Failed to update event.', 'gp-translation-events' ), array( 'status' => 422 ) ); + return new WP_Error( 'cannot_update_event', esc_html__( 'Failed to update event.', 'gp-translation-events' ), array( 'status' => 422 ) ); } $result = $this->event_repository->update_event( $event ); if ( $result instanceof WP_Error ) { - return new WP_Error( '', esc_html__( 'Failed to update event.', 'gp-translation-events' ), array( 'status' => 422 ) ); + return new WP_Error( 'cannot_update_event', esc_html__( 'Failed to update event.', 'gp-translation-events' ), array( 'status' => 422 ) ); } $response_message = esc_html__( 'Event updated successfully', 'gp-translation-events' ); From 662dbab789249ec17990d0946164b4245e103cfb Mon Sep 17 00:00:00 2001 From: Tosin Oguntuyi Date: Fri, 13 Dec 2024 17:00:48 +0100 Subject: [PATCH 10/19] Add test for user with no permission --- tests/event/event-form-handler.php | 16 ++++++++++++++++ 1 file changed, 16 insertions(+) diff --git a/tests/event/event-form-handler.php b/tests/event/event-form-handler.php index 790ea7f1..c8753615 100644 --- a/tests/event/event-form-handler.php +++ b/tests/event/event-form-handler.php @@ -42,4 +42,20 @@ public function test_form_name_is_invalid() { $this->assertEquals( 'Invalid form name.', $response->get_error_message() ); } + + public function test_invalid_permissions() { + + $form_data = $this->event_form_handler_factory->future_inactive_event_form_data( 'create_event', $this->now ); + $response = $this->event_form_handler->process_form( $form_data ); + $this->assertEquals( 'You do not have permissions to create events.', $response->get_error_message() ); + + $form_data = $this->event_form_handler_factory->future_inactive_event_form_data( 'edit_event', $this->now ); + $response = $this->event_form_handler->process_form( $form_data ); + $this->assertEquals( 'You do not have permissions to edit this event.', $response->get_error_message() ); + + $form_data = $this->event_form_handler_factory->future_inactive_event_form_data( 'trash_event', $this->now ); + $response = $this->event_form_handler->process_form( $form_data ); + $this->assertEquals( 'You do not have permissions to delete this event.', $response->get_error_message() ); + } } + From 746071fcd38d73290798abab8f109ac41f450dc9 Mon Sep 17 00:00:00 2001 From: Tosin Oguntuyi Date: Mon, 16 Dec 2024 01:00:41 +0100 Subject: [PATCH 11/19] Fix variable names --- tests/event/event-form-handler.php | 20 +++++++++----------- 1 file changed, 9 insertions(+), 11 deletions(-) diff --git a/tests/event/event-form-handler.php b/tests/event/event-form-handler.php index c8753615..78ce7dbd 100644 --- a/tests/event/event-form-handler.php +++ b/tests/event/event-form-handler.php @@ -44,18 +44,16 @@ public function test_form_name_is_invalid() { } public function test_invalid_permissions() { + $form_data_1 = $this->event_form_handler_factory->future_inactive_event_form_data( 'create_event', $this->now ); + $response_1 = $this->event_form_handler->process_form( $form_data_1 ); + $this->assertEquals( 'You do not have permissions to create events.', $response_1->get_error_message() ); - $form_data = $this->event_form_handler_factory->future_inactive_event_form_data( 'create_event', $this->now ); - $response = $this->event_form_handler->process_form( $form_data ); - $this->assertEquals( 'You do not have permissions to create events.', $response->get_error_message() ); - - $form_data = $this->event_form_handler_factory->future_inactive_event_form_data( 'edit_event', $this->now ); - $response = $this->event_form_handler->process_form( $form_data ); - $this->assertEquals( 'You do not have permissions to edit this event.', $response->get_error_message() ); + $form_data_2 = $this->event_form_handler_factory->future_inactive_event_form_data( 'edit_event', $this->now ); + $response_2 = $this->event_form_handler->process_form( $form_data_2 ); + $this->assertEquals( 'You do not have permissions to edit this event.', $response_2->get_error_message() ); - $form_data = $this->event_form_handler_factory->future_inactive_event_form_data( 'trash_event', $this->now ); - $response = $this->event_form_handler->process_form( $form_data ); - $this->assertEquals( 'You do not have permissions to delete this event.', $response->get_error_message() ); + $form_data_3 = $this->event_form_handler_factory->future_inactive_event_form_data( 'trash_event', $this->now ); + $response_3 = $this->event_form_handler->process_form( $form_data_3 ); + $this->assertEquals( 'You do not have permissions to delete this event.', $response_3->get_error_message() ); } } - From 72ebd90a296383d92fa179c64daf4bf7b8a71337 Mon Sep 17 00:00:00 2001 From: Tosin Oguntuyi Date: Mon, 16 Dec 2024 01:12:15 +0100 Subject: [PATCH 12/19] Test for invalid nonce --- tests/event/event-form-handler.php | 9 +++++++++ 1 file changed, 9 insertions(+) diff --git a/tests/event/event-form-handler.php b/tests/event/event-form-handler.php index 78ce7dbd..888d956c 100644 --- a/tests/event/event-form-handler.php +++ b/tests/event/event-form-handler.php @@ -56,4 +56,13 @@ public function test_invalid_permissions() { $response_3 = $this->event_form_handler->process_form( $form_data_3 ); $this->assertEquals( 'You do not have permissions to delete this event.', $response_3->get_error_message() ); } + + public function test_invalid_nonce() { + add_filter( 'gp_translation_events_can_crud_event', '__return_true' ); + $form_data = $this->event_form_handler_factory->future_inactive_event_form_data( 'create_event', $this->now ); + $form_data['_event_nonce'] = wp_create_nonce( 'invalid_nonce' ); + $response = $this->event_form_handler->process_form( $form_data ); + + $this->assertEquals( 'Nonce verification failed.', $response->get_error_message() ); + } } From 3c7f9ab286f56e5b1118a6879a5f149f58feca6b Mon Sep 17 00:00:00 2001 From: Tosin Oguntuyi Date: Mon, 16 Dec 2024 01:17:28 +0100 Subject: [PATCH 13/19] Assert if array has key for the error --- tests/event/event-form-handler.php | 1 + 1 file changed, 1 insertion(+) diff --git a/tests/event/event-form-handler.php b/tests/event/event-form-handler.php index 888d956c..51ad831f 100644 --- a/tests/event/event-form-handler.php +++ b/tests/event/event-form-handler.php @@ -63,6 +63,7 @@ public function test_invalid_nonce() { $form_data['_event_nonce'] = wp_create_nonce( 'invalid_nonce' ); $response = $this->event_form_handler->process_form( $form_data ); + $this->assertArrayHasKey( 'invalid_nonce', $response->error_data ); $this->assertEquals( 'Nonce verification failed.', $response->get_error_message() ); } } From 6d87a7468f9120b94838eff32c898f8c99673008 Mon Sep 17 00:00:00 2001 From: Tosin Oguntuyi Date: Mon, 16 Dec 2024 01:21:46 +0100 Subject: [PATCH 14/19] Correct timezone in factory --- tests/lib/event-form-handler-factory.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tests/lib/event-form-handler-factory.php b/tests/lib/event-form-handler-factory.php index 7713f702..929d6207 100644 --- a/tests/lib/event-form-handler-factory.php +++ b/tests/lib/event-form-handler-factory.php @@ -8,7 +8,7 @@ class Event_Form_Handler_Factory { public function future_inactive_event_form_data( $form_name, DateTimeImmutable $now, $event_id = 0 ): array { - $timezone = new DateTimeZone( 'Europe/Lisbon' ); + $timezone = 'Europe/Lisbon'; $event_title = 'Foo title'; $event_description = 'Foo description'; $event_attendance_mode = 'hybrid'; From 1c900b4f7343db376eb6fdae2fd5323595437534 Mon Sep 17 00:00:00 2001 From: Tosin Oguntuyi Date: Tue, 17 Dec 2024 01:35:26 +0100 Subject: [PATCH 15/19] Fix date error in form handler factory --- tests/lib/event-form-handler-factory.php | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/tests/lib/event-form-handler-factory.php b/tests/lib/event-form-handler-factory.php index 929d6207..90a54f4f 100644 --- a/tests/lib/event-form-handler-factory.php +++ b/tests/lib/event-form-handler-factory.php @@ -21,8 +21,8 @@ public function future_inactive_event_form_data( $form_name, DateTimeImmutable $ 'event_form_action' => 'publish', 'event_title' => $event_title, 'event_description' => $event_description, - 'event_start' => $now->modify( '+1 month' ), - 'event_end' => $now->modify( '+2 month' ), + 'event_start' => $now->modify( '+1 month' )->format( 'Y-m-d H:i:s' ), + 'event_end' => $now->modify( '+2 month' )->format( 'Y-m-d H:i:s' ), 'event_timezone' => $timezone, 'event_attendance_mode' => $event_attendance_mode, ); From 6289a5a378bf1aa9edc618b04fed176b4d8c4727 Mon Sep 17 00:00:00 2001 From: Tosin Oguntuyi Date: Tue, 17 Dec 2024 02:08:45 +0100 Subject: [PATCH 16/19] Add missing event_id --- tests/event/event-form-handler.php | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/tests/event/event-form-handler.php b/tests/event/event-form-handler.php index 51ad831f..8aec0104 100644 --- a/tests/event/event-form-handler.php +++ b/tests/event/event-form-handler.php @@ -44,15 +44,16 @@ public function test_form_name_is_invalid() { } public function test_invalid_permissions() { + $event_id = 9999; $form_data_1 = $this->event_form_handler_factory->future_inactive_event_form_data( 'create_event', $this->now ); $response_1 = $this->event_form_handler->process_form( $form_data_1 ); $this->assertEquals( 'You do not have permissions to create events.', $response_1->get_error_message() ); - $form_data_2 = $this->event_form_handler_factory->future_inactive_event_form_data( 'edit_event', $this->now ); + $form_data_2 = $this->event_form_handler_factory->future_inactive_event_form_data( 'edit_event', $this->now, $event_id ); $response_2 = $this->event_form_handler->process_form( $form_data_2 ); $this->assertEquals( 'You do not have permissions to edit this event.', $response_2->get_error_message() ); - $form_data_3 = $this->event_form_handler_factory->future_inactive_event_form_data( 'trash_event', $this->now ); + $form_data_3 = $this->event_form_handler_factory->future_inactive_event_form_data( 'trash_event', $this->now, $event_id ); $response_3 = $this->event_form_handler->process_form( $form_data_3 ); $this->assertEquals( 'You do not have permissions to delete this event.', $response_3->get_error_message() ); } From 0ef618fccf1bd609a953601d3a4616eb56348bf4 Mon Sep 17 00:00:00 2001 From: Tosin Oguntuyi Date: Tue, 17 Dec 2024 03:26:35 +0100 Subject: [PATCH 17/19] Add nonce to event form data factory --- tests/lib/event-form-handler-factory.php | 2 ++ 1 file changed, 2 insertions(+) diff --git a/tests/lib/event-form-handler-factory.php b/tests/lib/event-form-handler-factory.php index 90a54f4f..abe38222 100644 --- a/tests/lib/event-form-handler-factory.php +++ b/tests/lib/event-form-handler-factory.php @@ -13,6 +13,7 @@ public function future_inactive_event_form_data( $form_name, DateTimeImmutable $ $event_description = 'Foo description'; $event_attendance_mode = 'hybrid'; $_event_id = isset( $event_id ) ? $event_id : 0; + $event_nonce = wp_create_nonce( '_event_nonce' ); return array( 'action' => 'submit_event_ajax', @@ -25,6 +26,7 @@ public function future_inactive_event_form_data( $form_name, DateTimeImmutable $ 'event_end' => $now->modify( '+2 month' )->format( 'Y-m-d H:i:s' ), 'event_timezone' => $timezone, 'event_attendance_mode' => $event_attendance_mode, + '_event_nonce' => $event_nonce, ); } } From 26050fe8cbde5b176fddc5c588f91cf458b04f41 Mon Sep 17 00:00:00 2001 From: Tosin Oguntuyi Date: Tue, 17 Dec 2024 03:41:27 +0100 Subject: [PATCH 18/19] Return array as response --- includes/event/event-form-handler.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/includes/event/event-form-handler.php b/includes/event/event-form-handler.php index f85111c6..20c5e979 100644 --- a/includes/event/event-form-handler.php +++ b/includes/event/event-form-handler.php @@ -174,7 +174,7 @@ public function process_form( array $form_data ) { $event_status = $new_event->status(); } - wp_send_json_success( + return( array( 'message' => $response_message, 'eventId' => $event_id, From 1371aa60ac1a933c7f2c436bd2eaad23847bb369 Mon Sep 17 00:00:00 2001 From: Tosin Oguntuyi Date: Tue, 17 Dec 2024 14:40:27 +0100 Subject: [PATCH 19/19] Test for empty form fields --- tests/event/event-form-handler.php | 35 +++++++++++++++++++++++++++++- 1 file changed, 34 insertions(+), 1 deletion(-) diff --git a/tests/event/event-form-handler.php b/tests/event/event-form-handler.php index 8aec0104..0519a8b4 100644 --- a/tests/event/event-form-handler.php +++ b/tests/event/event-form-handler.php @@ -44,7 +44,7 @@ public function test_form_name_is_invalid() { } public function test_invalid_permissions() { - $event_id = 9999; + $event_id = 9999; $form_data_1 = $this->event_form_handler_factory->future_inactive_event_form_data( 'create_event', $this->now ); $response_1 = $this->event_form_handler->process_form( $form_data_1 ); $this->assertEquals( 'You do not have permissions to create events.', $response_1->get_error_message() ); @@ -67,4 +67,37 @@ public function test_invalid_nonce() { $this->assertArrayHasKey( 'invalid_nonce', $response->error_data ); $this->assertEquals( 'Nonce verification failed.', $response->get_error_message() ); } + + /** + * @dataProvider emptyFormDataProvider + */ + public function test_empty_form_fields( $action, $field, $error_key ) { + add_filter( 'gp_translation_events_can_crud_event', '__return_true' ); + $form_data = $this->event_form_handler_factory->future_inactive_event_form_data( $action, $this->now ); + $form_data[ $field ] = ''; + $response = $this->event_form_handler->process_form( $form_data ); + + $this->assertArrayHasKey( $error_key, $response->error_data ); + } + + /** + * Data provider for invalid form data test cases. + */ + public function emptyFormDataProvider() { + return array( + array( 'create_event', 'event_title', 'invalid_title' ), + array( 'create_event', 'event_description', 'invalid_title' ), + array( 'create_event', 'event_start', 'invalid_start_date' ), + array( 'create_event', 'event_end', 'invalid_end_date' ), + array( 'create_event', 'event_timezone', 'invalid_timezone' ), + array( 'create_event', 'event_attendance_mode', 'invalid event_attendance_mode' ), + array( 'edit_event', 'event_title', 'invalid_title' ), + array( 'edit_event', 'event_description', 'invalid_title' ), + array( 'edit_event', 'event_start', 'invalid_start_date' ), + array( 'edit_event', 'event_end', 'invalid_end_date' ), + array( 'edit_event', 'event_timezone', 'invalid_timezone' ), + array( 'edit_event', 'event_attendance_mode', 'invalid event_attendance_mode' ), + ); + } + }