@@ -58,23 +58,28 @@ private function set_up_dashboard_screen() {
5858 * @param string $title Post title.
5959 * @param int $years_ago Number of years before today.
6060 * @param string $time Post time.
61+ * @param array $post_args Additional post arguments.
6162 * @return int Post ID.
6263 */
6364 private function create_matching_post (
6465 int $ author_id ,
6566 string $ title = 'A memory from last year ' ,
6667 int $ years_ago = 1 ,
67- string $ time = '12:00:00 '
68+ string $ time = '12:00:00 ' ,
69+ array $ post_args = array ()
6870 ): int {
6971 $ post_date = current_datetime ()->modify ( '- ' . $ years_ago . ' years ' )->format ( 'Y-m-d ' ) . ' ' . $ time ;
7072
7173 return self ::factory ()->post ->create (
72- array (
73- 'post_author ' => $ author_id ,
74- 'post_date ' => $ post_date ,
75- 'post_date_gmt ' => get_gmt_from_date ( $ post_date ),
76- 'post_status ' => 'publish ' ,
77- 'post_title ' => $ title ,
74+ array_merge (
75+ array (
76+ 'post_author ' => $ author_id ,
77+ 'post_date ' => $ post_date ,
78+ 'post_date_gmt ' => get_gmt_from_date ( $ post_date ),
79+ 'post_status ' => 'publish ' ,
80+ 'post_title ' => $ title ,
81+ ),
82+ $ post_args
7883 )
7984 );
8085 }
@@ -338,6 +343,162 @@ public function test_widget_groups_posts_by_year() {
338343 /**
339344 * @ticket 65116
340345 *
346+ * @covers ::wp_dashboard_on_this_day
347+ */
348+ public function test_widget_includes_trimmed_excerpt_for_untitled_posts () {
349+ wp_set_current_user ( self ::$ user_id );
350+
351+ $ words = array ();
352+ for ( $ n = 1 ; $ n <= 20 ; $ n ++ ) {
353+ $ words [] = 'word ' . $ n ;
354+ }
355+
356+ $ this ->create_matching_post (
357+ self ::$ user_id ,
358+ '' ,
359+ 1 ,
360+ '12:00:00 ' ,
361+ array (
362+ 'post_excerpt ' => implode ( ' ' , $ words ),
363+ )
364+ );
365+
366+ ob_start ();
367+ wp_dashboard_on_this_day ();
368+ $ output = ob_get_clean ();
369+
370+ $ this ->assertStringContainsString ( '(no title) ' , $ output );
371+ $ this ->assertStringContainsString ( 'word15 ' , $ output , 'The 15th word should be present. ' );
372+ $ this ->assertStringNotContainsString ( 'word16 ' , $ output , 'The 16th word should be trimmed. ' );
373+ $ this ->assertStringContainsString ( '… ' , $ output , 'The excerpt should end with an ellipsis. ' );
374+ }
375+
376+ /**
377+ * @ticket 65116
378+ *
379+ * @covers ::wp_dashboard_on_this_day
380+ */
381+ public function test_widget_does_not_append_excerpt_to_titled_posts () {
382+ wp_set_current_user ( self ::$ user_id );
383+
384+ $ this ->create_matching_post (
385+ self ::$ user_id ,
386+ 'A titled anniversary memory ' ,
387+ 1 ,
388+ '12:00:00 ' ,
389+ array (
390+ 'post_excerpt ' => 'This excerpt should not be shown. ' ,
391+ )
392+ );
393+
394+ ob_start ();
395+ wp_dashboard_on_this_day ();
396+ $ output = ob_get_clean ();
397+
398+ $ this ->assertStringContainsString ( 'A titled anniversary memory ' , $ output );
399+ $ this ->assertStringNotContainsString ( 'This excerpt should not be shown. ' , $ output );
400+ }
401+
402+ /**
403+ * @ticket 65116
404+ *
405+ * @covers ::wp_dashboard_on_this_day
406+ */
407+ public function test_widget_includes_trimmed_excerpt_for_untitled_private_posts_authored_by_current_user () {
408+ $ this ->set_up_dashboard_screen ();
409+
410+ wp_set_current_user ( self ::$ user_id );
411+
412+ $ this ->create_matching_post (
413+ self ::$ user_id ,
414+ '' ,
415+ 1 ,
416+ '12:00:00 ' ,
417+ array (
418+ 'post_excerpt ' => 'Readable private anniversary memory. ' ,
419+ 'post_status ' => 'private ' ,
420+ )
421+ );
422+
423+ add_filter ( 'wp_dashboard_on_this_day_query_args ' , array ( $ this , 'filter_on_this_day_query_private_posts ' ) );
424+
425+ ob_start ();
426+ try {
427+ wp_dashboard_on_this_day ();
428+ $ output = ob_get_clean ();
429+ } finally {
430+ remove_filter ( 'wp_dashboard_on_this_day_query_args ' , array ( $ this , 'filter_on_this_day_query_private_posts ' ) );
431+ }
432+
433+ $ this ->assertStringContainsString ( '(no title) ' , $ output );
434+ $ this ->assertStringContainsString ( 'Readable private anniversary memory. ' , $ output );
435+ }
436+
437+ /**
438+ * @ticket 65116
439+ *
440+ * @covers ::wp_dashboard_on_this_day
441+ */
442+ public function test_widget_hides_untitled_post_excerpt_for_unreadable_posts () {
443+ $ this ->set_up_dashboard_screen ();
444+
445+ wp_set_current_user ( self ::$ user_id );
446+
447+ $ post_id = $ this ->create_matching_post (
448+ self ::$ other_user_id ,
449+ '' ,
450+ 1 ,
451+ '12:00:00 ' ,
452+ array (
453+ 'post_excerpt ' => 'Unreadable private anniversary memory. ' ,
454+ 'post_status ' => 'private ' ,
455+ )
456+ );
457+
458+ add_filter ( 'wp_dashboard_on_this_day_query_args ' , array ( $ this , 'filter_on_this_day_query_private_posts ' ) );
459+
460+ ob_start ();
461+ try {
462+ wp_dashboard_on_this_day ();
463+ $ output = ob_get_clean ();
464+ } finally {
465+ remove_filter ( 'wp_dashboard_on_this_day_query_args ' , array ( $ this , 'filter_on_this_day_query_private_posts ' ) );
466+ }
467+
468+ $ this ->assertFalse ( current_user_can ( 'read_post ' , $ post_id ) );
469+ $ this ->assertStringContainsString ( '(no title) ' , $ output );
470+ $ this ->assertStringNotContainsString ( 'Unreadable private anniversary memory. ' , $ output );
471+ }
472+
473+ /**
474+ * @ticket 65116
475+ *
476+ * @covers ::wp_dashboard_on_this_day
477+ */
478+ public function test_widget_hides_untitled_post_excerpt_for_password_protected_posts () {
479+ $ this ->set_up_dashboard_screen ();
480+
481+ wp_set_current_user ( self ::$ user_id );
482+
483+ $ this ->create_matching_post (
484+ self ::$ user_id ,
485+ '' ,
486+ 1 ,
487+ '12:00:00 ' ,
488+ array (
489+ 'post_excerpt ' => 'Private anniversary memory. ' ,
490+ 'post_password ' => 'secret ' ,
491+ )
492+ );
493+
494+ ob_start ();
495+ wp_dashboard_on_this_day ();
496+ $ output = ob_get_clean ();
497+
498+ $ this ->assertStringNotContainsString ( 'Private anniversary memory. ' , $ output );
499+ }
500+
501+ /**
341502 * @covers ::wp_dashboard_on_this_day
342503 * @covers ::wp_dashboard_on_this_day_get_posts
343504 */
@@ -353,8 +514,20 @@ public function test_widget_limits_posts_to_ten() {
353514 $ output = ob_get_clean ();
354515
355516 $ this ->assertStringContainsString ( '10 posts have been published on <strong> ' . wp_date ( 'F jS ' ) . '</strong>: ' , $ output );
356- $ this ->assertStringContainsString ( 'Anniversary post 1< ' , $ output );
357- $ this ->assertStringContainsString ( 'Anniversary post 10< ' , $ output );
517+ $ this ->assertMatchesRegularExpression ( '/>\s* Anniversary post 1\s*<\/a>/ ' , $ output );
518+ $ this ->assertMatchesRegularExpression ( '/>\s* Anniversary post 10\s*<\/a>/ ' , $ output );
358519 $ this ->assertStringNotContainsString ( 'Anniversary post 11 ' , $ output );
359520 }
521+
522+ /**
523+ * Filters the On This Day query to include private posts.
524+ *
525+ * @param array $args WP_Query arguments.
526+ * @return array Filtered query arguments.
527+ */
528+ public function filter_on_this_day_query_private_posts ( $ args ) {
529+ $ args ['post_status ' ] = array ( 'private ' );
530+
531+ return $ args ;
532+ }
360533}
0 commit comments