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
@@ -31,7 +32,13 @@ protected function setUp(): void {
31
32
32
33
$ collection = \Mockery::mock ( MslsBlogCollection::class );
33
34
$ collection ->shouldReceive ( 'get ' )->andReturn ( $ blogs );
34
-
35
+ $ collection ->shouldReceive ( 'has_current_blog ' )->andReturnTrue ();
36
+ $ collection ->shouldReceive ( 'get_current_blog ' )->andReturn ( $ blogs [0 ] );
37
+ $ collection ->shouldReceive ( 'get_blog_id ' )->andReturnUsing (
38
+ function ( $ language ) {
39
+ return $ language === 'de_DE ' ? 1 : null ;
40
+ }
41
+ );
35
42
$ this ->test = new MslsPostTag ( $ options , $ collection );
36
43
}
37
44
@@ -83,6 +90,8 @@ public function test_edit_input(): void {
83
90
$ taxonomy ->shouldReceive ( 'get_request ' )->atLeast ()->once ()->andReturn ( 'post ' );
84
91
$ taxonomy ->shouldReceive ( 'acl_request ' )->atLeast ()->once ()->andReturn ( array ( 'taxonomy ' , 'post_tag ' ) );
85
92
93
+ $ term = \Mockery::mock ( \WP_Term::class );
94
+
86
95
Functions \expect ( 'msls_content_types ' )->atLeast ()->once ()->andReturn ( $ taxonomy );
87
96
Functions \expect ( 'get_queried_object_id ' )->atLeast ()->once ()->andReturn ( 42 );
88
97
Functions \expect ( 'is_woocommerce ' )->atLeast ()->once ()->andReturn ( false );
@@ -91,6 +100,8 @@ public function test_edit_input(): void {
91
100
Functions \expect ( 'add_query_arg ' )->atLeast ()->once ()->andReturn ( 'added_query_arg ' );
92
101
Functions \expect ( 'get_current_blog_id ' )->atLeast ()->once ()->andReturn ( 23 );
93
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 ' );
94
105
95
106
$ output = '<tr>
96
107
<th colspan="2">
@@ -100,10 +111,10 @@ public function test_edit_input(): void {
100
111
</th>
101
112
</tr><tr class="form-field">
102
113
<th scope="row">
103
- <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>
104
115
</th>
105
116
<td>
106
- <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 "/>
107
118
<input class="msls_title" id="msls_title_" name="msls_title_" type="text" value=""/>
108
119
</td>
109
120
</tr><tr class="form-field">
@@ -117,13 +128,10 @@ public function test_edit_input(): void {
117
128
</tr> ' ;
118
129
119
130
$ this ->expectOutputString ( $ output );
120
-
121
- $ tag = \Mockery::mock ( \WP_Term::class );
122
-
123
- $ this ->test ->edit_input ( $ tag , 'test ' );
131
+ $ this ->test ->edit_input ( $ term , 'test ' );
124
132
125
133
// second call should not output anything
126
- $ this ->test ->edit_input ( $ tag , 'test ' );
134
+ $ this ->test ->edit_input ( $ term , 'test ' );
127
135
}
128
136
129
137
public function test_add_input (): void {
@@ -140,6 +148,7 @@ public function test_add_input(): void {
140
148
Functions \expect ( 'add_query_arg ' )->atLeast ()->once ()->andReturn ( 'added_query_arg ' );
141
149
Functions \expect ( 'get_current_blog_id ' )->atLeast ()->once ()->andReturn ( 23 );
142
150
Functions \expect ( 'get_admin_url ' )->atLeast ()->once ()->andReturn ( '/wp-admin/edit-tags.php ' );
151
+ Functions \expect ( 'get_term ' )->atLeast ()->once ()->andReturnNull ();
143
152
144
153
$ output = '<div class="form-field"><h3>Multisite Language Switcher</h3>
145
154
<input type="hidden" name="msls_post_type" id="msls_post_type" value="post"/>
@@ -163,4 +172,77 @@ public function test_the_input_no_blogs(): void {
163
172
164
173
$ this ->assertFalse ( $ value );
165
174
}
175
+
176
+ public function test_set () {
177
+ $ taxonomy = \Mockery::mock ( MslsTaxonomy::class );
178
+ $ taxonomy ->shouldReceive ( 'acl_request ' )->once ()->andReturn ( 'post_tag ' );
179
+
180
+ Functions \expect ( 'msls_content_types ' )->once ()->andReturn ( $ taxonomy );
181
+ Functions \expect ( 'delete_option ' )->twice ();
182
+ Functions \expect ( 'switch_to_blog ' )->twice ();
183
+ Functions \expect ( 'restore_current_blog ' )->twice ();
184
+
185
+ $ this ->expectNotToPerformAssertions ();
186
+ $ this ->test ->set ( 42 );
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
+ }
166
248
}
0 commit comments