/** * Set up. */ function setUp() { global $wp_customize; require_once ABSPATH . WPINC . '/class-wp-customize-manager.php'; parent::setUp(); $this->widget_posts = new Widget_Posts($this->plugin); $this->plugin->widget_number_incrementing = new Widget_Number_Incrementing($this->plugin); $this->widget_posts->init(); wp_widgets_init(); $this->widget_posts->register_instance_post_type(); $this->widget_posts->migrate_widgets_from_options(); wp_set_current_user($this->factory->user->create(array('role' => 'administrator'))); $wp_customize = $this->customize_manager = new \WP_Customize_Manager(); $this->widget_posts->add_customize_hooks(); // This did nothing in init since Customizer was not loaded. // Make sure the widgets get re-registered now using the new settings source. global $wp_registered_widgets; $wp_registered_widgets = array(); wp_widgets_init(); do_action('wp_loaded'); }
/** * @see Widget_Posts::migrate_widgets_from_options() */ function test_migrate_widgets_from_options() { $id_base = 'meta'; $original_instances = get_option("widget_{$id_base}"); unset($original_instances['_multiwidget']); $instance = new Widget_Posts($this->plugin); $instance->init(); wp_widgets_init(); $instance->register_instance_post_type(); $this->assertEmpty($instance->get_widget_instance_numbers($id_base)); $instance->migrate_widgets_from_options(); // @todo this should be getting called! wp_cache_delete( $id_base, 'widget_instance_numbers' ); $this->assertGreaterThan(0, did_action('widget_posts_import_success')); $shallow_instances = $instance->get_widget_instance_numbers($id_base); $this->assertNotEmpty($shallow_instances); $this->assertEqualSets(array_keys($original_instances), array_keys($shallow_instances)); foreach ($shallow_instances as $widget_number => $post_id) { $this->assertInternalType('int', $widget_number); $this->assertInternalType('int', $post_id); $post = get_post($post_id); $this->assertEquals(Widget_Posts::INSTANCE_POST_TYPE, $post->post_type); $this->assertEquals("{$id_base}-{$widget_number}", $post->post_name); $this->assertEquals($original_instances[$widget_number], Widget_Posts::get_post_content_filtered($post)); } // Before any Widget_Settings::offsetGet() gets called, try iterating $settings = new Widget_Settings($shallow_instances); foreach ($settings as $widget_number => $hydrated_instance) { $this->assertEquals($original_instances[$widget_number], $hydrated_instance); } // Now make sure that offsetGet() also does the right thing. $settings = new Widget_Settings($shallow_instances); foreach ($original_instances as $widget_number => $original_instance) { $this->assertArrayHasKey($widget_number, $settings); $this->assertEquals($original_instance, $settings[$widget_number]); } $this->assertEquals(0, did_action('update_option_widget_meta'), 'Expected update_option( "widget_meta" ) to short-circuit.'); }