Skip to content

Commit

Permalink
Merge pull request #99 from WordPress/tests-stats
Browse files Browse the repository at this point in the history
Tests for stats listener
  • Loading branch information
psrpinto authored Feb 23, 2024
2 parents 8fa700c + 4d6a81e commit b266815
Show file tree
Hide file tree
Showing 13 changed files with 393 additions and 36 deletions.
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
5 changes: 5 additions & 0 deletions bin/install-wp-tests.sh
Original file line number Diff line number Diff line change
Expand Up @@ -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
9 changes: 0 additions & 9 deletions bin/run-tests.sh

This file was deleted.

4 changes: 2 additions & 2 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -10,11 +10,11 @@
"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'",
"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": {
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>
41 changes: 39 additions & 2 deletions tests/bootstrap.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,15 @@
$_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 @@ -19,14 +28,42 @@
// 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.
*/
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' );
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

0 comments on commit b266815

Please sign in to comment.