Skip to content

Commit

Permalink
"add_attachment" callback refactored.
Browse files Browse the repository at this point in the history
  • Loading branch information
kidunot89 committed Aug 27, 2020
1 parent 0c28b64 commit 19f3fc2
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 2 deletions.
2 changes: 1 addition & 1 deletion connectors/class-connector-media.php
Original file line number Diff line number Diff line change
Expand Up @@ -151,7 +151,7 @@ public function callback_add_attachment( $post_id ) {
$url = $post->guid;
$parent_id = $post->post_parent;
$parent = get_post( $parent_id );
$parent_title = $parent_id ? $parent->post_title : null;
$parent_title = $parent instanceof \WP_Post ? $parent->post_title : 'Unidentifiable post';
$attachment_type = $this->get_attachment_type( $post->guid );

$this->log(
Expand Down
34 changes: 33 additions & 1 deletion tests/tests/connectors/test-class-connector-media.php
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ public function test_callback_add_attachment() {
$post_id = self::factory()->post->create( array( 'post_title' => 'Test post' ) );

// Expected log calls.
$this->mock->expects( $this->exactly( 2 ) )
$this->mock->expects( $this->exactly( 3 ) )
->method( 'log' )
->withConsecutive(
array(
Expand Down Expand Up @@ -72,6 +72,28 @@ function( $subject ) use ( $post_id ) {
$this->greaterThan( 0 ),
$this->equalTo( 'document' ),
$this->equalTo( 'attached' ),
),
array(
$this->equalTo(
_x(
'Attached "%1$s" to "%2$s"',
'1: Attachment title, 2: Parent post title',
'stream'
)
),
$this->callback(
function( $subject ) {
$expected = array(
'name' => 'Document one',
'parent_title' => 'Unidentifiable post',
'parent_id' => 42,
);
return $expected === array_intersect_key( $expected, $subject );
}
),
$this->greaterThan( 0 ),
$this->equalTo( 'document' ),
$this->equalTo( 'attached' ),
)
);

Expand All @@ -92,6 +114,16 @@ function( $subject ) use ( $post_id ) {
)
);

// Create attachment with invalid post parent.
self::factory()->post->create(
array(
'post_title' => 'Document one',
'post_type' => 'attachment',
'post_content' => 'some description',
'post_parent' => 42
)
);

// Check callback test action.
$this->assertFalse( 0 === did_action( 'wp_stream_test_callback_add_attachment' ) );
}
Expand Down

0 comments on commit 19f3fc2

Please sign in to comment.