|
| 1 | +<?php |
| 2 | +/** |
| 3 | + * Test metadata compatibility with other plugins. |
| 4 | + * |
| 5 | + * @package Feedzy |
| 6 | + * @subpackage Tests |
| 7 | + * @copyright Copyright (c) 2026, Marius Cristea |
| 8 | + * @license http://opensource.org/licenses/gpl-2.0.php GNU Public License |
| 9 | + */ |
| 10 | + |
| 11 | +/** |
| 12 | + * Class Test_Feedzy_Metadata_Compatibility |
| 13 | + * |
| 14 | + * Tests that Feedzy's metadata filters don't interfere with other plugins. |
| 15 | + * Specifically tests the issue where Slider Hero button text changes don't save |
| 16 | + * when Feedzy is active. |
| 17 | + */ |
| 18 | +class Test_Feedzy_Metadata_Compatibility extends WP_UnitTestCase { |
| 19 | + |
| 20 | + /** |
| 21 | + * Test that metadata updates on non-feedzy post types work correctly. |
| 22 | + * |
| 23 | + * This simulates the Slider Hero plugin saving button text metadata. |
| 24 | + * |
| 25 | + * @access public |
| 26 | + */ |
| 27 | + public function test_non_feedzy_post_meta_updates() { |
| 28 | + // Create a regular post (simulating Slider Hero's post type). |
| 29 | + $user_id = $this->factory->user->create( |
| 30 | + array( |
| 31 | + 'role' => 'administrator', |
| 32 | + ) |
| 33 | + ); |
| 34 | + wp_set_current_user( $user_id ); |
| 35 | + |
| 36 | + $post_id = $this->factory->post->create( |
| 37 | + array( |
| 38 | + 'post_title' => 'Test Slider', |
| 39 | + 'post_type' => 'post', // Not a feedzy post type |
| 40 | + 'post_author' => $user_id, |
| 41 | + ) |
| 42 | + ); |
| 43 | + |
| 44 | + // Ensure the post was created. |
| 45 | + $this->assertGreaterThan( 0, $post_id ); |
| 46 | + |
| 47 | + // Simulate Slider Hero saving button text metadata. |
| 48 | + $button_text = 'Click Me Now'; |
| 49 | + $result = update_post_meta( $post_id, 'slider_button_text', $button_text ); |
| 50 | + |
| 51 | + // Verify the metadata was saved successfully. |
| 52 | + $this->assertNotFalse( $result, 'Metadata update should succeed' ); |
| 53 | + |
| 54 | + // Retrieve the metadata and verify it matches. |
| 55 | + $saved_value = get_post_meta( $post_id, 'slider_button_text', true ); |
| 56 | + $this->assertEquals( $button_text, $saved_value, 'Button text should be saved correctly' ); |
| 57 | + |
| 58 | + // Update it again with different text. |
| 59 | + $new_button_text = 'Updated Button Text'; |
| 60 | + $result = update_post_meta( $post_id, 'slider_button_text', $new_button_text ); |
| 61 | + |
| 62 | + $this->assertNotFalse( $result, 'Second metadata update should succeed' ); |
| 63 | + |
| 64 | + // Verify the new value. |
| 65 | + $saved_value = get_post_meta( $post_id, 'slider_button_text', true ); |
| 66 | + $this->assertEquals( $new_button_text, $saved_value, 'Updated button text should be saved correctly' ); |
| 67 | + } |
| 68 | + |
| 69 | + /** |
| 70 | + * Test that multiple metadata updates on non-feedzy posts work. |
| 71 | + * |
| 72 | + * @access public |
| 73 | + */ |
| 74 | + public function test_multiple_non_feedzy_meta_updates() { |
| 75 | + $user_id = $this->factory->user->create( |
| 76 | + array( |
| 77 | + 'role' => 'administrator', |
| 78 | + ) |
| 79 | + ); |
| 80 | + wp_set_current_user( $user_id ); |
| 81 | + |
| 82 | + $post_id = $this->factory->post->create( |
| 83 | + array( |
| 84 | + 'post_title' => 'Test Multiple Meta', |
| 85 | + 'post_type' => 'page', // Different post type |
| 86 | + 'post_author' => $user_id, |
| 87 | + ) |
| 88 | + ); |
| 89 | + |
| 90 | + // Save multiple pieces of metadata. |
| 91 | + $meta_data = array( |
| 92 | + 'button_text' => 'Click Here', |
| 93 | + 'button_url' => 'https://example.com', |
| 94 | + 'button_style' => 'primary', |
| 95 | + ); |
| 96 | + |
| 97 | + foreach ( $meta_data as $key => $value ) { |
| 98 | + $result = update_post_meta( $post_id, $key, $value ); |
| 99 | + $this->assertNotFalse( $result, "Metadata update for {$key} should succeed" ); |
| 100 | + } |
| 101 | + |
| 102 | + // Verify all metadata was saved. |
| 103 | + foreach ( $meta_data as $key => $value ) { |
| 104 | + $saved_value = get_post_meta( $post_id, $key, true ); |
| 105 | + $this->assertEquals( $value, $saved_value, "Metadata {$key} should match saved value" ); |
| 106 | + } |
| 107 | + } |
| 108 | + |
| 109 | + /** |
| 110 | + * Test that feedzy_categories metadata still works correctly. |
| 111 | + * |
| 112 | + * This ensures our fix doesn't break existing functionality. |
| 113 | + * |
| 114 | + * @access public |
| 115 | + */ |
| 116 | + public function test_feedzy_category_meta_still_works() { |
| 117 | + $user_id = $this->factory->user->create( |
| 118 | + array( |
| 119 | + 'role' => 'administrator', |
| 120 | + ) |
| 121 | + ); |
| 122 | + wp_set_current_user( $user_id ); |
| 123 | + |
| 124 | + $post_id = $this->factory->post->create( |
| 125 | + array( |
| 126 | + 'post_title' => 'Test Feedzy Category', |
| 127 | + 'post_type' => 'feedzy_categories', |
| 128 | + 'post_author' => $user_id, |
| 129 | + ) |
| 130 | + ); |
| 131 | + |
| 132 | + $feed_url = 'https://example.com/feed'; |
| 133 | + |
| 134 | + // Set up the POST data as Feedzy expects. |
| 135 | + $_POST['id'] = $post_id; |
| 136 | + $_POST['feedzy_category_meta_noncename'] = wp_create_nonce( FEEDZY_BASEFILE ); |
| 137 | + $_POST['post_type'] = 'feedzy_categories'; |
| 138 | + $_POST['feedzy_category_feed'] = $feed_url; |
| 139 | + |
| 140 | + // Trigger the save_post action. |
| 141 | + $post = get_post( $post_id ); |
| 142 | + do_action( 'save_post', $post_id, $post ); |
| 143 | + |
| 144 | + // Verify the feed URL was validated and saved. |
| 145 | + $saved_feed = get_post_meta( $post_id, 'feedzy_category_feed', true ); |
| 146 | + |
| 147 | + // Note: The validate_category_feeds filter may modify the URL or return empty if invalid. |
| 148 | + // For this test, we just need to verify that the meta operation completed without errors. |
| 149 | + $this->assertNotNull( $saved_feed, 'Feedzy category feed metadata should be processed' ); |
| 150 | + } |
| 151 | + |
| 152 | + /** |
| 153 | + * Test add_post_meta for non-feedzy post types. |
| 154 | + * |
| 155 | + * @access public |
| 156 | + */ |
| 157 | + public function test_add_meta_for_non_feedzy_posts() { |
| 158 | + $user_id = $this->factory->user->create( |
| 159 | + array( |
| 160 | + 'role' => 'administrator', |
| 161 | + ) |
| 162 | + ); |
| 163 | + wp_set_current_user( $user_id ); |
| 164 | + |
| 165 | + $post_id = $this->factory->post->create( |
| 166 | + array( |
| 167 | + 'post_title' => 'Test Add Meta', |
| 168 | + 'post_type' => 'post', |
| 169 | + 'post_author' => $user_id, |
| 170 | + ) |
| 171 | + ); |
| 172 | + |
| 173 | + // Use add_post_meta instead of update_post_meta. |
| 174 | + $result = add_post_meta( $post_id, 'new_slider_field', 'New Value', true ); |
| 175 | + |
| 176 | + // add_post_meta returns meta_id on success, false on failure. |
| 177 | + $this->assertNotFalse( $result, 'add_post_meta should succeed for non-feedzy posts' ); |
| 178 | + $this->assertIsNumeric( $result, 'add_post_meta should return meta_id' ); |
| 179 | + |
| 180 | + // Verify the metadata. |
| 181 | + $saved_value = get_post_meta( $post_id, 'new_slider_field', true ); |
| 182 | + $this->assertEquals( 'New Value', $saved_value, 'Metadata should be saved correctly via add_post_meta' ); |
| 183 | + } |
| 184 | +} |
0 commit comments