Skip to content
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

New API function introduced - msls_blog_collection() #259

Merged
merged 1 commit into from
Dec 4, 2023
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
8 changes: 8 additions & 0 deletions MultisiteLanguageSwitcher.php
Original file line number Diff line number Diff line change
Expand Up @@ -121,4 +121,12 @@ function get_msls_permalink( $locale ) {
return $blog->get_url( $options );
}

/**
* Gets the MslsBlogCollection instance
*
* @return \lloc\Msls\MslsBlogCollection
*/
function msls_blog_collection(): \lloc\Msls\MslsBlogCollection {
return \lloc\Msls\MslsBlogCollection::instance();
}
}
2 changes: 1 addition & 1 deletion includes/ContentImport/MetaBox.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ public function render() {
$input_lang = isset( $_GET['msls_lang'] ) ? $_GET['msls_lang'] : null;
$input_id = isset( $_GET['msls_id'] ) ? $_GET['msls_id'] : null;
$has_input = null !== $input_lang && null !== $input_id;
$blogs = MslsBlogCollection::instance();
$blogs = msls_blog_collection();
$available = array_filter( array_map( function ( $lang ) use ( $mydata ) {
return $mydata->{$lang};
}, array_keys( $languages ) ) );
Expand Down
2 changes: 1 addition & 1 deletion includes/MslsAdmin.php
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ class MslsAdmin extends MslsMain {
public static function init() {
if ( ! ( $obj = MslsRegistry::get_object( __CLASS__ ) ) ) {
$options = MslsOptions::instance();
$collection = MslsBlogCollection::instance();
$collection = msls_blog_collection();

$obj = new static( $options, $collection );

Expand Down
2 changes: 1 addition & 1 deletion includes/MslsBlog.php
Original file line number Diff line number Diff line change
Expand Up @@ -107,7 +107,7 @@ public function get_alpha2() {
* @return string|null
*/
public function get_url( $options ) {
if ( $this->obj->userblog_id == MslsBlogCollection::instance()->get_current_blog_id() ) {
if ( $this->obj->userblog_id == msls_blog_collection()->get_current_blog_id() ) {
return $options->get_current_link();
}

Expand Down
4 changes: 3 additions & 1 deletion includes/MslsBlogCollection.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@

namespace lloc\Msls;

use const Patchwork\CodeManipulation\Actions\RedefinitionOfNew\publicizeConstructors;

/**
* Collection of blog-objects
*
Expand Down Expand Up @@ -330,4 +332,4 @@ public static function get_blog_language( $blog_id = null, $default = 'en_US' )
return '' !== $language ? $language : $default;
}

}
}
2 changes: 1 addition & 1 deletion includes/MslsCustomColumn.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ class MslsCustomColumn extends MslsMain {
*/
public static function init() {
$options = MslsOptions::instance();
$collection = MslsBlogCollection::instance();
$collection = msls_blog_collection();
$obj = new static( $options, $collection );

if ( ! $options->is_excluded() ) {
Expand Down
2 changes: 1 addition & 1 deletion includes/MslsCustomColumnTaxonomy.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ class MslsCustomColumnTaxonomy extends MslsCustomColumn {
*/
public static function init() {
$options = MslsOptions::instance();
$collection = MslsBlogCollection::instance();
$collection = msls_blog_collection();
$obj = new static( $options, $collection );

if ( ! $options->is_excluded() ) {
Expand Down
2 changes: 1 addition & 1 deletion includes/MslsCustomFilter.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ class MslsCustomFilter extends MslsMain {
*/
public static function init() {
$options = MslsOptions::instance();
$collection = MslsBlogCollection::instance();
$collection = msls_blog_collection();
$obj = new static( $options, $collection );

if ( ! $options->is_excluded() ) {
Expand Down
4 changes: 2 additions & 2 deletions includes/MslsMain.php
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ public function __construct( MslsOptions $options, MslsBlogCollection $collectio
*/
public static function init() {
$options = MslsOptions::instance();
$collection = MslsBlogCollection::instance();
$collection = msls_blog_collection();

return new static( $options, $collection );
}
Expand Down Expand Up @@ -169,7 +169,7 @@ protected function save( $object_id, $class ) {
}

if ( ! $this->collection->has_current_blog() ) {
$this->debugger( 'MslsBlogCollection::instance()->has_current_blog returns false.' );
$this->debugger( 'BlogCollection returns false when calling has_current_blog.' );

return;
}
Expand Down
2 changes: 1 addition & 1 deletion includes/MslsMetaBox.php
Original file line number Diff line number Diff line change
Expand Up @@ -103,7 +103,7 @@ public static function get_suggested_fields( $json, $args ) {
*/
public static function init() {
$options = MslsOptions::instance();
$collection = MslsBlogCollection::instance();
$collection = msls_blog_collection();
$obj = new static( $options, $collection );

if ( ! $options->is_excluded() ) {
Expand Down
2 changes: 1 addition & 1 deletion includes/MslsOptionsPost.php
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ public function get_postlink( $language ) {
}

global $current_site;
$blog_id = MslsBlogCollection::instance()->get_blog_id( $language );
$blog_id = msls_blog_collection()->get_blog_id( $language );
if ( $current_site->blog_id != $blog_id ) {
$option = get_blog_option( $blog_id, 'msls' );
//error_log( print_r( $option, true ) );
Expand Down
2 changes: 1 addition & 1 deletion includes/MslsOutput.php
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,7 @@ public function get( $display, $filter = false, $exists = false ) {
* @return string
*/
public function get_alternate_links() {
$blogs = MslsBlogCollection::instance();
$blogs = msls_blog_collection();
$hreflang = new HrefLang( $blogs );
$options = MslsOptions::create();

Expand Down
2 changes: 1 addition & 1 deletion includes/MslsPlugin.php
Original file line number Diff line number Diff line change
Expand Up @@ -123,7 +123,7 @@ public static function get_output() {
public static function update_adminbar( \WP_Admin_Bar $wp_admin_bar ): void {
$icon_type = MslsOptions::instance()->get_icon_type();

$blog_collection = MslsBlogCollection::instance();
$blog_collection = msls_blog_collection();
foreach ( $blog_collection->get_plugin_active_blogs() as $blog ) {
$title = $blog->get_blavatar() . $blog->get_title( $icon_type );

Expand Down
2 changes: 1 addition & 1 deletion includes/MslsPostTag.php
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@ public static function suggest() {
*/
public static function init() {
$options = MslsOptions::instance();
$collection = MslsBlogCollection::instance();
$collection = msls_blog_collection();

if ( $options->activate_autocomplete ) {
$obj = new static( $options, $collection );
Expand Down
32 changes: 12 additions & 20 deletions includes/MslsWidget.php
Original file line number Diff line number Diff line change
Expand Up @@ -37,33 +37,25 @@ public function __construct() {
* @user MslsOutput
*/
public function widget( $args, $instance ) {
$args = wp_parse_args(
$args,
[
'before_widget' => '',
'after_widget' => '',
'before_title' => '',
'after_title' => '',
]
);
$default = [
'before_widget' => '',
'after_widget' => '',
'before_title' => '',
'after_title' => '',
];

$args = wp_parse_args( $args, $default );

/** This filter is documented in wp-includes/default-widgets.php */
$title = apply_filters(
'widget_title',
( $instance['title'] ?? '' ),
$instance,
$this->id_base
);
$title = apply_filters( 'widget_title', $instance['title'] ?? '', $instance, $this->id_base );
if ( $title ) {
$title = $args['before_title'] . esc_attr( $title ) . $args['after_title'];
}

$content = MslsOutput::init()->__toString();
if ( '' == $content ) {
$content = apply_filters(
'msls_widget_alternative_content',
__( 'No available translations found', 'multisite-language-switcher' )
);
if ( '' === $content ) {
$text = __( 'No available translations found', 'multisite-language-switcher' );
$content = apply_filters( 'msls_widget_alternative_content', $text );
}

echo $args['before_widget'], $title, $content, $args['after_widget'];
Expand Down
24 changes: 0 additions & 24 deletions tests/bootstrap.php
Original file line number Diff line number Diff line change
Expand Up @@ -27,30 +27,6 @@ protected function setUp(): void {
Functions\when( '__' )->returnArg();
}

/**
* Get a of the MslsBlogCollection class that contains some blogs
*
* @param $map
*
* @return void
*/
public function getBlogsCollection( $map = [ 'de_DE' => 'de', 'en_US' => 'en' ] ): MslsBlogCollection {
foreach ( $map as $locale => $alpha2 ) {
$blog = \Mockery::mock( MslsBlog::class );
$blog->shouldReceive( [
'get_alpha2' => $alpha2,
'get_language' => $locale,
] );

$blogs[] = $blog;
}

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

return $collection;
}

protected function tearDown(): void {
restore_error_handler();
Expand Down
114 changes: 100 additions & 14 deletions tests/test-mslsblog.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,28 +4,91 @@

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

class WP_Test_MslsBlog extends Msls_UnitTestCase {

/**
* Verify the __get-method
*/
function test___get_method() {
public function get_blog(): MslsBlog {
Functions\expect( 'get_blog_option' )->once()->andReturn( 'it_IT' );
Functions\expect( 'add_query_arg' )->once()->andReturn( 'https://example.org/added-args' );
Functions\expect( 'plugin_dir_path' )->atLeast( 1 )->andReturn( dirname( __DIR__, 1 ) . '/' );

$blog = new \stdClass();
$blog->userblog_id = 1;
$blog->blogname = 'Test';

$obj = new MslsBlog( $blog, 'Italiano' );
return new MslsBlog( $blog, 'Italiano' );
}

public function test_get_userblog_id(): void {
$this->assertEquals( 1, $this->get_blog()->userblog_id );
}

$this->assertEquals( 1, $obj->userblog_id );
$this->assertEquals( 'Italiano', $obj->get_description() );
$this->assertEquals( 'it_IT', $obj->get_language() );
$this->assertEquals( 'it', $obj->get_alpha2() );
$this->assertEquals( 'Test <span class="msls-icon-wrapper flag"><span class="flag-icon flag-icon-it">it_IT</span></span>', $obj->get_title() );
public function test_get_description(): void {
$this->assertEquals( 'Italiano', $this->get_blog()->get_description() );
}

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

$option = \Mockery::mock( MslsOptions::class );
$option->shouldReceive( 'get_current_link' )->andReturn( $url );

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

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

$this->assertEquals( $url, $this->get_blog()->get_url( $option ) );
}

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

$option = \Mockery::mock( MslsOptions::class );
$option->shouldReceive( 'get_permalink' )->once()->andReturn( $url );

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

Functions\expect( 'msls_blog_collection' )->once()->andReturn( $collection );
Functions\expect( 'is_front_page' )->once()->andReturn( true );
Functions\expect( 'switch_to_blog' )->once();
Functions\expect( 'restore_current_blog' )->once();

$this->assertEquals( $url, $this->get_blog()->get_url( $option ) );
}

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

$option = \Mockery::mock( MslsOptions::class );
$option->shouldReceive( 'get_permalink' )->once()->andReturn( $url );
$option->shouldReceive( 'has_value' )->once()->andReturn( true );

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

Functions\expect( 'msls_blog_collection' )->once()->andReturn( $collection );
Functions\expect( 'is_front_page' )->once()->andReturn( false );
Functions\expect( 'switch_to_blog' )->once();
Functions\expect( 'restore_current_blog' )->once();

$this->assertEquals( $url, $this->get_blog()->get_url( $option ) );
}

public function test_get_language(): void {
$this->assertEquals( 'it_IT', $this->get_blog()->get_language() );
}

public function test_get_alpha2(): void {
$this->assertEquals( 'it', $this->get_blog()->get_alpha2() );
}

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

$this->assertEquals( 'Test <span class="msls-icon-wrapper flag"><span class="flag-icon flag-icon-it">it_IT</span></span>', $this->get_blog()->get_title() );
}

/**
Expand All @@ -47,28 +110,51 @@ public function compareProvider() {
* Verify the _cmp-method
* @dataProvider compareProvider
*/
function test__cmp_method( $a, $b, $expected ) {
public function test__cmp_method( $a, $b, $expected ): void {
$this->assertEquals( $expected, MslsBlog::_cmp( $a, $b ) );

$obj = new MslsBlog( null, null );
$this->assertEquals( $expected, $obj->_cmp( $a, $b ) );
}

/**
* Verify the language-method
*/
function test_language_cmp() {
public function test_language_cmp(): void {
$a = new MslsBlog( null, null );
$b = new MslsBlog( null, null );

$this->assertEquals( 0, $a->language( $a, $b ) );
}

/**
* Verify the description-method
*/
function test_description_cmp() {
public function test_description_cmp(): void {
$a = new MslsBlog( null, null );
$b = new MslsBlog( null, null );

$this->assertEquals( 0, $a->description( $a, $b ) );
}

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( '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" />', $this->get_blog()->get_blavatar() );
}

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( '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" />', $this->get_blog()->get_blavatar() );
}

}
Loading