Skip to content

Commit c3d45cc

Browse files
committed
MslsAdminBar tested
1 parent f2d6f31 commit c3d45cc

File tree

3 files changed

+118
-26
lines changed

3 files changed

+118
-26
lines changed

includes/MslsAdminBar.php

Lines changed: 54 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -4,44 +4,73 @@
44

55
class MslsAdminBar {
66

7+
protected string $icon_type;
8+
9+
protected MslsBlogCollection $blog_collection;
10+
11+
public function __construct( MslsOptions $options, MslsBlogCollection $blog_collection ) {
12+
$this->icon_type = $options->get_icon_type();
13+
$this->blog_collection = $blog_collection;
14+
}
715

816
/**
917
* @codeCoverageIgnore
1018
*/
1119
public function init(): void {
20+
$obj = new MslsAdminBar( msls_options(), msls_blog_collection() );
21+
1222
if ( is_admin_bar_showing() ) {
13-
add_action( 'admin_bar_menu', array( __CLASS__, 'update_admin_bar' ), 999 );
23+
add_action( 'admin_bar_menu', array( $obj, 'update_admin_bar' ), 999 );
1424
}
1525
}
1626

1727
/**
18-
* @param $wp_admin_bar
19-
*
20-
* @return void
28+
* Callback that updates the admin bar with the blog information
2129
*/
22-
public static function update_admin_bar( \WP_Admin_Bar $wp_admin_bar ): void {
23-
$icon_type = msls_options()->get_icon_type();
24-
25-
$blog_collection = msls_blog_collection();
26-
foreach ( $blog_collection->get_plugin_active_blogs() as $blog ) {
27-
$title = $blog->get_blavatar() . $blog->get_title( $icon_type );
28-
29-
$wp_admin_bar->add_node(
30-
array(
31-
'id' => 'blog-' . $blog->userblog_id,
32-
'title' => $title,
33-
)
34-
);
30+
public function update_admin_bar( \WP_Admin_Bar $wp_admin_bar ): void {
31+
foreach ( $this->blog_collection->get_plugin_active_blogs() as $blog ) {
32+
$title = $this->get_title( $blog, true );
33+
34+
$title && $this->add_node( $wp_admin_bar, 'blog-' . $blog->userblog_id, $title );
3535
}
3636

37-
$blog = $blog_collection->get_current_blog();
38-
if ( is_object( $blog ) && method_exists( $blog, 'get_title' ) ) {
39-
$wp_admin_bar->add_node(
40-
array(
41-
'id' => 'site-name',
42-
'title' => $blog->get_title( $icon_type ),
43-
)
44-
);
37+
$blog = $this->blog_collection->get_current_blog();
38+
$title = $this->get_title( $blog );
39+
40+
$title && $this->add_node( $wp_admin_bar, 'site-name', $title );
41+
}
42+
43+
/**
44+
* Adds node information to an existing node
45+
*/
46+
public function add_node( \WP_Admin_Bar $wp_admin_bar, string $node_id, string $title ): bool {
47+
$node = $wp_admin_bar->get_node( $node_id );
48+
if ( is_null( $node ) ) {
49+
return false;
4550
}
51+
52+
$wp_admin_bar->add_node(
53+
array(
54+
'id' => $node_id,
55+
'title' => $title,
56+
)
57+
);
58+
59+
return true;
60+
}
61+
62+
/**
63+
* Gets a title with label orflag (depending on the settings) for the blog
64+
*
65+
* It uses a blavatar icon as prefix if $blavatar is set to true
66+
*/
67+
protected function get_title( ?MslsBlog $blog, bool $blavatar = false ): ?string {
68+
if ( is_null( $blog ) ) {
69+
return $blog;
70+
}
71+
72+
$prefix = $blavatar ? $blog->get_blavatar() : '';
73+
74+
return $prefix . $blog->get_title( $this->icon_type );
4675
}
4776
}

includes/MslsPlugin.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -145,7 +145,7 @@ public function content_filter( string $content ) {
145145
}
146146

147147
/**
148-
* Create filterstring for msls_content_filter()
148+
* Create filter-string for msls_content_filter()
149149
*
150150
* @param string $pref
151151
* @param string $post

tests/phpunit/TestMslsAdminBar.php

Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
<?php declare( strict_types=1 );
2+
3+
namespace lloc\MslsTests;
4+
5+
use Brain\Monkey\Functions;
6+
use lloc\Msls\MslsAdminBar;
7+
use lloc\Msls\MslsBlog;
8+
use lloc\Msls\MslsBlogCollection;
9+
use lloc\Msls\MslsLink;
10+
use lloc\Msls\MslsOptions;
11+
12+
class TestMslsAdminBar extends MslsUnitTestCase {
13+
14+
15+
public function setUp(): void {
16+
parent::setUp();
17+
18+
$options = \Mockery::mock( MslsOptions::class );
19+
$options->shouldReceive( 'get_icon_type' )->andReturn( 'label' );
20+
21+
$blog_a = \Mockery::mock( MslsBlog::class );
22+
$blog_a->userblog_id = 1;
23+
$blog_a->shouldReceive( 'get_title' )->andReturn( 'Blog A' );
24+
$blog_a->shouldReceive( 'get_blavatar' )->andReturn( '<div>Blavatar</div>' );
25+
26+
$blog_b = \Mockery::mock( MslsBlog::class );
27+
$blog_b->userblog_id = 2;
28+
$blog_b->shouldReceive( 'get_title' )->andReturn( 'Blog B' );
29+
$blog_b->shouldReceive( 'get_blavatar' )->andReturn( '<div>Blavatar</div>' );
30+
31+
$blog_c = null;
32+
33+
$collection = \Mockery::mock( MslsBlogCollection::class );
34+
$collection->shouldReceive( 'get_current_blog' )->andReturn( $blog_a );
35+
$collection->shouldReceive( 'get_plugin_active_blogs' )->andReturn( array( $blog_a, $blog_b, $blog_c ) );
36+
37+
$this->test = new MslsAdminBar( $options, $collection );
38+
}
39+
40+
public function test_add_node_false(): void {
41+
$wp_admin_bar = \Mockery::mock( \WP_Admin_Bar::class );
42+
$wp_admin_bar->shouldReceive( 'get_node' )->once()->andReturnNull();
43+
44+
$this->assertFalse( $this->test->add_node( $wp_admin_bar, 'node-id', 'title' ) );
45+
}
46+
47+
public function test_add_node_true(): void {
48+
$wp_admin_bar = \Mockery::mock( \WP_Admin_Bar::class );
49+
$wp_admin_bar->shouldReceive( 'get_node' )->once()->andReturn( (object) array() );
50+
$wp_admin_bar->shouldReceive( 'add_node' )->once();
51+
52+
$this->assertTrue( $this->test->add_node( $wp_admin_bar, 'node-id', 'title' ) );
53+
}
54+
55+
public function test_update_admin_bar(): void {
56+
$wp_admin_bar = \Mockery::mock( \WP_Admin_Bar::class );
57+
$wp_admin_bar->shouldReceive( 'get_node' )->times( 3 )->andReturn( (object) array() );
58+
$wp_admin_bar->shouldReceive( 'add_node' )->times( 3 );
59+
60+
$this->expectOutputString( '' );
61+
$this->test->update_admin_bar( $wp_admin_bar );
62+
}
63+
}

0 commit comments

Comments
 (0)