|
| 1 | +<?php |
| 2 | +/** |
| 3 | + * Widgets importer test cases. |
| 4 | + * |
| 5 | + * Covers the dde-patch v1 slug resolver that rewrites the |
| 6 | + * `nav_menu` widget setting from a source-site term_id to the |
| 7 | + * fresh term_id on the target site based on a `_ti_nav_menu_slug` |
| 8 | + * hint carried in the exported payload. |
| 9 | + * |
| 10 | + * @package templates-patterns-collection |
| 11 | + */ |
| 12 | + |
| 13 | +use TIOB\Importers\Widgets_Importer; |
| 14 | + |
| 15 | +/** |
| 16 | + * Class Widgets_Import_Test |
| 17 | + */ |
| 18 | +class Widgets_Import_Test extends WP_UnitTestCase { |
| 19 | + |
| 20 | + /** |
| 21 | + * Register the Neve-style footer sidebar used by the Beauty demo so |
| 22 | + * the importer treats it as a known sidebar and does not park |
| 23 | + * widgets in `wp_inactive_widgets`. |
| 24 | + */ |
| 25 | + public function setUp(): void { |
| 26 | + parent::setUp(); |
| 27 | + |
| 28 | + register_sidebar( |
| 29 | + array( |
| 30 | + 'name' => 'Footer Two', |
| 31 | + 'id' => 'footer-two-widgets', |
| 32 | + ) |
| 33 | + ); |
| 34 | + |
| 35 | + // Make sure the classic nav_menu widget is registered so the |
| 36 | + // importer's available_widgets() check passes. |
| 37 | + if ( ! isset( $GLOBALS['wp_registered_widget_controls']['nav_menu-1'] ) ) { |
| 38 | + wp_widgets_init(); |
| 39 | + } |
| 40 | + |
| 41 | + // Start from a known-clean widget state. |
| 42 | + update_option( 'widget_nav_menu', array( '_multiwidget' => 1 ) ); |
| 43 | + update_option( |
| 44 | + 'sidebars_widgets', |
| 45 | + array( |
| 46 | + 'wp_inactive_widgets' => array(), |
| 47 | + 'footer-two-widgets' => array(), |
| 48 | + ) |
| 49 | + ); |
| 50 | + } |
| 51 | + |
| 52 | + public function tearDown(): void { |
| 53 | + delete_option( 'widget_nav_menu' ); |
| 54 | + delete_option( 'sidebars_widgets' ); |
| 55 | + parent::tearDown(); |
| 56 | + } |
| 57 | + |
| 58 | + /** |
| 59 | + * When a nav_menu widget carries `_ti_nav_menu_slug`, the importer |
| 60 | + * must rewrite `nav_menu` to the term_id of the menu that matches |
| 61 | + * that slug on the current site and strip the hint before persist. |
| 62 | + */ |
| 63 | + public function test_nav_menu_widget_slug_is_resolved_to_target_term_id() { |
| 64 | + $menu_id = wp_create_nav_menu( 'Services' ); |
| 65 | + $this->assertNotInstanceOf( WP_Error::class, $menu_id ); |
| 66 | + |
| 67 | + $menu = wp_get_nav_menu_object( $menu_id ); |
| 68 | + $target = (int) $menu->term_id; |
| 69 | + $this->assertNotSame( 999, $target, 'Freshly created menu should not collide with the stale stub id.' ); |
| 70 | + |
| 71 | + $payload = array( |
| 72 | + 'footer-two-widgets' => array( |
| 73 | + 'nav_menu-2' => array( |
| 74 | + 'title' => 'Services', |
| 75 | + 'nav_menu' => 999, |
| 76 | + '_ti_nav_menu_slug' => $menu->slug, |
| 77 | + ), |
| 78 | + ), |
| 79 | + ); |
| 80 | + |
| 81 | + $importer = new Widgets_Importer(); |
| 82 | + $result = $importer->actually_import( $payload ); |
| 83 | + |
| 84 | + $this->assertNotInstanceOf( WP_Error::class, $result ); |
| 85 | + |
| 86 | + $stored = get_option( 'widget_nav_menu' ); |
| 87 | + $this->assertIsArray( $stored ); |
| 88 | + |
| 89 | + $instance = null; |
| 90 | + foreach ( $stored as $key => $value ) { |
| 91 | + if ( '_multiwidget' === $key ) { |
| 92 | + continue; |
| 93 | + } |
| 94 | + if ( isset( $value['title'] ) && 'Services' === $value['title'] ) { |
| 95 | + $instance = $value; |
| 96 | + break; |
| 97 | + } |
| 98 | + } |
| 99 | + |
| 100 | + $this->assertNotNull( $instance, 'Imported nav_menu widget instance should be present.' ); |
| 101 | + $this->assertSame( $target, (int) $instance['nav_menu'], 'Resolver should rewrite nav_menu to the target term_id.' ); |
| 102 | + $this->assertArrayNotHasKey( '_ti_nav_menu_slug', $instance, 'Slug hint should be stripped before persist.' ); |
| 103 | + } |
| 104 | + |
| 105 | + /** |
| 106 | + * When the hinted slug does not match any existing menu on the |
| 107 | + * target site, the importer must leave the original `nav_menu` |
| 108 | + * value intact (non-fatal fallback) and still strip the hint. |
| 109 | + */ |
| 110 | + public function test_nav_menu_widget_unresolvable_slug_keeps_stale_id() { |
| 111 | + $payload = array( |
| 112 | + 'footer-two-widgets' => array( |
| 113 | + 'nav_menu-2' => array( |
| 114 | + 'title' => 'Services', |
| 115 | + 'nav_menu' => 999, |
| 116 | + '_ti_nav_menu_slug' => 'does-not-exist-on-target', |
| 117 | + ), |
| 118 | + ), |
| 119 | + ); |
| 120 | + |
| 121 | + $importer = new Widgets_Importer(); |
| 122 | + $result = $importer->actually_import( $payload ); |
| 123 | + |
| 124 | + $this->assertNotInstanceOf( WP_Error::class, $result ); |
| 125 | + |
| 126 | + $stored = get_option( 'widget_nav_menu' ); |
| 127 | + $instance = null; |
| 128 | + foreach ( $stored as $key => $value ) { |
| 129 | + if ( '_multiwidget' === $key ) { |
| 130 | + continue; |
| 131 | + } |
| 132 | + if ( isset( $value['title'] ) && 'Services' === $value['title'] ) { |
| 133 | + $instance = $value; |
| 134 | + break; |
| 135 | + } |
| 136 | + } |
| 137 | + |
| 138 | + $this->assertNotNull( $instance ); |
| 139 | + $this->assertSame( 999, (int) $instance['nav_menu'], 'Unresolved slug should leave the stale id alone.' ); |
| 140 | + $this->assertArrayNotHasKey( '_ti_nav_menu_slug', $instance, 'Hint is always stripped, even on failure.' ); |
| 141 | + } |
| 142 | + |
| 143 | + /** |
| 144 | + * The `ti_tpc_widget_pre_import` filter must be invoked after the |
| 145 | + * built-in resolver and be able to mutate the widget instance. |
| 146 | + */ |
| 147 | + public function test_ti_tpc_widget_pre_import_filter_runs() { |
| 148 | + $captured = array(); |
| 149 | + $filter = function ( $widget, $id_base, $instance_id, $sidebar_id ) use ( &$captured ) { |
| 150 | + $captured[] = compact( 'widget', 'id_base', 'instance_id', 'sidebar_id' ); |
| 151 | + $widget['title'] = 'Filtered'; |
| 152 | + return $widget; |
| 153 | + }; |
| 154 | + add_filter( 'ti_tpc_widget_pre_import', $filter, 10, 4 ); |
| 155 | + |
| 156 | + $payload = array( |
| 157 | + 'footer-two-widgets' => array( |
| 158 | + 'nav_menu-2' => array( |
| 159 | + 'title' => 'Services', |
| 160 | + 'nav_menu' => 0, |
| 161 | + ), |
| 162 | + ), |
| 163 | + ); |
| 164 | + |
| 165 | + $importer = new Widgets_Importer(); |
| 166 | + $importer->actually_import( $payload ); |
| 167 | + |
| 168 | + remove_filter( 'ti_tpc_widget_pre_import', $filter, 10 ); |
| 169 | + |
| 170 | + $this->assertCount( 1, $captured ); |
| 171 | + $this->assertSame( 'nav_menu', $captured[0]['id_base'] ); |
| 172 | + $this->assertSame( 'nav_menu-2', $captured[0]['instance_id'] ); |
| 173 | + $this->assertSame( 'footer-two-widgets', $captured[0]['sidebar_id'] ); |
| 174 | + |
| 175 | + $stored = get_option( 'widget_nav_menu' ); |
| 176 | + $titles = array_column( array_filter( $stored, 'is_array' ), 'title' ); |
| 177 | + $this->assertContains( 'Filtered', $titles, 'Filter mutation should be persisted.' ); |
| 178 | + } |
| 179 | +} |
0 commit comments