Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
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
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,8 @@
/**
* Actions and filters.
*/
add_action( 'transition_post_status', __NAMESPACE__ . '\flag_status_change', 10, 3 );
// Before `check_flag_threshold()`, so a new flag is logged ahead of the pattern status change it causes.
add_action( 'wp_after_insert_post', __NAMESPACE__ . '\flag_status_change', 5, 4 );

/**
* Check if logging is enabled for patterns.
Expand All @@ -30,15 +31,17 @@ function logging_enabled() {
}

/**
* Add a log entry to a pattern when a flag's status changes.
* Add a log entry to a pattern when a flag's status changes, once the flag is fully saved.
*
* @param string $new_status New post status.
* @param string $old_status Previous post status.
* @param \WP_Post $post Post being processed.
* @param int $post_id Post ID.
* @param \WP_Post $post Post object.
* @param bool $update Whether this is an existing post being updated.
* @param null|\WP_Post $post_before Null for new posts, the WP_Post object prior
* to the update for updated posts.
*
* @return void
*/
function flag_status_change( $new_status, $old_status, $post ) {
function flag_status_change( $post_id, $post, $update, $post_before ) {
if ( ! logging_enabled() ) {
return;
}
Expand All @@ -57,17 +60,24 @@ function flag_status_change( $new_status, $old_status, $post ) {
return;
}

$new = get_post_status_object( $new_status );
$user = get_user_by( 'id', $post->post_author );
$user_handle = sprintf(
'@%s',
$user->user_login
);
// Without the post as it was, an update's old status is unknown.
if ( $update && ! $post_before ) {
return;
}

$new_status = $post->post_status;
$old_status = $update ? $post_before->post_status : 'new';

$msg = '';
if ( $new_status === $old_status ) {
return;
} elseif ( 'new' === $old_status && PENDING_STATUS === $new_status ) {
}

$user = get_user_by( 'id', $post->post_author );
$user_handle = $user ? '@' . $user->user_login : __( 'a deleted user', 'wporg-patterns' );
$new = get_post_status_object( $new_status );

$msg = '';
if ( 'new' === $old_status && PENDING_STATUS === $new_status ) {
$msg = sprintf(
// translators: User name.
__( 'New flag submitted by %s', 'wporg-patterns' ),
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,167 @@
<?php
/**
* Test the log notes written when a flag's status changes.
*
* @package WordPress\Pattern_Directory
*/

declare( strict_types = 1 );

namespace WordPressdotorg\Pattern_Directory\Tests;

use WP_UnitTestCase;
use WP_UnitTest_Factory;
use WordPressdotorg\InternalNotes;
use const WordPressdotorg\Pattern_Directory\Pattern_Post_Type\POST_TYPE;
use const WordPressdotorg\Pattern_Directory\Pattern_Flag_Post_Type\{ PENDING_STATUS, RESOLVED_STATUS, POST_TYPE as FLAG_POST_TYPE };

/**
* Flag status changes are logged on the pattern once the flag is saved (#480).
*
* @group pattern-flags
*/
class Flag_Logging_Test extends WP_UnitTestCase {
/**
* The reporting user.
*
* @var \WP_User
*/
protected static $reporter;

/**
* The flagged pattern.
*
* @var int
*/
protected static $pattern_id;

/**
* Set up shared fixtures.
*
* @param WP_UnitTest_Factory $factory Test factory.
*/
public static function wpSetUpBeforeClass( WP_UnitTest_Factory $factory ): void {
self::$reporter = $factory->user->create_and_get( array( 'user_login' => 'flag-reporter' ) );
self::$pattern_id = $factory->post->create(
array(
'post_type' => POST_TYPE,
'post_status' => 'publish',
)
);
}

/**
* Start each test without recorded notes.
*/
public function set_up(): void {
parent::set_up();

$GLOBALS['pattern_directory_test_notes'] = array();
}

/**
* The log messages the stand-in recorded on the pattern.
*
* @return string[]
*/
protected function get_log_messages(): array {
$notes = array_filter(
$GLOBALS['pattern_directory_test_notes'],
function ( array $note ): bool {
return self::$pattern_id === $note['post_parent'] && InternalNotes\LOG_POST_TYPE === ( $note['post_type'] ?? '' );
}
);

return array_values( array_column( $notes, 'post_excerpt' ) );
}

/**
* Insert a flag the way the REST API does: after-insert hooks run once the request has saved everything.
*
* @param bool $fire_after_hooks Whether to run the after-insert hooks right away.
* @param int $author_id The reporter, the shared one by default.
* @return int The flag ID.
*/
protected function insert_flag( bool $fire_after_hooks = true, int $author_id = 0 ): int {
return wp_insert_post(
array(
'post_type' => FLAG_POST_TYPE,
'post_parent' => self::$pattern_id,
'post_author' => $author_id ? $author_id : self::$reporter->ID,
'post_status' => PENDING_STATUS,
),
false,
$fire_after_hooks
);
}

/**
* Catches a new flag going unlogged.
*/
public function test_new_flag_is_logged(): void {
$this->insert_flag();

$this->assertSame( array( 'New flag submitted by @flag-reporter' ), $this->get_log_messages() );
}

/**
* Catches a flag logged before its save finished, as with a REST request whose later steps fail.
*/
public function test_flag_is_logged_only_after_it_is_fully_saved(): void {
$flag_id = $this->insert_flag( false );

$this->assertSame( array(), $this->get_log_messages(), 'The flag was logged before its save finished.' );

wp_after_insert_post( $flag_id, false, null );

$this->assertSame( array( 'New flag submitted by @flag-reporter' ), $this->get_log_messages() );
}

/**
* Catches resolving or trashing a flag going unlogged, or an unchanged status being logged.
*/
public function test_status_changes_are_logged(): void {
$flag_id = $this->insert_flag();

wp_update_post(
array(
'ID' => $flag_id,
'post_title' => 'Same status',
)
);
wp_update_post(
array(
'ID' => $flag_id,
'post_status' => RESOLVED_STATUS,
)
);
wp_trash_post( $flag_id );

$resolved = get_post_status_object( RESOLVED_STATUS )->label;
$this->assertSame(
array(
'New flag submitted by @flag-reporter',
'Flag submitted by @flag-reporter marked as ' . $resolved,
'Flag submitted by @flag-reporter moved to trash.',
),
$this->get_log_messages()
);
}

/**
* Catches moderation of a flag whose reporter's account is gone dropping out of the pattern's log.
*/
public function test_flag_from_a_deleted_user_is_logged(): void {
$flag_id = $this->insert_flag( true, 999999 );

wp_trash_post( $flag_id );

$this->assertSame(
array(
'New flag submitted by a deleted user',
'Flag submitted by a deleted user moved to trash.',
),
$this->get_log_messages()
);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
<?php
/**
* Stand-in for the wporg-internal-notes plugin, which the tests don't load.
*
* The PHPUnit bootstrap loads it for every test. It records notes in memory instead of saving them.
*
* @package WordPress\Pattern_Directory
*/

declare( strict_types = 1 );

namespace WordPressdotorg\InternalNotes;

if ( ! function_exists( __NAMESPACE__ . '\create_note' ) ) {
if ( ! defined( __NAMESPACE__ . '\LOG_POST_TYPE' ) ) {
define( __NAMESPACE__ . '\LOG_POST_TYPE', 'wporg-log-note' );
}

$GLOBALS['pattern_directory_test_notes'] = array();

/**
* Record a note for a post.
*
* @param int $post_id The post the note belongs to.
* @param array $data The note data.
*
* @return void
*/
function create_note( int $post_id, array $data ): void {
$GLOBALS['pattern_directory_test_notes'][] = array_merge( $data, array( 'post_parent' => $post_id ) );
}
}
3 changes: 3 additions & 0 deletions public_html/wp-content/tests/phpunit/bootstrap.php
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,9 @@ function _manually_load_plugins() {
require_once $_locales;
}

// Loaded for the whole run, as the real plugin is in production, so no test depends on file load order.
require_once dirname( dirname( __DIR__ ) ) . '/plugins/pattern-directory/tests/phpunit/stub-internal-notes.php';

require dirname( dirname( __DIR__ ) ) . '/plugins/pattern-directory/bootstrap.php';
require dirname( dirname( __DIR__ ) ) . '/plugins/pattern-creator/pattern-creator.php';
require dirname( dirname( __DIR__ ) ) . '/plugins/pattern-translations/pattern-translations.php';
Expand Down
Loading