function setUp()
 {
     parent::setUp();
     $this->plugin->widget_number_incrementing = new Widget_Number_Incrementing($this->plugin);
     $this->plugin->widget_posts = new Widget_Posts($this->plugin);
     wp_widgets_init();
 }
Пример #2
0
 function loadWidgets()
 {
     global $wp_registered_widgets;
     wp_widgets_init();
     include BP_BASEDIR . DS . 'widgets' . DS . 'custom.php';
     $this->widgets = $wp_registered_widgets;
 }
 function setUp()
 {
     global $wp_widget_factory;
     // For why these hooks have to be removed, see https://github.com/Automattic/nginx-http-concat/issues/5
     $this->css_concat_init_priority = has_action('init', 'css_concat_init');
     if ($this->css_concat_init_priority) {
         remove_action('init', 'css_concat_init', $this->css_concat_init_priority);
     }
     $this->js_concat_init_priority = has_action('init', 'js_concat_init');
     if ($this->js_concat_init_priority) {
         remove_action('init', 'js_concat_init', $this->js_concat_init_priority);
     }
     parent::setUp();
     $this->plugin = new Plugin();
     $this->plugin->widget_factory = $wp_widget_factory;
     $this->plugin->widget_number_incrementing = new Widget_Number_Incrementing($this->plugin);
     $this->plugin->widget_posts = new Widget_Posts($this->plugin);
     $widgets_init_hook = 'widgets_init';
     $callable = array($wp_widget_factory, '_register_widgets');
     $priority = has_action($widgets_init_hook, $callable);
     if (false !== $priority) {
         remove_action($widgets_init_hook, $callable, $priority);
     }
     wp_widgets_init();
     if (false !== $priority) {
         add_action($widgets_init_hook, $callable, $priority);
     }
     $this->plugin->widget_posts->migrate_widgets_from_options();
     $this->plugin->widget_posts->init();
     $this->plugin->widget_posts->prepare_widget_data();
     // Has to be called here because of wp_widgets_init() footwork done above.
     $this->plugin->widget_posts->register_instance_post_type();
     // Normally called at init action.
 }
 function test_register_settings()
 {
     wp_widgets_init();
     parent::test_register_settings();
     $this->assertInstanceOf(__NAMESPACE__ . '\\WP_Customize_Widget_Setting', $this->manager->get_setting('widget_categories[2]'));
     $this->assertEquals('widget', $this->manager->get_setting('widget_categories[2]')->type);
     $this->assertInstanceOf(__NAMESPACE__ . '\\Widget_Settings', get_option('widget_categories'));
 }
 /**
  * @see Non_Autoloaded_Widget_Options::fix_widget_options()
  */
 function test_fix_widget_options_for_newly_registered_widget()
 {
     $this->plugin->non_autoloaded_widget_options = new Non_Autoloaded_Widget_Options($this->plugin);
     $this->plugin->non_autoloaded_widget_options->fix_widget_options();
     add_action('widgets_init', function () {
         register_widget(__NAMESPACE__ . '\\Acme_Widget');
     });
     wp_widgets_init();
     $wpdb = $this->wpdb;
     $autoload = $wpdb->get_var("SELECT autoload FROM {$wpdb->options} WHERE autoload = 'no' AND option_name = 'widget_acme'");
     // db call okay; cache okay
     $this->assertEquals('no', $autoload);
 }
 /**
  * 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');
 }
Пример #7
0
 /**
  * @see WP_Widget::save_settings()
  */
 function test_wp_widget_save_settings_delete()
 {
     global $wp_registered_widgets;
     wp_widgets_init();
     $wp_widget_search = $wp_registered_widgets['search-2']['callback'][0];
     $settings = $wp_widget_search->get_settings();
     $this->assertArrayHasKey(2, $settings);
     unset($settings[2]);
     $wp_widget_search->save_settings($settings);
     $option_value = get_option($wp_widget_search->option_name);
     $this->assertArrayNotHasKey(2, $option_value);
 }
 /**
  * @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.');
 }
Пример #9
0
 /**
  * @see wp_widget_control()
  */
 function test_wp_widget_control()
 {
     global $wp_registered_widgets;
     wp_widgets_init();
     require_once ABSPATH . '/wp-admin/includes/widgets.php';
     $widget_id = 'search-2';
     $widget = $wp_registered_widgets[$widget_id];
     $params = array('widget_id' => $widget['id'], 'widget_name' => $widget['name']);
     $args = wp_list_widget_controls_dynamic_sidebar(array(0 => $params, 1 => $widget['params'][0]));
     ob_start();
     call_user_func_array('wp_widget_control', $args);
     $control = ob_get_clean();
     $this->assertNotEmpty($control);
     $this->assertContains('<div class="widget-top">', $control);
     $this->assertContains('<div class="widget-title-action">', $control);
     $this->assertContains('<div class="widget-title">', $control);
     $this->assertContains('<form method="post">', $control);
     $this->assertContains('<div class="widget-content">', $control);
     $this->assertContains('<input class="widefat"', $control);
     $this->assertContains('<input type="hidden" name="id_base" class="id_base" value="search"', $control);
     $this->assertContains('<div class="widget-control-actions">', $control);
     $this->assertContains('<div class="alignleft">', $control);
     $this->assertContains('widget-control-remove', $control);
     $this->assertContains('widget-control-close', $control);
     $this->assertContains('<div class="alignright">', $control);
     $this->assertContains('<input type="submit" name="savewidget"', $control);
     $param_overrides = array('before_form' => '<!-- before_form -->', 'after_form' => '<!-- after_form -->', 'before_widget_content' => '<!-- before_widget_content -->', 'after_widget_content' => '<!-- after_widget_content -->');
     $params = array_merge($params, $param_overrides);
     $args = wp_list_widget_controls_dynamic_sidebar(array(0 => $params, 1 => $widget['params'][0]));
     ob_start();
     call_user_func_array('wp_widget_control', $args);
     $control = ob_get_clean();
     $this->assertNotEmpty($control);
     $this->assertNotContains('<form method="post">', $control);
     $this->assertNotContains('<div class="widget-content">', $control);
     foreach ($param_overrides as $contained) {
         $this->assertContains($contained, $control);
     }
 }
 /**
  * @see Widget_Number_Incrementing::request_incr_widget_number()
  */
 function test_request_incr_widget_number()
 {
     $instance = new Widget_Number_Incrementing($this->plugin);
     wp_widgets_init();
     $registered_widget_objs = $this->plugin->get_registered_widget_objects();
     $id_base = key($registered_widget_objs);
     // -----
     wp_set_current_user($this->factory->user->create(array('role' => 'contributor')));
     $params = array('nonce' => wp_create_nonce('incr_widget_number'), 'id_base' => $id_base);
     try {
         $exception = null;
         $instance->request_incr_widget_number($params);
     } catch (Exception $e) {
         $exception = $e;
     }
     $this->assertNotNull($exception);
     $this->assertEquals(403, $exception->getCode());
     // -----
     wp_set_current_user($this->factory->user->create(array('role' => 'administrator')));
     $params['nonce'] = 'bad_nonce';
     try {
         $exception = null;
         $instance->request_incr_widget_number($params);
     } catch (Exception $e) {
         $exception = $e;
     }
     $this->assertNotNull($exception);
     $this->assertEquals(403, $exception->getCode());
     // -----
     try {
         $exception = null;
         $instance->request_incr_widget_number(array());
     } catch (Exception $e) {
         $exception = $e;
     }
     $this->assertNotNull($exception);
     $this->assertEquals(400, $exception->getCode());
     // -----
     $params['nonce'] = wp_create_nonce('incr_widget_number');
     try {
         $exception = null;
         $instance->request_incr_widget_number(array_merge($params, array('id_base' => 'bad_id')));
     } catch (Exception $e) {
         $exception = $e;
     }
     $this->assertNotNull($exception);
     $this->assertEquals(400, $exception->getCode());
     // ----
     $previous_widget_number = $instance->get_widget_number($id_base);
     $result = $instance->request_incr_widget_number($params);
     $this->assertArrayHasKey('number', $result);
     $this->assertEquals($result['number'], $previous_widget_number + 1);
 }
 /**
  * @see Plugin::is_recognized_widget_id_base()
  */
 function test_is_recognized_widget_id_base()
 {
     wp_widgets_init();
     $id_bases = array_keys($this->plugin->get_registered_widget_objects());
     foreach ($id_bases as $id_base) {
         $this->assertTrue($this->plugin->is_recognized_widget_id_base($id_base));
     }
     $this->assertFalse($this->plugin->is_recognized_widget_id_base('bad'));
 }