forked from lloc/Multisite-Language-Switcher
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathTestMslsBlog.php
More file actions
210 lines (162 loc) · 7.25 KB
/
TestMslsBlog.php
File metadata and controls
210 lines (162 loc) · 7.25 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
<?php declare( strict_types=1 );
namespace lloc\MslsTests;
use Brain\Monkey\Functions;
use lloc\Msls\MslsBlog;
use lloc\Msls\MslsBlogCollection;
use lloc\Msls\MslsOptions;
final class TestMslsBlog extends MslsUnitTestCase {
private function MslsBlogFactory(): MslsBlog {
Functions\expect( 'get_blog_option' )->once()->andReturn( 'it_IT' );
Functions\expect( 'plugin_dir_path' )->atLeast( 1 )->andReturn( dirname( __DIR__, 2 ) . '/' );
$blog = new \stdClass();
$blog->userblog_id = 1;
$blog->blogname = 'Test';
return new MslsBlog( $blog, 'Italiano' );
}
public function test_get_userblog_id(): void {
$this->assertEquals( 1, $this->MslsBlogFactory()->userblog_id );
}
public function test_get_description(): void {
$this->assertEquals( 'Italiano', $this->MslsBlogFactory()->get_description() );
}
public function test_get_url_current(): void {
$url = 'https://msls.co/';
$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->MslsBlogFactory()->get_url( $option ) );
}
public function test_get_frontpage(): void {
$url = 'https://msls.co/';
$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' )->atLeast()->once()->andReturn( true );
Functions\expect( 'is_home' )->zeroOrMoreTimes()->andReturn( false );
Functions\expect( 'switch_to_blog' )->once();
Functions\expect( 'restore_current_blog' )->once();
$this->assertEquals( $url, $this->MslsBlogFactory()->get_url( $option ) );
}
public function test_get_url(): void {
$url = 'https://msls.co/';
$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' )->atLeast()->once()->andReturn( false );
Functions\expect( 'is_home' )->atLeast()->once()->andReturn( false );
Functions\expect( 'switch_to_blog' )->once();
Functions\expect( 'restore_current_blog' )->once();
$this->assertEquals( $url, $this->MslsBlogFactory()->get_url( $option ) );
}
public function test_get_posts_page(): void {
$url = 'https://msls.co/sv/blogg/';
$option = \Mockery::mock( MslsOptions::class );
$option->shouldReceive( 'has_value' )->once()->andReturn( false );
$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' )->atLeast()->once()->andReturn( false );
Functions\expect( 'is_home' )->atLeast()->once()->andReturn( true );
Functions\expect( 'switch_to_blog' )->once();
Functions\expect( 'restore_current_blog' )->once();
Functions\expect( 'get_option' )->atLeast()->once()->andReturn( 42 );
Functions\expect( 'get_permalink' )->once()->with( 42 )->andReturn( $url );
$this->assertEquals( $url, $this->MslsBlogFactory()->get_url( $option ) );
}
public function test_get_posts_page_with_translation(): void {
$url = 'https://msls.co/sv/blogg/';
$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' )->atLeast()->once()->andReturn( false );
Functions\expect( 'is_home' )->atLeast()->once()->andReturn( true );
Functions\expect( 'switch_to_blog' )->once();
Functions\expect( 'restore_current_blog' )->once();
$this->assertEquals( $url, $this->MslsBlogFactory()->get_url( $option ) );
}
public function test_get_language(): void {
$this->assertEquals( 'it_IT', $this->MslsBlogFactory()->get_language() );
}
public function test_get_alpha2(): void {
$this->assertEquals( 'it', $this->MslsBlogFactory()->get_alpha2() );
}
public function test_get_title(): void {
Functions\expect( 'add_query_arg' )->once()->andReturn( 'https://msls.co/added-args' );
$this->assertEquals(
'Test <span class="msls-icon-wrapper flag"><span class="flag-icon flag-icon-it">it_IT</span></span>',
$this->MslsBlogFactory()->get_title()
);
}
/**
* Dataprovider
*
* @return array[]
*/
public static function compareProvider(): array {
return array(
array( 0, 0, 0 ),
array( 0, 1, - 1 ),
array( 1, 0, 1 ),
array( - 1, - 2, 1 ),
array( - 2, - 1, - 1 ),
);
}
/**
* Verify the _cmp-method
*
* @dataProvider compareProvider
*/
public function test__cmp_method( int $a, int $b, int $expected ): void {
$this->assertEquals( $expected, MslsBlog::internal_cmp( $a, $b ) );
$obj = new MslsBlog( null, null );
$this->assertEquals( $expected, $obj->internal_cmp( $a, $b ) );
}
/**
* Verify the language-method
*/
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
*/
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://msls.co/icons/abc.png' );
Functions\expect( 'wp_lazy_loading_enabled' )->once()->andReturn( true );
$this->assertEquals(
'<img class="blavatar" src="https://msls.co/icons/abc.png" srcset="https://msls.co/icons/abc.png 2x" alt="" width="16" height="16" loading="lazy" />',
$this->MslsBlogFactory()->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://msls.co/icons/abc.png' );
Functions\expect( 'wp_lazy_loading_enabled' )->once()->andReturn( false );
$this->assertEquals(
'<img class="blavatar" src="https://msls.co/icons/abc.png" srcset="https://msls.co/icons/abc.png 2x" alt="" width="16" height="16" />',
$this->MslsBlogFactory()->get_blavatar()
);
}
}