Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Tests for stats listener #99

Merged
merged 34 commits into from
Feb 23, 2024
Merged
Show file tree
Hide file tree
Changes from 31 commits
Commits
Show all changes
34 commits
Select commit Hold shift + click to select a range
d882dd1
Rename class
psrpinto Feb 19, 2024
1558784
Add stats listener test
psrpinto Feb 19, 2024
e227a53
Load GlotPress when running tests
psrpinto Feb 19, 2024
c690a5c
Add event factory
psrpinto Feb 20, 2024
3c3f3b2
Create translation
psrpinto Feb 20, 2024
d05639f
Use latest stable WordPress in wp-env
psrpinto Feb 21, 2024
15b634b
Make tests run locally
psrpinto Feb 21, 2024
49af368
Explain why tests fail
psrpinto Feb 21, 2024
27c3c04
Clone GlotPress in CI
psrpinto Feb 21, 2024
b186c4f
Make tests run on CI
psrpinto Feb 22, 2024
3808e0f
Use same php version on CI and wp-env
psrpinto Feb 22, 2024
4bf762f
Declare property
psrpinto Feb 22, 2024
9f6c7da
Create translation events table
psrpinto Feb 22, 2024
f52ab80
Create active event
psrpinto Feb 22, 2024
e600143
Set user as attending
psrpinto Feb 22, 2024
94f48eb
Create active event
psrpinto Feb 22, 2024
98ff1fc
Add test stubs
psrpinto Feb 22, 2024
0362226
Extend GP_UnitTestCase instead of GP_UnitTestCase_Route
psrpinto Feb 22, 2024
c7d3813
Add translation factory
psrpinto Feb 22, 2024
803cea0
Add function to get stats
psrpinto Feb 22, 2024
e775447
Add create_event()
psrpinto Feb 22, 2024
a084cda
Add functions to create inactive events
psrpinto Feb 22, 2024
266c574
Test stats values
psrpinto Feb 22, 2024
98a2b8d
Test with two active events
psrpinto Feb 22, 2024
a61d18a
test_does_not_store_action_for_inactive_events()
psrpinto Feb 22, 2024
4853685
test_does_not_store_action_if_user_not_attending()
psrpinto Feb 22, 2024
c9bb56b
Don't add user as attendee if not requested
psrpinto Feb 22, 2024
208faa4
test_does_not_store_action_for_draft_events()
psrpinto Feb 22, 2024
d94eeb5
test_stores_action_approve()
psrpinto Feb 22, 2024
9166594
test_stores_action_reject()
psrpinto Feb 22, 2024
2193fae
test_stores_action_request_changes()
psrpinto Feb 22, 2024
f86cab8
No need to install GlotPress
psrpinto Feb 23, 2024
16b424d
Apply schema in bootstrap.php
psrpinto Feb 23, 2024
4d6a81e
No longer need the run-tests.sh script
psrpinto Feb 23, 2024
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions .github/workflows/tests.yml
Original file line number Diff line number Diff line change
Expand Up @@ -46,4 +46,6 @@ jobs:
run: echo "::add-matcher::${{ runner.tool_cache }}/phpunit.json"

