Skip to content

Commit eb824fd

Browse files
authored
Merge pull request #3293 from jasonbahl/fix/2982-mediaDetails-file
fix: correct the resolver for the MediaDetails.file field to return the file name
2 parents a005192 + 7b09603 commit eb824fd

File tree

3 files changed

+46
-3
lines changed

3 files changed

+46
-3
lines changed

src/Type/ObjectType/MediaDetails.php

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,9 @@ public static function register_type() {
2626
'file' => [
2727
'type' => 'String',
2828
'description' => __( 'The filename of the mediaItem', 'wp-graphql' ),
29+
'resolve' => static function ( $media_details ) {
30+
return ! empty( $media_details['file'] ) ? basename( $media_details['file'] ) : null;
31+
},
2932
],
3033
'sizes' => [
3134
'type' => [

tests/wpunit/MediaItemMutationsTest.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -494,7 +494,7 @@ public function testCreateMediaItemAttachToParentAsAdmin() {
494494
'mediaType' => 'image',
495495
'sourceUrl' => $attachment_url,
496496
'mediaDetails' => [
497-
'file' => $attachment_details['file'],
497+
'file' => basename( $attachment_details['file'] ),
498498
'height' => $attachment_details['height'],
499499
'meta' => [
500500
'aperture' => 0.0,
@@ -682,7 +682,7 @@ public function testCreateMediaItemDefaultValues() {
682682
'parent' => null,
683683
'sourceUrl' => $attachment_url,
684684
'mediaDetails' => [
685-
'file' => $attachment_details['file'],
685+
'file' => basename( $attachment_details['file'] ),
686686
'height' => $attachment_details['height'],
687687
'meta' => [
688688
'aperture' => 0.0,
@@ -761,7 +761,7 @@ public function testCreateMediaItemMutation() {
761761
'mediaType' => 'image',
762762
'sourceUrl' => $attachment_url,
763763
'mediaDetails' => [
764-
'file' => $attachment_details['file'],
764+
'file' => basename( $attachment_details['file'] ),
765765
'height' => $attachment_details['height'],
766766
'meta' => [
767767
'aperture' => 0.0,

tests/wpunit/MediaItemQueriesTest.php

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -762,4 +762,44 @@ public function testGetSourceUrlBySize() {
762762

763763
wp_delete_attachment( $attachment_id, true );
764764
}
765+
766+
/**
767+
* Test that MediaDetails.file returns just the filename without the path
768+
*/
769+
public function testMediaDetailsFile() {
770+
// Upload a test image
771+
$filename = ( WPGRAPHQL_PLUGIN_DIR . 'tests/_data/images/test.png' );
772+
$attachment_id = $this->factory()->attachment->create_upload_object( $filename );
773+
774+
$query = '
775+
query GetMediaDetailsFile($id: ID!) {
776+
mediaItem(id: $id, idType: DATABASE_ID) {
777+
mediaDetails {
778+
file
779+
}
780+
}
781+
}
782+
';
783+
784+
$variables = [
785+
'id' => $attachment_id,
786+
];
787+
788+
$actual = $this->graphql( compact( 'query', 'variables' ) );
789+
790+
$this->assertArrayNotHasKey( 'errors', $actual );
791+
792+
// Get the metadata to verify filename
793+
$metadata = wp_get_attachment_metadata( $attachment_id );
794+
$this->assertNotEmpty( $metadata['file'], 'Attachment metadata file should not be empty' );
795+
796+
// Test that MediaDetails.file returns just the filename
797+
$this->assertEquals(
798+
basename( $metadata['file'] ),
799+
$actual['data']['mediaItem']['mediaDetails']['file'],
800+
'MediaDetails.file should return just the filename without the path'
801+
);
802+
803+
wp_delete_attachment( $attachment_id, true );
804+
}
765805
}

0 commit comments

Comments
 (0)