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 Feb 10, 2021
1 parent 7f0b46d commit 3e9e674
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 6 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
42 changes: 37 additions & 5 deletions 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,8 +114,18 @@ 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' ) );
$this->assertFalse( 0 === did_action( $this->action_prefix . 'callback_add_attachment' ) );
}

public function test_callback_edit_attachment() {
Expand Down Expand Up @@ -124,7 +156,7 @@ public function test_callback_edit_attachment() {


// Check callback test action.
$this->assertFalse( 0 === did_action( 'wp_stream_test_callback_edit_attachment' ) );
$this->assertFalse( 0 === did_action( $this->action_prefix . 'callback_edit_attachment' ) );
}

public function test_callback_delete_attachment() {
Expand Down Expand Up @@ -159,7 +191,7 @@ function( $subject ) {
wp_delete_attachment( $attachment_id, true );

// Check callback test action.
$this->assertFalse( 0 === did_action( 'wp_stream_test_callback_delete_attachment' ) );
$this->assertFalse( 0 === did_action( $this->action_prefix . 'callback_delete_attachment' ) );
}

public function test_callback_wp_save_image_editor_file() {
Expand Down Expand Up @@ -197,6 +229,6 @@ public function test_callback_wp_save_image_editor_file() {
);

// Check callback test action.
$this->assertFalse( 0 === did_action( 'wp_stream_test_callback_wp_save_image_editor_file' ) );
$this->assertFalse( 0 === did_action( $this->action_prefix . 'callback_wp_save_image_editor_file' ) );
}
}

0 comments on commit 3e9e674

Please sign in to comment.