|
| 1 | +<?php |
| 2 | +/** |
| 3 | + * Test ACF_Form_Attachment class. |
| 4 | + * |
| 5 | + * @package wordpress/secure-custom-fields |
| 6 | + */ |
| 7 | + |
| 8 | +use WorDBless\BaseTestCase; |
| 9 | + |
| 10 | +// Load the acf_form_attachment class. |
| 11 | +acf_include( 'includes/forms/form-attachment.php' ); |
| 12 | + |
| 13 | +/** |
| 14 | + * Class Test_Form_Attachment |
| 15 | + */ |
| 16 | +class Test_Form_Attachment extends BaseTestCase { |
| 17 | + |
| 18 | + /** |
| 19 | + * Test if the acf_form_attachment class exists. |
| 20 | + */ |
| 21 | + public function test_form_attachment_class_exists() { |
| 22 | + $this->assertTrue( class_exists( 'acf_form_attachment' ), 'acf_form_attachment class should exist' ); |
| 23 | + } |
| 24 | + |
| 25 | + /** |
| 26 | + * Test if the acf_form_attachment class is properly initialized. |
| 27 | + */ |
| 28 | + public function test_form_attachment_initialization() { |
| 29 | + $form_attachment = new acf_form_attachment(); |
| 30 | + |
| 31 | + $this->assertInstanceOf( 'acf_form_attachment', $form_attachment, 'acf_form_attachment should be properly initialized' ); |
| 32 | + } |
| 33 | + |
| 34 | + /** |
| 35 | + * Test constructor registers correct actions. |
| 36 | + */ |
| 37 | + public function test_constructor_registers_actions() { |
| 38 | + $form_attachment = new acf_form_attachment(); |
| 39 | + |
| 40 | + $this->assertNotFalse( |
| 41 | + has_action( 'admin_enqueue_scripts', array( $form_attachment, 'admin_enqueue_scripts' ) ), |
| 42 | + 'Should register admin_enqueue_scripts action' |
| 43 | + ); |
| 44 | + |
| 45 | + $this->assertNotFalse( |
| 46 | + has_filter( 'attachment_fields_to_edit', array( $form_attachment, 'edit_attachment' ) ), |
| 47 | + 'Should register edit_attachment filter' |
| 48 | + ); |
| 49 | + |
| 50 | + $this->assertNotFalse( |
| 51 | + has_filter( 'attachment_fields_to_save', array( $form_attachment, 'save_attachment' ) ), |
| 52 | + 'Should register save_attachment filter' |
| 53 | + ); |
| 54 | + } |
| 55 | + |
| 56 | + /** |
| 57 | + * Test edit_attachment returns form_fields unchanged when no field groups. |
| 58 | + */ |
| 59 | + public function test_edit_attachment_returns_form_fields_when_no_field_groups() { |
| 60 | + $form_attachment = new acf_form_attachment(); |
| 61 | + |
| 62 | + // Create a mock post object. |
| 63 | + $post = new stdClass(); |
| 64 | + $post->ID = 1; |
| 65 | + $post->post_type = 'attachment'; |
| 66 | + $post->post_status = 'inherit'; |
| 67 | + |
| 68 | + $form_fields = array( |
| 69 | + 'existing_field' => array( |
| 70 | + 'label' => 'Existing', |
| 71 | + 'input' => 'text', |
| 72 | + ), |
| 73 | + ); |
| 74 | + |
| 75 | + // Ensure no field groups match. |
| 76 | + add_filter( |
| 77 | + 'acf/get_field_groups', |
| 78 | + function () { |
| 79 | + return array(); |
| 80 | + } |
| 81 | + ); |
| 82 | + |
| 83 | + $result = $form_attachment->edit_attachment( $form_fields, $post ); |
| 84 | + |
| 85 | + $this->assertEquals( $form_fields, $result, 'Should return form_fields unchanged when no field groups match' ); |
| 86 | + } |
| 87 | + |
| 88 | + /** |
| 89 | + * Test save_attachment returns post when nonce fails. |
| 90 | + */ |
| 91 | + public function test_save_attachment_returns_post_when_nonce_fails() { |
| 92 | + $form_attachment = new acf_form_attachment(); |
| 93 | + |
| 94 | + $post = array( 'ID' => 123 ); |
| 95 | + $attachment = array(); |
| 96 | + |
| 97 | + // Clear any nonce. |
| 98 | + unset( $_POST['_acf_nonce'] ); |
| 99 | + |
| 100 | + $result = $form_attachment->save_attachment( $post, $attachment ); |
| 101 | + |
| 102 | + $this->assertEquals( $post, $result, 'Should return post when nonce verification fails' ); |
| 103 | + } |
| 104 | + |
| 105 | + /** |
| 106 | + * Test save_attachment processes AJAX save request. |
| 107 | + */ |
| 108 | + public function test_save_attachment_handles_ajax_request() { |
| 109 | + $form_attachment = new acf_form_attachment(); |
| 110 | + |
| 111 | + $post = array( 'ID' => 456 ); |
| 112 | + $attachment = array(); |
| 113 | + |
| 114 | + // Set up valid nonce. |
| 115 | + $_POST['_acf_screen'] = 'attachment'; |
| 116 | + $_POST['_acf_nonce'] = wp_create_nonce( 'attachment' ); |
| 117 | + |
| 118 | + // Track if acf_save_post was conceptually called. |
| 119 | + $saved_post_id = null; |
| 120 | + add_action( |
| 121 | + 'acf/save_post', |
| 122 | + function ( $post_id ) use ( &$saved_post_id ) { |
| 123 | + $saved_post_id = $post_id; |
| 124 | + } |
| 125 | + ); |
| 126 | + |
| 127 | + // The function should return the post array. |
| 128 | + $result = $form_attachment->save_attachment( $post, $attachment ); |
| 129 | + |
| 130 | + $this->assertEquals( $post, $result, 'Should return post after processing' ); |
| 131 | + |
| 132 | + // Cleanup. |
| 133 | + unset( $_POST['_acf_screen'] ); |
| 134 | + unset( $_POST['_acf_nonce'] ); |
| 135 | + } |
| 136 | + |
| 137 | + /** |
| 138 | + * Test admin_footer outputs JavaScript. |
| 139 | + */ |
| 140 | + public function test_admin_footer_outputs_script() { |
| 141 | + $form_attachment = new acf_form_attachment(); |
| 142 | + |
| 143 | + ob_start(); |
| 144 | + $form_attachment->admin_footer(); |
| 145 | + $output = ob_get_clean(); |
| 146 | + |
| 147 | + $this->assertStringContainsString( '<script type="text/javascript">', $output, 'Should output script tag' ); |
| 148 | + $this->assertStringContainsString( 'acf.unload.active = 0', $output, 'Should disable unload warning' ); |
| 149 | + } |
| 150 | + |
| 151 | + /** |
| 152 | + * Test admin_enqueue_scripts bails early on non-attachment screens. |
| 153 | + */ |
| 154 | + public function test_admin_enqueue_scripts_bails_on_wrong_screen() { |
| 155 | + $form_attachment = new acf_form_attachment(); |
| 156 | + |
| 157 | + // Mock a non-attachment screen. |
| 158 | + set_current_screen( 'edit-post' ); |
| 159 | + |
| 160 | + // Call the method. |
| 161 | + $form_attachment->admin_enqueue_scripts(); |
| 162 | + |
| 163 | + // On wrong screen, admin_footer action should NOT be added. |
| 164 | + $this->assertFalse( |
| 165 | + has_action( 'admin_footer', array( $form_attachment, 'admin_footer' ) ), |
| 166 | + 'admin_footer action should not be added on non-attachment screens' |
| 167 | + ); |
| 168 | + } |
| 169 | + |
| 170 | + /** |
| 171 | + * Data provider for attachment save scenarios. |
| 172 | + * |
| 173 | + * @return array |
| 174 | + */ |
| 175 | + public function save_scenarios_provider() { |
| 176 | + return array( |
| 177 | + 'no_nonce' => array( |
| 178 | + 'nonce' => '', |
| 179 | + 'expected' => false, |
| 180 | + ), |
| 181 | + 'invalid_nonce' => array( |
| 182 | + 'nonce' => 'invalid_nonce_value', |
| 183 | + 'expected' => false, |
| 184 | + ), |
| 185 | + ); |
| 186 | + } |
| 187 | + |
| 188 | + /** |
| 189 | + * Test save_attachment with various nonce scenarios. |
| 190 | + * |
| 191 | + * @dataProvider save_scenarios_provider |
| 192 | + * |
| 193 | + * @param string $nonce The nonce value to test. |
| 194 | + * @param bool $expected Whether save should proceed. |
| 195 | + */ |
| 196 | + public function test_save_attachment_nonce_scenarios( $nonce, $expected ) { |
| 197 | + $form_attachment = new acf_form_attachment(); |
| 198 | + |
| 199 | + $post = array( 'ID' => 789 ); |
| 200 | + $attachment = array(); |
| 201 | + |
| 202 | + if ( $nonce ) { |
| 203 | + $_POST['_acf_screen'] = 'attachment'; |
| 204 | + $_POST['_acf_nonce'] = $nonce; |
| 205 | + } |
| 206 | + |
| 207 | + $result = $form_attachment->save_attachment( $post, $attachment ); |
| 208 | + |
| 209 | + // Should always return the post array. |
| 210 | + $this->assertEquals( $post, $result ); |
| 211 | + |
| 212 | + // Cleanup. |
| 213 | + unset( $_POST['_acf_screen'] ); |
| 214 | + unset( $_POST['_acf_nonce'] ); |
| 215 | + } |
| 216 | +} |
0 commit comments