Skip to content

Commit 433c087

Browse files
authored
Merge pull request #356 from lloc/refactoring-2-9
Refactoring 2 9
2 parents f387e7f + 647bfee commit 433c087

File tree

5 files changed

+168
-23
lines changed

5 files changed

+168
-23
lines changed

includes/MslsCustomColumnTaxonomy.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,7 @@ public function column_default( $deprecated, $column_name, $item_id ) {
5353
*
5454
* @param int $object_id
5555
*/
56-
public function delete( $object_id ) {
56+
public function delete( $object_id ): void {
5757
$this->save( $object_id, MslsOptionsTax::class );
5858
}
5959
}

includes/MslsMain.php

Lines changed: 5 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -113,7 +113,7 @@ protected function get_input_value( $key, $value ) {
113113
*
114114
* @return bool
115115
*/
116-
public function is_autosave( $post_id ) {
116+
public function is_autosave( $post_id ): bool {
117117
return ( defined( 'DOING_AUTOSAVE' ) && DOING_AUTOSAVE ) || wp_is_post_revision( $post_id );
118118
}
119119

@@ -122,11 +122,8 @@ public function is_autosave( $post_id ) {
122122
*
123123
* @return boolean
124124
*/
125-
public function verify_nonce() {
126-
return (
127-
MslsRequest::has_var( MslsFields::FIELD_MSLS_NONCENAME ) &&
128-
wp_verify_nonce( MslsRequest::get_var( MslsFields::FIELD_MSLS_NONCENAME ), MslsPlugin::path() )
129-
);
125+
public function verify_nonce(): bool {
126+
return MslsRequest::has_var( MslsFields::FIELD_MSLS_NONCENAME ) && wp_verify_nonce( MslsRequest::get_var( MslsFields::FIELD_MSLS_NONCENAME ), MslsPlugin::path() );
130127
}
131128

132129
/**
@@ -136,7 +133,7 @@ public function verify_nonce() {
136133
*
137134
* @codeCoverageIgnore
138135
*/
139-
public function delete( $object_id ) {
136+
public function delete( $object_id ): void {
140137
$this->save( $object_id, MslsOptionsPost::class );
141138
}
142139

@@ -148,7 +145,7 @@ public function delete( $object_id ) {
148145
*
149146
* @codeCoverageIgnore
150147
*/
151-
protected function save( $object_id, $class ) {
148+
protected function save( $object_id, $class ): void {
152149
if ( has_action( 'msls_main_save' ) ) {
153150
/**
154151
* Calls completely customized save-routine

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: 156 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
namespace lloc\MslsTests;
44

55
use Brain\Monkey\Functions;
6+
use lloc\Msls\MslsBlog;
67
use lloc\Msls\MslsBlogCollection;
78
use lloc\Msls\MslsFields;
89
use lloc\Msls\MslsJson;
@@ -11,9 +12,15 @@
1112

1213
class TestMslsMetaBox extends MslsUnitTestCase {
1314

15+
1416
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+
1622
$collection = \Mockery::mock( MslsBlogCollection::class );
23+
$collection->shouldReceive( 'get' )->andReturn( array( $blog ) );
1724

1825
$this->test = new MslsMetaBox( $options, $collection );
1926
}
@@ -30,10 +37,8 @@ public function test_suggest(): void {
3037
Functions\expect( 'filter_input' )->once()->with( INPUT_GET, MslsFields::FIELD_S, FILTER_SANITIZE_FULL_SPECIAL_CHARS )->andReturn( 17 );
3138
Functions\expect( 'get_post_stati' )->once()->andReturn( array( 'pending', 'draft', 'future' ) );
3239
Functions\expect( 'get_the_title' )->once()->andReturn( 'Test' );
33-
3440
Functions\expect( 'sanitize_text_field' )->times( 2 )->andReturnFirstArg();
3541
Functions\expect( 'get_posts' )->once()->andReturn( array( $post ) );
36-
3742
Functions\expect( 'switch_to_blog' )->once();
3843
Functions\expect( 'restore_current_blog' )->once();
3944
Functions\expect( 'wp_reset_postdata' )->once();
@@ -80,4 +85,152 @@ public function test_render_options() {
8085

8186
$this->assertEquals( '<option value="42" selected="selected">A random title</option>', $this->test->render_options( 'post', 42 ) );
8287
}
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>&nbsp;</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>&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+
}
83236
}

0 commit comments

Comments
 (0)