-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #99 from WordPress/tests-stats
Tests for stats listener
- Loading branch information
Showing
13 changed files
with
393 additions
and
36 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,94 @@ | ||
<?php | ||
|
||
namespace Wporg\TranslationEvents\Tests; | ||
|
||
use DateTimeImmutable; | ||
use DateTimeZone; | ||
use WP_UnitTest_Factory_For_Post; | ||
use WP_UnitTest_Generator_Sequence; | ||
use Wporg\TranslationEvents\Route; | ||
|
||
class Event_Factory extends WP_UnitTest_Factory_For_Post { | ||
public function __construct( $factory = null ) { | ||
parent::__construct( $factory ); | ||
$this->default_generation_definitions = array( | ||
'post_status' => 'publish', | ||
'post_title' => new WP_UnitTest_Generator_Sequence( 'Event title %s' ), | ||
'post_content' => new WP_UnitTest_Generator_Sequence( 'Event content %s' ), | ||
'post_excerpt' => new WP_UnitTest_Generator_Sequence( 'Event excerpt %s' ), | ||
'post_type' => 'event', | ||
); | ||
} | ||
|
||
public function create_draft(): int { | ||
$now = new DateTimeImmutable( 'now', new DateTimeZone( 'UTC' ) ); | ||
|
||
$event_id = $this->create_event( | ||
$now->modify( '-1 hours' ), | ||
$now->modify( '+1 hours' ), | ||
array(), | ||
); | ||
|
||
$event = get_post( $event_id ); | ||
$event->post_status = 'draft'; | ||
wp_update_post( $event ); | ||
|
||
return $event_id; | ||
} | ||
|
||
public function create_active( array $attendee_ids = array() ): int { | ||
$now = new DateTimeImmutable( 'now', new DateTimeZone( 'UTC' ) ); | ||
|
||
return $this->create_event( | ||
$now->modify( '-1 hours' ), | ||
$now->modify( '+1 hours' ), | ||
$attendee_ids, | ||
); | ||
} | ||
|
||
public function create_inactive_past( array $attendee_ids = array() ): int { | ||
$now = new DateTimeImmutable( 'now', new DateTimeZone( 'UTC' ) ); | ||
|
||
return $this->create_event( | ||
$now->modify( '-2 hours' ), | ||
$now->modify( '-1 hours' ), | ||
$attendee_ids, | ||
); | ||
} | ||
|
||
public function create_inactive_future( array $attendee_ids = array() ): int { | ||
$now = new DateTimeImmutable( 'now', new DateTimeZone( 'UTC' ) ); | ||
|
||
return $this->create_event( | ||
$now->modify( '+1 hours' ), | ||
$now->modify( '+2 hours' ), | ||
$attendee_ids, | ||
); | ||
} | ||
|
||
private function create_event( DateTimeImmutable $start, DateTimeImmutable $end, array $attendee_ids ): int { | ||
$event_id = $this->create(); | ||
$meta_key = Route::USER_META_KEY_ATTENDING; | ||
|
||
$user_id = get_current_user_id(); | ||
if ( ! in_array( $user_id, $attendee_ids, true ) ) { | ||
// The current user will have been added as attending the event, but it was not specified as an attendee by | ||
// the caller of this function. So we remove the current user as attendee. | ||
$event_ids = get_user_meta( $user_id, $meta_key, true ); | ||
unset( $event_ids[ $event_id ] ); | ||
update_user_meta( $user_id, $meta_key, array() ); | ||
} | ||
|
||
update_post_meta( $event_id, '_event_start', $start->format( 'Y-m-d H:i:s' ) ); | ||
update_post_meta( $event_id, '_event_end', $end->format( 'Y-m-d H:i:s' ) ); | ||
update_post_meta( $event_id, '_event_timezone', 'Europe/Lisbon' ); | ||
|
||
foreach ( $attendee_ids as $user_id ) { | ||
$event_ids = get_user_meta( $user_id, $meta_key, true ) ?: array(); | ||
$event_ids[] = $event_id; | ||
update_user_meta( $user_id, $meta_key, $event_ids ); | ||
} | ||
|
||
return $event_id; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
<?php | ||
|
||
namespace Wporg\TranslationEvents\Tests; | ||
|
||
use GP_UnitTest_Factory; | ||
|
||
class Translation_Factory { | ||
private GP_UnitTest_Factory $gp_factory; | ||
private $set; | ||
|
||
public function __construct( GP_UnitTest_Factory $gp_factory ) { | ||
$this->gp_factory = $gp_factory; | ||
$this->set = $this->gp_factory->translation_set->create_with_project_and_locale(); | ||
} | ||
|
||
public function create( int $user_id ) { | ||
$original = $this->gp_factory->original->create( | ||
array( | ||
'project_id' => $this->set->project->id, | ||
'status' => '+active', | ||
'singular' => 'foo', | ||
) | ||
); | ||
|
||
return $this->gp_factory->translation->create( | ||
array( | ||
'user_id' => $user_id, | ||
'translation_set_id' => $this->set->id, | ||
'original_id' => $original->id, | ||
'status' => 'waiting', | ||
) | ||
); | ||
} | ||
} |
Oops, something went wrong.