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

Correctly filter revision fields for story revisions #12611

Merged
merged 3 commits into from
Nov 7, 2022
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
17 changes: 16 additions & 1 deletion includes/Story_Revisions.php
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,12 @@
* name: string,
* diff: string
* }
* @phpstan-type PostData array{
* post_parent: int,
* post_type: string,
* post_content?: string,
* post_content_filtered?: string
* }
*/
class Story_Revisions extends Service_Base {

Expand Down Expand Up @@ -101,13 +107,22 @@ public function revisions_to_keep( $num ): int {
* @param array|mixed $fields Array of allowed revision fields.
* @param array<string,mixed> $story Story post array.
* @return array|mixed Array of allowed fields.
*
* @phpstan-param PostData $story
*/
public function filter_revision_fields( $fields, array $story ) {
if ( ! \is_array( $fields ) ) {
return $fields;
}

if ( $this->story_post_type->get_slug() === $story['post_type'] ) {
if (
$this->story_post_type->get_slug() === $story['post_type'] ||
(
'revision' === $story['post_type'] &&
swissspidy marked this conversation as resolved.
Show resolved Hide resolved
! empty( $story['post_parent'] ) &&
get_post_type( $story['post_parent'] ) === $this->story_post_type->get_slug()
)
) {
$fields['post_content_filtered'] = __( 'Story data', 'web-stories' );
}

Expand Down
61 changes: 61 additions & 0 deletions tests/phpunit/integration/tests/Story_Revisions.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@

namespace Google\Web_Stories\Tests\Integration;

use Google\Web_Stories\Story_Post_Type;

/**
* @coversDefaultClass \Google\Web_Stories\Admin\Admin
*/
Expand Down Expand Up @@ -102,4 +104,63 @@ public function data_test_revisions_to_keep(): array {
],
];
}

public function test_filter_revision_fields_not_an_array(): void {
$this->assertSame( 'foo', $this->instance->filter_revision_fields( 'foo', [] ) );
}

public function test_filter_revision_fields_wrong_post_type(): void {
$fields = [
'post_title' => 'Post title',
];

$this->assertSame(
$fields,
$this->instance->filter_revision_fields(
$fields,
[
'post_type' => 'post',
'post_parent' => 0,
]
)
);
}

public function test_filter_revision_fields_story_post_type(): void {
$fields = [
'post_title' => 'Post title',
];

$actual = $this->instance->filter_revision_fields(
$fields,
[
'post_type' => Story_Post_Type::POST_TYPE_SLUG,
'post_parent' => 0,
]
);

$this->assertArrayHasKey( 'post_content_filtered', $actual );
}

public function test_filter_revision_fields_story_post_type_revision(): void {
swissspidy marked this conversation as resolved.
Show resolved Hide resolved
$fields = [
'post_title' => 'Post title',
];

$story = self::factory()->post->create(
[
'post_type' => Story_Post_Type::POST_TYPE_SLUG,
]
);

$actual = $this->instance->filter_revision_fields(
$fields,
[
'post_type' => 'revision',
'post_parent' => $story,
]
);

$this->assertArrayHasKey( 'post_content_filtered', $actual );
}
}