Skip to content

Commit d9e8df3

Browse files
authored
Merge pull request #1856 from benitoalba/fix/1797-flesch-kincaid-grade-threshold
Fix Flesch-Kincaid grade 9 threshold
2 parents 3c7163d + ba28f91 commit d9e8df3

4 files changed

Lines changed: 168 additions & 3 deletions

File tree

admin/class-ajax.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -673,7 +673,7 @@ public function readability() {
673673
$edac_summary = get_post_meta( $post_id, '_edac_summary', true );
674674
$post_grade_readability = ( isset( $edac_summary['readability'] ) ) ? $edac_summary['readability'] : 0;
675675
$post_grade = (int) filter_var( $post_grade_readability, FILTER_SANITIZE_NUMBER_INT );
676-
$post_grade_failed = ( $post_grade < 9 ) ? false : true;
676+
$post_grade_failed = $post_grade > 9;
677677

678678
$simplified_summary_grade = 0;
679679
if ( class_exists( 'DaveChild\TextStatistics\TextStatistics' ) ) {

includes/classes/class-rest-api.php

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1072,15 +1072,16 @@ private function get_readability_data( $post_id ) {
10721072
$edac_summary = get_post_meta( $post_id, '_edac_summary', true );
10731073
$post_grade_readability = isset( $edac_summary['readability'] ) ? $edac_summary['readability'] : 0;
10741074
$post_grade = (int) filter_var( $post_grade_readability, FILTER_SANITIZE_NUMBER_INT );
1075-
$post_grade_failed = $post_grade > 9; // Treat Flesch-Kincaid grade 9+ (above roughly 8th-grade reading level recommended for plain language) as a readability failure.
1075+
// Treat Flesch-Kincaid grades above 9 (grade 10+) as readability failures.
1076+
$post_grade_failed = $post_grade > 9;
10761077

10771078
$simplified_summary_grade = 0;
10781079
if ( class_exists( 'DaveChild\TextStatistics\TextStatistics' ) ) {
10791080
$text_statistics = new \DaveChild\TextStatistics\TextStatistics();
10801081
$simplified_summary_grade = edac_normalize_fk_grade( $text_statistics->fleschKincaidGradeLevel( $simplified_summary ) );
10811082
}
10821083

1083-
$simplified_summary_grade_failed = $simplified_summary_grade >= 9;
1084+
$simplified_summary_grade_failed = $simplified_summary_grade > 9;
10841085
$simplified_summary_grade_readability = edac_ordinal( $simplified_summary_grade );
10851086
$simplified_summary_prompt = get_option( 'edac_simplified_summary_prompt' );
10861087

Lines changed: 129 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,129 @@
1+
<?php
2+
/**
3+
* AJAX readability behavior tests.
4+
*
5+
* @package Accessibility_Checker
6+
*/
7+
8+
use EDAC\Admin\Ajax;
9+
10+
/**
11+
* Tests for the AJAX readability response.
12+
*/
13+
class AjaxReadabilityTest extends WP_Ajax_UnitTestCase {
14+
15+
/**
16+
* AJAX handler under test.
17+
*
18+
* @var Ajax
19+
*/
20+
private $ajax;
21+
22+
/**
23+
* Admin user ID.
24+
*
25+
* @var int
26+
*/
27+
protected static $admin_id;
28+
29+
/**
30+
* Post ID used for tests.
31+
*
32+
* @var int
33+
*/
34+
protected static $post_id;
35+
36+
/**
37+
* Create shared fixtures for this test class.
38+
*
39+
* @param WP_UnitTest_Factory $factory Factory instance.
40+
* @return void
41+
*/
42+
public static function wpSetUpBeforeClass( $factory ) {
43+
self::$admin_id = $factory->user->create( [ 'role' => 'administrator' ] );
44+
self::$post_id = $factory->post->create(
45+
[
46+
'post_type' => 'post',
47+
'post_status' => 'publish',
48+
'post_author' => self::$admin_id,
49+
'post_content' => '<p>Content for AJAX readability tests.</p>',
50+
]
51+
);
52+
}
53+
54+
/**
55+
* Set up before each test.
56+
*/
57+
protected function setUp(): void {
58+
parent::setUp();
59+
60+
wp_set_current_user( self::$admin_id );
61+
62+
$this->ajax = new Ajax();
63+
add_action( 'wp_ajax_edac_readability_ajax', [ $this->ajax, 'readability' ] );
64+
}
65+
66+
/**
67+
* Clean up after each test.
68+
*/
69+
protected function tearDown(): void {
70+
remove_action( 'wp_ajax_edac_readability_ajax', [ $this->ajax, 'readability' ] );
71+
wp_set_current_user( 0 );
72+
73+
parent::tearDown();
74+
}
75+
76+
/**
77+
* Verify that only reading levels above ninth grade fail.
78+
*
79+
* @dataProvider data_readability_grade_threshold
80+
*
81+
* @param string $readability Stored readability label.
82+
* @param string $expected_text Expected response text.
83+
* @param string $expected_css Expected result CSS class.
84+
*/
85+
public function test_readability_uses_above_ninth_grade_threshold( string $readability, string $expected_text, string $expected_css ) {
86+
update_post_meta(
87+
self::$post_id,
88+
'_edac_summary',
89+
[ 'readability' => $readability ]
90+
);
91+
92+
$_POST['nonce'] = wp_create_nonce( 'ajax-nonce' );
93+
$_POST['post_id'] = self::$post_id;
94+
95+
try {
96+
$this->_handleAjax( 'edac_readability_ajax' );
97+
} catch ( WPAjaxDieContinueException $exception ) {
98+
$this->assertNotEmpty( $this->_last_response );
99+
}
100+
101+
$response = json_decode( $this->_last_response, true );
102+
$this->assertTrue( $response['success'] );
103+
104+
$html = json_decode( $response['data'] );
105+
$this->assertIsString( $html );
106+
$this->assertStringContainsString( $expected_text, $html );
107+
$this->assertStringContainsString( $expected_css, $html );
108+
}
109+
110+
/**
111+
* Data provider for the ninth-grade boundary.
112+
*
113+
* @return array<string, array{string, string, string}>
114+
*/
115+
public static function data_readability_grade_threshold(): array {
116+
return [
117+
'grade 9 passes' => [
118+
'9th Grade',
119+
'A simplified summary is not necessary when content reading level is 9th grade or below.',
120+
'passed-text-color',
121+
],
122+
'grade 10 fails' => [
123+
'10th Grade',
124+
'Your post has a reading level higher than 9th grade.',
125+
'failed-text-color',
126+
],
127+
];
128+
}
129+
}

tests/phpunit/includes/classes/RestApiSidebarDataTest.php

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -93,6 +93,8 @@ protected function setUp(): void {
9393
* Clean up after each test.
9494
*/
9595
protected function tearDown(): void {
96+
delete_post_meta( self::$post_id, '_edac_simplified_summary' );
97+
9698
global $wpdb;
9799
$table_name = edac_get_valid_table_name( $wpdb->prefix . 'accessibility_checker' );
98100
if ( $table_name ) {
@@ -149,6 +151,39 @@ public function test_get_summary_data_uses_meta() {
149151
$this->assertSame( $meta, $result );
150152
}
151153

154+
/**
155+
* Verify that only simplified summaries above ninth grade fail.
156+
*
157+
* @dataProvider data_readability_grade_threshold
158+
*
159+
* @param int $word_count Number of one-syllable words in the test summary.
160+
* @param int $expected_grade Expected normalized Flesch-Kincaid grade.
161+
* @param bool $expected_failed Whether the summary should fail the threshold.
162+
*/
163+
public function test_get_readability_data_uses_above_ninth_grade_threshold( int $word_count, int $expected_grade, bool $expected_failed ) {
164+
$simplified_summary = str_repeat( 'cat ', $word_count - 1 ) . 'cat.';
165+
update_post_meta( self::$post_id, '_edac_simplified_summary', $simplified_summary );
166+
167+
$api = new REST_Api();
168+
$method = $this->get_private_method( $api, 'get_readability_data' );
169+
$data = $method->invoke( $api, self::$post_id );
170+
171+
$this->assertSame( $expected_grade, $data['simplified_summary_grade'] );
172+
$this->assertSame( $expected_failed, $data['simplified_summary_grade_failed'] );
173+
}
174+
175+
/**
176+
* Data provider for the ninth-grade boundary.
177+
*
178+
* @return array<string, array{int, int, bool}>
179+
*/
180+
public static function data_readability_grade_threshold(): array {
181+
return [
182+
'grade 9 passes' => [ 33, 9, false ],
183+
'grade 10 fails' => [ 36, 10, true ],
184+
];
185+
}
186+
152187
/**
153188
* Ensure get_details_data returns counts and passes rules without rows.
154189
*/

0 commit comments

Comments
 (0)