forked from WordPress/wordpress-develop
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgetTermField.php
More file actions
174 lines (136 loc) · 5.72 KB
/
getTermField.php
File metadata and controls
174 lines (136 loc) · 5.72 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
<?php
/**
* @group taxonomy
*/
class Tests_Term_getTermField extends WP_UnitTestCase {
public $taxonomy = 'wptests_tax';
function setUp() {
parent::setUp();
register_taxonomy( $this->taxonomy, 'post' );
}
/**
* @ticket 34245
*/
public function test_get_term_field_should_not_return_error_for_empty_taxonomy() {
$term = self::factory()->term->create_and_get( array( 'taxonomy' => $this->taxonomy ) );
$found = get_term_field( 'taxonomy', $term->term_id, '' );
$this->assertNotWPError( $found );
$this->assertSame( $this->taxonomy, $found );
}
/**
* @ticket 34245
*/
public function test_get_term_field_supplying_a_taxonomy() {
$term = self::factory()->term->create_and_get( array( 'taxonomy' => $this->taxonomy ) );
$found = get_term_field( 'taxonomy', $term->term_id, $term->taxonomy );
$this->assertSame( $this->taxonomy, $found );
}
/**
* @ticket 34245
*/
public function test_get_term_field_supplying_no_taxonomy() {
$term = self::factory()->term->create_and_get( array( 'taxonomy' => $this->taxonomy ) );
$found = get_term_field( 'taxonomy', $term->term_id );
$this->assertSame( $this->taxonomy, $found );
}
/**
* @ticket 34245
*/
public function test_get_term_field_should_accept_a_WP_Term_object_term_id_or_object() {
$term = self::factory()->term->create_and_get( array( 'taxonomy' => $this->taxonomy ) );
$this->assertInstanceOf( 'WP_Term', $term );
$this->assertSame( $term->term_id, get_term_field( 'term_id', $term ) );
$this->assertSame( $term->term_id, get_term_Field( 'term_id', $term->data ) );
$this->assertSame( $term->term_id, get_term_field( 'term_id', $term->term_id ) );
}
/**
* @ticket 34245
*/
public function test_get_term_field_invalid_taxonomy_should_return_WP_Error() {
$term = self::factory()->term->create_and_get( array( 'taxonomy' => $this->taxonomy ) );
$found = get_term_field( 'taxonomy', $term, 'foo-taxonomy' );
$this->assertWPError( $found );
$this->assertSame( 'invalid_taxonomy', $found->get_error_code() );
}
/**
* @ticket 34245
*/
public function test_get_term_field_invalid_term_should_return_WP_Error() {
$found = get_term_field( 'taxonomy', 0, $this->taxonomy );
$this->assertWPError( $found );
$this->assertSame( 'invalid_term', $found->get_error_code() );
$_found = get_term_field( 'taxonomy', 0 );
$this->assertWPError( $_found );
$this->assertSame( 'invalid_term', $_found->get_error_code() );
}
public function test_get_term_field_term_id() {
$term = self::factory()->term->create_and_get( array( 'taxonomy' => $this->taxonomy ) );
$this->assertSame( $term->term_id, get_term_field( 'term_id', $term ) );
$this->assertSame( $term->term_id, get_term_field( 'term_id', $term->data ) );
$this->assertSame( $term->term_id, get_term_field( 'term_id', $term->term_id ) );
}
public function test_get_term_field_name() {
$name = rand_str( 15 );
$term = self::factory()->term->create_and_get( array(
'name' => $name,
'taxonomy' => $this->taxonomy
) );
$this->assertSame( $name, get_term_field( 'name', $term ) );
$this->assertSame( $name, get_term_field( 'name', $term->data ) );
$this->assertSame( $name, get_term_field( 'name', $term->term_id ) );
}
public function test_get_term_field_slug_when_slug_is_set() {
$slug = rand_str( 15 );
$term = self::factory()->term->create_and_get( array(
'taxonomy' => $this->taxonomy,
'slug' => $slug
) );
$this->assertSame( $slug, get_term_field( 'slug', $term ) );
$this->assertSame( $slug, get_term_field( 'slug', $term->data ) );
$this->assertSame( $slug, get_term_field( 'slug', $term->term_id ) );
}
public function test_get_term_field_slug_when_slug_falls_back_from_name() {
$name = rand_str( 15 );
$term = self::factory()->term->create_and_get( array(
'taxonomy' => $this->taxonomy,
'name' => $name
) );
$this->assertSame( $name, get_term_field( 'slug', $term ) );
$this->assertSame( $name, get_term_field( 'slug', $term->data ) );
$this->assertSame( $name, get_term_field( 'slug', $term->term_id ) );
}
public function test_get_term_field_slug_when_slug_and_name_are_not_set() {
$term = self::factory()->term->create_and_get( array(
'taxonomy' => $this->taxonomy
) );
$this->assertSame( $term->slug, get_term_field( 'slug', $term ) );
$this->assertSame( $term->slug, get_term_field( 'slug', $term->data ) );
$this->assertSame( $term->slug, get_term_field( 'slug', $term->term_id ) );
}
public function test_get_term_field_taxonomy() {
$term = self::factory()->term->create_and_get( array( 'taxonomy' => $this->taxonomy ) );
$this->assertSame( $this->taxonomy, get_term_field( 'taxonomy', $term ) );
$this->assertSame( $this->taxonomy, get_term_field( 'taxonomy', $term->data ) );
$this->assertSame( $this->taxonomy, get_term_field( 'taxonomy', $term->term_id ) );
}
public function test_get_term_field_description() {
$desc = wpautop( rand_str() );
$term = self::factory()->term->create_and_get( array(
'taxonomy' => $this->taxonomy,
'description' => $desc
) );
$this->assertSame( $desc, get_term_field( 'description', $term ) );
$this->assertSame( $desc, get_term_field( 'description', $term->data ) );
$this->assertSame( $desc, get_term_field( 'description', $term->term_id ) );
}
public function test_get_term_field_parent() {
$parent = self::factory()->term->create_and_get( array( 'taxonomy' => $this->taxonomy ) );
$term = self::factory()->term->create_and_get( array(
'taxonomy' => $this->taxonomy,
'parent' => $parent->term_id
) );
$this->assertSame( $parent->term_id, get_term_field( 'parent', $term ) );
$this->assertSame( $parent->term_id, get_term_field( 'parent', $term->data ) );
$this->assertSame( $parent->term_id, get_term_field( 'parent', $term->term_id ) );
}
}