|
| 1 | +<?php |
| 2 | + |
| 3 | +/** |
| 4 | + * Tests for sanitize_category(). |
| 5 | + * |
| 6 | + * @group taxonomy |
| 7 | + * @group category |
| 8 | + * |
| 9 | + * @covers ::sanitize_category |
| 10 | + */ |
| 11 | +class Tests_Category_SanitizeCategory extends WP_UnitTestCase { |
| 12 | + |
| 13 | + /** |
| 14 | + * Tests that sanitize_category() sanitizes a category object, casts integer fields |
| 15 | + * to non-negative integers, and sets the filter property to the default 'display' context. |
| 16 | + * |
| 17 | + * @ticket 65819 |
| 18 | + */ |
| 19 | + public function test_sanitizes_category_object_with_default_context() { |
| 20 | + $category = (object) array( |
| 21 | + 'term_id' => '42', |
| 22 | + 'name' => 'Category Name', |
| 23 | + 'description' => 'Category Description', |
| 24 | + 'slug' => 'category-name', |
| 25 | + 'count' => '7', |
| 26 | + 'parent' => '-3', |
| 27 | + 'term_group' => '2', |
| 28 | + 'term_taxonomy_id' => '55', |
| 29 | + 'object_id' => '100', |
| 30 | + ); |
| 31 | + |
| 32 | + $result = sanitize_category( $category ); |
| 33 | + |
| 34 | + $this->assertIsObject( $result ); |
| 35 | + $this->assertSame( 42, $result->term_id ); |
| 36 | + $this->assertSame( 7, $result->count ); |
| 37 | + $this->assertSame( 0, $result->parent ); |
| 38 | + $this->assertSame( 2, $result->term_group ); |
| 39 | + $this->assertSame( 55, $result->term_taxonomy_id ); |
| 40 | + $this->assertSame( 100, $result->object_id ); |
| 41 | + $this->assertSame( 'display', $result->filter ); |
| 42 | + } |
| 43 | + |
| 44 | + /** |
| 45 | + * Tests that sanitize_category() sanitizes a category array, casts integer fields |
| 46 | + * to non-negative integers, and sets the 'filter' key to the given context. |
| 47 | + * |
| 48 | + * @ticket 65819 |
| 49 | + */ |
| 50 | + public function test_sanitizes_category_array() { |
| 51 | + $category = array( |
| 52 | + 'term_id' => '15', |
| 53 | + 'name' => '<b>Category</b>', |
| 54 | + 'description' => '<p>Description</p>', |
| 55 | + 'slug' => 'category', |
| 56 | + 'count' => '-5', |
| 57 | + 'parent' => '3', |
| 58 | + 'term_group' => '-1', |
| 59 | + 'term_taxonomy_id' => '19', |
| 60 | + 'object_id' => '8', |
| 61 | + ); |
| 62 | + |
| 63 | + $result = sanitize_category( $category, 'edit' ); |
| 64 | + |
| 65 | + $this->assertIsArray( $result ); |
| 66 | + $this->assertSame( 15, $result['term_id'] ); |
| 67 | + $this->assertSame( esc_attr( '<b>Category</b>' ), $result['name'] ); |
| 68 | + $this->assertSame( esc_html( '<p>Description</p>' ), $result['description'] ); |
| 69 | + $this->assertSame( 0, $result['count'] ); |
| 70 | + $this->assertSame( 3, $result['parent'] ); |
| 71 | + $this->assertSame( 0, $result['term_group'] ); |
| 72 | + $this->assertSame( 19, $result['term_taxonomy_id'] ); |
| 73 | + $this->assertSame( 8, $result['object_id'] ); |
| 74 | + $this->assertSame( 'edit', $result['filter'] ); |
| 75 | + } |
| 76 | + |
| 77 | + /** |
| 78 | + * Tests that sanitize_category() passes the category ID and 'category' taxonomy |
| 79 | + * to the context filters, defaulting term_id to 0 when omitted. |
| 80 | + * |
| 81 | + * @ticket 65819 |
| 82 | + */ |
| 83 | + public function test_passes_category_id_and_taxonomy_to_filters() { |
| 84 | + $term_filter = new MockAction(); |
| 85 | + $category_filter = new MockAction(); |
| 86 | + |
| 87 | + add_filter( 'edit_term_name', array( $term_filter, 'filter' ), 10, 3 ); |
| 88 | + add_filter( 'edit_category_name', array( $category_filter, 'filter' ), 10, 2 ); |
| 89 | + |
| 90 | + sanitize_category( |
| 91 | + array( |
| 92 | + 'term_id' => 25, |
| 93 | + 'name' => 'News', |
| 94 | + ), |
| 95 | + 'edit' |
| 96 | + ); |
| 97 | + |
| 98 | + // Also test when term_id is omitted (defaults to 0). |
| 99 | + sanitize_category( |
| 100 | + (object) array( |
| 101 | + 'name' => 'Updates', |
| 102 | + ), |
| 103 | + 'edit' |
| 104 | + ); |
| 105 | + |
| 106 | + $this->assertSame( |
| 107 | + array( |
| 108 | + array( 'News', 25, 'category' ), |
| 109 | + array( 'Updates', 0, 'category' ), |
| 110 | + ), |
| 111 | + $term_filter->get_args() |
| 112 | + ); |
| 113 | + $this->assertSame( |
| 114 | + array( |
| 115 | + array( 'News', 25 ), |
| 116 | + array( 'Updates', 0 ), |
| 117 | + ), |
| 118 | + $category_filter->get_args() |
| 119 | + ); |
| 120 | + } |
| 121 | + |
| 122 | + /** |
| 123 | + * Tests that sanitize_category() does not add fields that are not set on the input. |
| 124 | + * |
| 125 | + * @ticket 65819 |
| 126 | + */ |
| 127 | + public function test_does_not_add_unset_fields() { |
| 128 | + $object_result = sanitize_category( (object) array( 'name' => 'Only Name' ), 'raw' ); |
| 129 | + $this->assertSame( |
| 130 | + array( |
| 131 | + 'name' => 'Only Name', |
| 132 | + 'filter' => 'raw', |
| 133 | + ), |
| 134 | + get_object_vars( $object_result ) |
| 135 | + ); |
| 136 | + |
| 137 | + $array_result = sanitize_category( array( 'name' => 'Only Name' ), 'raw' ); |
| 138 | + $this->assertSame( |
| 139 | + array( |
| 140 | + 'name' => 'Only Name', |
| 141 | + 'filter' => 'raw', |
| 142 | + ), |
| 143 | + $array_result |
| 144 | + ); |
| 145 | + } |
| 146 | +} |
0 commit comments