Skip to content

Commit

Permalink
Merge pull request #598 from GatherPress/mauteri-cleanup-1
Browse files Browse the repository at this point in the history
Code clean up and new tests.
  • Loading branch information
mauteri authored Mar 10, 2024
2 parents 916cc14 + 41ba120 commit feefbfe
Show file tree
Hide file tree
Showing 8 changed files with 107 additions and 27 deletions.
8 changes: 4 additions & 4 deletions includes/core/classes/class-cli.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,8 @@

namespace GatherPress\Core;

use GatherPress\Core\Commands\Cli_Event;
use GatherPress\Core\Commands\Cli_General;
use GatherPress\Core\Commands\Event_Cli;
use GatherPress\Core\Commands\Develop_Cli;
use GatherPress\Core\Traits\Singleton;
use WP_CLI;

Expand Down Expand Up @@ -41,8 +41,8 @@ class Cli {
*/
protected function __construct() {
if ( defined( 'WP_CLI' ) && WP_CLI ) {
WP_CLI::add_command( 'gatherpress', Cli_General::class );
WP_CLI::add_command( 'gatherpress event', Cli_Event::class );
WP_CLI::add_command( 'gatherpress develop', Develop_Cli::class );
WP_CLI::add_command( 'gatherpress event', Event_Cli::class );
}
}
}
22 changes: 11 additions & 11 deletions includes/core/classes/class-utility.php
Original file line number Diff line number Diff line change
Expand Up @@ -234,24 +234,24 @@ public static function maybe_convert_utc_offset( string $timezone ): string {
* @return string The timezone string representing the system's default timezone. Falls back to a UTC offset representation if a named timezone string is not set.
*/
public static function get_system_timezone(): string {
$current_offset = get_option( 'gmt_offset' );
$tzstring = get_option( 'timezone_string' );
$gmt_offset = intval( get_option( 'gmt_offset' ) );
$timezone_string = get_option( 'timezone_string' );

// Remove old Etc mappings. Fallback to gmt_offset.
if ( str_contains( $tzstring, 'Etc/GMT' ) ) {
$tzstring = '';
if ( str_contains( $timezone_string, 'Etc/GMT' ) ) {
$timezone_string = '';
}

if ( empty( $tzstring ) ) { // Create a UTC+- zone if no timezone string exists.
if ( 0 === $current_offset ) {
$tzstring = 'UTC+0';
} elseif ( $current_offset < 0 ) {
$tzstring = 'UTC' . $current_offset;
if ( empty( $timezone_string ) ) { // Create a UTC+- zone if no timezone string exists.
if ( 0 === $gmt_offset ) {
$timezone_string = 'UTC+0';
} elseif ( $gmt_offset < 0 ) {
$timezone_string = 'UTC' . $gmt_offset;
} else {
$tzstring = 'UTC+' . $current_offset;
$timezone_string = 'UTC+' . $gmt_offset;
}
}

return $tzstring;
return $timezone_string;
}
}
2 changes: 1 addition & 1 deletion includes/core/classes/class-venue.php
Original file line number Diff line number Diff line change
Expand Up @@ -152,7 +152,7 @@ public function register_post_meta(): void {
$post_meta = array(
'venue_information' => array(
'auth_callback' => static function () {
return current_user_can( 'edit_posts' );
return current_user_can( 'edit_posts' ); // @codeCoverageIgnore
},
'sanitize_callback' => 'sanitize_text_field',
'show_in_rest' => true,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@
*
* @since 1.0.0
*/
class Cli_General extends WP_CLI {
class Develop_Cli extends WP_CLI {
/**
* Generate credits data for the credits page.
*
Expand All @@ -36,7 +36,7 @@ class Cli_General extends WP_CLI {
* ## EXAMPLES
*
* # Generate credits.
* $ wp gatherpress generate_credits --version=1.0.0
* $ wp gatherpress develop generate_credits --version=1.0.0
* Success: New latest.php file has been generated.
*
* @codeCoverageIgnore Command is for internal purposes only.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@
* @package GatherPress\Core
* @since 1.0.0
*/
class Cli_Event extends WP_CLI {
class Event_Cli extends WP_CLI {
/**
* Update RSVP status for an event.
*
Expand Down Expand Up @@ -62,9 +62,9 @@ class Cli_Event extends WP_CLI {
public function rsvp( array $args = array(), array $assoc_args = array() ): void {
$event_id = (int) $assoc_args['event_id'];
$user_id = (int) $assoc_args['user_id'];
$guests = (int) $assoc_args['guests'] ?? 0;
$anonymous = (int) $assoc_args['anonymous'] ?? 0;
$status = (string) $assoc_args['status'] ?? 'attending';
$guests = ! empty( $assoc_args['guests'] ) ? (int) $assoc_args['guests'] : 0;
$anonymous = ! empty( $assoc_args['anonymous'] ) ? (int) $assoc_args['anonymous'] : 0;
$status = ! empty( $assoc_args['status'] ) ? (string) $assoc_args['status'] : 'attending';
$event = new Event( $event_id );
$response = $event->rsvp->save( $user_id, $status, $anonymous, $guests );

Expand Down
12 changes: 12 additions & 0 deletions test/unit/php/includes/core/classes/class-test-rsvp.php
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,18 @@ public function test_save(): void {
$user_2_id = $this->factory->user->create();

$this->assertSame( 'no_status', $rsvp->save( $user_2_id, 'no_status' )['status'], 'Failed to assert that user 2 is no_status.' );

$post = $this->mock->post(
array(
'post_type' => 'gp_event',
'post_meta' => array(
'max_guest_limit' => 2,
),
)
)->get();
$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.' );
}

/**
Expand Down
68 changes: 68 additions & 0 deletions test/unit/php/includes/core/classes/class-test-utility.php
Original file line number Diff line number Diff line change
Expand Up @@ -183,4 +183,72 @@ public function test_list_timezone_and_utc_offsets(): void {
$this->assertContains( $timezone, $list, 'Failed to assert timezone is in list.' );
}
}

/**
* Data provider for get_system_timezone test.
*
* @return array
*/
public function data_get_system_timezone(): array {
return array(
array(
false,
false,
'UTC+0',
),
array(
5,
false,
'UTC+5',
),
array(
-4,
false,
'UTC-4',
),
array(
false,
'Europe/London',
'Europe/London',
),
array(
false,
'Etc/GMT+3',
'UTC-3',
),
);
}

/**
* Coverage for get_system_timezone method.
*
* @dataProvider data_get_system_timezone
*
* @covers ::get_system_timezone
*
* @param int|boolean $gmt_offset The GMT offset to simulate getting from WordPress settings for testing.
* @param string|boolean $timezone_string The timezone string to simulate getting from WordPress settings for testing.
* @param string $expects The expected timezone string result from get_system_timezone.
*
* @return void
*/
public function test_get_system_timezone( $gmt_offset, $timezone_string, $expects ): void {
$gmt_offset_filter = add_filter(
'option_gmt_offset',
static function () use ( $gmt_offset ) {
return $gmt_offset;
}
);
$timezone_string_filter = add_filter(
'option_timezone_string',
static function () use ( $timezone_string ) {
return $timezone_string;
}
);

$this->assertSame( $expects, Utility::get_system_timezone() );

remove_filter( 'option_gmt_offset', $gmt_offset_filter );
remove_filter( 'option_timezone_string', $timezone_string_filter );
}
}
Original file line number Diff line number Diff line change
@@ -1,24 +1,24 @@
<?php
/**
* Class handles unit tests for GatherPress\Core\Commands\Cli_Event.
* Class handles unit tests for GatherPress\Core\Commands\Event_Cli.
*
* @package GatherPress\Core
* @since 1.0.0
*/

namespace GatherPress\Tests\Core\Commands;

use GatherPress\Core\Commands\Cli_Event;
use GatherPress\Core\Commands\Event_Cli;
use GatherPress\Core\Event;
use PMC\Unit_Test\Base;
use PMC\Unit_Test\Utility;

/**
* Class Test_Block.
*
* @coversDefaultClass \GatherPress\Core\Commands\Cli_Event
* @coversDefaultClass \GatherPress\Core\Commands\Event_Cli
*/
class Test_Cli_Event extends Base {
class Test_Event_Cli extends Base {
/**
* Coverage for rsvp.
*
Expand All @@ -27,7 +27,7 @@ class Test_Cli_Event extends Base {
* @return void
*/
public function test_rsvp(): void {
$cli_event = new Cli_Event();
$cli_event = new Event_Cli();
$event = $this->mock->post( array( 'post_type' => Event::POST_TYPE ) )->get();
$user = $this->mock->user()->get();
$status = 'not_attending';
Expand Down

0 comments on commit feefbfe

Please sign in to comment.