Skip to content

Commit c727dc4

Browse files
authored
Merge pull request #354 from lloc/refactoring-2-9
Bugix
2 parents 5e89e49 + c1fc52b commit c727dc4

2 files changed

Lines changed: 69 additions & 24 deletions

File tree

includes/MslsContentFilter.php

Lines changed: 22 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -43,11 +43,11 @@ public function content_filter( string $content ) {
4343
* @return string
4444
*/
4545
public function filter_string( $pref = '<p id="msls">', $post = '</p>' ) {
46-
$obj = MslsOutput::init();
47-
$links = $obj->get( 1, true, true );
46+
$links_arr = MslsOutput::init()->get( 1, true, true );
47+
$links_str = $this->format_available_languages( $links_arr );
4848

4949
/* translators: %s: list of languages */
50-
$output = __( 'This post is also available in %s.', 'multisite-language-switcher' );
50+
$format = __( 'This post is also available in %s.', 'multisite-language-switcher' );
5151

5252
if ( has_filter( 'msls_filter_string' ) ) {
5353
/**
@@ -58,18 +58,28 @@ public function filter_string( $pref = '<p id="msls">', $post = '</p>' ) {
5858
*
5959
* @since 1.0
6060
*/
61-
$output = apply_filters( 'msls_filter_string', $output, $links );
62-
} elseif ( count( $links ) > 1 ) {
63-
$last = array_pop( $links );
61+
$output = apply_filters( 'msls_filter_string', $format, $links_arr );
62+
} elseif ( $links_str ) {
63+
$output = sprintf( $format, $links_str );
64+
}
6465

65-
/* translators: %1$s: list of languages separated by a comma, %2$s: last language */
66-
$format = __( '%1$s and %2$s', 'multisite-language-switcher' );
66+
return ! empty( $output ) ? $pref . $output . $post : '';
67+
}
6768

68-
$output = sprintf( $output, sprintf( $format, implode( ', ', $links ), $last ) );
69-
} elseif ( 1 == count( $links ) ) {
70-
$output = sprintf( $output, $links[0] );
69+
public function format_available_languages( array $links ): ?string {
70+
if ( empty( $links ) ) {
71+
return null;
7172
}
7273

73-
return ! empty( $output ) ? $pref . $output . $post : '';
74+
if ( 1 == count( $links ) ) {
75+
return $links[0];
76+
}
77+
78+
$last = array_pop( $links );
79+
80+
/* translators: %1$s: list of languages separated by a comma, %2$s: last language */
81+
$format = __( '%1$s and %2$s', 'multisite-language-switcher' );
82+
83+
return sprintf( $format, implode( ', ', $links ), $last );
7484
}
7585
}

tests/phpunit/TestMslsContentFilter.php

Lines changed: 47 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -86,7 +86,33 @@ public function test_content_filter_one_link() {
8686
$this->assertEquals( $expected, $test->content_filter( $content ) );
8787
}
8888

