Skip to content

Commit

Permalink
Correctly filter revision fields for story revisions (#12611)
Browse files Browse the repository at this point in the history
  • Loading branch information
swissspidy authored Nov 7, 2022
1 parent fb0d9c2 commit 1103f6c
Show file tree
Hide file tree
Showing 2 changed files with 90 additions and 2 deletions.
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'] &&
! 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
75 changes: 74 additions & 1 deletion tests/phpunit/integration/tests/Story_Revisions.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,10 @@

namespace Google\Web_Stories\Tests\Integration;

use Google\Web_Stories\Story_Post_Type;

/**
* @coversDefaultClass \Google\Web_Stories\Admin\Admin
* @coversDefaultClass \Google\Web_Stories\Story_Revisions
*/
class Story_Revisions extends DependencyInjectedTestCase {

Expand Down Expand Up @@ -102,4 +104,75 @@ public function data_test_revisions_to_keep(): array {
],
];
}

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

/**
* @covers ::filter_revision_fields
*/
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,
]
)
);
}

/**
* @covers ::filter_revision_fields
*/
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 );
}

/**
* @covers ::filter_revision_fields
*/
public function test_filter_revision_fields_story_post_type_revision(): void {
$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 );
}
}

0 comments on commit 1103f6c

Please sign in to comment.