@@ -10,6 +10,8 @@ class Tests_Admin_wpDashboardOnThisDay extends WP_UnitTestCase {
1010
1111 protected static int $ other_user_id ;
1212
13+ protected static int $ subscriber_id ;
14+
1315 public static function wpSetUpBeforeClass ( WP_UnitTest_Factory $ factory ) {
1416 require_once ABSPATH . 'wp-admin/includes/dashboard-on-this-day.php ' ;
1517
@@ -25,30 +27,24 @@ public static function wpSetUpBeforeClass( WP_UnitTest_Factory $factory ) {
2527 'role ' => 'author ' ,
2628 )
2729 );
30+ self ::$ subscriber_id = $ factory ->user ->create (
31+ array (
32+ 'display_name ' => 'Reader ' ,
33+ 'role ' => 'subscriber ' ,
34+ )
35+ );
2836 }
2937
3038 public static function wpTearDownAfterClass () {
3139 self ::delete_user ( self ::$ user_id );
3240 self ::delete_user ( self ::$ other_user_id );
41+ self ::delete_user ( self ::$ subscriber_id );
3342 }
3443
35- public function tear_down () {
36- unset( $ GLOBALS ['wp_meta_boxes ' ]['dashboard ' ] );
37-
38- parent ::tear_down ();
39- }
40-
41- /**
42- * Sets up the globals needed to register dashboard widgets.
43- */
44- private function set_up_dashboard_screen () {
45- if ( ! function_exists ( 'wp_add_dashboard_widget ' ) ) {
46- require_once ABSPATH . 'wp-admin/includes/dashboard.php ' ;
47- }
44+ public function set_up () {
45+ parent ::set_up ();
4846
4947 set_current_screen ( 'dashboard ' );
50-
51- $ GLOBALS ['wp_meta_boxes ' ]['dashboard ' ] = array ();
5248 }
5349
5450 /**
@@ -119,71 +115,6 @@ private static function get_date_query_clause( string $date ): array {
119115 return _wp_dashboard_on_this_day_date_query_clause ( new DateTimeImmutable ( $ date , wp_timezone () ) );
120116 }
121117
122- /**
123- * @ticket 65116
124- *
125- * @covers ::wp_dashboard_on_this_day_setup
126- */
127- public function test_setup_always_registers_widget_and_postbox_class_filter () {
128- $ this ->set_up_dashboard_screen ();
129-
130- wp_set_current_user ( self ::$ user_id );
131-
132- wp_dashboard_on_this_day_setup ();
133-
134- $ dashboard_widgets = $ GLOBALS ['wp_meta_boxes ' ]['dashboard ' ]['normal ' ]['core ' ] ?? array ();
135-
136- $ this ->assertArrayHasKey ( 'wp_dashboard_on_this_day ' , $ dashboard_widgets );
137- $ this ->assertSame ( 'On This Day ' , $ dashboard_widgets ['wp_dashboard_on_this_day ' ]['title ' ] );
138- $ this ->assertNotFalse (
139- has_filter (
140- 'postbox_classes_dashboard_wp_dashboard_on_this_day ' ,
141- 'wp_dashboard_on_this_day_postbox_classes '
142- )
143- );
144- }
145-
146- /**
147- * @ticket 65116
148- *
149- * @covers ::wp_dashboard_on_this_day_postbox_classes
150- */
151- public function test_postbox_classes_hides_widget_without_matching_posts () {
152- wp_set_current_user ( self ::$ user_id );
153-
154- $ this ->assertContains ( 'hidden ' , wp_dashboard_on_this_day_postbox_classes ( array ( '' ) ) );
155- }
156-
157- /**
158- * @ticket 65116
159- *
160- * @covers ::wp_dashboard_on_this_day_postbox_classes
161- */
162- public function test_postbox_classes_does_not_hide_widget_with_matching_posts () {
163- wp_set_current_user ( self ::$ user_id );
164- $ this ->create_matching_post ( self ::$ user_id );
165-
166- $ this ->assertNotContains ( 'hidden ' , wp_dashboard_on_this_day_postbox_classes ( array ( '' ) ) );
167- }
168-
169- /**
170- * @ticket 65116
171- *
172- * @covers ::wp_dashboard_on_this_day_setup
173- */
174- public function test_setup_adds_dashboard_widget_with_matching_post_from_another_author () {
175- $ this ->set_up_dashboard_screen ();
176-
177- wp_set_current_user ( self ::$ user_id );
178- $ this ->create_matching_post ( self ::$ other_user_id );
179-
180- wp_dashboard_on_this_day_setup ();
181-
182- $ dashboard_widgets = $ GLOBALS ['wp_meta_boxes ' ]['dashboard ' ]['normal ' ]['core ' ] ?? array ();
183-
184- $ this ->assertArrayHasKey ( 'wp_dashboard_on_this_day ' , $ dashboard_widgets );
185- }
186-
187118 /**
188119 * @ticket 65116
189120 *
@@ -255,9 +186,28 @@ public function test_widget_outputs_placeholder_without_matching_posts() {
255186 $ output = ob_get_clean ();
256187
257188 $ this ->assertStringContainsString ( 'No posts were published on this day in previous years. ' , $ output );
189+ $ this ->assertStringContainsString ( 'Write one today ' , $ output );
190+ $ this ->assertStringContainsString ( admin_url ( 'post-new.php ' ), $ output );
258191 $ this ->assertStringNotContainsString ( '<ul> ' , $ output );
259192 }
260193
194+ /**
195+ * @ticket 65116
196+ *
197+ * @covers ::wp_dashboard_on_this_day
198+ */
199+ public function test_widget_placeholder_omits_link_without_edit_posts_capability () {
200+ wp_set_current_user ( self ::$ subscriber_id );
201+
202+ ob_start ();
203+ wp_dashboard_on_this_day ();
204+ $ output = ob_get_clean ();
205+
206+ $ this ->assertStringContainsString ( 'No posts were published on this day in previous years. ' , $ output );
207+ $ this ->assertStringNotContainsString ( 'Write one today ' , $ output );
208+ $ this ->assertStringNotContainsString ( admin_url ( 'post-new.php ' ), $ output );
209+ }
210+
261211 /**
262212 * @ticket 65116
263213 *
@@ -405,8 +355,6 @@ public function test_widget_does_not_append_excerpt_to_titled_posts() {
405355 * @covers ::wp_dashboard_on_this_day
406356 */
407357 public function test_widget_includes_trimmed_excerpt_for_untitled_private_posts_authored_by_current_user () {
408- $ this ->set_up_dashboard_screen ();
409-
410358 wp_set_current_user ( self ::$ user_id );
411359
412360 $ this ->create_matching_post (
@@ -440,8 +388,6 @@ public function test_widget_includes_trimmed_excerpt_for_untitled_private_posts_
440388 * @covers ::wp_dashboard_on_this_day
441389 */
442390 public function test_widget_hides_untitled_post_excerpt_for_unreadable_posts () {
443- $ this ->set_up_dashboard_screen ();
444-
445391 wp_set_current_user ( self ::$ user_id );
446392
447393 $ post_id = $ this ->create_matching_post (
@@ -476,8 +422,6 @@ public function test_widget_hides_untitled_post_excerpt_for_unreadable_posts() {
476422 * @covers ::wp_dashboard_on_this_day
477423 */
478424 public function test_widget_hides_untitled_post_excerpt_for_password_protected_posts () {
479- $ this ->set_up_dashboard_screen ();
480-
481425 wp_set_current_user ( self ::$ user_id );
482426
483427 $ this ->create_matching_post (
0 commit comments