|
| 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 | +} |
0 commit comments