Skip to content

Commit e3e6893

Browse files
authored
Merge pull request #1013 from GatherPress/unit-tests
Added tests for General_Block class.
2 parents c48fb79 + 25d2075 commit e3e6893

File tree

2 files changed

+298
-1
lines changed

2 files changed

+298
-1
lines changed

test/unit/php/includes/core/classes/blocks/class-test-dropdown.php

+1-1
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@
1212
use PMC\Unit_Test\Base;
1313

1414
/**
15-
* Class Test_Dropdown_Item.
15+
* Class Test_Dropdown.
1616
*
1717
* @coversDefaultClass \GatherPress\Core\Blocks\Dropdown
1818
*/
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,297 @@
1+
<?php
2+
/**
3+
* Class handles unit tests for GatherPress\Core\Blocks\General_Block.
4+
*
5+
* @package GatherPress\Core
6+
* @since 1.0.0
7+
*/
8+
9+
namespace GatherPress\Tests\Core\Blocks;
10+
11+
use GatherPress\Core\Blocks\General_Block;
12+
use PMC\Unit_Test\Base;
13+
14+
/**
15+
* Class Test_General_Block.
16+
*
17+
* @coversDefaultClass \GatherPress\Core\Blocks\General_Block
18+
*/
19+
class Test_General_Block extends Base {
20+
/**
21+
* Tests the setup_hooks method.
22+
*
23+
* Verifies that the appropriate filters are registered during setup,
24+
* ensuring the hooks are properly configured for General_Block.
25+
*
26+
* @since 1.0.0
27+
* @covers ::__construct
28+
* @covers ::setup_hooks
29+
*
30+
* @return void
31+
*/
32+
public function test_setup_hooks(): void {
33+
$instance = General_Block::get_instance();
34+
$hooks = array(
35+
array(
36+
'type' => 'filter',
37+
'name' => 'render_block',
38+
'priority' => 10,
39+
'callback' => array( $instance, 'remove_block_if_user_logged_in' ),
40+
),
41+
array(
42+
'type' => 'filter',
43+
'name' => 'render_block',
44+
'priority' => 10,
45+
'callback' => array( $instance, 'remove_block_if_registration_disabled' ),
46+
),
47+
);
48+
49+
$this->assert_hooks( $hooks, $instance );
50+
}
51+
52+
/**
53+
* Test block content is removed when user is logged in and block has login URL class.
54+
*
55+
* @since 1.0.0
56+
* @covers ::remove_block_if_user_logged_in
57+
*
58+
* @return void
59+
*/
60+
public function test_block_removed_when_user_logged_in(): void {
61+
$general_block = General_Block::get_instance();
62+
63+
// Create and log in a test user.
64+
$user_id = $this->factory->user->create();
65+
wp_set_current_user( $user_id );
66+
67+
$block_content = '<div>Test content</div>';
68+
$block = array(
69+
'attrs' => array(
70+
'className' => 'wp-block-example gatherpress--has-login-url',
71+
),
72+
);
73+
74+
$result = $general_block->remove_block_if_user_logged_in( $block_content, $block );
75+
76+
$this->assertEmpty(
77+
$result,
78+
'Block content should be empty when user is logged in and block has login URL class.'
79+
);
80+
81+
wp_set_current_user( 0 );
82+
}
83+
84+
/**
85+
* Test block content remains when user is not logged in but block has login URL class.
86+
*
87+
* @since 1.0.0
88+
* @covers ::remove_block_if_user_logged_in
89+
*
90+
* @return void
91+
*/
92+
public function test_block_remains_when_user_not_logged_in(): void {
93+
$general_block = General_Block::get_instance();
94+
95+
// Ensure no user is logged in.
96+
wp_set_current_user( 0 );
97+
98+
$block_content = '<div>Test content</div>';
99+
$block = array(
100+
'attrs' => array(
101+
'className' => 'wp-block-example gatherpress--has-login-url',
102+
),
103+
);
104+
105+
$result = $general_block->remove_block_if_user_logged_in( $block_content, $block );
106+
107+
$this->assertEquals(
108+
$block_content,
109+
$result,
110+
'Block content should remain unchanged when user is not logged in.'
111+
);
112+
}
113+
114+
/**
115+
* Test block content remains when user is logged in but block doesn't have login URL class.
116+
*
117+
* @since 1.0.0
118+
* @covers ::remove_block_if_user_logged_in
119+
*
120+
* @return void
121+
*/
122+
public function test_block_remains_without_login_url_class(): void {
123+
$general_block = General_Block::get_instance();
124+
125+
// Create and log in a test user.
126+
$user_id = $this->factory->user->create();
127+
wp_set_current_user( $user_id );
128+
129+
$block_content = '<div>Test content</div>';
130+
$block = array(
131+
'attrs' => array(
132+
'className' => 'wp-block-example',
133+
),
134+
);
135+
136+
$result = $general_block->remove_block_if_user_logged_in( $block_content, $block );
137+
138+
$this->assertEquals(
139+
$block_content,
140+
$result,
141+
'Block content should remain unchanged when block does not have login URL class.'
142+
);
143+
144+
wp_set_current_user( 0 );
145+
}
146+
147+
/**
148+
* Test block content remains when block has no className attribute.
149+
*
150+
* @since 1.0.0
151+
* @covers ::remove_block_if_user_logged_in
152+
*
153+
* @return void
154+
*/
155+
public function test_login_block_remains_without_classname_attribute(): void {
156+
$general_block = General_Block::get_instance();
157+
158+
// Create and log in a test user.
159+
$user_id = $this->factory->user->create();
160+
wp_set_current_user( $user_id );
161+
162+
$block_content = '<div>Test content</div>';
163+
$block = array(
164+
'attrs' => array(),
165+
);
166+
167+
$result = $general_block->remove_block_if_user_logged_in( $block_content, $block );
168+
169+
$this->assertEquals(
170+
$block_content,
171+
$result,
172+
'Block content should remain unchanged when block has no className attribute.'
173+
);
174+
175+
wp_set_current_user( 0 );
176+
}
177+
178+
/**
179+
* Test block content is removed when registration is disabled and block has registration URL class.
180+
*
181+
* @since 1.0.0
182+
* @covers ::remove_block_if_registration_disabled
183+
*
184+
* @return void
185+
*/
186+
public function test_block_removed_when_registration_disabled(): void {
187+
$general_block = General_Block::get_instance();
188+
189+
// Disable user registration.
190+
update_option( 'users_can_register', 0 );
191+
192+
$block_content = '<div>Test content</div>';
193+
$block = array(
194+
'attrs' => array(
195+
'className' => 'wp-block-example gatherpress--has-registration-url',
196+
),
197+
);
198+
199+
$result = $general_block->remove_block_if_registration_disabled( $block_content, $block );
200+
201+
$this->assertEmpty(
202+
$result,
203+
'Block content should be empty when registration is disabled and block has registration URL class.'
204+
);
205+
}
206+
207+
208+
/**
209+
* Test block content remains when registration is enabled and block has registration URL class.
210+
*
211+
* @since 1.0.0
212+
* @covers ::remove_block_if_registration_disabled
213+
*
214+
* @return void
215+
*/
216+
public function test_block_remains_when_registration_enabled(): void {
217+
$general_block = General_Block::get_instance();
218+
219+
// Enable user registration.
220+
update_option( 'users_can_register', 1 );
221+
222+
$block_content = '<div>Test content</div>';
223+
$block = array(
224+
'attrs' => array(
225+
'className' => 'wp-block-example gatherpress--has-registration-url',
226+
),
227+
);
228+
229+
$result = $general_block->remove_block_if_registration_disabled( $block_content, $block );
230+
231+
$this->assertEquals(
232+
$block_content,
233+
$result,
234+
'Block content should remain unchanged when registration is enabled.'
235+
);
236+
}
237+
238+
239+
/**
240+
* Test block content remains when registration is disabled but block doesn't have registration URL class.
241+
*
242+
* @since 1.0.0
243+
* @covers ::remove_block_if_registration_disabled
244+
*
245+
* @return void
246+
*/
247+
public function test_block_remains_without_registration_url_class(): void {
248+
$general_block = General_Block::get_instance();
249+
250+
// Disable user registration.
251+
update_option( 'users_can_register', 0 );
252+
253+
$block_content = '<div>Test content</div>';
254+
$block = array(
255+
'attrs' => array(
256+
'className' => 'wp-block-example',
257+
),
258+
);
259+
260+
$result = $general_block->remove_block_if_registration_disabled( $block_content, $block );
261+
262+
$this->assertEquals(
263+
$block_content,
264+
$result,
265+
'Block content should remain unchanged when block does not have registration URL class.'
266+
);
267+
}
268+
269+
270+
/**
271+
* Test block content remains when block has no className attribute.
272+
*
273+
* @since 1.0.0
274+
* @covers ::remove_block_if_registration_disabled
275+
*
276+
* @return void
277+
*/
278+
public function test_registration_block_remains_without_classname_attribute(): void {
279+
$general_block = General_Block::get_instance();
280+
281+
// Disable user registration.
282+
update_option( 'users_can_register', 0 );
283+
284+
$block_content = '<div>Test content</div>';
285+
$block = array(
286+
'attrs' => array(),
287+
);
288+
289+
$result = $general_block->remove_block_if_registration_disabled( $block_content, $block );
290+
291+
$this->assertEquals(
292+
$block_content,
293+
$result,
294+
'Block content should remain unchanged when block has no className attribute.'
295+
);
296+
}
297+
}

0 commit comments

Comments
 (0)