Skip to content
Open
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
Empty file modified bin/install-wp-tests.sh
100644 → 100755
Empty file.
3 changes: 3 additions & 0 deletions php/class-fieldmanager-media.php
Original file line number Diff line number Diff line change
Expand Up @@ -152,6 +152,7 @@ public function presave( $value, $current_value = array() ) {
* @return string HTML string.
*/
public function form_element( $value = array() ) {
do_action( 'fieldmanager_media_pre_form_element' );
if ( is_numeric( $value ) && $value > 0 ) {
$attachment = get_post( $value );
if ( strpos( $attachment->post_mime_type, 'image/' ) === 0 ) {
Expand All @@ -173,6 +174,8 @@ public function form_element( $value = array() ) {
} else {
$preview = '';
}

do_action( 'fieldmanager_media_post_form_element' );
return sprintf(
'<input type="button" class="fm-media-button button-secondary fm-incrementable" id="%1$s" value="%3$s" data-choose="%7$s" data-update="%8$s" data-preview-size="%6$s" data-mime-type="%9$s" %10$s />
<input type="hidden" name="%2$s" value="%4$s" class="fm-element fm-media-id" />
Expand Down
42 changes: 42 additions & 0 deletions tests/php/test-fieldmanager-media-field.php
Original file line number Diff line number Diff line change
Expand Up @@ -130,4 +130,46 @@ public function test_attributes() {
$html = ob_get_clean();
$this->assertRegExp( '/<input[^>]+type=[\'"]button[\'"][^>]+data-test=[\'"]' . $args['attributes']['data-test'] . '[\'"]/', $html );
}

public function test_form_element_hooks() {

// Use a mock so we can verify the action function is called.
$pre_mock = $this->build_callback_mock( 'pre_form_element_function' );

// Add the hook.
add_action( 'fieldmanager_media_pre_form_element', array($pre_mock, 'pre_form_element_function'));

// Do the same for the post hook as above for the pre hook.
$post_mock = $this->build_callback_mock( 'post_form_element_function' );
add_action( 'fieldmanager_media_post_form_element', array($post_mock, 'post_form_element_function'));

$args = array(
'name' => 'test_media',
);

$attachment = self::factory()->attachment->create_object(
'image.jpg',
0,
array(
'post_mime_type' => 'image/jpeg',
'post_type' => 'attachment',
'post_status' => 'inherit',
)
);

$fm = new Fieldmanager_Media( $args );
$fm->form_element( $attachment );
}

private function build_callback_mock( $callback_name = 'callback_function', $expected_return = true ) {
$mock = $this->getMockBuilder('stdClass')
->setMethods(array( $callback_name ))
->getMock();

$mock->expects( $this->once() )
->method( $callback_name )
->willReturn( $expected_return );

return $mock;
}
}