|
| 1 | +<?php |
| 2 | + |
| 3 | +/** |
| 4 | + * Tests that wp_delete_attachment_files() removes the transcoded video companion. |
| 5 | + * |
| 6 | + * @group media |
| 7 | + * @covers ::wp_delete_attachment_files |
| 8 | + */ |
| 9 | +class Tests_Media_wpDeleteAttachmentOptimizedVideo extends WP_UnitTestCase { |
| 10 | + |
| 11 | + public function tear_down(): void { |
| 12 | + $this->remove_added_uploads(); |
| 13 | + |
| 14 | + parent::tear_down(); |
| 15 | + } |
| 16 | + |
| 17 | + /** |
| 18 | + * @ticket 65998 |
| 19 | + */ |
| 20 | + public function test_deletes_companion_recorded_in_metadata(): void { |
| 21 | + $attachment_id = self::factory()->attachment->create_upload_object( DIR_TESTDATA . '/uploads/small-video.mov' ); |
| 22 | + $this->assertIsInt( $attachment_id ); |
| 23 | + |
| 24 | + $attached_file = get_attached_file( $attachment_id, true ); |
| 25 | + $this->assertIsString( $attached_file ); |
| 26 | + $dir = dirname( $attached_file ); |
| 27 | + $video_name = 'optimized-' . wp_generate_password( 6, false ) . '.mp4'; |
| 28 | + $video_path = $dir . '/' . $video_name; |
| 29 | + |
| 30 | + // Create a dummy companion file on disk. |
| 31 | + file_put_contents( $video_path, 'test' ); |
| 32 | + $this->assertFileExists( $video_path, 'Video fixture should be on disk.' ); |
| 33 | + |
| 34 | + // Record the companion as the finalize route does. |
| 35 | + $metadata = (array) wp_get_attachment_metadata( $attachment_id, true ); |
| 36 | + $metadata['optimized_video'] = $video_name; |
| 37 | + wp_update_attachment_metadata( $attachment_id, $metadata ); |
| 38 | + |
| 39 | + wp_delete_attachment( $attachment_id, true ); |
| 40 | + |
| 41 | + $this->assertNull( get_post( $attachment_id ) ); |
| 42 | + $this->assertFileDoesNotExist( $video_path, 'Video companion should be deleted alongside the attachment.' ); |
| 43 | + $this->assertFileDoesNotExist( $attached_file, 'The original video should be deleted as before.' ); |
| 44 | + } |
| 45 | + |
| 46 | + /** |
| 47 | + * @ticket 65998 |
| 48 | + */ |
| 49 | + public function test_noop_when_no_companion_metadata(): void { |
| 50 | + $attachment_id = self::factory()->attachment->create_upload_object( DIR_TESTDATA . '/uploads/small-video.mov' ); |
| 51 | + $this->assertIsInt( $attachment_id ); |
| 52 | + |
| 53 | + $metadata = (array) wp_get_attachment_metadata( $attachment_id, true ); |
| 54 | + $this->assertArrayNotHasKey( 'optimized_video', $metadata ); |
| 55 | + |
| 56 | + // Deletion should complete cleanly even though no companion file is recorded. |
| 57 | + wp_delete_attachment( $attachment_id, true ); |
| 58 | + |
| 59 | + $this->assertNull( get_post( $attachment_id ) ); |
| 60 | + } |
| 61 | + |
| 62 | + /** |
| 63 | + * A path-traversal value in the metadata only ever resolves inside the |
| 64 | + * attachment's own directory, so a file outside it is never touched. |
| 65 | + * |
| 66 | + * @ticket 65998 |
| 67 | + */ |
| 68 | + public function test_traversal_value_does_not_delete_outside_attachment_dir(): void { |
| 69 | + $attachment_id = self::factory()->attachment->create_upload_object( DIR_TESTDATA . '/uploads/small-video.mov' ); |
| 70 | + $this->assertIsInt( $attachment_id ); |
| 71 | + |
| 72 | + $attached_file = get_attached_file( $attachment_id, true ); |
| 73 | + $this->assertIsString( $attached_file ); |
| 74 | + |
| 75 | + // A file one level above the attachment's directory. |
| 76 | + $outside_path = dirname( $attached_file, 2 ) . '/outside-' . wp_generate_password( 6, false ) . '.mp4'; |
| 77 | + file_put_contents( $outside_path, 'test' ); |
| 78 | + $this->assertFileExists( $outside_path, 'Test fixture should be on disk.' ); |
| 79 | + |
| 80 | + $metadata = (array) wp_get_attachment_metadata( $attachment_id, true ); |
| 81 | + $metadata['optimized_video'] = '../' . wp_basename( $outside_path ); |
| 82 | + wp_update_attachment_metadata( $attachment_id, $metadata ); |
| 83 | + |
| 84 | + wp_delete_attachment( $attachment_id, true ); |
| 85 | + |
| 86 | + $this->assertNull( get_post( $attachment_id ) ); |
| 87 | + $this->assertFileExists( $outside_path, 'A file outside the attachment directory must not be deleted.' ); |
| 88 | + |
| 89 | + wp_delete_file( $outside_path ); |
| 90 | + } |
| 91 | + |
| 92 | + /** |
| 93 | + * Guards against the companion key holding a non-string value. |
| 94 | + * |
| 95 | + * @ticket 65998 |
| 96 | + */ |
| 97 | + public function test_noop_when_companion_metadata_is_not_a_string(): void { |
| 98 | + $attachment_id = self::factory()->attachment->create_upload_object( DIR_TESTDATA . '/uploads/small-video.mov' ); |
| 99 | + $this->assertIsInt( $attachment_id ); |
| 100 | + $attached_file = get_attached_file( $attachment_id, true ); |
| 101 | + $this->assertIsString( $attached_file ); |
| 102 | + |
| 103 | + $bystander_path = dirname( $attached_file ) . '/should-not-delete.mp4'; |
| 104 | + file_put_contents( $bystander_path, 'test' ); |
| 105 | + $this->assertFileExists( $bystander_path, 'Test fixture should be on disk.' ); |
| 106 | + |
| 107 | + $metadata = (array) wp_get_attachment_metadata( $attachment_id, true ); |
| 108 | + $metadata['optimized_video'] = array( 'file' => 'should-not-delete.mp4' ); |
| 109 | + wp_update_attachment_metadata( $attachment_id, $metadata ); |
| 110 | + |
| 111 | + wp_delete_attachment( $attachment_id, true ); |
| 112 | + |
| 113 | + $this->assertNull( get_post( $attachment_id ) ); |
| 114 | + $this->assertFileExists( $bystander_path, 'The non-string guard must prevent any file deletion.' ); |
| 115 | + |
| 116 | + wp_delete_file( $bystander_path ); |
| 117 | + } |
| 118 | +} |
0 commit comments