|
| 1 | +<?php |
| 2 | + |
| 3 | +/** |
| 4 | + * @group taxonomy |
| 5 | + * |
| 6 | + * @covers ::sanitize_term |
| 7 | + */ |
| 8 | +class Tests_Term_SanitizeTerm extends WP_UnitTestCase { |
| 9 | + |
| 10 | + /** |
| 11 | + * Tests sanitize_term() inputs and outputs. |
| 12 | + * |
| 13 | + * @ticket 64238 |
| 14 | + * @dataProvider data_sanitize_term |
| 15 | + * |
| 16 | + * @param Closure(): (object|array<string, mixed>) $input_callback Callback to get the term data. |
| 17 | + * @param string $context Context in which to sanitize the term. |
| 18 | + * @param string $expected_description Expected sanitized description. |
| 19 | + */ |
| 20 | + public function test_sanitize_term( Closure $input_callback, string $context, string $expected_description ): void { |
| 21 | + $input = $input_callback(); |
| 22 | + $taxonomy = 'category'; |
| 23 | + |
| 24 | + $sanitized = sanitize_term( $input, $taxonomy, $context ); |
| 25 | + |
| 26 | + if ( is_object( $input ) ) { |
| 27 | + $this->assertInstanceOf( stdClass::class, $sanitized ); |
| 28 | + $this->assertSame( $context, $sanitized->filter ); |
| 29 | + if ( isset( $input->description ) ) { |
| 30 | + $this->assertSame( $expected_description, $sanitized->description ); |
| 31 | + } |
| 32 | + } else { |
| 33 | + $this->assertIsArray( $sanitized ); |
| 34 | + $this->assertSame( $context, $sanitized['filter'] ); |
| 35 | + if ( isset( $input['description'] ) ) { |
| 36 | + $this->assertSame( $expected_description, $sanitized['description'] ); |
| 37 | + } |
| 38 | + } |
| 39 | + } |
| 40 | + |
| 41 | + /** |
| 42 | + * Data provider for test_sanitize_term. |
| 43 | + * |
| 44 | + * @return array<string, array{ |
| 45 | + * input_callback: Closure(): (object|array<string, mixed>), |
| 46 | + * context: string, |
| 47 | + * expected_description: string, |
| 48 | + * }> |
| 49 | + */ |
| 50 | + public function data_sanitize_term(): array { |
| 51 | + $description = 'Test <script>document.write("Hello")</script> Description'; |
| 52 | + |
| 53 | + return array( |
| 54 | + 'Object with term_id, edit context' => array( |
| 55 | + 'input_callback' => fn() => (object) array( |
| 56 | + 'term_id' => 123, |
| 57 | + 'name' => 'Test Term', |
| 58 | + 'description' => $description, |
| 59 | + ), |
| 60 | + 'context' => 'edit', |
| 61 | + 'expected_description' => esc_html( $description ), |
| 62 | + ), |
| 63 | + 'Object without term_id, edit context' => array( |
| 64 | + 'input_callback' => fn() => (object) array( |
| 65 | + 'name' => 'Test Term', |
| 66 | + 'description' => $description, |
| 67 | + ), |
| 68 | + 'context' => 'edit', |
| 69 | + 'expected_description' => esc_html( $description ), |
| 70 | + ), |
| 71 | + 'Array with term_id, edit context' => array( |
| 72 | + 'input_callback' => fn() => array( |
| 73 | + 'term_id' => 123, |
| 74 | + 'name' => 'Test Term', |
| 75 | + 'description' => $description, |
| 76 | + ), |
| 77 | + 'context' => 'edit', |
| 78 | + 'expected_description' => esc_html( $description ), |
| 79 | + ), |
| 80 | + 'Array without term_id, edit context' => array( |
| 81 | + 'input_callback' => fn() => array( |
| 82 | + 'name' => 'Test Term', |
| 83 | + 'description' => $description, |
| 84 | + ), |
| 85 | + 'context' => 'edit', |
| 86 | + 'expected_description' => esc_html( $description ), |
| 87 | + ), |
| 88 | + 'Raw context' => array( |
| 89 | + 'input_callback' => fn() => (object) array( |
| 90 | + 'term_id' => 123, |
| 91 | + 'description' => $description, |
| 92 | + ), |
| 93 | + 'context' => 'raw', |
| 94 | + 'expected_description' => $description, |
| 95 | + ), |
| 96 | + 'Display context' => array( |
| 97 | + 'input_callback' => fn() => (object) array( |
| 98 | + 'term_id' => 123, |
| 99 | + 'description' => $description, |
| 100 | + ), |
| 101 | + 'context' => 'display', |
| 102 | + 'expected_description' => "<p>$description</p>\n", |
| 103 | + ), |
| 104 | + 'Attribute context' => array( |
| 105 | + 'input_callback' => fn() => (object) array( |
| 106 | + 'term_id' => 123, |
| 107 | + 'description' => $description, |
| 108 | + ), |
| 109 | + 'context' => 'attribute', |
| 110 | + 'expected_description' => esc_attr( "<p>$description</p>\n" ), |
| 111 | + ), |
| 112 | + 'JS context' => array( |
| 113 | + 'input_callback' => fn() => (object) array( |
| 114 | + 'term_id' => 123, |
| 115 | + 'description' => $description, |
| 116 | + ), |
| 117 | + 'context' => 'js', |
| 118 | + 'expected_description' => esc_js( "<p>$description</p>\n" ), |
| 119 | + ), |
| 120 | + ); |
| 121 | + } |
| 122 | +} |
0 commit comments