Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
0.00% covered (danger)
0.00%
0 / 62
0.00% covered (danger)
0.00%
0 / 2
CRAP
n/a
0 / 0
jetpack_debugger_ajax_local_testing_suite
0.00% covered (danger)
0.00%
0 / 5
0.00% covered (danger)
0.00%
0 / 1
6
jetpack_debugger_site_status_tests
0.00% covered (danger)
0.00%
0 / 57
0.00% covered (danger)
0.00%
0 / 1
72
1<?php
2/**
3 * WP Site Health debugging functions.
4 *
5 * @package automattic/jetpack
6 */
7
8/**
9 * Test runner for Core's Site Health module.
10 *
11 * @since 7.3.0
12 */
13function jetpack_debugger_ajax_local_testing_suite() {
14    check_ajax_referer( 'health-check-site-status' );
15    if ( ! current_user_can( 'jetpack_manage_modules' ) ) {
16        // @phan-suppress-next-line PhanTypeMismatchArgumentProbablyReal -- It takes null, but its phpdoc only says int.
17        wp_send_json_error( null, null, JSON_UNESCAPED_SLASHES );
18    }
19    $tests = new Jetpack_Cxn_Tests();
20    // @phan-suppress-next-line PhanTypeMismatchArgumentProbablyReal -- It takes null, but its phpdoc only says int.
21    wp_send_json_success( $tests->output_results_for_core_async_site_health(), null, JSON_UNESCAPED_SLASHES );
22}
23/**
24 * Adds the Jetpack Local Testing Suite to the Core Site Health system.
25 *
26 * @since 7.3.0
27 *
28 * @param array $core_tests Array of tests from Core's Site Health.
29 *
30 * @return array $core_tests Array of tests for Core's Site Health.
31 */
32function jetpack_debugger_site_status_tests( $core_tests ) {
33    $cxn_tests = new Jetpack_Cxn_Tests();
34    $tests     = $cxn_tests->list_tests( 'direct' );
35    foreach ( $tests as $test ) {
36
37        $core_tests['direct'][ $test['name'] ] = array(
38            'label' => __( 'Jetpack: ', 'jetpack' ) . $test['name'],
39            /**
40             * Callable for Core's Site Health system to execute.
41             *
42             * @var array $test A Jetpack Testing Suite test array.
43             * @var Jetpack_Cxn_Tests $cxn_tests An instance of the Jetpack Test Suite.
44             *
45             * @return array {
46             *      A results array to match the format expected by WordPress Core.
47             *
48             *      @type string $label Name for the test.
49             *      @type string $status 'critical', 'recommended', or 'good'.
50             *      @type array $badge Array for Site Health status. Keys label and color.
51             *      @type string $description Description of the test result.
52             *      @type string $action HTML to a link to resolve issue.
53             *      @type string $test Unique test identifier.
54             *  }
55             */
56            'test'  => function () use ( $test, $cxn_tests ) {
57                $results = $cxn_tests->run_test( $test['name'] );
58                if ( is_wp_error( $results ) ) {
59                    return;
60                }
61
62                $label = $results['label'] ?
63                    $results['label'] :
64                    ucwords(
65                        str_replace(
66                            '_',
67                            ' ',
68                            str_replace( 'test__', '', $test['name'] )
69                        )
70                    );
71                if ( $results['long_description'] ) {
72                    $description = $results['long_description'];
73                } elseif ( $results['short_description'] ) {
74                    $description = sprintf(
75                        '<p>%s</p>',
76                        $results['short_description']
77                    );
78                } else {
79                    $description = sprintf(
80                        '<p>%s</p>',
81                        __( 'This test successfully passed!', 'jetpack' )
82                    );
83                }
84
85                $return = array(
86                    'label'       => $label,
87                    'status'      => 'good',
88                    'badge'       => array(
89                        'label' => __( 'Jetpack', 'jetpack' ),
90                        'color' => 'green',
91                    ),
92                    'description' => $description,
93                    'actions'     => '',
94                    'test'        => 'jetpack_' . $test['name'],
95                );
96
97                if ( false === $results['pass'] ) {
98                    $return['status'] = $results['severity'];
99                    if ( ! empty( $results['action'] ) ) {
100                        $return['actions'] = sprintf(
101                            '<a href="%1$s" target="_blank" rel="noopener noreferrer">%2$s <span class="screen-reader-text">%3$s</span><span aria-hidden="true" class="dashicons dashicons-external"></span></a>',
102                            esc_url( $results['action'] ),
103                            $results['action_label'],
104                            /* translators: accessibility text */
105                            __( '(opens in a new tab)', 'jetpack' )
106                        );
107                    }
108                }
109
110                return $return;
111            },
112        );
113    }
114    $core_tests['async']['jetpack_test_suite'] = array(
115        'label' => __( 'Jetpack Tests', 'jetpack' ),
116        'test'  => 'jetpack_local_testing_suite',
117    );
118
119    return $core_tests;
120}