Skip to content

Commit

Permalink
Merge pull request #270 from lloc/post-tag-tests
Browse files Browse the repository at this point in the history
Right callback value types and tests
  • Loading branch information
lloc authored Jan 7, 2024
2 parents 3250bc3 + 39149a7 commit d6db046
Show file tree
Hide file tree
Showing 4 changed files with 123 additions and 38 deletions.
21 changes: 12 additions & 9 deletions includes/MslsPostTag.php
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,8 @@ public static function suggest() {
/**
* Init
*
* @codeCoverageIgnore
*
* @return MslsPostTag
*/
public static function init() {
Expand All @@ -82,7 +84,7 @@ public static function init() {
$taxonomy = MslsContentTypes::create()->acl_request();
if ( '' != $taxonomy ) {
add_action( "{$taxonomy}_add_form_fields", [ $obj, 'add_input' ] );
add_action( "{$taxonomy}_edit_form_fields", [ $obj, 'edit_input' ] );
add_action( "{$taxonomy}_edit_form_fields", [ $obj, 'edit_input' ], 10, 2 );
add_action( "edited_{$taxonomy}", [ $obj, 'set' ] );
add_action( "create_{$taxonomy}", [ $obj, 'set' ] );
}
Expand All @@ -109,16 +111,17 @@ public function add_input( string $taxonomy ): void {
<input class="msls_title" id="msls_title_%1$s" name="msls_title_%1$s" type="text" value="%5$s"/>';

echo '<div class="form-field">';
$this->the_input( $taxonomy, $title_format, $item_format );
$this->the_input( null, $title_format, $item_format );
echo '</div>';
}

/**
* Add the input fields to the edit-screen of the taxonomies
*
* @param \WP_Term $tag
* @param string $taxonomy
*/
public function edit_input( string $taxonomy ): void {
public function edit_input( \WP_Term $tag, string $taxonomy ): void {
if ( did_action( "{$taxonomy}_edit_form_fields" ) !== 1 ) {
return;
}
Expand All @@ -141,22 +144,22 @@ public function edit_input( string $taxonomy ): void {
</td>
</tr>';

$this->the_input( $taxonomy, $title_format, $item_format );
$this->the_input( $tag, $title_format, $item_format );
}

/**
* Print the input fields
*
* Returns true if the blogcollection is not empty
*
* @param mixed $tag
* @param ?\WP_Term $tag
* @param string $title_format
* @param string $item_format
*
* @return boolean
*/
public function the_input( $tag, $title_format, $item_format ) {
$term_id = ( is_object( $tag ) ? $tag->term_id : 0 );
public function the_input( ?\WP_Term $tag, $title_format, $item_format ) {
$term_id = $tag->term_id ?? 0;
$blogs = $this->collection->get();
if ( $blogs ) {
$my_data = MslsOptionsTax::create( $term_id );
Expand Down Expand Up @@ -191,9 +194,9 @@ public function the_input( $tag, $title_format, $item_format ) {
}
}

printf( $item_format, $blog->userblog_id, $icon, $language, $value, $title );

restore_current_blog();

printf( $item_format, $blog->userblog_id, $icon, $language, $value, $title );
}

return true;
Expand Down
5 changes: 3 additions & 2 deletions includes/MslsPostTagClassic.php
Original file line number Diff line number Diff line change
Expand Up @@ -33,16 +33,17 @@ public function add_input( string $taxonomy ): void {
</select>';

echo '<div class="form-field">';
$this->the_input( $taxonomy, $title_format, $item_format );
$this->the_input( null, $title_format, $item_format );
echo '</div>';
}

/**
* Add the input fields to the edit-screen of the taxonomies
*
* @param \WP_Term $tag
* @param string $taxonomy
*/
public function edit_input( string $taxonomy ): void {
public function edit_input( \WP_Term $tag, string $taxonomy ): void {
if ( did_action( "{$taxonomy}_edit_form_fields" ) !== 1 ) {
return;
}
Expand Down
35 changes: 19 additions & 16 deletions tests/test-mslsposttag.php
Original file line number Diff line number Diff line change
Expand Up @@ -35,9 +35,7 @@ public function setUp(): void {
$collection = \Mockery::mock( MslsBlogCollection::class );
$collection->shouldReceive( 'get' )->andReturn( $blogs );

Functions\expect( 'msls_blog_collection' )->once()->andReturn( $collection );

$this->test = MslsPostTag::init();
$this->test = new MslsPostTag( $options, $collection );
}

/**
Expand All @@ -64,30 +62,33 @@ public function test_edit_input() {
$output = '<tr>
<th colspan="2">
<strong>Multisite Language Switcher</strong>
<input type="hidden" name="msls_post_type" id="msls_post_type" value="post"/>
<input type="hidden" name="msls_action" id="msls_action" value="suggest_terms"/>
</th>
</tr><tr class="form-field">
<th scope="row">
<label for="msls_input_de_DE"><a title="Create a new translation in the de_DE-blog" href="/wp-admin/edit-tags.php"><span class="flag-icon flag-icon-de">de_DE</span></a>&nbsp;</label></th>
<label for="msls_title_"><a title="Create a new translation in the de_DE-blog" href="/wp-admin/edit-tags.php"><span class="language-badge de_DE"><span>de</span><span>DE</span></span></a>&nbsp;</label>
</th>
<td>
<select class="msls-translations" name="msls_input_de_DE">
<option value=""></option>
</select></td>
<input type="hidden" id="msls_id_" name="msls_input_de_DE" value=""/>
<input class="msls_title" id="msls_title_" name="msls_title_" type="text" value=""/>
</td>
</tr><tr class="form-field">
<th scope="row">
<label for="msls_input_en_US"><a title="Create a new translation in the en_US-blog" href="/wp-admin/edit-tags.php"><span class="flag-icon flag-icon-us">en_US</span></a>&nbsp;</label></th>
<label for="msls_title_"><a title="Create a new translation in the en_US-blog" href="/wp-admin/edit-tags.php"><span class="language-badge en_US"><span>en</span><span>US</span></span></a>&nbsp;</label>
</th>
<td>
<select class="msls-translations" name="msls_input_en_US">
<option value=""></option>
</select></td>
<input type="hidden" id="msls_id_" name="msls_input_en_US" value=""/>
<input class="msls_title" id="msls_title_" name="msls_title_" type="text" value=""/>
</td>
</tr>';

self::expectOutputString( $output );

$this->test->edit_input( 'test' );
}
$tag = \Mockery::mock( \WP_Term::class );

$this->test->edit_input( $tag, 'test' );
}

public function test_add_input_second_call() {
Functions\expect( 'did_action' )->andReturn( 2 );
Expand All @@ -98,11 +99,13 @@ public function test_add_input_second_call() {
}

public function test_edit_input_second_call() {
$tag = \Mockery::mock( \WP_Term::class );

Functions\expect( 'did_action' )->andReturn( 2 );

self::expectOutputString( '' );

$this->test->edit_input( 'test' );
$this->test->edit_input( $tag, 'test' );
}

}
100 changes: 89 additions & 11 deletions tests/test-mslsposttagclassic.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,30 +2,108 @@

namespace lloc\MslsTests;

use lloc\Msls\MslsBlog;
use lloc\Msls\MslsPostTagClassic;
use lloc\Msls\MslsOptions;
use lloc\Msls\MslsBlogCollection;
use Brain\Monkey\Functions;

/**
* WP_Test_MslsPostTag
*/
class WP_Test_MslsPostTagClassic extends Msls_UnitTestCase {

public function get_sut() {
$options = \Mockery::mock( MslsOptions::class );
$collection = \Mockery::mock( MslsBlogCollection::class );
public function setUp(): void {
parent::setUp();

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

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

$collection->shouldReceive( 'get' )->once()->andReturn( [] );
$blogs[] = $blog;
}

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

$collection = \Mockery::mock( MslsBlogCollection::class );
$collection->shouldReceive( 'get' )->andReturn( $blogs );

return new MslsPostTagClassic( $options, $collection );
$this->test = new MslsPostTagClassic( $options, $collection );
}

/**
* Verify the static the_input-method
* Verify the static suggest-method
*/
public function test_the_input_method() {
$obj = $this->get_sut();
public function test_suggest(): void {
Functions\expect( 'wp_die' );

self::expectOutputString( '' );

MslsPostTagClassic::suggest();
}

public function test_edit_input() {
Functions\expect( 'did_action' )->andReturn( 1 );
Functions\expect( 'get_queried_object_id' )->andReturn( 42 );
Functions\expect( 'get_current_blog_id' )->andReturn( 23 );
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( 'plugin_dir_path' )->atLeast( 1 )->andReturn( dirname( __DIR__, 1 ) . '/' );

$output = '<tr>
<th colspan="2">
<strong>Multisite Language Switcher</strong>
</th>
</tr><tr class="form-field">
<th scope="row">
<label for="msls_input_de_DE"><a title="Create a new translation in the de_DE-blog" href="/wp-admin/edit-tags.php"><span class="language-badge de_DE"><span>de</span><span>DE</span></span></a>&nbsp;</label></th>
<td>
<select class="msls-translations" name="msls_input_de_DE">
<option value=""></option>
</select></td>
</tr><tr class="form-field">
<th scope="row">
<label for="msls_input_en_US"><a title="Create a new translation in the en_US-blog" href="/wp-admin/edit-tags.php"><span class="language-badge en_US"><span>en</span><span>US</span></span></a>&nbsp;</label></th>
<td>
<select class="msls-translations" name="msls_input_en_US">
<option value=""></option>
</select></td>
</tr>';

$tag = new \StdClass;
$tag->term_id = 1;
self::expectOutputString( $output );

$this->assertIsBool( $obj->the_input( $tag, 'test', 'test' ) );
$tag = \Mockery::mock( \WP_Term::class );

$this->test->edit_input( $tag, 'test' );
}

public function test_add_input_second_call() {
Functions\expect( 'did_action' )->andReturn( 2 );

self::expectOutputString( '' );

$this->test->add_input( 'test' );
}

public function test_edit_input_second_call() {
$tag = \Mockery::mock( \WP_Term::class );

Functions\expect( 'did_action' )->andReturn( 2 );

self::expectOutputString( '' );

$this->test->edit_input( $tag, 'test' );
}

}

0 comments on commit d6db046

Please sign in to comment.