Skip to content

Commit

Permalink
Merge pull request #262 from lloc/msls-blog-collection-api-function
Browse files Browse the repository at this point in the history
Tests added
  • Loading branch information
lloc authored Dec 10, 2023
2 parents c11ade8 + 15dcf53 commit 1e85669
Show file tree
Hide file tree
Showing 4 changed files with 94 additions and 34 deletions.
3 changes: 2 additions & 1 deletion includes/MslsOptions.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,11 @@
* General options class
* @package Msls
* @property bool $activate_autocomplete
* @property bool output_current_blog
* @property int $display
* @property string $admin_display
* @property int $reference_user
* @property int $content_priority
* @property string $admin_display
* @property string $admin_language
* @property string $description
* @property string $before_item
Expand Down
9 changes: 8 additions & 1 deletion tests/test-mslsadmin.php
Original file line number Diff line number Diff line change
Expand Up @@ -116,10 +116,17 @@ function test_blog_language() {
function test_display() {
$obj = $this->get_sut();

$this->expectOutputRegex( '/^<select id="display" name="msls\[display\]">.*$/' );
$this->expectOutputString( '<select id="display" name="msls[display]"><option value="0" >Flag and description</option><option value="1" >Description only</option><option value="2" >Flag only</option><option value="3" >Description and flag</option></select>' );
$obj->display();
}

function test_admin_display() {
$obj = $this->get_sut();

$this->expectOutputString( '<select id="admin_display" name="msls[admin_display]"><option value="flag" >Flag</option><option value="label" >Label</option></select>' );
$obj->admin_display();
}

function test_reference_user() {
$users = [];
$too_much = MslsAdmin::MAX_REFERENCE_USERS + 1;
Expand Down
51 changes: 25 additions & 26 deletions tests/test-mslsadminicontaxonomy.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,64 +7,63 @@

class WP_Test_MslsAdminIconTaxonomy extends Msls_UnitTestCase {

protected $lang = 'de_DE';
const LANGUAGE = 'de_DE';
const IMAGE_SRC = '/dev/german_flag.png';

protected $src = '/dev/german_flag.png';
public function setUp(): void {
parent::setUp();

public function get_test() {
return ( new MslsAdminIconTaxonomy( 'post_tag' ) )
->set_path()
->set_language( $this->lang )
->set_src( $this->src );
Functions\expect( 'add_query_arg' )->twice()->andReturn( 'https://example.org/added-args' );
}

function test_get_img() {
Functions\expect( 'add_query_arg' )->twice()->andReturn( 'https://example.org/added-args' );
public function test_get_img(): void {
Functions\expect( 'get_query_var' )->once()->andReturn( 'post_tag' );
Functions\expect( 'get_taxonomies' )->once()->andReturn( [] );

$obj = $this->get_test();
$obj = ( new MslsAdminIconTaxonomy( 'post_tag' ) )->set_path()->set_language( self::LANGUAGE )->set_src( self::IMAGE_SRC );

$this->assertEquals( '<img alt="de_DE" src="' . $this->src . '" />', $obj->get_img() );
$expected = sprintf( '<img alt="de_DE" src="%s" />', self::IMAGE_SRC );

$this->assertEquals( $expected, $obj->get_img() );
}

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

Functions\expect( 'add_query_arg' )->twice()->andReturn( 'https://example.org/added-args' );
Functions\expect( 'get_admin_url' )->once()->andReturn( $admin_url );
Functions\expect( 'get_current_blog_id' )->once()->andReturn( 1 );

$obj = $this->get_test();
$obj = ( new MslsAdminIconTaxonomy( 'post_tag' ) )->set_path()->set_language( self::LANGUAGE )->set_src( self::IMAGE_SRC );

$this->assertEquals( $admin_url, $obj->get_edit_new() );
}

function test_set_href() {
Functions\expect( 'add_query_arg' )->twice()->andReturn( 'add-query-args' );
public function test_set_href(): void {
Functions\expect( 'get_edit_term_link' )->once()->andReturn( 'get-edit-post-link' );

$obj = $this->get_test();
$obj = ( new MslsAdminIconTaxonomy( 'post_tag' ) )->set_path()->set_language( self::LANGUAGE )->set_src( self::IMAGE_SRC );

$this->assertInstanceOf( MslsAdminIconTaxonomy::class, $obj->set_href( 42 ) );
$value = '<a title="Edit the translation in the de_DE-blog" href="get-edit-post-link"><span class="dashicons dashicons-edit"></span></a>&nbsp;';
$this->assertEquals( $value, $obj->get_a() );
$this->assertEquals( $value, $obj->__toString() );

$expected = '<a title="Edit the translation in the de_DE-blog" href="get-edit-post-link"><span class="dashicons dashicons-edit"></span></a>&nbsp;';

$this->assertEquals( $expected, $obj->get_a() );
$this->assertEquals( $expected, $obj->__toString() );
}

function test_set_href_empty() {
public function test_set_href_empty(): void {
Functions\expect( 'get_current_blog_id' )->twice()->andReturn( 1 );
Functions\expect( 'add_query_arg' )->twice()->andReturn( 'add-query-args' );
Functions\expect( 'get_edit_term_link' )->once()->andReturn( '' );
Functions\expect( 'get_admin_url' )->twice()->andReturn( 'admin-url-empty' );

$obj = $this->get_test();
$obj = ( new MslsAdminIconTaxonomy( 'post_tag' ) )->set_path()->set_language( self::LANGUAGE )->set_src( self::IMAGE_SRC );

$this->assertInstanceOf( MslsAdminIconTaxonomy::class, $obj->set_href( 0 ) );

$value = '<a title="Create a new translation in the de_DE-blog" href="admin-url-empty"><span class="dashicons dashicons-plus"></span></a>&nbsp;';
$this->assertEquals( $value, $obj->get_a() );
$this->assertEquals( $value, $obj->__toString() );
$expected = '<a title="Create a new translation in the de_DE-blog" href="admin-url-empty"><span class="dashicons dashicons-plus"></span></a>&nbsp;';

$this->assertEquals( $expected, $obj->get_a() );
$this->assertEquals( $expected, $obj->__toString() );
}

}
65 changes: 59 additions & 6 deletions tests/test-mslsblogcollection.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,27 +23,31 @@ public function setUp(): void {
$b = \Mockery::mock( MslsBlog::class );
$b->userblog_id = 2;

$c = \Mockery::mock( MslsBlog::class );
$c->userblog_id = 3;

Functions\expect( 'get_current_blog_id' )->atLeast( 1 )->andReturn( 1 );
Functions\expect( 'get_users' )->atLeast()->once()->andReturn( [] );
Functions\expect( 'get_blogs_of_user' )->atLeast()->once()->andReturn( [ $a, $b ] );
Functions\expect( 'get_site_option' )->once()->andReturn( [] );
Functions\expect( 'get_blogs_of_user' )->atLeast()->once()->andReturn( [ $a, $b, $c ] );

Functions\expect( 'get_option' )->andReturn( [] );
Functions\expect( 'get_option' )->andReturn( [ 'output_current_blog' => true ] );

Functions\expect( 'get_blog_option' )->atLeast( 1 )->andReturnUsing( function( $blog_id, $option ) {
$wplang = [
1 => 'de_DE',
2 => 'it_IT',
3 => 'fr_FR',
];

$msls = [
1 => [ 'description' => 'Deutsch' ],
2 => [ 'description' => 'Italiano' ],
3 => [ 'description' => 'Français' ],
];

switch( $option ) {
case 'active_plugins':
$value = in_array( $blog_id, [ 1, 2 ] ) ? ['multisite-language-switcher/MultisiteLanguageSwitcher.php' ] : [];
$value = in_array( $blog_id, [ 1, 2 ] ) ? [ 'multisite-language-switcher/MultisiteLanguageSwitcher.php' ] : [];
break;
case 'WPLANG':
$value = $wplang[ $blog_id ] ?? false;
Expand All @@ -58,15 +62,22 @@ public function setUp(): void {
}

public function test_get_configured_blog_description_empty(): void {
Functions\expect( 'get_site_option' )->once()->andReturn( [] );

$obj = new MslsBlogCollection();

$this->assertEquals( 'Test', $obj->get_configured_blog_description( 0, 'Test' ) );

$this->assertEquals( 'Deutsch', $obj->get_configured_blog_description( 1 ) );
$this->assertEquals( 'Italiano', $obj->get_configured_blog_description( 2 ) );
$this->assertFalse( $obj->get_configured_blog_description( 3 ) );
$this->assertEquals( 'Français', $obj->get_configured_blog_description( 3 ) );

$this->assertFalse( $obj->get_configured_blog_description( 4 ) );
}

public function test_get_blogs_of_reference_user(): void {
Functions\expect( 'get_site_option' )->once()->andReturn( [] );

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

Expand All @@ -76,18 +87,24 @@ public function test_get_blogs_of_reference_user(): void {
}

public function test_get_current_blog_id(): void {
Functions\expect( 'get_site_option' )->once()->andReturn( [] );

$obj = new MslsBlogCollection();

$this->assertIsInt( $obj->get_current_blog_id() );
}

public function test_has_current_blog(): void {
Functions\expect( 'get_site_option' )->once()->andReturn( [] );

$obj = new MslsBlogCollection();

$this->assertIsBool( $obj->has_current_blog() );
}

public function test_is_current_blog_true(): void {
Functions\expect( 'get_site_option' )->once()->andReturn( [] );

$obj = new MslsBlogCollection();

$blog = \Mockery::mock( MslsBlog::class );
Expand All @@ -97,6 +114,8 @@ public function test_is_current_blog_true(): void {
}

public function test_is_current_blog_false(): void {
Functions\expect( 'get_site_option' )->once()->andReturn( [] );

$obj = new MslsBlogCollection();

$blog = \Mockery::mock( MslsBlog::class );
Expand All @@ -106,60 +125,94 @@ public function test_is_current_blog_false(): void {
}

public function test_get_objects(): void {
Functions\expect( 'get_site_option' )->once()->andReturn( [] );

$obj = new MslsBlogCollection();

$this->assertIsArray( $obj->get_objects() );
}

public function test_is_plugin_active_networkwide(): void {
Functions\expect( 'get_site_option' )->once()->andReturn( [ 'multisite-language-switcher/MultisiteLanguageSwitcher.php' => 'Multisite Language Switcher' ] );

$obj = new MslsBlogCollection();

$this->assertTrue( $obj->is_plugin_active( 4 ) );
}

public function test_is_plugin_active(): void {
Functions\expect( 'get_site_option' )->once()->andReturn( [] );

$obj = new MslsBlogCollection();

$this->assertIsBool( $obj->is_plugin_active( 0 ) );
$this->assertTrue( $obj->is_plugin_active( 1 ) );
$this->assertTrue( $obj->is_plugin_active( 2 ) );

$this->assertFalse( $obj->is_plugin_active( 3 ) );
}

public function test_get_plugin_active_blogs(): void {
Functions\expect( 'get_site_option' )->once()->andReturn( [] );

$obj = new MslsBlogCollection();

$this->assertIsArray( $obj->get_plugin_active_blogs() );
}

public function test_get(): void {
Functions\expect( 'get_site_option' )->once()->andReturn( [] );

$obj = new MslsBlogCollection();

$this->assertIsArray( $obj->get() );
}

public function test_get_filtered(): void {
Functions\expect( 'get_site_option' )->once()->andReturn( [] );

$obj = new MslsBlogCollection();

$this->assertIsArray( $obj->get_filtered() );
$this->assertIsArray( $obj->get_filtered( true ) );
}

public function test_get_users(): void {
Functions\expect( 'get_site_option' )->once()->andReturn( [] );

$obj = new MslsBlogCollection();

$this->assertIsArray( $obj->get_users() );
}

public function test_get_current_blog(): void {
Functions\expect( 'get_site_option' )->once()->andReturn( [] );

$obj = new MslsBlogCollection();

$this->assertInstanceOf( MslsBlog::class, $obj->get_current_blog() );
}

public function test_get_blog_language(): void {
Functions\expect( 'get_site_option' )->once()->andReturn( [] );

$obj = new MslsBlogCollection();

$this->assertEquals( 'de_DE', $obj->get_blog_language( 1 ) );
$this->assertEquals( 'it_IT', $obj->get_blog_language( 2 ) );
$this->assertEquals( 'fr_FR', $obj->get_blog_language( 3 ) );

$this->assertEquals( 'de_DE', $obj->get_blog_language() );
}

public function test_get_blog_id(): void {
Functions\expect( 'get_site_option' )->once()->andReturn( [] );

$obj = new MslsBlogCollection();

$this->assertEquals( 1, $obj->get_blog_id( 'de_DE' ) );
$this->assertEquals( 2, $obj->get_blog_id( 'it_IT' ) );

$this->assertNull( $obj->get_blog_id( 'fr_FR' ) );
}

}

0 comments on commit 1e85669

Please sign in to comment.