89-
public function test_content_filter_two_links() {
89+
public function test_content_filter_zero_links() {
90+
$collection = \Mockery::mock( MslsBlogCollection::class );
91+
$collection->shouldReceive( 'get_filtered' )->once()->andReturn( array() );
92+
93+
$options = \Mockery::mock( MslsOptions::class );
94+
$options->shouldReceive( 'is_content_filter' )->andReturn( true );
95+
96+
$post = \Mockery::mock( 'WP_Post' );
97+
$post->post_status = 'publish';
98+
$post->post_type = 'post';
99+
100+
$post_object = \Mockery::mock( 'WP_Post_Type' );
101+
$post_object->rewrite['with_front'] = true;
102+
103+
Functions\expect( 'is_front_page' )->once()->andReturn( false );
104+
Functions\expect( 'is_singular' )->once()->andReturn( true );
105+
Functions\expect( 'msls_options' )->once()->andReturn( $options );
106+
Functions\expect( 'msls_blog_collection' )->once()->andReturn( $collection );
107+
108+
$test = new MslsContentFilter( $options );
109+
110+
$content = '<p>Test>/p>';
111+
$expected = '<p>Test>/p>';
112+
$this->assertEquals( $expected, $test->content_filter( $content ) );
113+
}
114+
115+
public function test_content_filter_more_links() {
90116
$a = \Mockery::mock( MslsBlog::class );
91117
$a->shouldReceive( 'get_language' )->once()->andReturn( 'it_IT' );
92118
$a->shouldReceive( 'get_description' )->once()->andReturn( 'Italiano' );
@@ -95,13 +121,20 @@ public function test_content_filter_two_links() {
95121
$b->shouldReceive( 'get_language' )->once()->andReturn( 'de_DE' );
96122
$b->shouldReceive( 'get_description' )->once()->andReturn( 'Deutsch' );
97123

124+
$c = \Mockery::mock( MslsBlog::class );
125+
$c->shouldReceive( 'get_language' )->once()->andReturn( 'fr_FR' );
126+
$c->shouldReceive( 'get_description' )->once()->andReturn( 'Français' );
127+
128+
$blogs = array( $a, $b, $c );
129+
$times = count( $blogs );
130+
98131
$collection = \Mockery::mock( MslsBlogCollection::class );
99-
$collection->shouldReceive( 'get_filtered' )->once()->andReturn( array( $a, $b ) );
100-
$collection->shouldReceive( 'is_current_blog' )->twice()->andReturn( false );
132+
$collection->shouldReceive( 'get_filtered' )->once()->andReturn( $blogs );
133+
$collection->shouldReceive( 'is_current_blog' )->times( $times )->andReturn( false );
101134

102135
$options = \Mockery::mock( MslsOptions::class );
103136
$options->shouldReceive( 'is_content_filter' )->andReturn( true );
104-
$options->shouldReceive( 'get_flag_url' )->twice()->andReturn( 'https://msls.co/wp-content/plugins/msls/flags/de.png' );
137+
$options->shouldReceive( 'get_flag_url' )->times( $times )->andReturn( 'https://msls.co/wp-content/plugins/msls/flags/de.png' );
105138

106139
$post = \Mockery::mock( 'WP_Post' );
107140
$post->post_status = 'publish';
@@ -110,8 +143,11 @@ public function test_content_filter_two_links() {
110143
$post_object = \Mockery::mock( 'WP_Post_Type' );
111144
$post_object->rewrite['with_front'] = true;
112145

113-
Functions\expect( 'get_post' )->twice()->andReturn( $post );
146+
Functions\expect( 'msls_options' )->once()->andReturn( $options );
147+
Functions\expect( 'msls_blog_collection' )->once()->andReturn( $collection );
148+
114149
Functions\expect( 'get_post_type_object' )->once()->andReturn( $post_object );
150+
Functions\expect( 'is_singular' )->once()->andReturn( true );
115151
Functions\expect( 'is_front_page' )->twice()->andReturn( false );
116152
Functions\expect( 'is_admin' )->once()->andReturn( false );
117153
Functions\expect( 'is_search' )->once()->andReturn( false );
@@ -127,19 +163,18 @@ public function test_content_filter_two_links() {
127163
array(
128164
'de_DE' => 42,
129165
'it_IT' => 17,
166+
'fr_FR' => 23,
130167
)
131168
);
132-
Functions\expect( 'is_singular' )->once()->andReturn( true );
133-
Functions\expect( 'msls_options' )->once()->andReturn( $options );
134-
Functions\expect( 'msls_blog_collection' )->once()->andReturn( $collection );
135-
Functions\expect( 'get_permalink' )->twice()->andReturn( 'https://msls.co/de/testpage/' );
136-
Functions\expect( 'switch_to_blog' )->twice();
137-
Functions\expect( 'restore_current_blog' )->twice();
169+
Functions\expect( 'get_post' )->times( $times )->andReturn( $post );
170+
Functions\expect( 'get_permalink' )->times( $times )->andReturn( 'https://msls.co/de/testpage/' );
171+
Functions\expect( 'switch_to_blog' )->times( $times );
172+
Functions\expect( 'restore_current_blog' )->times( $times );
138173

139174
$test = new MslsContentFilter( $options );
140175

141176
$content = '<p>Test>/p>';
142-
$expected = '<p>Test>/p><p id="msls">This post is also available in <a href="https://msls.co/de/testpage/" title="Italiano">Italiano</a> and <a href="https://msls.co/de/testpage/" title="Deutsch">Deutsch</a>.</p>';
177+
$expected = '<p>Test>/p><p id="msls">This post is also available in <a href="https://msls.co/de/testpage/" title="Italiano">Italiano</a>, <a href="https://msls.co/de/testpage/" title="Deutsch">Deutsch</a> and <a href="https://msls.co/de/testpage/" title="Français">Français</a>.</p>';
143178
$this->assertEquals( $expected, $test->content_filter( $content ) );
144179
}
145180

0 commit comments

Comments
 (0)