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

Add test for HTML in direct messages #1093

Merged
merged 9 commits into from
Dec 19, 2024
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
6 changes: 6 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,12 @@ All notable changes to this project will be documented in this file.
The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.1.0/),
and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).

## [Unreleased]

### Improved

* Direct Messages: Improve HTML to e-mail text conversion

## [4.5.1] - 2024-12-18

### Improved
Expand Down
9 changes: 8 additions & 1 deletion includes/class-mailer.php
Original file line number Diff line number Diff line change
Expand Up @@ -171,10 +171,17 @@ public static function direct_message( $activity, $user_id ) {
$email = $user->user_email;
}

$content = \html_entity_decode(
\wp_strip_all_tags(
str_replace( '</p>', PHP_EOL . PHP_EOL, $activity['object']['content'] )
),
ENT_QUOTES | ENT_SUBSTITUTE | ENT_HTML401
);

/* translators: 1: Blog name, 2 Actor name */
$subject = \sprintf( \esc_html__( '[%1$s] Direct Message from: %2$s', 'activitypub' ), \esc_html( get_option( 'blogname' ) ), \esc_html( $actor['name'] ) );
/* translators: 1: Blog name, 2: Actor name */
$message = \sprintf( \esc_html__( 'New Direct Message: %2$s', 'activitypub' ), \esc_html( get_option( 'blogname' ) ), \wp_strip_all_tags( $activity['object']['content'] ) ) . "\r\n\r\n";
$message = \sprintf( \esc_html__( 'New Direct Message: %2$s', 'activitypub' ), \esc_html( get_option( 'blogname' ) ), $content ) . "\r\n\r\n";
/* translators: Actor name */
$message .= \sprintf( \esc_html__( 'From: %s', 'activitypub' ), \esc_html( $actor['name'] ) ) . "\r\n";
/* translators: Actor URL */
Expand Down
4 changes: 4 additions & 0 deletions readme.txt
Original file line number Diff line number Diff line change
Expand Up @@ -132,6 +132,10 @@ For reasons of data protection, it is not possible to see the followers of other

== Changelog ==

= Unreleased =

* Improved: HTML to e-mail text conversion

= 4.5.1 =

* Improved: Reactions block: Remove the `wp-block-editor` dependency for frontend views
Expand Down
66 changes: 66 additions & 0 deletions tests/includes/class-test-mailer.php
Original file line number Diff line number Diff line change
Expand Up @@ -307,4 +307,70 @@ function ( $args ) {
remove_all_filters( 'wp_mail' );
wp_delete_user( $user_id );
}

/**
* Data provider for direct message notification text.
*
* @return array
*/
public function direct_message_text_provider() {
return array(
'HTML entities' => array(
json_decode( '"<p>Interesting story from <span class=\"h-card\" translate=\"no\"><a href=\"https:\/\/example.com\/@test\" class=\"u-url mention\">@<span>test<\/span><\/a><\/span> about people who don&#39;t own their own domain.<\/p><p>&quot;This is not a new issue, of course, but Service\u2019s implementation shows limitations.&quot;<\/p>"' ),
'Interesting story from @test about people who don\'t own their own domain.' . PHP_EOL . PHP_EOL . '"This is not a new issue, of course, but Service’s implementation shows limitations."',
),
'invalid HTML' => array(
json_decode( '"<ptest"' ),
'',
),
);
}

/**
* Test direct message notification text.
*
* @param string $text Text to test.
* @param string $expected Expected result.
*
* @covers ::direct_message
* @dataProvider direct_message_text_provider
*/
public function test_direct_message_text( $text, $expected ) {
$user_id = self::$user_id;

$activity = array(
'actor' => 'https://example.com/author',
'object' => array(
'content' => $text,
),
);

// Mock remote metadata.
add_filter(
'pre_get_remote_metadata_by_actor',
function () {
return array(
'name' => 'Test Sender',
'url' => 'https://example.com/author',
);
}
);

// Capture email.
add_filter(
'wp_mail',
function ( $args ) use ( $expected, $user_id ) {
$this->assertStringContainsString( $expected, $args['message'] );
$this->assertEquals( get_user_by( 'id', $user_id )->user_email, $args['to'] );
return $args;
}
);

Mailer::direct_message( $activity, $user_id );

// Clean up.
remove_all_filters( 'pre_get_remote_metadata_by_actor' );
remove_all_filters( 'wp_mail' );
wp_delete_user( $user_id );
}
}
Loading