Skip to content

Commit

Permalink
Merge pull request #261 from lloc/msls-blog-collection-api-function
Browse files Browse the repository at this point in the history
Review API functions
  • Loading branch information
lloc authored Dec 8, 2023
2 parents e8b22aa + e511c52 commit c11ade8
Show file tree
Hide file tree
Showing 6 changed files with 140 additions and 54 deletions.
32 changes: 25 additions & 7 deletions MultisiteLanguageSwitcher.php
Original file line number Diff line number Diff line change
Expand Up @@ -101,24 +101,41 @@ function get_msls_flag_url( string $locale ): string {
*
* @return string
*/
function get_msls_blog_description( string $locale ): string {
$blog = \lloc\Msls\MslsBlogCollection::instance()->get_blog( $locale );
function get_msls_blog_description( string $locale, string $default = '' ): string {
$blog = msls_blog( $locale );

return $blog->get_description();
return $blog ? $blog->get_description() : $default;
}

/**
* Gets the permalink for a translation of the current post in a given language
*
* @param string $locale
* @param string $default
*
* @return string
*/
function get_msls_permalink( $locale ) {
$options = \lloc\Msls\MslsOptions::create();
$blog = \lloc\Msls\MslsBlogCollection::instance()->get_blog( $locale );
function get_msls_permalink( string $locale, string $default = '' ): string {
$url = null;
$blog = msls_blog( $locale );

return $blog->get_url( $options );
if ( $blog ) {
$options = \lloc\Msls\MslsOptions::create();
$url = $blog->get_url( $options );
}

return $url ?? $default;
}

/**
* Gets a blog by locale
*
* @param string $locale
*
* @return \lloc\Msls\MslsBlog|null
*/
function msls_blog( string $locale ): ?\lloc\Msls\MslsBlog {
return msls_blog_collection()->get_blog( $locale );
}

/**
Expand All @@ -129,4 +146,5 @@ function get_msls_permalink( $locale ) {
function msls_blog_collection(): \lloc\Msls\MslsBlogCollection {
return \lloc\Msls\MslsBlogCollection::instance();
}

}
6 changes: 3 additions & 3 deletions includes/MslsBlogCollection.php
Original file line number Diff line number Diff line change
Expand Up @@ -102,21 +102,21 @@ public function __construct() {
}

/**
* Returns the description of an configured blog or false if it is not configured
* Returns the description of a configured blog or false if it is not configured
*
* @param int $blog_id
* @param string|bool $description
*
* @return string|bool
*/
public static function get_configured_blog_description( $blog_id, $description = false ) {
if ( false != $description ) {
if ( $description ) {
return $description;
}

$temp = get_blog_option( $blog_id, 'msls' );
if ( is_array( $temp ) && empty( $temp['exclude_current_blog'] ) ) {
return $temp['description'];
return $temp['description'] ?? '';
}

return false;
Expand Down
3 changes: 3 additions & 0 deletions phpunit.xml
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,9 @@
<directory suffix=".php">./includes/</directory>
</include>
</coverage>
<php>
<const name="MSLS_PLUGIN_PATH" value="multisite-language-switcher/MultisiteLanguageSwitcher.php"/>
</php>
<testsuite name="Internal tests">
<directory prefix="test-" suffix=".php">./tests/</directory>
</testsuite>
Expand Down
2 changes: 0 additions & 2 deletions tests/bootstrap.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,6 @@

namespace lloc\MslsTests;

use lloc\Msls\MslsBlog;
use lloc\Msls\MslsBlogCollection;
use PHPUnit\Framework\TestCase;
use Brain\Monkey;
use Brain\Monkey\Functions;
Expand Down
138 changes: 102 additions & 36 deletions tests/test-mslsblogcollection.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,98 +2,164 @@

namespace lloc\MslsTests;

use lloc\Msls\MslsBlog;
use lloc\Msls\MslsBlogCollection;
use lloc\Msls\MslsOptions;

use Brain\Monkey\Functions;
use lloc\Msls\MslsPlugin;

/**
* WP_Test_MslsBlogCollection
*/
class WP_Test_MslsBlogCollection extends Msls_UnitTestCase {

function get_test() {
Functions\expect( 'get_users' )->atLeast()->once()->andReturn( [] );
Functions\expect( 'get_blogs_of_user' )->atLeast()->once()->andReturn( [] );
Functions\expect( 'get_current_blog_id' )->once()->andReturn( 1 );
public function setUp(): void {
parent::setUp(); // TODO: Change the autogenerated stub

return new MslsBlogCollection();
}
$a = \Mockery::mock( MslsBlog::class );
$a->userblog_id = 1;

function test_get_configured_blog_description_not_empty() {
Functions\expect( 'get_option' )->andReturn( [] );
$b = \Mockery::mock( MslsBlog::class );
$b->userblog_id = 2;

$obj = $this->get_test();
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( [] );

$this->assertEquals( 'Test', $obj->get_configured_blog_description( 0, 'Test' ) );
}
Functions\expect( 'get_option' )->andReturn( [] );

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

$msls = [
1 => [ 'description' => 'Deutsch' ],
2 => [ 'description' => 'Italiano' ],
];

switch( $option ) {
case 'active_plugins':
$value = in_array( $blog_id, [ 1, 2 ] ) ? ['multisite-language-switcher/MultisiteLanguageSwitcher.php' ] : [];
break;
case 'WPLANG':
$value = $wplang[ $blog_id ] ?? false;
break;
case 'msls':
$value = $msls[ $blog_id ] ?? false;
break;
}

return $value;
} );
}

$obj = $this->get_test();
public function test_get_configured_blog_description_empty(): void {
$obj = new MslsBlogCollection();

$this->assertEquals( false, $obj->get_configured_blog_description( 0, false ) );
$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 ) );
}

function test_get_blogs_of_reference_user() {
public function test_get_blogs_of_reference_user(): void {
$options = \Mockery::mock( MslsOptions::class );
$options->shouldReceive( 'has_value' )->andReturn( true );

$obj = $this->get_test();
$obj = new MslsBlogCollection();

$this->assertIsArray( $obj->get_blogs_of_reference_user( $options ) );
}

function test_get_current_blog_id() {
$obj = $this->get_test();
public function test_get_current_blog_id(): void {
$obj = new MslsBlogCollection();

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

function test_has_current_blog() {
$obj = $this->get_test();
public function test_has_current_blog(): void {
$obj = new MslsBlogCollection();

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

function test_get_objects() {
$obj = $this->get_test();
public function test_is_current_blog_true(): void {
$obj = new MslsBlogCollection();

$this->assertIsArray( $obj->get_objects() );
$blog = \Mockery::mock( MslsBlog::class );
$blog->userblog_id = 1;

$this->assertTrue( $obj->is_current_blog( $blog ) );
}

function test_is_plugin_active() {
Functions\expect( 'get_site_option' )->once()->andReturn( [] );
Functions\expect( 'get_blog_option' )->once()->andReturn( [] );
public function test_is_current_blog_false(): void {
$obj = new MslsBlogCollection();

$blog = \Mockery::mock( MslsBlog::class );
$blog->userblog_id = 2;

$obj = $this->get_test();
$this->assertFalse( $obj->is_current_blog( $blog ) );
}

public function test_get_objects(): void {
$obj = new MslsBlogCollection();

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

public function test_is_plugin_active(): void {
$obj = new MslsBlogCollection();

$this->assertIsBool( $obj->is_plugin_active( 0 ) );
}

function test_get_plugin_active_blogs() {
$obj = $this->get_test();
public function test_get_plugin_active_blogs(): void {
$obj = new MslsBlogCollection();

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

function test_get() {
$obj = $this->get_test();
public function test_get(): void {
$obj = new MslsBlogCollection();

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

function test_get_filtered() {
$obj = $this->get_test();
public function test_get_filtered(): void {
$obj = new MslsBlogCollection();

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

function test_get_users() {
$obj = $this->get_test();
public function test_get_users(): void {
$obj = new MslsBlogCollection();

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

public function test_get_current_blog(): void {
$obj = new MslsBlogCollection();

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

public function test_get_blog_language(): void {
$obj = new MslsBlogCollection();

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

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

public function test_get_blog_id(): void {
$obj = new MslsBlogCollection();

$this->assertEquals( 1, $obj->get_blog_id( 'de_DE' ) );
$this->assertEquals( 2, $obj->get_blog_id( 'it_IT' ) );
}
}
13 changes: 7 additions & 6 deletions tests/test-mslsplugin.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ function get_test() {
/**
* Verify the static init-method
*/
function test_admin_menu_method() {
function test_admin_menu(): void {
Functions\when( 'wp_enqueue_style' )->returnArg();
Functions\when( 'plugins_url' )->justReturn( 'https://lloc.de/wp-content/plugins' );

Expand All @@ -29,7 +29,7 @@ function test_admin_menu_method() {
/**
* Verify the static init_widget-method
*/
function test_init_widget_method() {
function test_init_widget(): void {
Functions\when( 'register_widget' )->justReturn( true );

$this->assertIsBool( $this->get_test()->init_widget() );
Expand All @@ -38,7 +38,7 @@ function test_init_widget_method() {
/**
* Verify the static init_i18n_support-method
*/
function test_init_i18n_support_method() {
function test_init_i18n_support(): void {
Functions\when( 'load_plugin_textdomain' )->justReturn( true );

$this->assertIsBool( $this->get_test()->init_i18n_support() );
Expand All @@ -47,15 +47,16 @@ function test_init_i18n_support_method() {
/**
* Verify the static message_handler-method
*/
function test_message_handler_method() {
function test_message_handler(): void {
$this->expectOutputString( '<div id="msls-warning" class="error"><p>Test</p></div>' );

MslsPlugin::message_handler( 'Test' );
}

/**
* Verify the static uninstall-method
*/
function test_uninstall_method() {
function test_uninstall(): void {
Functions\when( 'delete_option' )->justReturn( false );

$this->assertIsBool( $this->get_test()->uninstall() );
Expand All @@ -64,7 +65,7 @@ function test_uninstall_method() {
/**
* Verify the static cleanup-method
*/
function test_cleanup_method() {
function test_cleanup(): void {
Functions\when( 'delete_option' )->justReturn( false );

$this->assertIsBool( MslsPlugin::cleanup() );
Expand Down

0 comments on commit c11ade8

Please sign in to comment.