Skip to content

Output tested #333

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Jun 9, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 0 additions & 1 deletion includes/MslsContentFilter.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@

class MslsContentFilter {


protected MslsOptions $options;

public function __construct( MslsOptions $options ) {
Expand Down
6 changes: 1 addition & 5 deletions includes/MslsOutput.php
Original file line number Diff line number Diff line change
Expand Up @@ -132,11 +132,7 @@ public function get_alternate_links() {
* @return string
*/
public function __toString() {
$display = (int) $this->options->display;
$filter = false;
$exists = isset( $this->options->only_with_translation );

$arr = $this->get( $display, $filter, $exists );
$arr = $this->get( $this->options->display, false, isset( $this->options->only_with_translation ) );
if ( empty( $arr ) ) {
return apply_filters( 'msls_output_no_translation_found', '' );
}
Expand Down
2 changes: 1 addition & 1 deletion tests/phpunit/TestMslsAdminIcon.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@

class TestMslsAdminIcon extends MslsUnitTestCase {

protected string $admin_url = 'https://example.org/wp-admin/';
protected string $admin_url = 'https://msls.co/wp-admin/';

protected string $lang = 'de_DE';

Expand Down
4 changes: 2 additions & 2 deletions tests/phpunit/TestMslsAdminIconTaxonomy.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ class TestMslsAdminIconTaxonomy extends MslsUnitTestCase {
protected function setUp(): void {
parent::setUp();

Functions\expect( 'add_query_arg' )->twice()->andReturn( 'https://example.org/added-args' );
Functions\expect( 'add_query_arg' )->twice()->andReturn( 'https://msls.co/added-args' );
}

public function test_get_img(): void {
Expand All @@ -30,7 +30,7 @@ public function test_get_img(): void {
}

public function test_get_edit_new(): void {
$admin_url = 'https://example.org/wp-admin/?new_tag';
$admin_url = 'https://msls.co/wp-admin/?new_tag';

Functions\expect( 'get_admin_url' )->once()->andReturn( $admin_url );
Functions\expect( 'get_current_blog_id' )->once()->andReturn( 1 );
Expand Down
16 changes: 8 additions & 8 deletions tests/phpunit/TestMslsBlog.php
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ public function test_get_description(): void {
}

public function test_get_url_current(): void {
$url = 'https://example.org/';
$url = 'https://msls.co/';

$option = \Mockery::mock( MslsOptions::class );
$option->shouldReceive( 'get_current_link' )->andReturn( $url );
Expand All @@ -43,7 +43,7 @@ public function test_get_url_current(): void {
}

public function test_get_frontpage(): void {
$url = 'https://example.org/';
$url = 'https://msls.co/';

$option = \Mockery::mock( MslsOptions::class );
$option->shouldReceive( 'get_permalink' )->once()->andReturn( $url );
Expand All @@ -60,7 +60,7 @@ public function test_get_frontpage(): void {
}

public function test_get_url(): void {
$url = 'https://example.org/';
$url = 'https://msls.co/';

$option = \Mockery::mock( MslsOptions::class );
$option->shouldReceive( 'get_permalink' )->once()->andReturn( $url );
Expand All @@ -86,7 +86,7 @@ public function test_get_alpha2(): void {
}

public function test_get_title(): void {
Functions\expect( 'add_query_arg' )->once()->andReturn( 'https://example.org/added-args' );
Functions\expect( 'add_query_arg' )->once()->andReturn( 'https://msls.co/added-args' );

$this->assertEquals(
'Test <span class="msls-icon-wrapper flag"><span class="flag-icon flag-icon-it">it_IT</span></span>',
Expand Down Expand Up @@ -145,11 +145,11 @@ public function test_get_blavatar_lazy(): void {
Functions\expect( 'switch_to_blog' )->once();
Functions\expect( 'restore_current_blog' )->once();
Functions\expect( 'has_site_icon' )->once()->andReturn( true );
Functions\expect( 'get_site_icon_url' )->twice()->andReturn( 'https://example.org/icons/abc.png' );
Functions\expect( 'get_site_icon_url' )->twice()->andReturn( 'https://msls.co/icons/abc.png' );
Functions\expect( 'wp_lazy_loading_enabled' )->once()->andReturn( true );

$this->assertEquals(
'<img class="blavatar" src="https://example.org/icons/abc.png" srcset="https://example.org/icons/abc.png 2x" alt="" width="16" height="16" loading="lazy" />',
'<img class="blavatar" src="https://msls.co/icons/abc.png" srcset="https://msls.co/icons/abc.png 2x" alt="" width="16" height="16" loading="lazy" />',
$this->get_blog()->get_blavatar()
);
}
Expand All @@ -158,11 +158,11 @@ public function test_get_blavatar(): void {
Functions\expect( 'switch_to_blog' )->once();
Functions\expect( 'restore_current_blog' )->once();
Functions\expect( 'has_site_icon' )->once()->andReturn( true );
Functions\expect( 'get_site_icon_url' )->twice()->andReturn( 'https://example.org/icons/abc.png' );
Functions\expect( 'get_site_icon_url' )->twice()->andReturn( 'https://msls.co/icons/abc.png' );
Functions\expect( 'wp_lazy_loading_enabled' )->once()->andReturn( false );

$this->assertEquals(
'<img class="blavatar" src="https://example.org/icons/abc.png" srcset="https://example.org/icons/abc.png 2x" alt="" width="16" height="16" />',
'<img class="blavatar" src="https://msls.co/icons/abc.png" srcset="https://msls.co/icons/abc.png 2x" alt="" width="16" height="16" />',
$this->get_blog()->get_blavatar()
);
}
Expand Down
130 changes: 126 additions & 4 deletions tests/phpunit/TestMslsContentFilter.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

namespace lloc\MslsTests;

use Brain\Monkey\Filters;
use Brain\Monkey\Functions;
use lloc\Msls\MslsBlog;
use lloc\Msls\MslsBlogCollection;
Expand Down Expand Up @@ -44,12 +45,21 @@ public function test_content_filter_one_link() {

$collection = \Mockery::mock( MslsBlogCollection::class );
$collection->shouldReceive( 'get_filtered' )->once()->andReturn( array( $blog ) );
$collection->shouldReceive( 'is_current_blog' )->once()->andReturn( true );
$collection->shouldReceive( 'is_current_blog' )->once()->andReturn( false );

$options = \Mockery::mock( MslsOptions::class );
$options->shouldReceive( 'is_content_filter' )->andReturn( true );
$options->shouldReceive( 'get_flag_url' )->once()->andReturn( 'https://lloc.de/wp-content/plugins/msls/flags/de.png' );
$options->shouldReceive( 'get_flag_url' )->once()->andReturn( 'https://msls.co/wp-content/plugins/msls/flags/de.png' );

$post = \Mockery::mock( 'WP_Post' );
$post->post_status = 'publish';
$post->post_type = 'post';

$post_object = \Mockery::mock( 'WP_Post_Type' );
$post_object->rewrite['with_front'] = true;

Functions\expect( 'get_post' )->once()->andReturn( $post );
Functions\expect( 'get_post_type_object' )->once()->andReturn( $post_object );
Functions\expect( 'is_front_page' )->twice()->andReturn( false );
Functions\expect( 'is_admin' )->once()->andReturn( false );
Functions\expect( 'is_search' )->once()->andReturn( false );
Expand All @@ -61,16 +71,128 @@ public function test_content_filter_one_link() {
Functions\expect( 'is_author' )->once()->andReturn( false );
Functions\expect( 'is_post_type_archive' )->once()->andReturn( false );
Functions\expect( 'get_queried_object_id' )->once()->andReturn( 42 );
Functions\expect( 'get_option' )->once()->andReturn( array() );
Functions\expect( 'get_option' )->once()->andReturn( array( 'de_DE' => 42 ) );
Functions\expect( 'is_singular' )->once()->andReturn( true );
Functions\expect( 'msls_options' )->once()->andReturn( $options );
Functions\expect( 'msls_blog_collection' )->once()->andReturn( $collection );
Functions\expect( 'get_permalink' )->once()->andReturn( 'https://msls.co/testpage/' );
Functions\expect( 'switch_to_blog' )->once();
Functions\expect( 'restore_current_blog' )->once();

$test = new MslsContentFilter( $options );

$content = '<p>Test>/p>';
$expected = '<p>Test>/p><p id="msls">This post is also available in <a href="https://msls.co/testpage/" title="Deutsch">Deutsch</a>.</p>';
$this->assertEquals( $expected, $test->content_filter( $content ) );
}

public function test_content_filter_two_links() {
$a = \Mockery::mock( MslsBlog::class );
$a->shouldReceive( 'get_language' )->once()->andReturn( 'it_IT' );
$a->shouldReceive( 'get_description' )->once()->andReturn( 'Italiano' );

$b = \Mockery::mock( MslsBlog::class );
$b->shouldReceive( 'get_language' )->once()->andReturn( 'de_DE' );
$b->shouldReceive( 'get_description' )->once()->andReturn( 'Deutsch' );

$collection = \Mockery::mock( MslsBlogCollection::class );
$collection->shouldReceive( 'get_filtered' )->once()->andReturn( array( $a, $b ) );
$collection->shouldReceive( 'is_current_blog' )->twice()->andReturn( false );

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

$post = \Mockery::mock( 'WP_Post' );
$post->post_status = 'publish';
$post->post_type = 'post';

$post_object = \Mockery::mock( 'WP_Post_Type' );
$post_object->rewrite['with_front'] = true;

Functions\expect( 'get_post' )->twice()->andReturn( $post );
Functions\expect( 'get_post_type_object' )->once()->andReturn( $post_object );
Functions\expect( 'is_front_page' )->twice()->andReturn( false );
Functions\expect( 'is_admin' )->once()->andReturn( false );
Functions\expect( 'is_search' )->once()->andReturn( false );
Functions\expect( 'is_404' )->once()->andReturn( false );
Functions\expect( 'is_category' )->once()->andReturn( false );
Functions\expect( 'is_tag' )->once()->andReturn( false );
Functions\expect( 'is_tax' )->once()->andReturn( false );
Functions\expect( 'is_date' )->once()->andReturn( false );
Functions\expect( 'is_author' )->once()->andReturn( false );
Functions\expect( 'is_post_type_archive' )->once()->andReturn( false );
Functions\expect( 'get_queried_object_id' )->once()->andReturn( 42 );
Functions\expect( 'get_option' )->once()->andReturn(
array(
'de_DE' => 42,
'it_IT' => 17,
)
);
Functions\expect( 'is_singular' )->once()->andReturn( true );
Functions\expect( 'msls_options' )->once()->andReturn( $options );
Functions\expect( 'msls_blog_collection' )->once()->andReturn( $collection );
Functions\expect( 'get_permalink' )->twice()->andReturn( 'https://msls.co/de/testpage/' );
Functions\expect( 'switch_to_blog' )->twice();
Functions\expect( 'restore_current_blog' )->twice();

$test = new MslsContentFilter( $options );

$content = '<p>Test>/p>';
$expected = '<p>Test>/p><p id="msls">This post is also available in <a href="https://msls.co/testpage/" title="Deutsch" class="current_language">Deutsch</a>.</p>';
$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>';
$this->assertEquals( $expected, $test->content_filter( $content ) );
}

public function test_content_filter_with_filter() {
$blog = \Mockery::mock( MslsBlog::class );
$blog->shouldReceive( 'get_language' )->once()->andReturn( 'de_DE' );
$blog->shouldReceive( 'get_description' )->once()->andReturn( 'Deutsch' );

$collection = \Mockery::mock( MslsBlogCollection::class );
$collection->shouldReceive( 'get_filtered' )->once()->andReturn( array( $blog ) );
$collection->shouldReceive( 'is_current_blog' )->once()->andReturn( false );

$options = \Mockery::mock( MslsOptions::class );
$options->shouldReceive( 'is_content_filter' )->andReturn( true );
$options->shouldReceive( 'get_flag_url' )->once()->andReturn( 'https://msls.co/wp-content/plugins/msls/flags/de.png' );

$post = \Mockery::mock( 'WP_Post' );
$post->post_status = 'publish';
$post->post_type = 'post';

$post_object = \Mockery::mock( 'WP_Post_Type' );
$post_object->rewrite['with_front'] = true;

Functions\expect( 'get_post' )->once()->andReturn( $post );
Functions\expect( 'get_post_type_object' )->once()->andReturn( $post_object );
Functions\expect( 'is_front_page' )->twice()->andReturn( false );
Functions\expect( 'is_admin' )->once()->andReturn( false );
Functions\expect( 'is_search' )->once()->andReturn( false );
Functions\expect( 'is_404' )->once()->andReturn( false );
Functions\expect( 'is_category' )->once()->andReturn( false );
Functions\expect( 'is_tag' )->once()->andReturn( false );
Functions\expect( 'is_tax' )->once()->andReturn( false );
Functions\expect( 'is_date' )->once()->andReturn( false );
Functions\expect( 'is_author' )->once()->andReturn( false );
Functions\expect( 'is_post_type_archive' )->once()->andReturn( false );
Functions\expect( 'get_queried_object_id' )->once()->andReturn( 42 );
Functions\expect( 'get_option' )->once()->andReturn( array( 'de_DE' => 42 ) );
Functions\expect( 'is_singular' )->once()->andReturn( true );
Functions\expect( 'msls_options' )->once()->andReturn( $options );
Functions\expect( 'msls_blog_collection' )->once()->andReturn( $collection );
Functions\expect( 'get_permalink' )->once()->andReturn( 'https://msls.co/testpage/' );
Functions\expect( 'has_filter' )->once()->with( 'msls_filter_string' )->andReturn( true );
Functions\expect( 'switch_to_blog' )->once();
Functions\expect( 'restore_current_blog' )->once();

$test = new MslsContentFilter( $options );

$content = '<p>Test>/p>';
$filter = 'This post is also available in <a href="https://msls.co/testpage/" title="Deutsch">Deutsch</a>.';

Filters\expectApplied( 'msls_filter_string' )->once()->andReturn( $filter );

$expected = '<p>Test>/p><p id="msls">' . $filter . '</p>';
$this->assertEquals( $expected, $test->content_filter( $content ) );
}
}
2 changes: 1 addition & 1 deletion tests/phpunit/TestMslsCustomColumn.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
class TestMslsCustomColumn extends MslsUnitTestCase {

public function test_th(): void {
Functions\expect( 'add_query_arg' )->twice()->andReturn( 'https://example.org/added-args' );
Functions\expect( 'add_query_arg' )->twice()->andReturn( 'https://msls.co/added-args' );
Functions\expect( 'get_the_ID' )->twice()->andReturnValues( array( 1, 2 ) );
Functions\when( 'plugin_dir_path' )->justReturn( dirname( __DIR__, 2 ) . '/' );

Expand Down
4 changes: 2 additions & 2 deletions tests/phpunit/TestMslsOptions.php
Original file line number Diff line number Diff line change
Expand Up @@ -111,7 +111,7 @@ function test_get_order_method(): void {
}

function test_get_url_method(): void {
Functions\when( 'plugins_url' )->justReturn( 'https://lloc.de/wp-content/plugins' );
Functions\when( 'plugins_url' )->justReturn( 'https://msls.co/wp-content/plugins' );

$obj = $this->get_test();

Expand All @@ -120,7 +120,7 @@ function test_get_url_method(): void {

function test_get_flag_url_method(): void {
Functions\when( 'is_admin' )->justReturn( true );
Functions\when( 'plugins_url' )->justReturn( 'https://lloc.de/wp-content/plugins' );
Functions\when( 'plugins_url' )->justReturn( 'https://msls.co/wp-content/plugins' );
Functions\when( 'plugin_dir_path' )->justReturn( dirname( __DIR__, 2 ) . '/' );

$obj = $this->get_test();
Expand Down
4 changes: 2 additions & 2 deletions tests/phpunit/TestMslsOptionsPost.php
Original file line number Diff line number Diff line change
Expand Up @@ -50,8 +50,8 @@ public function test_get_postlink_post_is_published(): void {
}

public function test_get_current_link(): void {
Functions\expect( 'get_permalink' )->once()->andReturn( 'https://example.org/a-post' );
Functions\expect( 'get_permalink' )->once()->andReturn( 'https://msls.co/a-post' );

$this->assertEquals( 'https://example.org/a-post', $this->test->get_current_link() );
$this->assertEquals( 'https://msls.co/a-post', $this->test->get_current_link() );
}
}
2 changes: 1 addition & 1 deletion tests/phpunit/TestMslsOptionsQuery.php
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,7 @@ public function test_create_is_null(): void {
}

public function test_current_get_postlink(): void {
$home_url = 'https://example.org/';
$home_url = 'https://msls.co/';

Functions\expect( 'get_option' )->once()->andReturn( array( 'de_DE' => 42 ) );
Functions\expect( 'home_url' )->once()->andReturn( $home_url );
Expand Down
4 changes: 2 additions & 2 deletions tests/phpunit/TestMslsOptionsQueryAuthor.php
Original file line number Diff line number Diff line change
Expand Up @@ -28,8 +28,8 @@ public function test_has_value_false(): void {
}

public function test_get_current_link_method(): void {
Functions\expect( 'get_author_posts_url' )->once()->andReturn( 'https://example.org/queried-author' );
Functions\expect( 'get_author_posts_url' )->once()->andReturn( 'https://msls.co/queried-author' );

$this->assertEquals( 'https://example.org/queried-author', $this->get_test( 42 )->get_current_link() );
$this->assertEquals( 'https://msls.co/queried-author', $this->get_test( 42 )->get_current_link() );
}
}
4 changes: 2 additions & 2 deletions tests/phpunit/TestMslsOptionsQueryDay.php
Original file line number Diff line number Diff line change
Expand Up @@ -30,8 +30,8 @@ public function test_has_value(): void {
}

public function test_get_current_link(): void {
Functions\expect( 'get_day_link' )->once()->andReturn( 'https://example.org/queried-day' );
Functions\expect( 'get_day_link' )->once()->andReturn( 'https://msls.co/queried-day' );

$this->assertEquals( 'https://example.org/queried-day', $this->get_test( 2015, 07, 02 )->get_current_link() );
$this->assertEquals( 'https://msls.co/queried-day', $this->get_test( 2015, 07, 02 )->get_current_link() );
}
}
4 changes: 2 additions & 2 deletions tests/phpunit/TestMslsOptionsQueryMonth.php
Original file line number Diff line number Diff line change
Expand Up @@ -30,8 +30,8 @@ public function test_has_value_false(): void {
}

public function test_get_current_link(): void {
Functions\expect( 'get_month_link' )->once()->andReturn( 'https://example.org/queried-month' );
Functions\expect( 'get_month_link' )->once()->andReturn( 'https://msls.co/queried-month' );

$this->assertEquals( 'https://example.org/queried-month', $this->get_test( 2015, 7 )->get_current_link() );
$this->assertEquals( 'https://msls.co/queried-month', $this->get_test( 2015, 7 )->get_current_link() );
}
}
4 changes: 2 additions & 2 deletions tests/phpunit/TestMslsOptionsQueryPostType.php
Original file line number Diff line number Diff line change
Expand Up @@ -41,8 +41,8 @@ public function test_has_value_not_existing(): void {
}

public function test_get_current_link(): void {
Functions\expect( 'get_post_type_archive_link' )->once()->andReturn( 'https://example.org/queried-posttype' );
Functions\expect( 'get_post_type_archive_link' )->once()->andReturn( 'https://msls.co/queried-posttype' );

$this->assertEquals( 'https://example.org/queried-posttype', $this->test->get_current_link() );
$this->assertEquals( 'https://msls.co/queried-posttype', $this->test->get_current_link() );
}
}
4 changes: 2 additions & 2 deletions tests/phpunit/TestMslsOptionsQueryYear.php
Original file line number Diff line number Diff line change
Expand Up @@ -30,8 +30,8 @@ public function test_has_value_false(): void {
}

public function test_get_current_link_method(): void {
Functions\expect( 'get_year_link' )->once()->andReturn( 'https://example.org/queried-year' );
Functions\expect( 'get_year_link' )->once()->andReturn( 'https://msls.co/queried-year' );

$this->assertEquals( 'https://example.org/queried-year', $this->get_test( 2015 )->get_current_link() );
$this->assertEquals( 'https://msls.co/queried-year', $this->get_test( 2015 )->get_current_link() );
}
}
Loading
Loading