From b7cd7f247c8c60a7aced4731e7c819fc2b5df78c Mon Sep 17 00:00:00 2001 From: Mike Auteri Date: Fri, 20 Sep 2024 14:00:05 -0700 Subject: [PATCH] Added new unit tests, small code changes. --- includes/core/classes/class-venue.php | 4 +++- .../includes/core/classes/class-test-rsvp.php | 19 +++++++++++++++++++ .../core/classes/class-test-venue.php | 15 +++++++++++++++ 3 files changed, 37 insertions(+), 1 deletion(-) diff --git a/includes/core/classes/class-venue.php b/includes/core/classes/class-venue.php index cf45b0553..7022e287f 100644 --- a/includes/core/classes/class-venue.php +++ b/includes/core/classes/class-venue.php @@ -158,10 +158,12 @@ public function register_post_type(): void { public static function get_localized_post_type_slug(): string { $switched_locale = switch_to_locale( get_locale() ); $slug = _x( 'venue', 'Post Type Slug', 'gatherpress' ); - $slug = sanitize_title( $slug, '', 'save' ); + $slug = sanitize_title( $slug ); + if ( $switched_locale ) { restore_previous_locale(); } + return $slug; } diff --git a/test/unit/php/includes/core/classes/class-test-rsvp.php b/test/unit/php/includes/core/classes/class-test-rsvp.php index f94191463..4342dd61e 100644 --- a/test/unit/php/includes/core/classes/class-test-rsvp.php +++ b/test/unit/php/includes/core/classes/class-test-rsvp.php @@ -11,6 +11,7 @@ use GatherPress\Core\Rsvp; use PMC\Unit_Test\Base; use PMC\Unit_Test\Utility; +use WP_Error; /** * Class Test_Rsvp. @@ -109,6 +110,24 @@ public function test_save(): void { $rsvp = new Rsvp( $post->ID ); $user_1_id = $this->factory->user->create(); $this->assertSame( 2, $rsvp->save( $user_1_id, 'attending', 0, 3 )['guests'], 'Failed to assert that user 1 can only bring 2 guests at most.' ); + + // Simulate error saving RSVP. + add_filter( 'query', '__return_false' ); + + $user_id = $this->factory->user->create(); + $result = $rsvp->save( $user_id, 'attending' ); + $expected = array( + 'post_id' => 0, + 'user_id' => 0, + 'timestamp' => '0000-00-00 00:00:00', + 'status' => 'no_status', + 'guests' => 0, + 'anonymous' => 0, + ); + + $this->assertEquals( $expected, $result ); + + remove_filter( 'query', '__return_false' ); } /** diff --git a/test/unit/php/includes/core/classes/class-test-venue.php b/test/unit/php/includes/core/classes/class-test-venue.php index f2af3a07d..3b14905b0 100644 --- a/test/unit/php/includes/core/classes/class-test-venue.php +++ b/test/unit/php/includes/core/classes/class-test-venue.php @@ -89,6 +89,21 @@ public function test_register_post_type(): void { $this->assertTrue( post_type_exists( Venue::POST_TYPE ), 'Failed to assert that post type exists.' ); } + /** + * Coverage for get_localized_post_type_slug method. + * + * @covers ::get_localized_post_type_slug + * + * @return void + */ + public function test_get_localized_post_type_slug(): void { + $this->assertSame( + 'venue', + Venue::get_localized_post_type_slug(), + 'Failed to assert that post type slug is same.' + ); + } + /** * Coverage for register_post_meta method. *