Skip to content

Refactoring version 2 8 #310

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 2 commits into from
May 27, 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
26 changes: 13 additions & 13 deletions includes/MslsCustomFilter.php
Original file line number Diff line number Diff line change
Expand Up @@ -49,12 +49,6 @@ function () {
* @uses selected
*/
public function add_filter(): void {
$id = (
filter_has_var( INPUT_GET, self::FILTER_NAME ) ?
filter_input( INPUT_GET, self::FILTER_NAME, FILTER_SANITIZE_NUMBER_INT ) :
'0'
);

$blogs = $this->collection->get();
if ( $blogs ) {
$options = array( '' => esc_html( __( 'Show all posts', 'multisite-language-switcher' ) ) );
Expand All @@ -65,6 +59,12 @@ public function add_filter(): void {
);
}

$id = (
filter_has_var( INPUT_GET, self::FILTER_NAME ) ?
filter_input( INPUT_GET, self::FILTER_NAME, FILTER_SANITIZE_NUMBER_INT ) :
'0'
);

echo ( new Select( self::FILTER_NAME, $options, $id ) )->render();
}
}
Expand All @@ -84,15 +84,15 @@ public function execute_filter( \WP_Query $query ) {
$id = filter_input( INPUT_GET, self::FILTER_NAME, FILTER_SANITIZE_NUMBER_INT );
$blog = $this->collection->get_object( intval( $id ) );

if ( $blog ) {
$sql_cache = MslsSqlCacher::create( __CLASS__, __METHOD__ );
if ( ! $blog ) {
return false;
}

// load post we need to exclude (they already have a translation) from search query
$query->query_vars['post__not_in'] = ( new TranslatedPostIdQuery( $sql_cache ) )( $blog->get_language() );
$sql_cache = MslsSqlCacher::create( __CLASS__, __METHOD__ );

return $query;
}
// load post we need to exclude (they already have a translation) from search query
$query->query_vars['post__not_in'] = ( new TranslatedPostIdQuery( $sql_cache ) )( $blog->get_language() );

return false;
return $query;
}
}
2 changes: 1 addition & 1 deletion phpunit.xml
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
<?xml version="1.0"?>
<phpunit xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" backupGlobals="false" colors="true" convertErrorsToExceptions="true" convertNoticesToExceptions="true" convertWarningsToExceptions="true" xsi:noNamespaceSchemaLocation="https://schema.phpunit.de/9.3/phpunit.xsd">
<phpunit xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" backupGlobals="true" colors="true" convertErrorsToExceptions="true" convertNoticesToExceptions="true" convertWarningsToExceptions="true" xsi:noNamespaceSchemaLocation="https://schema.phpunit.de/9.3/phpunit.xsd">
<coverage>
<include>
<directory suffix=".php">./includes/</directory>
Expand Down
5 changes: 4 additions & 1 deletion tests/MslsUnitTestCase.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@ protected function setUp(): void {
parent::setUp();
Monkey\setUp();

\Mockery::namedMock( 'WooCommerce', \stdClass::class );

Functions\when( 'esc_html' )->returnArg();
Functions\when( 'esc_attr' )->returnArg();
Functions\when( 'esc_url' )->returnArg();
Expand All @@ -30,7 +32,8 @@ protected function tearDown(): void {
restore_error_handler();

Monkey\tearDown();
\Mockery::close();

parent::tearDown();
}

}
94 changes: 90 additions & 4 deletions tests/TestMslsOptionsTax.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,27 +11,113 @@
*/
class TestMslsOptionsTax extends MslsUnitTestCase {

protected $woo;

protected function setUp(): void {
parent::setUp();

Functions\expect( 'get_option' )->once()->andReturn( array() );
Functions\expect( 'get_option' )->once()->andReturn( array( 'de_DE' => 42 ) );

$this->test = new MslsOptionsTax( 0 );
}

public function test_get_tax_query(): void {
$this->assertIsString( $this->test->get_tax_query() );
Functions\expect( 'is_woocommerce' )->once()->andReturn( false );

$this->assertEquals( '', $this->test->get_tax_query() );
}

public function test_get_tax_query_woo(): void {
global $wp_query;

$expected = 'taxonomy_query_string_1';
$wp_query = (object) array(
'tax_query' => (object) array(
'queries' => array(
1 => array( 'taxonomy' => $expected ),
),
),
);

Functions\expect( 'is_woocommerce' )->once()->andReturn( true );

$this->assertEquals( $expected, $this->test->get_tax_query( array() ) );
}

public function test_get_tax_query_set(): void {
global $wp_query;

$expected = 'taxonomy_query_string_0';
$wp_query = (object) array(
'tax_query' => (object) array(
'queries' => array(
0 => array( 'taxonomy' => $expected ),
),
),
);

Functions\expect( 'is_woocommerce' )->once()->andReturn( false );

$this->assertEquals( $expected, $this->test->get_tax_query( array() ) );
}

public function test_get_postlink(): void {
$this->assertIsString( $this->test->get_postlink( 'de_DE' ) );
Functions\expect( 'is_woocommerce' )->once()->andReturn( false );

$this->assertEquals( '', $this->test->get_postlink( 'de_DE' ) );
}

public function test_get_postlink_empty(): void {
Functions\expect( 'is_woocommerce' )->never();

$this->assertEquals( '', $this->test->get_postlink( 'it_IT' ) );
}

public function test_get_current_link(): void {
$this->assertIsString( $this->test->get_current_link() );
}

public function test_get_term_link(): void {
$this->assertIsString( $this->test->get_term_link( 42 ) );
global $wp_query;

$wp_query = (object) array(
'tax_query' => (object) array(
'queries' => array(
0 => array( 'taxonomy' => 'taxonomy_query_string_0' ),
),
),
);

$expected = 'http://example.com/term_link';

Functions\expect( 'is_woocommerce' )->once()->andReturn( false );
Functions\expect( 'get_term_link' )->once()->andReturn( $expected );

$this->assertEquals( $expected, $this->test->get_term_link( 42 ) );
}

public function test_get_term_link_wp_error(): void {
global $wp_query;

$wp_query = (object) array(
'tax_query' => (object) array(
'queries' => array(
0 => array( 'taxonomy' => 'taxonomy_query_string_0' ),
),
),
);

$wp_error = \Mockery::mock( 'WP_Error' );

Functions\expect( 'is_woocommerce' )->once()->andReturn( false );
Functions\expect( 'get_term_link' )->once()->andReturn( $wp_error );

$this->assertEquals( '', $this->test->get_term_link( 42 ) );
}

public function test_get_term_link_empty(): void {
Functions\expect( 'is_woocommerce' )->once()->andReturn( false );

$this->assertEquals( '', $this->test->get_term_link( 42 ) );
}
}
1 change: 1 addition & 0 deletions tests/TestMslsPostTag.php
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,7 @@ public function test_edit_input(): void {
Functions\expect( 'restore_current_blog' )->atLeast();
Functions\expect( 'get_terms' )->andReturn( array() );
Functions\expect( 'plugin_dir_path' )->atLeast( 1 )->andReturn( dirname( __DIR__, 1 ) . '/' );
Functions\expect( 'is_woocommerce' )->once()->andReturn( false );

$output = '<tr>
<th colspan="2">
Expand Down
18 changes: 10 additions & 8 deletions tests/TestMslsPostTagClassic.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,15 +16,17 @@ class TestMslsPostTagClassic extends MslsUnitTestCase {
protected function setUp(): void {
parent::setUp();

Functions\when( 'get_option' )->justReturn( [] );
Functions\when( 'get_option' )->justReturn( array() );
Functions\expect( 'is_admin' )->andReturn( true );
Functions\expect( 'get_post_types' )->andReturn( [ 'post', 'page' ] );
Functions\expect( 'get_post_types' )->andReturn( array( 'post', 'page' ) );

foreach ( [ 'de_DE', 'en_US' ] as $locale ) {
foreach ( array( 'de_DE', 'en_US' ) as $locale ) {
$blog = \Mockery::mock( MslsBlog::class );
$blog->shouldReceive( [
'get_language' => $locale,
] );
$blog->shouldReceive(
array(
'get_language' => $locale,
)
);

$blogs[] = $blog;
}
Expand Down Expand Up @@ -56,8 +58,9 @@ public function test_edit_input(): void {
Functions\expect( 'get_admin_url' )->andReturn( '/wp-admin/edit-tags.php' );
Functions\expect( 'switch_to_blog' )->atLeast();
Functions\expect( 'restore_current_blog' )->atLeast();
Functions\expect( 'get_terms' )->andReturn( [] );
Functions\expect( 'get_terms' )->andReturn( array() );
Functions\expect( 'plugin_dir_path' )->atLeast( 1 )->andReturn( dirname( __DIR__, 1 ) . '/' );
Functions\expect( 'is_woocommerce' )->once()->andReturn( false );

$output = '<tr>
<th colspan="2">
Expand Down Expand Up @@ -90,5 +93,4 @@ public function test_edit_input(): void {
// second call should not output anything
$this->test->edit_input( $tag, 'test' );
}

}
33 changes: 12 additions & 21 deletions tests/TestMslsTaxonomy.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,31 +9,27 @@

class TestMslsTaxonomy extends MslsUnitTestCase {

/**
* @param bool $exluded
*
* @return MslsTaxonomy
*/
public function get_test( bool $exluded = false ): MslsTaxonomy {
parent::setUp();
/**
* @param bool $exluded
*
* @return MslsTaxonomy
*/
public function get_test( bool $exluded = false ): MslsTaxonomy {
parent::setUp();

$options = \Mockery::mock( MslsOptions::class );
$options->shouldReceive( 'is_excluded' )->andReturn( $exluded );
$options = \Mockery::mock( MslsOptions::class );
$options->shouldReceive( 'is_excluded' )->andReturn( $exluded );

Functions\expect( 'msls_options' )->zeroOrMoreTimes()->andReturn( $options );
Functions\expect( 'msls_options' )->zeroOrMoreTimes()->andReturn( $options );

Functions\expect( 'apply_filters' )->atLeast()->once();
Functions\expect( 'apply_filters' )->atLeast()->once();

Functions\expect( 'get_taxonomies' )->atLeast()->once()->andReturn( [] );
Functions\expect( 'get_taxonomies' )->atLeast()->once()->andReturn( array() );
Functions\expect( 'get_query_var' )->atLeast()->once()->with( 'taxonomy' )->andReturn( 'category' );

return new MslsTaxonomy();
}

/**
* @runInSeparateProcess
* @preserveGlobalState disabled
*/
public function test_acl_request_included(): void {
$cap = new \stdClass();
$cap->manage_terms = 'manage_categories';
Expand All @@ -46,10 +42,6 @@ public function test_acl_request_included(): void {
$this->assertEquals( 'category', $this->get_test()->acl_request() );
}

/**
* @runInSeparateProcess
* @preserveGlobalState disabled
*/
public function test_acl_request_excluded(): void {
$this->assertEquals( '', $this->get_test( true )->acl_request() );
}
Expand All @@ -69,5 +61,4 @@ public function test_is_taxonomy(): void {
public function test_get_request(): void {
$this->assertEquals( 'category', $this->get_test()->get_request() );
}

}
Loading