Completed
Push — fix/sso_atomic_users ( 866d56...9ebcb8 )
by
unknown
10:04
created

Test_Blocks::test_is_standalone_block()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
nc 1
nop 0
dl 0
loc 3
rs 10
c 0
b 0
f 0
1
<?php // phpcs:ignore WordPress.Files.FileName.InvalidClassFileName
2
/**
3
 * Test methods from Automattic\Jetpack\Blocks
4
 *
5
 * @since 9.0.0
6
 *
7
 * @package automattic/jetpack-blocks
8
 */
9
10
namespace Automattic\Jetpack;
11
12
use PHPUnit\Framework\TestCase;
13
14
/**
15
 * Class Test_Blocks
16
 */
17
class Test_Blocks extends TestCase {
18
	use \Yoast\PHPUnitPolyfills\Polyfills\AssertStringContains;
19
20
	/**
21
	 * Test block name.
22
	 *
23
	 * @var string
24
	 */
25
	public $block_name = 'jetpack/apple';
26
27
	/**
28
	 * Setup runs before each test.
29
	 *
30
	 * @before
31
	 */
32
	public function set_up() {
33
		// Register a test block.
34
		Blocks::jetpack_register_block( $this->block_name );
35
	}
36
37
	/**
38
	 * Teardown runs after each test.
39
	 *
40
	 * @after
41
	 */
42
	public function tear_down() {
43
		// Unregister the test Jetpack block we may have created for our tests.
44
		unregister_block_type( $this->block_name );
45
	}
46
47
	/**
48
	 * Test the different inputs and matching output for Classes.
49
	 *
50
	 * @since 9.0.0
51
	 *
52
	 * @covers Automattic\Jetpack\Blocks::classes
53
	 */
54
	public function test_block_classes() {
55
		$block_name = 'foo';
56
		$attr       = array(
57
			'bar'       => 'baz',
58
			'align'     => 'wide',
59
			'className' => 'editorclass',
60
		);
61
		$extra      = array( 'extraclass' );
62
63
		$block_classes = Blocks::classes( $block_name, $attr, $extra );
64
65
		$this->assertStringContainsString( 'wp-block-jetpack-foo', $block_classes ); // a general class is created from the block name.
66
		$this->assertStringNotContainsString( 'bar', $block_classes ); // The extra 'bar' attribute should be dropped.
67
		$this->assertStringNotContainsString( 'baz', $block_classes ); // The extra 'baz' attribute should be dropped.
68
		$this->assertStringNotContainsString( 'align ', $block_classes ); // The align attribute should only be used to create a new attribute.
69
		$this->assertStringNotContainsString( 'className', $block_classes ); // The className attribute should be dropped, only the editorclass value should remain.
70
		$this->assertStringContainsString( 'alignwide', $block_classes ); // an alignment class is created.
71
		$this->assertStringContainsString( 'editorclass', $block_classes ); // className classes are passed.
72
		$this->assertStringContainsString( 'extraclass', $block_classes ); // Extra class remains.
73
	}
74
75
	/**
76
	 * Test for invalid alignment values.
77
	 *
78
	 * @since 9.0.0
79
	 *
80
	 * @covers Automattic\Jetpack\Blocks::classes
81
	 */
82
	public function test_block_classes_invalid_align() {
83
		$attr          = array( 'align' => 'test' );
84
		$block_classes = Blocks::classes( 'test', $attr );
85
86
		$this->assertStringNotContainsString( 'aligntest', $block_classes );
87
	}
88
89
	/**
90
	 * Test whether we can detect an AMP view.
91
	 *
92
	 * @since 9.0.0
93
	 *
94
	 * @covers Automattic\Jetpack\Blocks::is_amp_request
95
	 */
96
	public function test_is_amp_request() {
97
		add_filter( 'jetpack_is_amp_request', '__return_true' );
98
		try {
99
			$this->assertTrue( Blocks::is_amp_request() );
100
		} finally {
101
			remove_filter( 'jetpack_is_amp_request', '__return_true' );
102
		}
103
	}
104
105
	/**
106
	 * Test whether we can detect an AMP view.
107
	 *
108
	 * @since 9.0.0
109
	 *
110
	 * @covers Automattic\Jetpack\Blocks::is_amp_request
111
	 */
112
	public function test_is_not_amp_request() {
113
		$this->assertFalse( Blocks::is_amp_request() );
114
	}
115
116
	/**
117
	 * Test WordPress and Gutenberg version requirements.
118
	 *
119
	 * @covers Automattic\Jetpack\Blocks::is_gutenberg_version_available
120
	 */
121
	public function test_returns_false_if_core_wp_version_less_than_minimum() {
122
		$version_gated = Blocks::is_gutenberg_version_available(
123
			array(
124
				'wp'        => '999999',
125
				'gutenberg' => '999999',
126
			),
127
			'gated_block'
128
		);
129
		$this->assertFalse( false, $version_gated );
130
	}
131
132
	/**
133
	 * Test WordPress and Gutenberg version requirements.
134
	 *
135
	 * @covers Automattic\Jetpack\Blocks::is_gutenberg_version_available
136
	 */
137
	public function test_returns_true_if_core_wp_version_greater_or_equal_to_minimum() {
138
		$version_gated = Blocks::is_gutenberg_version_available(
139
			array(
140
				'wp'        => '1',
141
				'gutenberg' => '999999',
142
			),
143
			'ungated_block'
144
		);
145
		$this->assertTrue( true, $version_gated );
146
	}
147
148
	/**
149
	 * Testing removing the Jetpack prefix from a block slug.
150
	 *
151
	 * @covers Automattic\Jetpack\Blocks::remove_extension_prefix
152
	 *
153
	 * @dataProvider get_extension_name_provider
154
	 *
155
	 * @param string $extension_slug      Block / Extension name.
156
	 * @param string $expected_short_slug Extension name without Jetpack prefix.
157
	 */
158
	public function test_remove_extension_prefix( $extension_slug, $expected_short_slug ) {
159
		$short_slug = Blocks::remove_extension_prefix( $extension_slug );
160
161
		$this->assertEquals( $expected_short_slug, $short_slug );
162
	}
163
164
	/**
165
	 * Get different possible block names.
166
	 *
167
	 * Data provider for test_remove_extension_prefix.
168
	 */
169
	public function get_extension_name_provider() {
170
		return array(
171
			'not_jetpack'    => array(
172
				'woocommerce/product-best-sellers',
173
				'woocommerce/product-best-sellers',
174
			),
175
			'jetpack_dash'   => array(
176
				'jetpack/shortlinks',
177
				'shortlinks',
178
			),
179
			'jetpack_hyphen' => array(
180
				'jetpack-shortlinks',
181
				'shortlinks',
182
			),
183
		);
184
	}
185
186
	/**
187
	 * Test to ensure that an extension is returned as registered.
188
	 *
189
	 * @covers Automattic\Jetpack\Blocks::is_registered
190
	 */
191
	public function test_is_extension_registered() {
192
		// Test for the block that is registered for all tests here.
193
		$this->assertTrue( Blocks::is_registered( $this->block_name ) );
194
		// Test for a non-existing block.
195
		$this->assertFalse( Blocks::is_registered( 'foo/bar' ) );
196
	}
197
198
	/**
199
	 * Ensure blocks cannot be registered twice.
200
	 *
201
	 * @covers Automattic\Jetpack\Blocks::jetpack_register_block
202
	 */
203
	public function test_jetpack_register_block_twice() {
204
		$result = Blocks::jetpack_register_block( $this->block_name );
205
		$this->assertFalse( $result );
206
	}
207
208
	/**
209
	 * Test to ensure blocks without a Jetpack prefix are registered, but with a jetpack prefix.
210
	 *
211
	 * @expectedIncorrectUsage Automattic\Jetpack\Blocks::jetpack_register_block
212
	 * @covers Automattic\Jetpack\Blocks::jetpack_register_block
213
	 */
214
	public function test_jetpack_register_block_without_jetpack() {
215
		$result = Blocks::jetpack_register_block( 'doing-it-wrong' );
216
		$this->assertEquals( 'jetpack/doing-it-wrong', $result->name );
217
	}
218
219
	/**
220
	 * Test that by default we are not running in a Jetpack plugin context.
221
	 *
222
	 * @since 9.6.0
223
	 *
224
	 * @covers Automattic\Jetpack\Blocks::is_standalone_block
225
	 */
226
	public function test_is_standalone_block() {
227
		$this->assertTrue( Blocks::is_standalone_block() );
228
	}
229
230
	/**
231
	 * Test that we are running in a Jetpack plugin context, and not
232
	 * as a standalone block.
233
	 *
234
	 * @since 9.6.0
235
	 *
236
	 * @covers Automattic\Jetpack\Blocks::is_standalone_block
237
	 */
238
	public function test_is_not_standalone_block() {
239
		add_filter( 'jetpack_is_standalone_block', '__return_false' );
240
		try {
241
			$this->assertFalse( Blocks::is_standalone_block() );
242
		} finally {
243
			remove_filter( 'jetpack_is_standalone_block', '__return_false' );
244
		}
245
	}
246
247
	/**
248
	 * Test to ensure registering a Jetpack block does not add in an editor style dependency,
249
	 * when the Jetpack_Gutenberg class is not available.
250
	 *
251
	 * @since 9.6.0
252
	 *
253
	 * @covers Automattic\Jetpack\Blocks::jetpack_register_block
254
	 */
255
	public function test_jetpack_register_block_without_editor_style() {
256
		$result = Blocks::jetpack_register_block( 'jetpack/block-without-editor-style' );
257
		$this->assertEquals( 'jetpack/block-without-editor-style', $result->name );
258
		$this->assertNull( $result->editor_style );
259
	}
260
261
	/**
262
	 * Test to ensure registering a Jetpack block adds in an editor style dependency,
263
	 * when the Jetpack_Gutenberg class is available.
264
	 *
265
	 * @since 9.6.0
266
	 *
267
	 * @covers Automattic\Jetpack\Blocks::jetpack_register_block
268
	 */
269 View Code Duplication
	public function test_jetpack_register_block_with_editor_style() {
270
		add_filter( 'jetpack_is_standalone_block', '__return_false' );
271
		try {
272
			$result = Blocks::jetpack_register_block( 'jetpack/block-with-editor-style' );
273
			$this->assertEquals( 'jetpack/block-with-editor-style', $result->name );
274
			$this->assertEquals( 'jetpack-blocks-editor', $result->editor_style );
275
		} finally {
276
			remove_filter( 'jetpack_is_standalone_block', '__return_false' );
277
		}
278
279
	}
280
281
	/**
282
	 * Test to ensure registering a Jetpack block does not override an existing dependency,
283
	 * when the Jetpack_Gutenberg class is available.
284
	 *
285
	 * @since 9.6.0
286
	 *
287
	 * @covers Automattic\Jetpack\Blocks::jetpack_register_block
288
	 */
289 View Code Duplication
	public function test_jetpack_register_block_with_existing_editor_style() {
290
		add_filter( 'jetpack_is_standalone_block', '__return_false' );
291
		try {
292
			$result = Blocks::jetpack_register_block(
293
				'jetpack/block-with-existing-editor-style',
294
				array(
295
					'editor_style' => 'custom-editor-style',
296
				)
297
			);
298
			$this->assertEquals( 'jetpack/block-with-existing-editor-style', $result->name );
299
			$this->assertEquals( 'custom-editor-style', $result->editor_style );
300
		} finally {
301
			remove_filter( 'jetpack_is_standalone_block', '__return_false' );
302
		}
303
	}
304
}
305