| 47 | * Test that registering a widget class and registering a widget instance work together. |
| 48 | * |
| 49 | * @see register_widget() |
| 50 | * @see unregister_widget() |
| 51 | * @ticket 28216 |
| 52 | */ |
| 53 | function test_register_and_unregister_widget_instance() { |
| 54 | global $wp_widget_factory, $wp_registered_widgets; |
| 55 | $this->assertEmpty( $wp_widget_factory->widgets ); |
| 56 | $this->assertEmpty( $wp_registered_widgets ); |
| 57 | |
| 58 | update_option( 'widget_search', array( |
| 59 | 2 => array( 'title' => '' ), |
| 60 | '_multiwidget' => 1, |
| 61 | ) ); |
| 62 | update_option( 'widget_better_search', array( |
| 63 | 3 => array( 'title' => '' ), |
| 64 | '_multiwidget' => 1, |
| 65 | ) ); |
| 66 | update_option( 'widget_best_search', array( |
| 67 | 4 => array( 'title' => '' ), |
| 68 | '_multiwidget' => 1, |
| 69 | ) ); |
| 70 | |
| 71 | register_widget( 'WP_Widget_Search' ); |
| 72 | $this->assertArrayHasKey( 'WP_Widget_Search', $wp_widget_factory->widgets ); |
| 73 | |
| 74 | $widget_better_search = new WP_Widget_Search(); |
| 75 | $widget_better_search->id_base = 'better_search'; |
| 76 | $widget_better_search->name = 'Better Search'; |
| 77 | $widget_better_search->option_name = 'widget_' . $widget_better_search->id_base; |
| 78 | $widget_better_search->widget_options['classname'] = 'widget_' . $widget_better_search->id_base; |
| 79 | $widget_better_search->control_options['id_base'] = $widget_better_search->id_base; |
| 80 | register_widget( $widget_better_search ); |
| 81 | $this->assertArrayHasKey( spl_object_hash( $widget_better_search ), $wp_widget_factory->widgets ); |
| 82 | |
| 83 | $widget_best_search = new WP_Widget_Search(); |
| 84 | $widget_best_search->id_base = 'best_search'; |
| 85 | $widget_best_search->name = 'Best Search'; |
| 86 | $widget_best_search->option_name = 'widget_' . $widget_best_search->id_base; |
| 87 | $widget_best_search->widget_options['classname'] = 'widget_' . $widget_best_search->id_base; |
| 88 | $widget_best_search->control_options['id_base'] = $widget_best_search->id_base; |
| 89 | register_widget( $widget_best_search ); |
| 90 | $this->assertArrayHasKey( spl_object_hash( $widget_best_search ), $wp_widget_factory->widgets ); |
| 91 | |
| 92 | $this->assertCount( 3, $wp_widget_factory->widgets ); |
| 93 | $this->assertArrayHasKey( 'WP_Widget_Search', $wp_widget_factory->widgets ); |
| 94 | $this->assertArrayHasKey( spl_object_hash( $widget_better_search ), $wp_widget_factory->widgets ); |
| 95 | $this->assertArrayHasKey( spl_object_hash( $widget_best_search ), $wp_widget_factory->widgets ); |
| 96 | |
| 97 | $wp_widget_factory->_register_widgets(); |
| 98 | |
| 99 | $this->assertArrayHasKey( 'search-2', $wp_registered_widgets ); |
| 100 | $this->assertArrayHasKey( 'better_search-3', $wp_registered_widgets ); |
| 101 | $this->assertArrayHasKey( 'best_search-4', $wp_registered_widgets ); |
| 102 | $this->assertInstanceOf( 'WP_Widget_Search', $wp_registered_widgets['search-2']['callback'][0] ); |
| 103 | $this->assertSame( $widget_better_search, $wp_registered_widgets['better_search-3']['callback'][0] ); |
| 104 | $this->assertSame( $widget_best_search, $wp_registered_widgets['best_search-4']['callback'][0] ); |
| 105 | |
| 106 | $this->assertArrayHasKey( spl_object_hash( $widget_better_search ), $wp_widget_factory->widgets ); |
| 107 | $this->assertArrayHasKey( spl_object_hash( $widget_best_search ), $wp_widget_factory->widgets ); |
| 108 | $this->assertArrayHasKey( 'WP_Widget_Search', $wp_widget_factory->widgets ); |
| 109 | unregister_widget( 'WP_Widget_Search' ); |
| 110 | unregister_widget( $widget_better_search ); |
| 111 | unregister_widget( $widget_best_search ); |
| 112 | $this->assertArrayNotHasKey( spl_object_hash( $widget_better_search ), $wp_widget_factory->widgets ); |
| 113 | $this->assertArrayNotHasKey( spl_object_hash( $widget_best_search ), $wp_widget_factory->widgets ); |
| 114 | $this->assertArrayNotHasKey( 'WP_Widget_Search', $wp_widget_factory->widgets ); |
| 115 | } |
| 116 | |
| 117 | /** |