File tree Expand file tree Collapse file tree 3 files changed +46
-3
lines changed
Expand file tree Collapse file tree 3 files changed +46
-3
lines changed Original file line number Diff line number Diff 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 ' => [
Original file line number Diff line number Diff 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 ,
Original file line number Diff line number Diff 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}
You can’t perform that action at this time.
0 commit comments