3
3
namespace lloc \MslsTests ;
4
4
5
5
use Brain \Monkey \Functions ;
6
+ use lloc \Msls \MslsBlog ;
6
7
use lloc \Msls \MslsBlogCollection ;
7
8
use lloc \Msls \MslsFields ;
8
9
use lloc \Msls \MslsJson ;
11
12
12
13
class TestMslsMetaBox extends MslsUnitTestCase {
13
14
15
+
14
16
protected function setUp (): void {
15
- $ options = \Mockery::mock ( MslsOptions::class );
17
+ $ blog = \Mockery::mock ( MslsBlog::class );
18
+ $ blog ->shouldReceive ( 'get_language ' )->andReturn ( 'de_DE ' );
19
+
20
+ $ options = \Mockery::mock ( MslsOptions::class );
21
+
16
22
$ collection = \Mockery::mock ( MslsBlogCollection::class );
23
+ $ collection ->shouldReceive ( 'get ' )->andReturn ( array ( $ blog ) );
17
24
18
25
$ this ->test = new MslsMetaBox ( $ options , $ collection );
19
26
}
@@ -30,10 +37,8 @@ public function test_suggest(): void {
30
37
Functions \expect ( 'filter_input ' )->once ()->with ( INPUT_GET , MslsFields::FIELD_S , FILTER_SANITIZE_FULL_SPECIAL_CHARS )->andReturn ( 17 );
31
38
Functions \expect ( 'get_post_stati ' )->once ()->andReturn ( array ( 'pending ' , 'draft ' , 'future ' ) );
32
39
Functions \expect ( 'get_the_title ' )->once ()->andReturn ( 'Test ' );
33
-
34
40
Functions \expect ( 'sanitize_text_field ' )->times ( 2 )->andReturnFirstArg ();
35
41
Functions \expect ( 'get_posts ' )->once ()->andReturn ( array ( $ post ) );
36
-
37
42
Functions \expect ( 'switch_to_blog ' )->once ();
38
43
Functions \expect ( 'restore_current_blog ' )->once ();
39
44
Functions \expect ( 'wp_reset_postdata ' )->once ();
@@ -80,4 +85,152 @@ public function test_render_options() {
80
85
81
86
$ this ->assertEquals ( '<option value="42" selected="selected">A random title</option> ' , $ this ->test ->render_options ( 'post ' , 42 ) );
82
87
}
88
+
89
+ public function add_data_provider () {
90
+ return array (
91
+ array ( array ( 'post ' , 'page ' ), 8 , 8 , true , true ),
92
+ array ( array ( 'book ' ), 3 , 6 , false , false ),
93
+ );
94
+ }
95
+
96
+ /**
97
+ * @dataProvider add_data_provider
98
+ */
99
+ public function test_add ( $ post_type , $ fcount , $ ocount , $ content_import , $ autocomplete ) {
100
+ $ options = \Mockery::mock ( MslsOptions::class );
101
+ $ options ->activate_content_import = $ content_import ;
102
+ $ options ->activate_autocomplete = $ autocomplete ;
103
+
104
+ Functions \expect ( 'get_post_types ' )->andReturn ( $ post_type );
105
+ Functions \expect ( 'add_meta_box ' )->times ( $ fcount );
106
+ Functions \expect ( '__ ' )->times ( $ fcount )->andReturnFirstArg ();
107
+ Functions \expect ( 'msls_options ' )->times ( $ ocount )->andReturn ( $ options );
108
+
109
+ $ this ->expectOutputString ( '' );
110
+
111
+ $ this ->test ->add ();
112
+ }
113
+
114
+ public function test_render_select_not_hierarchical () {
115
+ global $ post ;
116
+
117
+ $ post = \Mockery::mock ( 'WP_Post ' );
118
+ $ post ->ID = 42 ;
119
+
120
+ $ post_type = \Mockery::mock ( \WP_Post_Type::class );
121
+ $ post_type ->hierarchical = false ;
122
+
123
+ Functions \expect ( 'get_post_type ' )->once ()->andReturn ( 'page ' );
124
+ Functions \expect ( 'get_option ' )->once ()->andReturn ( array () );
125
+ Functions \expect ( 'wp_nonce_field ' )->once ()->andReturn ( 'nonce_field ' );
126
+ Functions \expect ( 'switch_to_blog ' )->once ();
127
+ Functions \expect ( 'restore_current_blog ' )->once ();
128
+ Functions \expect ( 'esc_attr ' )->times ( 4 )->andReturnFirstArg ();
129
+ Functions \expect ( 'esc_url ' )->once ()->andReturnFirstArg ();
130
+ Functions \expect ( 'wp_kses ' )->once ()->andReturnFirstArg ();
131
+ Functions \expect ( '__ ' )->once ()->andReturnFirstArg ();
132
+ Functions \expect ( 'add_query_arg ' )->once ()->andReturn ( 'query_args ' );
133
+ Functions \expect ( 'get_post_type_object ' )->once ()->andReturn ( $ post_type );
134
+ Functions \expect ( 'get_post_stati ' )->once ()->andReturn ( array ( 'draft ' , 'public ' , 'private ' ) );
135
+ Functions \expect ( 'get_posts ' )->once ()->andReturn ( array () );
136
+ Functions \expect ( 'get_current_blog_id ' )->once ()->andReturn ( 1 );
137
+ Functions \expect ( 'get_admin_url ' )->once ()->andReturn ( 'admin-url-empty ' );
138
+
139
+ $ expected = '<ul><li><label for="msls_input_de_DE msls-icon-wrapper "><a title="Create a new translation in the de_DE-blog" href="admin-url-empty"><span class="language-badge de_DE"><span>de</span><span>DE</span></span></a> </label><select name="msls_input_de_DE"><option value="0"></option></select></li></ul> ' ;
140
+ $ this ->expectOutputString ( $ expected );
141
+
142
+ $ this ->test ->render_select ();
143
+ }
144
+
145
+ public function test_render_select_hierarchical () {
146
+ global $ post ;
147
+
148
+ $ post = \Mockery::mock ( 'WP_Post ' );
149
+ $ post ->ID = 42 ;
150
+
151
+ $ post_type = \Mockery::mock ( \WP_Post_Type::class );
152
+ $ post_type ->hierarchical = true ;
153
+
154
+ Functions \expect ( 'get_post_type ' )->once ()->andReturn ( 'page ' );
155
+ Functions \expect ( 'get_option ' )->once ()->andReturn ( array ( 'de_DE ' => 42 ) );
156
+ Functions \expect ( 'wp_nonce_field ' )->once ()->andReturn ( 'nonce_field ' );
157
+ Functions \expect ( 'switch_to_blog ' )->once ();
158
+ Functions \expect ( 'restore_current_blog ' )->once ();
159
+ Functions \expect ( 'esc_attr ' )->times ( 4 )->andReturnFirstArg ();
160
+ Functions \expect ( 'esc_url ' )->once ()->andReturnFirstArg ();
161
+ Functions \expect ( 'wp_kses ' )->once ()->andReturnFirstArg ();
162
+ Functions \expect ( '__ ' )->once ()->andReturnFirstArg ();
163
+ Functions \expect ( 'add_query_arg ' )->once ()->andReturn ( 'query_args ' );
164
+ Functions \expect ( 'get_post_type_object ' )->once ()->andReturn ( $ post_type );
165
+ Functions \expect ( 'wp_dropdown_pages ' )->once ()->andReturn ( '<select name="msls_input_region_Code"><option value="0">--some value</option></select> ' );
166
+ Functions \expect ( 'get_edit_post_link ' )->once ()->andReturn ( 'edit-post-link ' );
167
+
168
+ $ expected = '<ul><li><label for="msls_input_de_DE msls-icon-wrapper "><a title="Edit the translation in the de_DE-blog" href="edit-post-link"><span class="language-badge de_DE"><span>de</span><span>DE</span></span></a> </label><select name="msls_input_region_Code"><option value="0">--some value</option></select></li></ul> ' ;
169
+ $ this ->expectOutputString ( $ expected );
170
+
171
+ $ this ->test ->render_select ();
172
+ }
173
+
174
+ public function test_render_input () {
175
+ global $ post ;
176
+
177
+ $ post = \Mockery::mock ( 'WP_Post ' );
178
+ $ post ->ID = 42 ;
179
+
180
+ $ post_type = \Mockery::mock ( \WP_Post_Type::class );
181
+
182
+ Functions \expect ( 'get_post_types ' )->once ()->andReturn ( array ( 'post ' , 'page ' ) );
183
+ Functions \expect ( 'get_post_type ' )->once ()->andReturn ( 'page ' );
184
+ Functions \expect ( 'get_the_title ' )->once ()->andReturn ( 'Test ' );
185
+
186
+ Functions \when ( 'plugin_dir_path ' )->justReturn ( dirname ( __DIR__ , 2 ) . '/ ' );
187
+
188
+ Functions \expect ( 'get_option ' )->once ()->andReturn ( array ( 'de_DE ' => 42 ) );
189
+ Functions \expect ( 'wp_nonce_field ' )->once ()->andReturn ( 'nonce_field ' );
190
+ Functions \expect ( 'switch_to_blog ' )->once ();
191
+ Functions \expect ( 'restore_current_blog ' )->once ();
192
+ Functions \expect ( 'esc_attr ' )->times ( 4 )->andReturnFirstArg ();
193
+ Functions \expect ( 'esc_html ' )->once ()->andReturnFirstArg ();
194
+ Functions \expect ( 'esc_url ' )->once ()->andReturnFirstArg ();
195
+ Functions \expect ( '__ ' )->once ()->andReturnFirstArg ();
196
+ Functions \expect ( 'get_edit_post_link ' )->once ()->andReturn ( 'edit-post-link ' );
197
+
198
+ $ expected = '<ul><li class=""><label for="msls_title_ msls-icon-wrapper "><a title="Edit the translation in the de_DE-blog" href="edit-post-link"><span class="flag-icon flag-icon-de">de_DE</span></a> </label><input type="hidden" id="msls_id_" name="msls_input_de_DE" value="42"/><input class="msls_title" id="msls_title_" name="msls_title_" type="text" value="Test"/></li></ul><input type="hidden" name="msls_post_type" id="msls_post_type" value="page"/><input type="hidden" name="msls_action" id="msls_action" value="suggest_posts"/> ' ;
199
+
200
+ $ this ->expectOutputString ( $ expected );
201
+
202
+ $ this ->test ->render_input ();
203
+ }
204
+
205
+ public function test_render_select_only_one_blog () {
206
+ $ options = \Mockery::mock ( MslsOptions::class );
207
+
208
+ $ collection = \Mockery::mock ( MslsBlogCollection::class );
209
+ $ collection ->shouldReceive ( 'get ' )->andReturn ( array () );
210
+
211
+ Functions \expect ( '__ ' )->once ()->andReturnFirstArg ();
212
+
213
+ $ this ->test = new MslsMetaBox ( $ options , $ collection );
214
+
215
+ $ expected = '<p>You should define at least another blog in a different language in order to have some benefit from this plugin!</p> ' ;
216
+ $ this ->expectOutputString ( $ expected );
217
+
218
+ $ this ->test ->render_select ();
219
+ }
220
+
221
+ public function test_render_input_only_one_blog () {
222
+ $ options = \Mockery::mock ( MslsOptions::class );
223
+
224
+ $ collection = \Mockery::mock ( MslsBlogCollection::class );
225
+ $ collection ->shouldReceive ( 'get ' )->andReturn ( array () );
226
+
227
+ Functions \expect ( '__ ' )->once ()->andReturnFirstArg ();
228
+
229
+ $ this ->test = new MslsMetaBox ( $ options , $ collection );
230
+
231
+ $ expected = '<p>You should define at least another blog in a different language in order to have some benefit from this plugin!</p> ' ;
232
+ $ this ->expectOutputString ( $ expected );
233
+
234
+ $ this ->test ->render_input ();
235
+ }
83
236
}
0 commit comments