@@ -23,6 +23,14 @@ public static function wpSetUpBeforeClass( WP_UnitTest_Factory $factory ) {
2323 }
2424 }
2525
26+ public function tear_down () {
27+ unset( $ GLOBALS ['wp_meta_boxes ' ]['dashboard ' ] );
28+ wp_dequeue_style ( 'on-this-day ' );
29+ wp_dequeue_script ( 'on-this-day ' );
30+
31+ parent ::tear_down ();
32+ }
33+
2634 /**
2735 * Invokes WP_Dashboard_Widget_On_This_Day::extract_excerpt_text().
2836 *
@@ -34,6 +42,93 @@ private static function extract_excerpt_text( $source, $max_chars = 160 ) {
3442 return self ::$ extract_excerpt_text ->invoke ( null , $ source , $ max_chars );
3543 }
3644
45+ /**
46+ * Sets up the globals needed to register dashboard widgets.
47+ */
48+ private function set_up_dashboard_screen () {
49+ if ( ! function_exists ( 'wp_add_dashboard_widget ' ) ) {
50+ require_once ABSPATH . 'wp-admin/includes/dashboard.php ' ;
51+ }
52+
53+ set_current_screen ( 'dashboard ' );
54+
55+ $ GLOBALS ['wp_meta_boxes ' ]['dashboard ' ] = array ();
56+
57+ wp_dequeue_style ( 'on-this-day ' );
58+ wp_dequeue_script ( 'on-this-day ' );
59+ }
60+
61+ /**
62+ * Creates a published post in the widget's prior-year date window.
63+ *
64+ * @param int $author_id Author ID.
65+ * @return int Post ID.
66+ */
67+ private function create_matching_post ( $ author_id ) {
68+ $ post_date = current_datetime ()->modify ( '-1 year ' )->format ( 'Y-m-d H:i:s ' );
69+
70+ return self ::factory ()->post ->create (
71+ array (
72+ 'post_author ' => $ author_id ,
73+ 'post_date ' => $ post_date ,
74+ 'post_date_gmt ' => get_gmt_from_date ( $ post_date ),
75+ 'post_status ' => 'publish ' ,
76+ 'post_title ' => 'A memory from last year ' ,
77+ )
78+ );
79+ }
80+
81+ /**
82+ * @covers ::register_widget
83+ */
84+ public function test_register_widget_does_not_add_dashboard_widget_without_matching_posts () {
85+ $ this ->set_up_dashboard_screen ();
86+
87+ $ user_id = self ::factory ()->user ->create ( array ( 'role ' => 'author ' ) );
88+ wp_set_current_user ( $ user_id );
89+
90+ WP_Dashboard_Widget_On_This_Day::register_widget ();
91+
92+ $ dashboard_widgets = $ GLOBALS ['wp_meta_boxes ' ]['dashboard ' ]['normal ' ]['core ' ] ?? array ();
93+
94+ $ this ->assertArrayNotHasKey ( 'wp_dashboard_on_this_day ' , $ dashboard_widgets );
95+ $ this ->assertFalse ( wp_style_is ( 'on-this-day ' , 'enqueued ' ) );
96+ $ this ->assertFalse ( wp_script_is ( 'on-this-day ' , 'enqueued ' ) );
97+ }
98+
99+ /**
100+ * @covers ::register_widget
101+ */
102+ public function test_register_widget_adds_dashboard_widget_with_matching_posts () {
103+ $ this ->set_up_dashboard_screen ();
104+
105+ $ user_id = self ::factory ()->user ->create ( array ( 'role ' => 'author ' ) );
106+ wp_set_current_user ( $ user_id );
107+ $ this ->create_matching_post ( $ user_id );
108+
109+ WP_Dashboard_Widget_On_This_Day::register_widget ();
110+
111+ $ dashboard_widgets = $ GLOBALS ['wp_meta_boxes ' ]['dashboard ' ]['normal ' ]['core ' ] ?? array ();
112+
113+ $ this ->assertArrayHasKey ( 'wp_dashboard_on_this_day ' , $ dashboard_widgets );
114+ $ this ->assertTrue ( wp_style_is ( 'on-this-day ' , 'enqueued ' ) );
115+ $ this ->assertTrue ( wp_script_is ( 'on-this-day ' , 'enqueued ' ) );
116+ }
117+
118+ /**
119+ * @covers ::render_dashboard_widget
120+ */
121+ public function test_render_dashboard_widget_outputs_nothing_without_matching_posts () {
122+ $ user_id = self ::factory ()->user ->create ( array ( 'role ' => 'author ' ) );
123+ wp_set_current_user ( $ user_id );
124+
125+ ob_start ();
126+ WP_Dashboard_Widget_On_This_Day::render_dashboard_widget ();
127+ $ output = ob_get_clean ();
128+
129+ $ this ->assertSame ( '' , $ output );
130+ }
131+
37132 /**
38133 * @dataProvider data_extract_excerpt_text_strips_html_formatting
39134 *
0 commit comments