Skip to content

Commit 6feb1c6

Browse files
committed
Render methods tested
1 parent ff3cd67 commit 6feb1c6

File tree

3 files changed

+100
-14
lines changed

3 files changed

+100
-14
lines changed

includes/MslsMetaBox.php

Lines changed: 4 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -279,11 +279,11 @@ public function render_option( $post_id, $msls_id ) {
279279
}
280280

281281
/**
282-
* Render the suggest input-field
282+
* Render a suggest input-field
283283
*
284284
* @param bool $echo Whether the metabox markup should be echoed to the page or not.
285285
*/
286-
public function render_input( $echo = true ) {
286+
public function render_input() {
287287
$blogs = $this->collection->get();
288288

289289
if ( $blogs ) {
@@ -321,11 +321,7 @@ public function render_input( $echo = true ) {
321321
}
322322

323323
$items .= sprintf(
324-
'<li class="">
325-
<label for="msls_title_%1$s msls-icon-wrapper %6$s">%2$s</label>
326-
<input type="hidden" id="msls_id_%1$s" name="msls_input_%3$s" value="%4$s"/>
327-
<input class="msls_title" id="msls_title_%1$s" name="msls_title_%1$s" type="text" value="%5$s"/>
328-
</li>',
324+
'<li class=""><label for="msls_title_%1$s msls-icon-wrapper %6$s">%2$s</label><input type="hidden" id="msls_id_%1$s" name="msls_input_%3$s" value="%4$s"/><input class="msls_title" id="msls_title_%1$s" name="msls_title_%1$s" type="text" value="%5$s"/></li>',
329325
$blog->userblog_id,
330326
$icon,
331327
$language,
@@ -338,9 +334,7 @@ public function render_input( $echo = true ) {
338334
}
339335

340336
printf(
341-
'<ul>%s</ul>
342-
<input type="hidden" name="msls_post_type" id="msls_post_type" value="%s"/>
343-
<input type="hidden" name="msls_action" id="msls_action" value="suggest_posts"/>',
337+
'<ul>%s</ul><input type="hidden" name="msls_post_type" id="msls_post_type" value="%s"/><input type="hidden" name="msls_action" id="msls_action" value="suggest_posts"/>',
344338
$items,
345339
$post_type
346340
);

includes/MslsOptions.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,8 +14,9 @@
1414
* General options class
1515
*
1616
* @package Msls
17+
* @property bool $activate_content_import
1718
* @property bool $activate_autocomplete
18-
* @property bool output_current_blog
19+
* @property bool $output_current_blog
1920
* @property int $display
2021
* @property int $reference_user
2122
* @property int $content_priority

tests/phpunit/TestMslsMetaBox.php

Lines changed: 94 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212

1313
class TestMslsMetaBox extends MslsUnitTestCase {
1414

15+
1516
protected function setUp(): void {
1617
$blog = \Mockery::mock( MslsBlog::class );
1718
$blog->shouldReceive( 'get_language' )->andReturn( 'de_DE' );
@@ -36,10 +37,8 @@ public function test_suggest(): void {
3637
Functions\expect( 'filter_input' )->once()->with( INPUT_GET, MslsFields::FIELD_S, FILTER_SANITIZE_FULL_SPECIAL_CHARS )->andReturn( 17 );
3738
Functions\expect( 'get_post_stati' )->once()->andReturn( array( 'pending', 'draft', 'future' ) );
3839
Functions\expect( 'get_the_title' )->once()->andReturn( 'Test' );
39-
4040
Functions\expect( 'sanitize_text_field' )->times( 2 )->andReturnFirstArg();
4141
Functions\expect( 'get_posts' )->once()->andReturn( array( $post ) );
42-
4342
Functions\expect( 'switch_to_blog' )->once();
4443
Functions\expect( 'restore_current_blog' )->once();
4544
Functions\expect( 'wp_reset_postdata' )->once();
@@ -112,7 +111,7 @@ public function test_add( $post_type, $fcount, $ocount, $content_import, $autoco
112111
$this->test->add();
113112
}
114113

115-
public function test_render_select() {
114+
public function test_render_select_not_hierarchical() {
116115
global $post;
117116

118117
$post = \Mockery::mock( 'WP_Post' );
@@ -142,4 +141,96 @@ public function test_render_select() {
142141

143142
$this->test->render_select();
144143
}
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>&nbsp;</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>&nbsp;</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+
}
145236
}

0 commit comments

Comments
 (0)