Skip to content

Commit 32e6cd0

Browse files
authored
Merge pull request #1168 from xwp/tests/taxonomies-connector
Taxonomies connector test implemented.
2 parents 6718a99 + 3250146 commit 32e6cd0

File tree

1 file changed

+143
-0
lines changed

1 file changed

+143
-0
lines changed
Lines changed: 143 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,143 @@
1+
<?php
2+
/**
3+
* Tests for Taxonomies Connector class callbacks.
4+
*
5+
* @package WP_Stream
6+
*/
7+
namespace WP_Stream;
8+
9+
class Test_WP_Stream_Connector_Taxonomies extends WP_StreamTestCase {
10+
/**
11+
* Runs before each test
12+
*/
13+
public function setUp() {
14+
parent::setUp();
15+
16+
$this->plugin->connectors->unload_connectors();
17+
18+
// Make partial of Connector_Taxonomies class, with mocked "log" function.
19+
$this->mock = $this->getMockBuilder( Connector_Taxonomies::class )
20+
->setMethods( array( 'log' ) )
21+
->getMock();
22+
23+
// Register connector.
24+
$this->mock->register();
25+
$this->mock->get_context_labels();
26+
}
27+
28+
public function test_callback_created_term() {
29+
// Expected log calls.
30+
$this->mock->expects( $this->once() )
31+
->method( 'log' )
32+
->with(
33+
$this->equalTo(
34+
_x(
35+
'"%1$s" %2$s created',
36+
'1: Term name, 2: Taxonomy singular label',
37+
'stream'
38+
)
39+
),
40+
$this->callback(
41+
function( $subject ) {
42+
$expected = array(
43+
'term_name' => 'test',
44+
'taxonomy_label' => 'Category',
45+
'taxonomy' => 'category',
46+
'term_parent' => 0
47+
);
48+
return $expected === array_intersect_key( $expected, $subject );
49+
}
50+
),
51+
$this->greaterThan( 0 ),
52+
$this->equalTo( 'category' ),
53+
$this->equalTo( 'created' )
54+
);
55+
56+
// Create term to trigger callback.
57+
wp_insert_term( 'test', 'category' );
58+
59+
// Check callback test action.
60+
$this->assertGreaterThan( 0, did_action( 'wp_stream_test_callback_created_term' ) );
61+
}
62+
63+
public function test_callback_delete_term() {
64+
// Create term for later use.
65+
$term_data = wp_insert_term( 'test', 'category' );
66+
67+
// Expected log calls.
68+
$this->mock->expects( $this->once() )
69+
->method( 'log' )
70+
->with(
71+
$this->equalTo(
72+
_x(
73+
'"%1$s" %2$s deleted',
74+
'1: Term name, 2: Taxonomy singular label',
75+
'stream'
76+
)
77+
),
78+
$this->equalTo(
79+
array(
80+
'term_name' => 'test',
81+
'taxonomy_label' => 'category',
82+
'term_id' => $term_data['term_id'],
83+
'taxonomy' => 'category',
84+
'term_parent' => 0
85+
)
86+
),
87+
$this->greaterThan( 0 ),
88+
$this->equalTo( 'category' ),
89+
$this->equalTo( 'deleted' )
90+
);
91+
92+
// Delete term to trigger callback.
93+
wp_delete_term( $term_data['term_id'], 'category' );
94+
95+
// Check callback test action.
96+
$this->assertGreaterThan( 0, did_action( 'wp_stream_test_callback_delete_term' ) );
97+
}
98+
99+
public function test_callback_edited_term() {
100+
// Create term for later use.
101+
$term_data = wp_insert_term( 'test', 'category' );
102+
103+
// Expected log calls.
104+
$this->mock->expects( $this->once() )
105+
->method( 'log' )
106+
->with(
107+
$this->equalTo(
108+
_x(
109+
'"%1$s" %2$s updated',
110+
'1: Term name, 2: Taxonomy singular label',
111+
'stream'
112+
)
113+
),
114+
$this->equalTo(
115+
array(
116+
'term_name' => 'test',
117+
'taxonomy_label' => 'category',
118+
'term_id' => $term_data['term_id'],
119+
'taxonomy' => 'category',
120+
'term_parent' => 0
121+
)
122+
),
123+
$this->greaterThan( 0 ),
124+
$this->equalTo( 'category' ),
125+
$this->equalTo( 'updated' )
126+
);
127+
128+
// Edit term to trigger callbacks.
129+
$this->assertFalse(
130+
is_wp_error(
131+
wp_update_term(
132+
$term_data['term_id'],
133+
'category',
134+
array( 'name' => 'testing' )
135+
)
136+
)
137+
);
138+
139+
// Check callback test action.
140+
$this->assertGreaterThan( 0, did_action( 'wp_stream_test_callback_edit_term' ) );
141+
$this->assertGreaterThan( 0, did_action( 'wp_stream_test_callback_edited_term' ) );
142+
}
143+
}

0 commit comments

Comments
 (0)