7
7
use lloc \Msls \MslsBlog ;
8
8
use lloc \Msls \MslsBlogCollection ;
9
9
use lloc \Msls \MslsOptions ;
10
+ use lloc \Msls \MslsOptionsTax ;
10
11
use lloc \Msls \MslsPostTag ;
11
12
use lloc \Msls \MslsTaxonomy ;
12
13
@@ -15,7 +16,7 @@ class TestMslsPostTag extends MslsUnitTestCase {
15
16
protected function setUp (): void {
16
17
parent ::setUp ();
17
18
18
- Functions \when ( 'get_option ' )->justReturn ( array () );
19
+ Functions \when ( 'get_option ' )->justReturn ( array ( ' de_DE ' => 42 ) );
19
20
Functions \expect ( 'is_admin ' )->andReturn ( true );
20
21
Functions \expect ( 'get_post_types ' )->andReturn ( array ( 'post ' , 'page ' ) );
21
22
@@ -33,7 +34,11 @@ protected function setUp(): void {
33
34
$ collection ->shouldReceive ( 'get ' )->andReturn ( $ blogs );
34
35
$ collection ->shouldReceive ( 'has_current_blog ' )->andReturnTrue ();
35
36
$ collection ->shouldReceive ( 'get_current_blog ' )->andReturn ( $ blogs [0 ] );
36
-
37
+ $ collection ->shouldReceive ( 'get_blog_id ' )->andReturnUsing (
38
+ function ( $ language ) {
39
+ return $ language === 'de_DE ' ? 1 : null ;
40
+ }
41
+ );
37
42
$ this ->test = new MslsPostTag ( $ options , $ collection );
38
43
}
39
44
@@ -85,6 +90,8 @@ public function test_edit_input(): void {
85
90
$ taxonomy ->shouldReceive ( 'get_request ' )->atLeast ()->once ()->andReturn ( 'post ' );
86
91
$ taxonomy ->shouldReceive ( 'acl_request ' )->atLeast ()->once ()->andReturn ( array ( 'taxonomy ' , 'post_tag ' ) );
87
92
93
+ $ term = \Mockery::mock ( \WP_Term::class );
94
+
88
95
Functions \expect ( 'msls_content_types ' )->atLeast ()->once ()->andReturn ( $ taxonomy );
89
96
Functions \expect ( 'get_queried_object_id ' )->atLeast ()->once ()->andReturn ( 42 );
90
97
Functions \expect ( 'is_woocommerce ' )->atLeast ()->once ()->andReturn ( false );
@@ -93,6 +100,8 @@ public function test_edit_input(): void {
93
100
Functions \expect ( 'add_query_arg ' )->atLeast ()->once ()->andReturn ( 'added_query_arg ' );
94
101
Functions \expect ( 'get_current_blog_id ' )->atLeast ()->once ()->andReturn ( 23 );
95
102
Functions \expect ( 'get_admin_url ' )->atLeast ()->once ()->andReturn ( '/wp-admin/edit-tags.php ' );
103
+ Functions \expect ( 'get_term ' )->atLeast ()->once ()->andReturn ( $ term );
104
+ Functions \expect ( 'get_edit_term_link ' )->atLeast ()->once ()->andReturn ( 'edit_term_link ' );
96
105
97
106
$ output = '<tr>
98
107
<th colspan="2">
@@ -102,10 +111,10 @@ public function test_edit_input(): void {
102
111
</th>
103
112
</tr><tr class="form-field">
104
113
<th scope="row">
105
- <label for="msls_title_"><a title="Create a new translation in the de_DE-blog" href="/wp-admin/edit-tags.php "><span class="language-badge de_DE"><span>de</span><span>DE</span></span></a> </label>
114
+ <label for="msls_title_"><a title="Edit the translation in the de_DE-blog" href="edit_term_link "><span class="language-badge de_DE"><span>de</span><span>DE</span></span></a> </label>
106
115
</th>
107
116
<td>
108
- <input type="hidden" id="msls_id_" name="msls_input_de_DE" value=""/>
117
+ <input type="hidden" id="msls_id_" name="msls_input_de_DE" value="42 "/>
109
118
<input class="msls_title" id="msls_title_" name="msls_title_" type="text" value=""/>
110
119
</td>
111
120
</tr><tr class="form-field">
@@ -119,13 +128,10 @@ public function test_edit_input(): void {
119
128
</tr> ' ;
120
129
121
130
$ this ->expectOutputString ( $ output );
122
-
123
- $ tag = \Mockery::mock ( \WP_Term::class );
124
-
125
- $ this ->test ->edit_input ( $ tag , 'test ' );
131
+ $ this ->test ->edit_input ( $ term , 'test ' );
126
132
127
133
// second call should not output anything
128
- $ this ->test ->edit_input ( $ tag , 'test ' );
134
+ $ this ->test ->edit_input ( $ term , 'test ' );
129
135
}
130
136
131
137
public function test_add_input (): void {
@@ -142,6 +148,7 @@ public function test_add_input(): void {
142
148
Functions \expect ( 'add_query_arg ' )->atLeast ()->once ()->andReturn ( 'added_query_arg ' );
143
149
Functions \expect ( 'get_current_blog_id ' )->atLeast ()->once ()->andReturn ( 23 );
144
150
Functions \expect ( 'get_admin_url ' )->atLeast ()->once ()->andReturn ( '/wp-admin/edit-tags.php ' );
151
+ Functions \expect ( 'get_term ' )->atLeast ()->once ()->andReturnNull ();
145
152
146
153
$ output = '<div class="form-field"><h3>Multisite Language Switcher</h3>
147
154
<input type="hidden" name="msls_post_type" id="msls_post_type" value="post"/>
@@ -178,4 +185,64 @@ public function test_set() {
178
185
$ this ->expectNotToPerformAssertions ();
179
186
$ this ->test ->set ( 42 );
180
187
}
188
+
189
+ public function test_maybe_set_linked_term_origin_lang (): void {
190
+ $ mydata = \Mockery::mock ( MslsOptionsTax::class );
191
+ $ mydata ->de_DE = 42 ;
192
+
193
+ Functions \expect ( 'filter_has_var ' )->twice ()->andReturnTrue ();
194
+ Functions \expect ( 'filter_input ' )->once ()->andReturn ( 'de_DE ' );
195
+
196
+ $ result = $ this ->test ->maybe_set_linked_term ( $ mydata );
197
+
198
+ $ this ->assertSame ( $ mydata , $ result );
199
+ }
200
+
201
+ public function test_maybe_set_linked_term_blog_id_null (): void {
202
+ $ mydata = \Mockery::mock ( MslsOptionsTax::class );
203
+ $ mydata ->de_DE = 42 ;
204
+
205
+ Functions \expect ( 'filter_has_var ' )->twice ()->andReturnTrue ();
206
+ Functions \expect ( 'filter_input ' )->twice ()->andReturn ( 'fr_FR ' , 13 );
207
+
208
+ $ result = $ this ->test ->maybe_set_linked_term ( $ mydata );
209
+
210
+ $ this ->assertSame ( $ mydata , $ result );
211
+ }
212
+
213
+ public function test_maybe_set_linked_term_origin_term_wrong (): void {
214
+ $ mydata = \Mockery::mock ( MslsOptionsTax::class );
215
+ $ mydata ->en_US = 42 ;
216
+
217
+ Functions \expect ( 'filter_has_var ' )->twice ()->andReturnTrue ();
218
+ Functions \expect ( 'filter_input ' )->twice ()->andReturn ( 'de_DE ' , 13 );
219
+ Functions \expect ( 'switch_to_blog ' )->once ();
220
+ Functions \expect ( 'get_term ' )->once ()->andReturn ( (object ) array () );
221
+ Functions \expect ( 'restore_current_blog ' )->once ();
222
+
223
+ $ result = $ this ->test ->maybe_set_linked_term ( $ mydata );
224
+
225
+ $ this ->assertSame ( $ mydata , $ result );
226
+ }
227
+
228
+ public function test_maybe_set_linked_term_happy_path (): void {
229
+ $ mydata = \Mockery::mock ( MslsOptionsTax::class );
230
+ $ mydata ->en_US = 42 ;
231
+
232
+ $ term = \Mockery::mock ( \WP_Term::class );
233
+ $ term ->en_US = 42 ;
234
+
235
+ Functions \expect ( 'filter_has_var ' )->twice ()->andReturnTrue ();
236
+ Functions \expect ( 'filter_input ' )->twice ()->andReturn ( 'de_DE ' , 13 );
237
+ Functions \expect ( 'switch_to_blog ' )->once ();
238
+ Functions \expect ( 'get_term ' )->once ()->andReturn ( $ term );
239
+ Functions \expect ( 'restore_current_blog ' )->once ();
240
+
241
+ $ result = $ this ->test ->maybe_set_linked_term ( $ mydata );
242
+
243
+ $ expected = array ( $ term ->en_US , 13 );
244
+ $ actual = array ( $ result ->en_US , $ result ->de_DE );
245
+
246
+ $ this ->assertEquals ( $ expected , $ actual );
247
+ }
181
248
}
0 commit comments