From d882dd138441b32aad91597cd800c282f0e6ad40 Mon Sep 17 00:00:00 2001 From: Paulo Pinto Date: Mon, 19 Feb 2024 13:01:45 +0000 Subject: [PATCH 01/34] Rename class --- includes/{translation-listener.php => stats-listener.php} | 2 +- wporg-gp-translation-events.php | 8 ++++---- 2 files changed, 5 insertions(+), 5 deletions(-) rename includes/{translation-listener.php => stats-listener.php} (99%) diff --git a/includes/translation-listener.php b/includes/stats-listener.php similarity index 99% rename from includes/translation-listener.php rename to includes/stats-listener.php index 011e4389..2eb1af9d 100644 --- a/includes/translation-listener.php +++ b/includes/stats-listener.php @@ -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'; diff --git a/wporg-gp-translation-events.php b/wporg-gp-translation-events.php index d566fc47..698c72fa 100644 --- a/wporg-gp-translation-events.php +++ b/wporg-gp-translation-events.php @@ -392,7 +392,7 @@ function () { require_once __DIR__ . '/includes/event.php'; require_once __DIR__ . '/includes/route.php'; require_once __DIR__ . '/includes/stats-calculator.php'; - require_once __DIR__ . '/includes/translation-listener.php'; + require_once __DIR__ . '/includes/stats-listener.php'; GP::$router->add( '/events?', array( 'Wporg\TranslationEvents\Route', 'events_list' ) ); GP::$router->add( '/events/new', array( 'Wporg\TranslationEvents\Route', 'events_create' ) ); @@ -401,9 +401,9 @@ function () { GP::$router->add( '/events/my-events', array( 'Wporg\TranslationEvents\Route', 'events_user_created' ) ); GP::$router->add( '/events/([a-z0-9_-]+)', array( 'Wporg\TranslationEvents\Route', 'events_details' ) ); - $active_events_cache = new Active_Events_Cache(); - $wporg_gp_translation_events_listener = new Translation_Listener( $active_events_cache ); - $wporg_gp_translation_events_listener->start(); + $active_events_cache = new Active_Events_Cache(); + $stats_listener = new Stats_Listener( $active_events_cache ); + $stats_listener->start(); } ); From 1558784805d577e2a4c53db7eeb40b9efc04898f Mon Sep 17 00:00:00 2001 From: Paulo Pinto Date: Mon, 19 Feb 2024 14:26:26 +0000 Subject: [PATCH 02/34] Add stats listener test --- phpunit.xml.dist | 4 ++-- tests/stats-listener.php | 11 +++++++++++ tests/unit/dummy.php | 14 -------------- 3 files changed, 13 insertions(+), 16 deletions(-) create mode 100644 tests/stats-listener.php delete mode 100644 tests/unit/dummy.php diff --git a/phpunit.xml.dist b/phpunit.xml.dist index ac7184ac..e324c36e 100644 --- a/phpunit.xml.dist +++ b/phpunit.xml.dist @@ -8,8 +8,8 @@ convertWarningsToExceptions="true" > - - ./tests/unit/ + + ./tests/ diff --git a/tests/stats-listener.php b/tests/stats-listener.php new file mode 100644 index 00000000..ed2571da --- /dev/null +++ b/tests/stats-listener.php @@ -0,0 +1,11 @@ +assertTrue( true ); + } +} diff --git a/tests/unit/dummy.php b/tests/unit/dummy.php deleted file mode 100644 index 671fbf68..00000000 --- a/tests/unit/dummy.php +++ /dev/null @@ -1,14 +0,0 @@ -assertTrue( true ); - } -} From e227a5375e6e8f639c7b2706f9e5d820895faeaa Mon Sep 17 00:00:00 2001 From: Paulo Pinto Date: Mon, 19 Feb 2024 14:44:36 +0000 Subject: [PATCH 03/34] Load GlotPress when running tests --- .wp-env.json | 2 +- composer.json | 2 +- tests/bootstrap.php | 16 +++++++++++++++- 3 files changed, 17 insertions(+), 3 deletions(-) diff --git a/.wp-env.json b/.wp-env.json index 2bf44411..0a560554 100644 --- a/.wp-env.json +++ b/.wp-env.json @@ -1,7 +1,7 @@ { "core": "WordPress/WordPress#master", "plugins": [ - "GlotPress/GlotPress#local", + "GlotPress/GlotPress", "https://downloads.wordpress.org/plugin/sqlite-object-cache.1.3.7.zip", "." ], diff --git a/composer.json b/composer.json index b6a3b1f9..06b4954b 100644 --- a/composer.json +++ b/composer.json @@ -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'", diff --git a/tests/bootstrap.php b/tests/bootstrap.php index b0a0c0a0..8e45c514 100644 --- a/tests/bootstrap.php +++ b/tests/bootstrap.php @@ -23,10 +23,24 @@ * Manually load the plugin being tested. */ function _manually_load_plugin() { - require dirname( __DIR__ ) . '/wporg-gp-translation-events.php'; + require_once dirname( __DIR__, 2 ) . '/glotpress/tests/phpunit/includes/loader.php'; + require_once dirname( __DIR__ ) . '/wporg-gp-translation-events.php'; } tests_add_filter( 'muplugins_loaded', '_manually_load_plugin' ); // Start up the WP testing environment. require "$_tests_dir/includes/bootstrap.php"; + +// Require GlotPress test code. +require_once dirname( __DIR__, 2 ) . '/glotpress/tests/phpunit/lib/testcase.php'; +require_once dirname( __DIR__, 2 ) . '/glotpress/tests/phpunit/lib/testcase-route.php'; +require_once dirname( __DIR__, 2 ) . '/glotpress/tests/phpunit/lib/testcase-request.php'; + +function _install_glotpress() { + require_once ABSPATH . 'wp-admin/includes/upgrade.php'; + require_once dirname( __DIR__, 2 ) . '/glotpress/gp-includes/schema.php'; + require_once dirname( __DIR__, 2 ) . '/glotpress/gp-includes/install-upgrade.php'; + gp_upgrade_db(); +} +_install_glotpress(); From c690a5c4a9b2240790f4e9d961ee2707975b9ff3 Mon Sep 17 00:00:00 2001 From: Paulo Pinto Date: Tue, 20 Feb 2024 12:31:00 +0000 Subject: [PATCH 04/34] Add event factory --- tests/lib/event-factory.php | 19 +++++++++++++++++++ tests/stats-listener.php | 14 +++++++++++--- 2 files changed, 30 insertions(+), 3 deletions(-) create mode 100644 tests/lib/event-factory.php diff --git a/tests/lib/event-factory.php b/tests/lib/event-factory.php new file mode 100644 index 00000000..65e87f31 --- /dev/null +++ b/tests/lib/event-factory.php @@ -0,0 +1,19 @@ +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', + ); + } +} diff --git a/tests/stats-listener.php b/tests/stats-listener.php index ed2571da..16d733f3 100644 --- a/tests/stats-listener.php +++ b/tests/stats-listener.php @@ -1,10 +1,18 @@ event_factory = new Event_Factory(); + } -class Stats_Listener_Test extends WP_Ajax_UnitTestCase { public function test_stores_translation_created() { $this->assertTrue( true ); } From 3c3f3b262064cb6073eb7f862a364fcf2c715e25 Mon Sep 17 00:00:00 2001 From: Paulo Pinto Date: Tue, 20 Feb 2024 12:40:29 +0000 Subject: [PATCH 05/34] Create translation --- tests/stats-listener.php | 32 +++++++++++++++++++++++++++++++- 1 file changed, 31 insertions(+), 1 deletion(-) diff --git a/tests/stats-listener.php b/tests/stats-listener.php index 16d733f3..841af288 100644 --- a/tests/stats-listener.php +++ b/tests/stats-listener.php @@ -14,6 +14,36 @@ public function setUp(): void { } public function test_stores_translation_created() { - $this->assertTrue( true ); + $this->event_factory->create(); + + $this->set_normal_user_as_current(); + $user_id = wp_get_current_user()->ID; + + $set = $this->factory->translation_set->create_with_project_and_locale(); + $original = $this->factory->original->create( + array( + 'project_id' => $set->project->id, + 'status' => '+active', + 'singular' => 'foo', + ) + ); + + $translation = $this->factory->translation->create( + array( + 'user_id' => $user_id, + 'translation_set_id' => $set->id, + 'original_id' => $original->id, + 'status' => 'waiting', + ) + ); + + global $wpdb; + // phpcs:disable WordPress.DB.DirectDatabaseQuery.DirectQuery + // phpcs:disable WordPress.DB.DirectDatabaseQuery.NoCaching + $rows = $wpdb->get_results( 'select * from wp_wporg_gp_translation_events_actions' ); + // phpcs:enable + + // TODO. + $this->assertNotEmpty( $rows ); } } From d05639f7b478a2fbd9c0979e99d7fa070584595b Mon Sep 17 00:00:00 2001 From: Paulo Pinto Date: Wed, 21 Feb 2024 15:59:08 +0000 Subject: [PATCH 06/34] Use latest stable WordPress in wp-env --- .wp-env.json | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.wp-env.json b/.wp-env.json index 0a560554..3d442b68 100644 --- a/.wp-env.json +++ b/.wp-env.json @@ -1,5 +1,5 @@ { - "core": "WordPress/WordPress#master", + "core": null, "plugins": [ "GlotPress/GlotPress", "https://downloads.wordpress.org/plugin/sqlite-object-cache.1.3.7.zip", From 15b634bd572fb21a4522b286e3bd4c9009419131 Mon Sep 17 00:00:00 2001 From: Paulo Pinto Date: Wed, 21 Feb 2024 17:45:10 +0000 Subject: [PATCH 07/34] Make tests run locally --- tests/bootstrap.php | 20 +++++++++++++------- 1 file changed, 13 insertions(+), 7 deletions(-) diff --git a/tests/bootstrap.php b/tests/bootstrap.php index 8e45c514..d3d057d2 100644 --- a/tests/bootstrap.php +++ b/tests/bootstrap.php @@ -5,6 +5,10 @@ $_tests_dir = rtrim( sys_get_temp_dir(), '/\\' ) . '/wordpress-tests-lib'; } +function _glotpress_path( string $path ): string { + return dirname( __DIR__, 2 ) . '/glotpress/' . $path; +} + // Forward custom PHPUnit Polyfills configuration to PHPUnit bootstrap file. $_phpunit_polyfills_path = getenv( 'WP_TESTS_PHPUNIT_POLYFILLS_PATH' ); if ( false !== $_phpunit_polyfills_path ) { @@ -23,24 +27,26 @@ * Manually load the plugin being tested. */ function _manually_load_plugin() { - require_once dirname( __DIR__, 2 ) . '/glotpress/tests/phpunit/includes/loader.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 dirname( __DIR__, 2 ) . '/glotpress/tests/phpunit/lib/testcase.php'; -require_once dirname( __DIR__, 2 ) . '/glotpress/tests/phpunit/lib/testcase-route.php'; -require_once dirname( __DIR__, 2 ) . '/glotpress/tests/phpunit/lib/testcase-request.php'; +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 dirname( __DIR__, 2 ) . '/glotpress/gp-includes/schema.php'; - require_once dirname( __DIR__, 2 ) . '/glotpress/gp-includes/install-upgrade.php'; + require_once _glotpress_path( '/gp-includes/schema.php' ); + require_once _glotpress_path( '/gp-includes/install-upgrade.php' ); gp_upgrade_db(); } _install_glotpress(); From 49af368dafdda2026c0b4a9c8e915d315718f8bd Mon Sep 17 00:00:00 2001 From: Paulo Pinto Date: Wed, 21 Feb 2024 17:51:55 +0000 Subject: [PATCH 08/34] Explain why tests fail --- tests/stats-listener.php | 1 + 1 file changed, 1 insertion(+) diff --git a/tests/stats-listener.php b/tests/stats-listener.php index 841af288..63b89195 100644 --- a/tests/stats-listener.php +++ b/tests/stats-listener.php @@ -14,6 +14,7 @@ public function setUp(): void { } public function test_stores_translation_created() { + // TODO: must create an active event. This test currently fails because there are no active events. $this->event_factory->create(); $this->set_normal_user_as_current(); From 27c3c0430a98744c3164275fa8f8cb985fd7ce05 Mon Sep 17 00:00:00 2001 From: Paulo Pinto Date: Wed, 21 Feb 2024 18:04:30 +0000 Subject: [PATCH 09/34] Clone GlotPress in CI --- bin/install-wp-tests.sh | 5 +++++ tests/bootstrap.php | 6 +++++- 2 files changed, 10 insertions(+), 1 deletion(-) diff --git a/bin/install-wp-tests.sh b/bin/install-wp-tests.sh index c6f53dc5..c6417b0d 100755 --- a/bin/install-wp-tests.sh +++ b/bin/install-wp-tests.sh @@ -176,6 +176,11 @@ install_db() { fi } +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 diff --git a/tests/bootstrap.php b/tests/bootstrap.php index d3d057d2..d0f8150c 100644 --- a/tests/bootstrap.php +++ b/tests/bootstrap.php @@ -6,7 +6,11 @@ } function _glotpress_path( string $path ): string { - return dirname( __DIR__, 2 ) . '/glotpress/' . $path; + $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. From b186c4f25b1129039af02cf2fbdbd229d194581f Mon Sep 17 00:00:00 2001 From: Paulo Pinto Date: Thu, 22 Feb 2024 11:44:13 +0000 Subject: [PATCH 10/34] Make tests run on CI --- .github/workflows/tests.yml | 2 ++ 1 file changed, 2 insertions(+) diff --git a/.github/workflows/tests.yml b/.github/workflows/tests.yml index 17902d48..c08f020b 100644 --- a/.github/workflows/tests.yml +++ b/.github/workflows/tests.yml @@ -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 From 3808e0ff1eeaae9d3132cee816450db432aa538b Mon Sep 17 00:00:00 2001 From: Paulo Pinto Date: Thu, 22 Feb 2024 12:11:26 +0000 Subject: [PATCH 11/34] Use same php version on CI and wp-env --- .wp-env.json | 1 + 1 file changed, 1 insertion(+) diff --git a/.wp-env.json b/.wp-env.json index 3d442b68..eede0128 100644 --- a/.wp-env.json +++ b/.wp-env.json @@ -1,5 +1,6 @@ { "core": null, + "phpVersion": "8.3", "plugins": [ "GlotPress/GlotPress", "https://downloads.wordpress.org/plugin/sqlite-object-cache.1.3.7.zip", From 4bf762f7d3743e42af219dedac9ce2312110d793 Mon Sep 17 00:00:00 2001 From: Paulo Pinto Date: Thu, 22 Feb 2024 12:11:38 +0000 Subject: [PATCH 12/34] Declare property --- tests/stats-listener.php | 1 + 1 file changed, 1 insertion(+) diff --git a/tests/stats-listener.php b/tests/stats-listener.php index 63b89195..b9159c2d 100644 --- a/tests/stats-listener.php +++ b/tests/stats-listener.php @@ -7,6 +7,7 @@ class Stats_Listener_Test extends GP_UnitTestCase_Route { public $route_class = 'GP_Route_Translation'; + private Event_Factory $event_factory; public function setUp(): void { parent::setUp(); From 9f6c7dabb1ebc90e9953a18b6e4e078fc58e4d77 Mon Sep 17 00:00:00 2001 From: Paulo Pinto Date: Thu, 22 Feb 2024 12:19:26 +0000 Subject: [PATCH 13/34] Create translation events table --- bin/install-wp-tests.sh | 3 +++ 1 file changed, 3 insertions(+) diff --git a/bin/install-wp-tests.sh b/bin/install-wp-tests.sh index c6417b0d..be601139 100755 --- a/bin/install-wp-tests.sh +++ b/bin/install-wp-tests.sh @@ -174,6 +174,9 @@ 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() { From f52ab80b6ae97730b748565a38d0232dc63be253 Mon Sep 17 00:00:00 2001 From: Paulo Pinto Date: Thu, 22 Feb 2024 12:48:32 +0000 Subject: [PATCH 14/34] Create active event --- tests/lib/event-factory.php | 12 ++++++++++++ tests/stats-listener.php | 2 +- 2 files changed, 13 insertions(+), 1 deletion(-) diff --git a/tests/lib/event-factory.php b/tests/lib/event-factory.php index 65e87f31..34c19dca 100644 --- a/tests/lib/event-factory.php +++ b/tests/lib/event-factory.php @@ -2,6 +2,8 @@ namespace Wporg\TranslationEvents\Tests; +use DateTimeImmutable; +use DateTimeZone; use WP_UnitTest_Factory_For_Post; use WP_UnitTest_Generator_Sequence; @@ -16,4 +18,14 @@ public function __construct( $factory = null ) { 'post_type' => 'event', ); } + + public function create_active(): int { + $event_id = $this->create(); + $now = new DateTimeImmutable( 'now', new DateTimeZone( 'UTC' ) ); + + update_post_meta( $event_id, '_event_start', $now->modify( '-1 hours' ) ); + update_post_meta( $event_id, '_event_end', $now->modify( '+1 hours' ) ); + + return $event_id; + } } diff --git a/tests/stats-listener.php b/tests/stats-listener.php index b9159c2d..ba454c25 100644 --- a/tests/stats-listener.php +++ b/tests/stats-listener.php @@ -16,7 +16,7 @@ public function setUp(): void { public function test_stores_translation_created() { // TODO: must create an active event. This test currently fails because there are no active events. - $this->event_factory->create(); + $this->event_factory->create_active(); $this->set_normal_user_as_current(); $user_id = wp_get_current_user()->ID; From e600143662aa2055716b68a0e3679ddcb5e61746 Mon Sep 17 00:00:00 2001 From: Paulo Pinto Date: Thu, 22 Feb 2024 12:55:23 +0000 Subject: [PATCH 15/34] Set user as attending --- tests/lib/event-factory.php | 10 +++++++++- tests/stats-listener.php | 6 +++--- 2 files changed, 12 insertions(+), 4 deletions(-) diff --git a/tests/lib/event-factory.php b/tests/lib/event-factory.php index 34c19dca..0e44e1f3 100644 --- a/tests/lib/event-factory.php +++ b/tests/lib/event-factory.php @@ -6,6 +6,7 @@ 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 ) { @@ -19,13 +20,20 @@ public function __construct( $factory = null ) { ); } - public function create_active(): int { + public function create_active( array $attendee_ids = array() ): int { $event_id = $this->create(); $now = new DateTimeImmutable( 'now', new DateTimeZone( 'UTC' ) ); update_post_meta( $event_id, '_event_start', $now->modify( '-1 hours' ) ); update_post_meta( $event_id, '_event_end', $now->modify( '+1 hours' ) ); + $meta_key = Route::USER_META_KEY_ATTENDING; + 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; } } diff --git a/tests/stats-listener.php b/tests/stats-listener.php index ba454c25..479bf1cd 100644 --- a/tests/stats-listener.php +++ b/tests/stats-listener.php @@ -15,12 +15,12 @@ public function setUp(): void { } public function test_stores_translation_created() { - // TODO: must create an active event. This test currently fails because there are no active events. - $this->event_factory->create_active(); - $this->set_normal_user_as_current(); $user_id = wp_get_current_user()->ID; + // TODO: must create an active event. This test currently fails because there are no active events. + $this->event_factory->create_active( array( $user_id ) ); + $set = $this->factory->translation_set->create_with_project_and_locale(); $original = $this->factory->original->create( array( From 94f48eb0f023b12ed840c925998c26724b1b2034 Mon Sep 17 00:00:00 2001 From: Paulo Pinto Date: Thu, 22 Feb 2024 13:43:13 +0000 Subject: [PATCH 16/34] Create active event --- tests/lib/event-factory.php | 5 +++-- tests/stats-listener.php | 1 - 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/tests/lib/event-factory.php b/tests/lib/event-factory.php index 0e44e1f3..0056f81c 100644 --- a/tests/lib/event-factory.php +++ b/tests/lib/event-factory.php @@ -24,8 +24,9 @@ public function create_active( array $attendee_ids = array() ): int { $event_id = $this->create(); $now = new DateTimeImmutable( 'now', new DateTimeZone( 'UTC' ) ); - update_post_meta( $event_id, '_event_start', $now->modify( '-1 hours' ) ); - update_post_meta( $event_id, '_event_end', $now->modify( '+1 hours' ) ); + update_post_meta( $event_id, '_event_start', $now->modify( '-1 hours' )->format( 'Y-m-d H:i:s' ) ); + update_post_meta( $event_id, '_event_end', $now->modify( '+1 hours' )->format( 'Y-m-d H:i:s' ) ); + update_post_meta( $event_id, '_event_timezone', 'Europe/Lisbon' ); $meta_key = Route::USER_META_KEY_ATTENDING; foreach ( $attendee_ids as $user_id ) { diff --git a/tests/stats-listener.php b/tests/stats-listener.php index 479bf1cd..8e3df5c8 100644 --- a/tests/stats-listener.php +++ b/tests/stats-listener.php @@ -18,7 +18,6 @@ public function test_stores_translation_created() { $this->set_normal_user_as_current(); $user_id = wp_get_current_user()->ID; - // TODO: must create an active event. This test currently fails because there are no active events. $this->event_factory->create_active( array( $user_id ) ); $set = $this->factory->translation_set->create_with_project_and_locale(); From 98ff1fc07c866d587490a177c2146c11e0400d63 Mon Sep 17 00:00:00 2001 From: Paulo Pinto Date: Thu, 22 Feb 2024 13:52:42 +0000 Subject: [PATCH 17/34] Add test stubs --- tests/stats-listener.php | 22 +++++++++++++++++++++- 1 file changed, 21 insertions(+), 1 deletion(-) diff --git a/tests/stats-listener.php b/tests/stats-listener.php index 8e3df5c8..86cb129b 100644 --- a/tests/stats-listener.php +++ b/tests/stats-listener.php @@ -14,7 +14,15 @@ public function setUp(): void { $this->event_factory = new Event_Factory(); } - public function test_stores_translation_created() { + public function test_does_not_store_action_for_inactive_event() { + $this->markTestSkipped( 'TODO' ); + } + + public function test_does_not_store_action_if_user_not_attending() { + $this->markTestSkipped( 'TODO' ); + } + + public function test_stores_action_create() { $this->set_normal_user_as_current(); $user_id = wp_get_current_user()->ID; @@ -47,4 +55,16 @@ public function test_stores_translation_created() { // TODO. $this->assertNotEmpty( $rows ); } + + public function test_stores_action_approve() { + $this->markTestSkipped( 'TODO' ); + } + + public function test_stores_action_reject() { + $this->markTestSkipped( 'TODO' ); + } + + public function test_stores_action_request_changes() { + $this->markTestSkipped( 'TODO' ); + } } From 0362226973ad05827b7ca71393d29f856b3e8083 Mon Sep 17 00:00:00 2001 From: Paulo Pinto Date: Thu, 22 Feb 2024 13:54:48 +0000 Subject: [PATCH 18/34] Extend GP_UnitTestCase instead of GP_UnitTestCase_Route --- tests/stats-listener.php | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) diff --git a/tests/stats-listener.php b/tests/stats-listener.php index 86cb129b..53f3f377 100644 --- a/tests/stats-listener.php +++ b/tests/stats-listener.php @@ -2,11 +2,10 @@ namespace Wporg\Tests; -use GP_UnitTestCase_Route; +use GP_UnitTestCase; use Wporg\TranslationEvents\Tests\Event_Factory; -class Stats_Listener_Test extends GP_UnitTestCase_Route { - public $route_class = 'GP_Route_Translation'; +class Stats_Listener_Test extends GP_UnitTestCase { private Event_Factory $event_factory; public function setUp(): void { From c7d38130e3f7e45e5d9c376010a4e8e0dea74f0b Mon Sep 17 00:00:00 2001 From: Paulo Pinto Date: Thu, 22 Feb 2024 14:25:22 +0000 Subject: [PATCH 19/34] Add translation factory --- tests/lib/translation-factory.php | 34 +++++++++++++++++++++++++++++++ tests/stats-listener.php | 25 +++++------------------ 2 files changed, 39 insertions(+), 20 deletions(-) create mode 100644 tests/lib/translation-factory.php diff --git a/tests/lib/translation-factory.php b/tests/lib/translation-factory.php new file mode 100644 index 00000000..c1ad71fa --- /dev/null +++ b/tests/lib/translation-factory.php @@ -0,0 +1,34 @@ +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', + ) + ); + } +} diff --git a/tests/stats-listener.php b/tests/stats-listener.php index 53f3f377..02b61a59 100644 --- a/tests/stats-listener.php +++ b/tests/stats-listener.php @@ -4,13 +4,15 @@ use GP_UnitTestCase; use Wporg\TranslationEvents\Tests\Event_Factory; +use Wporg\TranslationEvents\Tests\Translation_Factory; class Stats_Listener_Test extends GP_UnitTestCase { private Event_Factory $event_factory; public function setUp(): void { parent::setUp(); - $this->event_factory = new Event_Factory(); + $this->translation_factory = new Translation_Factory( $this->factory ); + $this->event_factory = new Event_Factory(); } public function test_does_not_store_action_for_inactive_event() { @@ -25,25 +27,8 @@ public function test_stores_action_create() { $this->set_normal_user_as_current(); $user_id = wp_get_current_user()->ID; - $this->event_factory->create_active( array( $user_id ) ); - - $set = $this->factory->translation_set->create_with_project_and_locale(); - $original = $this->factory->original->create( - array( - 'project_id' => $set->project->id, - 'status' => '+active', - 'singular' => 'foo', - ) - ); - - $translation = $this->factory->translation->create( - array( - 'user_id' => $user_id, - 'translation_set_id' => $set->id, - 'original_id' => $original->id, - 'status' => 'waiting', - ) - ); + $event_id = $this->event_factory->create_active( array( $user_id ) ); + $translation = $this->translation_factory->create( $user_id ); global $wpdb; // phpcs:disable WordPress.DB.DirectDatabaseQuery.DirectQuery From 803cea076f60e0ad0caedd21a7fb03e082b7586b Mon Sep 17 00:00:00 2001 From: Paulo Pinto Date: Thu, 22 Feb 2024 14:27:34 +0000 Subject: [PATCH 20/34] Add function to get stats --- tests/stats-listener.php | 16 ++++++++++------ 1 file changed, 10 insertions(+), 6 deletions(-) diff --git a/tests/stats-listener.php b/tests/stats-listener.php index 02b61a59..a3401000 100644 --- a/tests/stats-listener.php +++ b/tests/stats-listener.php @@ -15,6 +15,14 @@ public function setUp(): void { $this->event_factory = new Event_Factory(); } + private function get_stats(): array { + global $wpdb; + // phpcs:disable WordPress.DB.DirectDatabaseQuery.DirectQuery + // phpcs:disable WordPress.DB.DirectDatabaseQuery.NoCaching + return $wpdb->get_results( 'select * from wp_wporg_gp_translation_events_actions' ); + // phpcs:enable + } + public function test_does_not_store_action_for_inactive_event() { $this->markTestSkipped( 'TODO' ); } @@ -30,14 +38,10 @@ public function test_stores_action_create() { $event_id = $this->event_factory->create_active( array( $user_id ) ); $translation = $this->translation_factory->create( $user_id ); - global $wpdb; - // phpcs:disable WordPress.DB.DirectDatabaseQuery.DirectQuery - // phpcs:disable WordPress.DB.DirectDatabaseQuery.NoCaching - $rows = $wpdb->get_results( 'select * from wp_wporg_gp_translation_events_actions' ); - // phpcs:enable + $stats = $this->get_stats(); // TODO. - $this->assertNotEmpty( $rows ); + $this->assertNotEmpty( $stats ); } public function test_stores_action_approve() { From e7754472d1319c73fbe7a4599d96754bca1e6776 Mon Sep 17 00:00:00 2001 From: Paulo Pinto Date: Thu, 22 Feb 2024 14:31:11 +0000 Subject: [PATCH 21/34] Add create_event() --- tests/lib/event-factory.php | 15 ++++++++++++--- 1 file changed, 12 insertions(+), 3 deletions(-) diff --git a/tests/lib/event-factory.php b/tests/lib/event-factory.php index 0056f81c..3db2f2c8 100644 --- a/tests/lib/event-factory.php +++ b/tests/lib/event-factory.php @@ -21,11 +21,20 @@ public function __construct( $factory = null ) { } 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 + ); + } + + private function create_event( DateTimeImmutable $start, DateTimeImmutable $end, array $attendee_ids ): int { $event_id = $this->create(); - $now = new DateTimeImmutable( 'now', new DateTimeZone( 'UTC' ) ); - update_post_meta( $event_id, '_event_start', $now->modify( '-1 hours' )->format( 'Y-m-d H:i:s' ) ); - update_post_meta( $event_id, '_event_end', $now->modify( '+1 hours' )->format( 'Y-m-d H:i:s' ) ); + 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' ); $meta_key = Route::USER_META_KEY_ATTENDING; From a084cda15eeb1d4ccca71d59829b5a7f8a1675c5 Mon Sep 17 00:00:00 2001 From: Paulo Pinto Date: Thu, 22 Feb 2024 14:32:29 +0000 Subject: [PATCH 22/34] Add functions to create inactive events --- tests/lib/event-factory.php | 20 ++++++++++++++++++++ 1 file changed, 20 insertions(+) diff --git a/tests/lib/event-factory.php b/tests/lib/event-factory.php index 3db2f2c8..e40dc84e 100644 --- a/tests/lib/event-factory.php +++ b/tests/lib/event-factory.php @@ -30,6 +30,26 @@ public function create_active( array $attendee_ids = array() ): int { ); } + 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(); From 266c574e532378522d8bcaf3880c40a764110b5b Mon Sep 17 00:00:00 2001 From: Paulo Pinto Date: Thu, 22 Feb 2024 14:38:37 +0000 Subject: [PATCH 23/34] Test stats values --- tests/stats-listener.php | 17 +++++++++++------ 1 file changed, 11 insertions(+), 6 deletions(-) diff --git a/tests/stats-listener.php b/tests/stats-listener.php index a3401000..9937a93c 100644 --- a/tests/stats-listener.php +++ b/tests/stats-listener.php @@ -19,7 +19,7 @@ private function get_stats(): array { global $wpdb; // phpcs:disable WordPress.DB.DirectDatabaseQuery.DirectQuery // phpcs:disable WordPress.DB.DirectDatabaseQuery.NoCaching - return $wpdb->get_results( 'select * from wp_wporg_gp_translation_events_actions' ); + return $wpdb->get_results( 'select * from wp_wporg_gp_translation_events_actions', ARRAY_A ); // phpcs:enable } @@ -33,15 +33,20 @@ public function test_does_not_store_action_if_user_not_attending() { public function test_stores_action_create() { $this->set_normal_user_as_current(); - $user_id = wp_get_current_user()->ID; + $user_id = wp_get_current_user()->ID; + $event_id = $this->event_factory->create_active( array( $user_id ) ); - $event_id = $this->event_factory->create_active( array( $user_id ) ); $translation = $this->translation_factory->create( $user_id ); + // Stats_Listener will have been called. $stats = $this->get_stats(); - - // TODO. - $this->assertNotEmpty( $stats ); + $this->assertCount( 1, $stats ); + $stats = $stats[0]; + $this->assertEquals( $event_id, $stats['event_id'] ); + $this->assertEquals( $user_id, $stats['user_id'] ); + $this->assertEquals( $translation->id, $stats['translation_id'] ); + $this->assertEquals( 'create', $stats['action'] ); + $this->assertEquals( 'aa', $stats['locale'] ); } public function test_stores_action_approve() { From 98a2b8deaafe1cdaf6f86d0f7d725a445a200d12 Mon Sep 17 00:00:00 2001 From: Paulo Pinto Date: Thu, 22 Feb 2024 14:43:03 +0000 Subject: [PATCH 24/34] Test with two active events --- tests/stats-listener.php | 27 ++++++++++++++++++--------- 1 file changed, 18 insertions(+), 9 deletions(-) diff --git a/tests/stats-listener.php b/tests/stats-listener.php index 9937a93c..3551a9a8 100644 --- a/tests/stats-listener.php +++ b/tests/stats-listener.php @@ -33,20 +33,29 @@ public function test_does_not_store_action_if_user_not_attending() { public function test_stores_action_create() { $this->set_normal_user_as_current(); - $user_id = wp_get_current_user()->ID; - $event_id = $this->event_factory->create_active( array( $user_id ) ); + $user_id = wp_get_current_user()->ID; + $event1_id = $this->event_factory->create_active( array( $user_id ) ); + $event2_id = $this->event_factory->create_active( array( $user_id ) ); $translation = $this->translation_factory->create( $user_id ); // Stats_Listener will have been called. $stats = $this->get_stats(); - $this->assertCount( 1, $stats ); - $stats = $stats[0]; - $this->assertEquals( $event_id, $stats['event_id'] ); - $this->assertEquals( $user_id, $stats['user_id'] ); - $this->assertEquals( $translation->id, $stats['translation_id'] ); - $this->assertEquals( 'create', $stats['action'] ); - $this->assertEquals( 'aa', $stats['locale'] ); + $this->assertCount( 2, $stats ); + + $event1_stats = $stats[0]; + $this->assertEquals( $event1_id, $event1_stats['event_id'] ); + $this->assertEquals( $user_id, $event1_stats['user_id'] ); + $this->assertEquals( $translation->id, $event1_stats['translation_id'] ); + $this->assertEquals( 'create', $event1_stats['action'] ); + $this->assertEquals( 'aa', $event1_stats['locale'] ); + + $event2_stats = $stats[1]; + $this->assertEquals( $event2_id, $event2_stats['event_id'] ); + $this->assertEquals( $user_id, $event2_stats['user_id'] ); + $this->assertEquals( $translation->id, $event2_stats['translation_id'] ); + $this->assertEquals( 'create', $event2_stats['action'] ); + $this->assertEquals( 'aa', $event2_stats['locale'] ); } public function test_stores_action_approve() { From a61d18a6d0ff22c28523cba37f48b98633adf09c Mon Sep 17 00:00:00 2001 From: Paulo Pinto Date: Thu, 22 Feb 2024 14:45:05 +0000 Subject: [PATCH 25/34] test_does_not_store_action_for_inactive_events() --- tests/stats-listener.php | 17 ++++++++++++++--- 1 file changed, 14 insertions(+), 3 deletions(-) diff --git a/tests/stats-listener.php b/tests/stats-listener.php index 3551a9a8..0aa76aba 100644 --- a/tests/stats-listener.php +++ b/tests/stats-listener.php @@ -23,8 +23,18 @@ private function get_stats(): array { // phpcs:enable } - public function test_does_not_store_action_for_inactive_event() { - $this->markTestSkipped( 'TODO' ); + public function test_does_not_store_action_for_inactive_events() { + $this->set_normal_user_as_current(); + $user_id = wp_get_current_user()->ID; + + $this->event_factory->create_inactive_past( array( $user_id ) ); + $this->event_factory->create_inactive_future( array( $user_id ) ); + + $this->translation_factory->create( $user_id ); + // Stats_Listener will have been called. + + $stats = $this->get_stats(); + $this->assertEmpty( $stats ); } public function test_does_not_store_action_if_user_not_attending() { @@ -33,7 +43,8 @@ public function test_does_not_store_action_if_user_not_attending() { public function test_stores_action_create() { $this->set_normal_user_as_current(); - $user_id = wp_get_current_user()->ID; + $user_id = wp_get_current_user()->ID; + $event1_id = $this->event_factory->create_active( array( $user_id ) ); $event2_id = $this->event_factory->create_active( array( $user_id ) ); From 48536857ee117add13e1897b083ec2345d9baea1 Mon Sep 17 00:00:00 2001 From: Paulo Pinto Date: Thu, 22 Feb 2024 14:46:39 +0000 Subject: [PATCH 26/34] test_does_not_store_action_if_user_not_attending() --- tests/stats-listener.php | 12 +++++++++++- 1 file changed, 11 insertions(+), 1 deletion(-) diff --git a/tests/stats-listener.php b/tests/stats-listener.php index 0aa76aba..ec8e5946 100644 --- a/tests/stats-listener.php +++ b/tests/stats-listener.php @@ -38,7 +38,17 @@ public function test_does_not_store_action_for_inactive_events() { } public function test_does_not_store_action_if_user_not_attending() { - $this->markTestSkipped( 'TODO' ); + $this->set_normal_user_as_current(); + $user_id = wp_get_current_user()->ID; + + $this->event_factory->create_active(); + $this->event_factory->create_active(); + + $this->translation_factory->create( $user_id ); + // Stats_Listener will have been called. + + $stats = $this->get_stats(); + $this->assertEmpty( $stats ); } public function test_stores_action_create() { From c9bb56b7ae793c3cbf2ffe3dc01dff345b30375a Mon Sep 17 00:00:00 2001 From: Paulo Pinto Date: Thu, 22 Feb 2024 15:22:30 +0000 Subject: [PATCH 27/34] Don't add user as attendee if not requested --- tests/lib/event-factory.php | 11 ++++++++++- tests/stats-listener.php | 6 ++++++ 2 files changed, 16 insertions(+), 1 deletion(-) diff --git a/tests/lib/event-factory.php b/tests/lib/event-factory.php index e40dc84e..b8e3a1bc 100644 --- a/tests/lib/event-factory.php +++ b/tests/lib/event-factory.php @@ -52,12 +52,21 @@ public function create_inactive_future( array $attendee_ids = array() ): int { 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' ); - $meta_key = Route::USER_META_KEY_ATTENDING; foreach ( $attendee_ids as $user_id ) { $event_ids = get_user_meta( $user_id, $meta_key, true ) ?: array(); $event_ids[] = $event_id; diff --git a/tests/stats-listener.php b/tests/stats-listener.php index ec8e5946..7d067cd4 100644 --- a/tests/stats-listener.php +++ b/tests/stats-listener.php @@ -3,10 +3,12 @@ namespace Wporg\Tests; use GP_UnitTestCase; +use Wporg\TranslationEvents\Route; use Wporg\TranslationEvents\Tests\Event_Factory; use Wporg\TranslationEvents\Tests\Translation_Factory; class Stats_Listener_Test extends GP_UnitTestCase { + private Translation_Factory $translation_factory; private Event_Factory $event_factory; public function setUp(): void { @@ -23,6 +25,10 @@ private function get_stats(): array { // phpcs:enable } + public function test_does_not_store_action_for_draft_events() { + $this->markTestSkipped( 'TODO' ); + } + public function test_does_not_store_action_for_inactive_events() { $this->set_normal_user_as_current(); $user_id = wp_get_current_user()->ID; From 208faa446daa54941d43a1b3bfc16fe1c38a36c5 Mon Sep 17 00:00:00 2001 From: Paulo Pinto Date: Thu, 22 Feb 2024 15:28:51 +0000 Subject: [PATCH 28/34] test_does_not_store_action_for_draft_events() --- tests/lib/event-factory.php | 22 +++++++++++++++++++--- tests/stats-listener.php | 12 +++++++++++- 2 files changed, 30 insertions(+), 4 deletions(-) diff --git a/tests/lib/event-factory.php b/tests/lib/event-factory.php index b8e3a1bc..e1e630ca 100644 --- a/tests/lib/event-factory.php +++ b/tests/lib/event-factory.php @@ -20,13 +20,29 @@ public function __construct( $factory = null ) { ); } + 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 + $attendee_ids, ); } @@ -36,7 +52,7 @@ public function create_inactive_past( array $attendee_ids = array() ): int { return $this->create_event( $now->modify( '-2 hours' ), $now->modify( '-1 hours' ), - $attendee_ids + $attendee_ids, ); } @@ -46,7 +62,7 @@ public function create_inactive_future( array $attendee_ids = array() ): int { return $this->create_event( $now->modify( '+1 hours' ), $now->modify( '+2 hours' ), - $attendee_ids + $attendee_ids, ); } diff --git a/tests/stats-listener.php b/tests/stats-listener.php index 7d067cd4..cb1edb93 100644 --- a/tests/stats-listener.php +++ b/tests/stats-listener.php @@ -26,7 +26,17 @@ private function get_stats(): array { } public function test_does_not_store_action_for_draft_events() { - $this->markTestSkipped( 'TODO' ); + $this->set_normal_user_as_current(); + $user_id = wp_get_current_user()->ID; + + $this->event_factory->create_draft(); + $this->event_factory->create_draft(); + + $this->translation_factory->create( $user_id ); + // Stats_Listener will have been called. + + $stats = $this->get_stats(); + $this->assertEmpty( $stats ); } public function test_does_not_store_action_for_inactive_events() { From d94eeb5148741193109626a927ffb13238703fc5 Mon Sep 17 00:00:00 2001 From: Paulo Pinto Date: Thu, 22 Feb 2024 15:36:58 +0000 Subject: [PATCH 29/34] test_stores_action_approve() --- tests/stats-listener.php | 42 ++++++++++++++++++++++++++++++++++++++-- 1 file changed, 40 insertions(+), 2 deletions(-) diff --git a/tests/stats-listener.php b/tests/stats-listener.php index cb1edb93..7dea40a8 100644 --- a/tests/stats-listener.php +++ b/tests/stats-listener.php @@ -2,8 +2,8 @@ namespace Wporg\Tests; +use GP_Translation; use GP_UnitTestCase; -use Wporg\TranslationEvents\Route; use Wporg\TranslationEvents\Tests\Event_Factory; use Wporg\TranslationEvents\Tests\Translation_Factory; @@ -25,6 +25,14 @@ private function get_stats(): array { // phpcs:enable } + private function clean_stats() { + global $wpdb; + // phpcs:disable WordPress.DB.DirectDatabaseQuery.DirectQuery + // phpcs:disable WordPress.DB.DirectDatabaseQuery.NoCaching + $wpdb->query( 'delete from wp_wporg_gp_translation_events_actions' ); + // phpcs:enable + } + public function test_does_not_store_action_for_draft_events() { $this->set_normal_user_as_current(); $user_id = wp_get_current_user()->ID; @@ -96,7 +104,37 @@ public function test_stores_action_create() { } public function test_stores_action_approve() { - $this->markTestSkipped( 'TODO' ); + $this->set_normal_user_as_current(); + $user_id = wp_get_current_user()->ID; + + $event1_id = $this->event_factory->create_active( array( $user_id ) ); + $event2_id = $this->event_factory->create_active( array( $user_id ) ); + + /** @var GP_Translation $translation */ + $translation = $this->translation_factory->create( $user_id ); + // Stats_Listener will have been called. + // Clean up stats because we won't care about the "created" action. + $this->clean_stats(); + + $translation->set_as_current(); + // Stats_Listener will have been called. + + $stats = $this->get_stats(); + $this->assertCount( 2, $stats ); + + $event1_stats = $stats[0]; + $this->assertEquals( $event1_id, $event1_stats['event_id'] ); + $this->assertEquals( $user_id, $event1_stats['user_id'] ); + $this->assertEquals( $translation->id, $event1_stats['translation_id'] ); + $this->assertEquals( 'approve', $event1_stats['action'] ); + $this->assertEquals( 'aa', $event1_stats['locale'] ); + + $event2_stats = $stats[1]; + $this->assertEquals( $event2_id, $event2_stats['event_id'] ); + $this->assertEquals( $user_id, $event2_stats['user_id'] ); + $this->assertEquals( $translation->id, $event2_stats['translation_id'] ); + $this->assertEquals( 'approve', $event2_stats['action'] ); + $this->assertEquals( 'aa', $event2_stats['locale'] ); } public function test_stores_action_reject() { From 916659449a916ee6658f90b3ac8a213714bec1ff Mon Sep 17 00:00:00 2001 From: Paulo Pinto Date: Thu, 22 Feb 2024 15:37:38 +0000 Subject: [PATCH 30/34] test_stores_action_reject() --- tests/stats-listener.php | 32 +++++++++++++++++++++++++++++++- 1 file changed, 31 insertions(+), 1 deletion(-) diff --git a/tests/stats-listener.php b/tests/stats-listener.php index 7dea40a8..78e09c37 100644 --- a/tests/stats-listener.php +++ b/tests/stats-listener.php @@ -138,7 +138,37 @@ public function test_stores_action_approve() { } public function test_stores_action_reject() { - $this->markTestSkipped( 'TODO' ); + $this->set_normal_user_as_current(); + $user_id = wp_get_current_user()->ID; + + $event1_id = $this->event_factory->create_active( array( $user_id ) ); + $event2_id = $this->event_factory->create_active( array( $user_id ) ); + + /** @var GP_Translation $translation */ + $translation = $this->translation_factory->create( $user_id ); + // Stats_Listener will have been called. + // Clean up stats because we won't care about the "created" action. + $this->clean_stats(); + + $translation->reject(); + // Stats_Listener will have been called. + + $stats = $this->get_stats(); + $this->assertCount( 2, $stats ); + + $event1_stats = $stats[0]; + $this->assertEquals( $event1_id, $event1_stats['event_id'] ); + $this->assertEquals( $user_id, $event1_stats['user_id'] ); + $this->assertEquals( $translation->id, $event1_stats['translation_id'] ); + $this->assertEquals( 'reject', $event1_stats['action'] ); + $this->assertEquals( 'aa', $event1_stats['locale'] ); + + $event2_stats = $stats[1]; + $this->assertEquals( $event2_id, $event2_stats['event_id'] ); + $this->assertEquals( $user_id, $event2_stats['user_id'] ); + $this->assertEquals( $translation->id, $event2_stats['translation_id'] ); + $this->assertEquals( 'reject', $event2_stats['action'] ); + $this->assertEquals( 'aa', $event2_stats['locale'] ); } public function test_stores_action_request_changes() { From 2193fae8c6abf903cdb42763cf014bf671088975 Mon Sep 17 00:00:00 2001 From: Paulo Pinto Date: Thu, 22 Feb 2024 15:38:18 +0000 Subject: [PATCH 31/34] test_stores_action_request_changes() --- tests/stats-listener.php | 32 +++++++++++++++++++++++++++++++- 1 file changed, 31 insertions(+), 1 deletion(-) diff --git a/tests/stats-listener.php b/tests/stats-listener.php index 78e09c37..d0b6a8cc 100644 --- a/tests/stats-listener.php +++ b/tests/stats-listener.php @@ -172,6 +172,36 @@ public function test_stores_action_reject() { } public function test_stores_action_request_changes() { - $this->markTestSkipped( 'TODO' ); + $this->set_normal_user_as_current(); + $user_id = wp_get_current_user()->ID; + + $event1_id = $this->event_factory->create_active( array( $user_id ) ); + $event2_id = $this->event_factory->create_active( array( $user_id ) ); + + /** @var GP_Translation $translation */ + $translation = $this->translation_factory->create( $user_id ); + // Stats_Listener will have been called. + // Clean up stats because we won't care about the "created" action. + $this->clean_stats(); + + $translation->set_as_changesrequested(); + // Stats_Listener will have been called. + + $stats = $this->get_stats(); + $this->assertCount( 2, $stats ); + + $event1_stats = $stats[0]; + $this->assertEquals( $event1_id, $event1_stats['event_id'] ); + $this->assertEquals( $user_id, $event1_stats['user_id'] ); + $this->assertEquals( $translation->id, $event1_stats['translation_id'] ); + $this->assertEquals( 'request_changes', $event1_stats['action'] ); + $this->assertEquals( 'aa', $event1_stats['locale'] ); + + $event2_stats = $stats[1]; + $this->assertEquals( $event2_id, $event2_stats['event_id'] ); + $this->assertEquals( $user_id, $event2_stats['user_id'] ); + $this->assertEquals( $translation->id, $event2_stats['translation_id'] ); + $this->assertEquals( 'request_changes', $event2_stats['action'] ); + $this->assertEquals( 'aa', $event2_stats['locale'] ); } } From f86cab8850225f8e80d0de2887d91d8a3552fc20 Mon Sep 17 00:00:00 2001 From: Paulo Pinto Date: Fri, 23 Feb 2024 14:10:51 +0000 Subject: [PATCH 32/34] No need to install GlotPress We're calling glotpress/tests/phpunit/includes/loader.php which already installs glotpress. --- tests/bootstrap.php | 8 -------- 1 file changed, 8 deletions(-) diff --git a/tests/bootstrap.php b/tests/bootstrap.php index d0f8150c..a6efc35f 100644 --- a/tests/bootstrap.php +++ b/tests/bootstrap.php @@ -46,11 +46,3 @@ function _manually_load_plugin() { 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(); -} -_install_glotpress(); From 16b424d18371c778af886ac404057776f6591741 Mon Sep 17 00:00:00 2001 From: Paulo Pinto Date: Fri, 23 Feb 2024 14:31:20 +0000 Subject: [PATCH 33/34] Apply schema in bootstrap.php --- bin/install-wp-tests.sh | 3 --- bin/run-tests.sh | 1 - tests/bootstrap.php | 21 +++++++++++++++++++++ 3 files changed, 21 insertions(+), 4 deletions(-) diff --git a/bin/install-wp-tests.sh b/bin/install-wp-tests.sh index be601139..c6417b0d 100755 --- a/bin/install-wp-tests.sh +++ b/bin/install-wp-tests.sh @@ -174,9 +174,6 @@ 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() { diff --git a/bin/run-tests.sh b/bin/run-tests.sh index 9aeab8c5..24ab22c8 100755 --- a/bin/run-tests.sh +++ b/bin/run-tests.sh @@ -5,5 +5,4 @@ set -ex -wp-env run tests-cli --env-cwd=wp-content/plugins/wporg-gp-translation-events sh -c 'wp db query < schema.sql' wp-env run tests-cli --env-cwd=wp-content/plugins/wporg-gp-translation-events ./vendor/bin/phpunit diff --git a/tests/bootstrap.php b/tests/bootstrap.php index a6efc35f..5f6a4cc2 100644 --- a/tests/bootstrap.php +++ b/tests/bootstrap.php @@ -10,6 +10,7 @@ function _glotpress_path( string $path ): string { if ( getenv( 'GITHUB_ACTIONS' ) ) { $glotpress_path = '/tmp/wordpress/wp-content/plugins/glotpress/'; } + return $glotpress_path . $path; } @@ -27,6 +28,26 @@ function _glotpress_path( string $path ): string { // Give access to tests_add_filter() function. require_once "$_tests_dir/includes/functions.php"; +function _apply_plugin_schema() { + $schema_path = __DIR__ . '/../schema.sql'; + // phpcs:ignore WordPress.WP.AlternativeFunctions.file_get_contents_file_get_contents + $statements = explode( ';', file_get_contents( $schema_path ) ); + + global $wpdb; + foreach ( $statements as $statement ) { + $sql = trim( $statement ); + if ( ! $sql ) { + continue; + } + // phpcs:disable WordPress.DB.DirectDatabaseQuery.DirectQuery + // phpcs:disable WordPress.DB.DirectDatabaseQuery.NoCaching + // phpcs:disable WordPress.DB.PreparedSQL.NotPrepared + $wpdb->query( $sql ); + // phpcs:enable + } +} +tests_add_filter( 'muplugins_loaded', '_apply_plugin_schema' ); + /** * Manually load the plugin being tested. */ From 4d6a81e94710566f503e1dd54481020c18b5fc9e Mon Sep 17 00:00:00 2001 From: Paulo Pinto Date: Fri, 23 Feb 2024 14:32:18 +0000 Subject: [PATCH 34/34] No longer need the run-tests.sh script --- bin/run-tests.sh | 8 -------- composer.json | 2 +- 2 files changed, 1 insertion(+), 9 deletions(-) delete mode 100755 bin/run-tests.sh diff --git a/bin/run-tests.sh b/bin/run-tests.sh deleted file mode 100755 index 24ab22c8..00000000 --- a/bin/run-tests.sh +++ /dev/null @@ -1,8 +0,0 @@ -#!/usr/bin/env bash - -# This is only for use in development environments, it's not used in CI. -# You can call this with: composer dev:test - -set -ex - -wp-env run tests-cli --env-cwd=wp-content/plugins/wporg-gp-translation-events ./vendor/bin/phpunit diff --git a/composer.json b/composer.json index 06b4954b..504366c3 100644 --- a/composer.json +++ b/composer.json @@ -14,7 +14,7 @@ "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'", - "dev:test": "bin/run-tests.sh" + "dev:test": "wp-env run tests-cli --env-cwd=wp-content/plugins/wporg-gp-translation-events ./vendor/bin/phpunit" }, "config": { "allow-plugins": {