- name: Run tests
env:
WP_TESTS_DIR: /tmp/wordpress-tests-lib/
run: vendor/bin/phpunit
5 changes: 3 additions & 2 deletions .wp-env.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
{
"core": "WordPress/WordPress#master",
"core": null,
"phpVersion": "8.3",
"plugins": [
"GlotPress/GlotPress#local",
"GlotPress/GlotPress",
"https://downloads.wordpress.org/plugin/sqlite-object-cache.1.3.7.zip",
"."
],
Expand Down
8 changes: 8 additions & 0 deletions bin/install-wp-tests.sh
Original file line number Diff line number Diff line change
Expand Up @@ -174,8 +174,16 @@ install_db() {
else
create_db
fi

# Create translation events table.
mysql --user="$DB_USER" --password="$DB_PASS" -D $DB_NAME$EXTRA < schema.sql
}

install_gp() {
git clone --branch develop --single-branch -q https://github.com/GlotPress/GlotPress.git "$WP_CORE_DIR/wp-content/plugins/glotpress"
}

install_wp
install_test_suite
install_db
install_gp
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
"scripts":{
"lint": "phpcs --standard=phpcs.xml -s",
"lint:fix": "phpcbf --standard=phpcs.xml",
"dev:start": "wp-env start --debug && wp-env run cli wp rewrite structure '/%postname%/'",
"dev:start": "wp-env start && wp-env run cli wp rewrite structure '/%postname%/'",
"dev:debug": "wp-env start --xdebug",
"dev:stop": "wp-env stop",
"dev:db:schema": "wp-env run cli --env-cwd=wp-content/plugins/wporg-gp-translation-events sh -c 'wp db query < schema.sql'",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
use GP_Translation;
use GP_Translation_Set;

class Translation_Listener {
class Stats_Listener {
const ACTION_CREATE = 'create';
const ACTION_APPROVE = 'approve';
const ACTION_REJECT = 'reject';
Expand Down
4 changes: 2 additions & 2 deletions phpunit.xml.dist
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,8 @@
convertWarningsToExceptions="true"
>
<testsuites>
<testsuite name="unit">
<directory suffix=".php">./tests/unit/</directory>
<testsuite name="tests">
<directory suffix=".php">./tests/</directory>
</testsuite>
</testsuites>
</phpunit>
28 changes: 26 additions & 2 deletions tests/bootstrap.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,14 @@
$_tests_dir = rtrim( sys_get_temp_dir(), '/\\' ) . '/wordpress-tests-lib';
}

function _glotpress_path( string $path ): string {
$glotpress_path = dirname( __DIR__, 2 ) . '/glotpress/';
if ( getenv( 'GITHUB_ACTIONS' ) ) {
$glotpress_path = '/tmp/wordpress/wp-content/plugins/glotpress/';
}
return $glotpress_path . $path;
}

// Forward custom PHPUnit Polyfills configuration to PHPUnit bootstrap file.
$_phpunit_polyfills_path = getenv( 'WP_TESTS_PHPUNIT_POLYFILLS_PATH' );
if ( false !== $_phpunit_polyfills_path ) {
Expand All @@ -23,10 +31,26 @@
* Manually load the plugin being tested.
*/
function _manually_load_plugin() {
require dirname( __DIR__ ) . '/wporg-gp-translation-events.php';
require_once _glotpress_path( '/tests/phpunit/includes/loader.php' );
require_once dirname( __DIR__ ) . '/wporg-gp-translation-events.php';
}

tests_add_filter( 'muplugins_loaded', '_manually_load_plugin' );

global $wp_tests_options;
$wp_tests_options['permalink_structure'] = '/%postname%';

// Start up the WP testing environment.
require "$_tests_dir/includes/bootstrap.php";

// Require GlotPress test code.
require_once _glotpress_path( '/tests/phpunit/lib/testcase.php' );
require_once _glotpress_path( '/tests/phpunit/lib/testcase-route.php' );
require_once _glotpress_path( '/tests/phpunit/lib/testcase-request.php' );

function _install_glotpress() {
require_once ABSPATH . 'wp-admin/includes/upgrade.php';
require_once _glotpress_path( '/gp-includes/schema.php' );
require_once _glotpress_path( '/gp-includes/install-upgrade.php' );
gp_upgrade_db();
psrpinto marked this conversation as resolved.
Show resolved Hide resolved
}
_install_glotpress();
94 changes: 94 additions & 0 deletions tests/lib/event-factory.php
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;
}
}
34 changes: 34 additions & 0 deletions tests/lib/translation-factory.php
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',
)
);
}
}
Loading
Loading