Completed
Push — fix/cover-block-nudge-in-site-... ( e981e8...d35272 )
by
unknown
221:21 queued 211:25
created

Test_Blocks::set_up()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
nc 1
nop 0
dl 0
loc 4
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
// phpcs:disable Generic.Files.OneObjectStructurePerFile.MultipleFound
11
12
/**
13
 * Helper function to create an empty Jetpack_Gutenberg class for the
14
 * purposes of tests that depend on its existence.
15
 */
16
function create_jetpack_gutenberg_class() {
17
	if ( ! class_exists( '\Jetpack_Gutenberg' ) ) {
18
		/**
19
		 * Empty class to simulate the existence of the Jetpack_Gutenberg class.
20
		 */
21
		class Jetpack_Gutenberg {}
22
	}
23
}
24
25
namespace Automattic\Jetpack;
0 ignored issues
show
Bug introduced by
This code did not parse for me. Apparently, there is an error somewhere around this line:

Namespace declaration statement has to be the very first statement in the script
Loading history...
26
27
use PHPUnit\Framework\TestCase;
28
29
/**
30
 * Class Test_Blocks
31
 */
32
class Test_Blocks extends TestCase {
33
34
	use \Yoast\PHPUnitPolyfills\Polyfills\AssertStringContains;
35
36
	/**
37
	 * Test block name.
38
	 *
39
	 * @var string
40
	 */
41
	public $block_name = 'jetpack/apple';
42
43
	/**
44
	 * Setup runs before each test.
45
	 *
46
	 * @before
47
	 */
48
	public function set_up() {
49
		// Register a test block.
50
		Blocks::jetpack_register_block( $this->block_name );
51
	}
52
53
	/**
54
	 * Teardown runs after each test.
55
	 *
56
	 * @after
57
	 */
58
	public function tear_down() {
59
		// Unregister the test Jetpack block we may have created for our tests.
60
		unregister_block_type( $this->block_name );
61
	}
62
63
	/**
64
	 * Test the different inputs and matching output for Classes.
65
	 *
66
	 * @since 9.0.0
67
	 *
68
	 * @covers Automattic\Jetpack\Blocks::classes
69
	 */
70
	public function test_block_classes() {
71
		$block_name = 'foo';
72
		$attr       = array(
73
			'bar'       => 'baz',
74
			'align'     => 'wide',
75
			'className' => 'editorclass',
76
		);
77
		$extra      = array( 'extraclass' );
78
79
		$block_classes = Blocks::classes( $block_name, $attr, $extra );
80
81
		$this->assertStringContainsString( 'wp-block-jetpack-foo', $block_classes ); // a general class is created from the block name.
82
		$this->assertStringNotContainsString( 'bar', $block_classes ); // The extra 'bar' attribute should be dropped.
83
		$this->assertStringNotContainsString( 'baz', $block_classes ); // The extra 'baz' attribute should be dropped.
84
		$this->assertStringNotContainsString( 'align ', $block_classes ); // The align attribute should only be used to create a new attribute.
85
		$this->assertStringNotContainsString( 'className', $block_classes ); // The className attribute should be dropped, only the editorclass value should remain.
86
		$this->assertStringContainsString( 'alignwide', $block_classes ); // an alignment class is created.
87
		$this->assertStringContainsString( 'editorclass', $block_classes ); // className classes are passed.
88
		$this->assertStringContainsString( 'extraclass', $block_classes ); // Extra class remains.
89
	}
90
91
	/**
92
	 * Test for invalid alignment values.
93
	 *
94
	 * @since 9.0.0
95
	 *
96
	 * @covers Automattic\Jetpack\Blocks::classes
97
	 */
98
	public function test_block_classes_invalid_align() {
99
		$attr          = array( 'align' => 'test' );
100
		$block_classes = Blocks::classes( 'test', $attr );
101
102
		$this->assertStringNotContainsString( 'aligntest', $block_classes );
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_amp_request() {
113
		add_filter( 'jetpack_is_amp_request', '__return_true' );
114
		try {
115
			$this->assertTrue( Blocks::is_amp_request() );
116
		} finally {
117
			remove_filter( 'jetpack_is_amp_request', '__return_true' );
118
		}
119
	}
120
121
	/**
122
	 * Test whether we can detect an AMP view.
123
	 *
124
	 * @since 9.0.0
125
	 *
126
	 * @covers Automattic\Jetpack\Blocks::is_amp_request
127
	 */
128
	public function test_is_not_amp_request() {
129
		$this->assertFalse( Blocks::is_amp_request() );
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_false_if_core_wp_version_less_than_minimum() {
138
		$version_gated = Blocks::is_gutenberg_version_available(
139
			array(
140
				'wp'        => '999999',
141
				'gutenberg' => '999999',
142
			),
143
			'gated_block'
144
		);
145
		$this->assertFalse( false, $version_gated );
146
	}
147
148
	/**
149
	 * Test WordPress and Gutenberg version requirements.
150
	 *
151
	 * @covers Automattic\Jetpack\Blocks::is_gutenberg_version_available
152
	 */
153
	public function test_returns_true_if_core_wp_version_greater_or_equal_to_minimum() {
154
		$version_gated = Blocks::is_gutenberg_version_available(
155
			array(
156
				'wp'        => '1',
157
				'gutenberg' => '999999',
158
			),
159
			'ungated_block'
160
		);
161
		$this->assertTrue( true, $version_gated );
162
	}
163
164
	/**
165
	 * Testing removing the Jetpack prefix from a block slug.
166
	 *
167
	 * @covers Automattic\Jetpack\Blocks::remove_extension_prefix
168
	 *
169
	 * @dataProvider get_extension_name_provider
170
	 *
171
	 * @param string $extension_slug      Block / Extension name.
172
	 * @param string $expected_short_slug Extension name without Jetpack prefix.
173
	 */
174
	public function test_remove_extension_prefix( $extension_slug, $expected_short_slug ) {
175
		$short_slug = Blocks::remove_extension_prefix( $extension_slug );
176
177
		$this->assertEquals( $expected_short_slug, $short_slug );
178
	}
179
180
	/**
181
	 * Get different possible block names.
182
	 *
183
	 * Data provider for test_remove_extension_prefix.
184
	 */
185
	public function get_extension_name_provider() {
186
		return array(
187
			'not_jetpack'    => array(
188
				'woocommerce/product-best-sellers',
189
				'woocommerce/product-best-sellers',
190
			),
191
			'jetpack_dash'   => array(
192
				'jetpack/shortlinks',
193
				'shortlinks',
194
			),
195
			'jetpack_hyphen' => array(
196
				'jetpack-shortlinks',
197
				'shortlinks',
198
			),
199
		);
200
	}
201
202
	/**
203
	 * Test to ensure that an extension is returned as registered.
204
	 *
205
	 * @covers Automattic\Jetpack\Blocks::is_registered
206
	 */
207
	public function test_is_extension_registered() {
208
		// Test for the block that is registered for all tests here.
209
		$this->assertTrue( Blocks::is_registered( $this->block_name ) );
210
		// Test for a non-existing block.
211
		$this->assertFalse( Blocks::is_registered( 'foo/bar' ) );
212
	}
213
214
	/**
215
	 * Ensure blocks cannot be registered twice.
216
	 *
217
	 * @covers Automattic\Jetpack\Blocks::jetpack_register_block
218
	 */
219
	public function test_jetpack_register_block_twice() {
220
		$result = Blocks::jetpack_register_block( $this->block_name );
221
		$this->assertFalse( $result );
222
	}
223
224
	/**
225
	 * Test to ensure blocks without a Jetpack prefix are registered, but with a jetpack prefix.
226
	 *
227
	 * @expectedIncorrectUsage Automattic\Jetpack\Blocks::jetpack_register_block
228
	 * @covers Automattic\Jetpack\Blocks::jetpack_register_block
229
	 */
230
	public function test_jetpack_register_block_without_jetpack() {
231
		$result = Blocks::jetpack_register_block( 'doing-it-wrong' );
232
		$this->assertEquals( 'jetpack/doing-it-wrong', $result->name );
233
	}
234
235
	/**
236
	 * Test to ensure registering a Jetpack block does not add in an editor style dependency,
237
	 * when the Jetpack_Gutenberg class is not available.
238
	 *
239
	 * @covers Automattic\Jetpack\Blocks::jetpack_register_block
240
	 */
241
	public function test_jetpack_register_block_without_editor_style() {
242
		$result = Blocks::jetpack_register_block( 'jetpack/block-without-editor-style' );
243
		$this->assertEquals( 'jetpack/block-without-editor-style', $result->name );
244
		$this->assertEquals( null, $result->editor_style );
245
	}
246
247
	/**
248
	 * Test to ensure registering a Jetpack block adds in an editor style dependency,
249
	 * when the Jetpack_Gutenberg class is available.
250
	 *
251
	 * @covers Automattic\Jetpack\Blocks::jetpack_register_block
252
	 */
253
	public function test_jetpack_register_block_with_editor_style() {
254
		// Ensure a Jetpack_Gutenberg class is available.
255
		create_jetpack_gutenberg_class();
256
257
		$result = Blocks::jetpack_register_block( 'jetpack/block-with-editor-style' );
258
		$this->assertEquals( 'jetpack/block-with-editor-style', $result->name );
259
		$this->assertEquals( 'jetpack-blocks-editor', $result->editor_style );
260
	}
261
262
	/**
263
	 * Test to ensure registering a Jetpack block does not override an existing dependency,
264
	 * when the Jetpack_Gutenberg class is available.
265
	 *
266
	 * @covers Automattic\Jetpack\Blocks::jetpack_register_block
267
	 */
268
	public function test_jetpack_register_block_with_existing_editor_style() {
269
		// Ensure a Jetpack_Gutenberg class is available.
270
		create_jetpack_gutenberg_class();
271
272
		$result = Blocks::jetpack_register_block(
273
			'jetpack/block-with-existing-editor-style',
274
			array(
275
				'editor_style' => 'custom-editor-style',
276
			)
277
		);
278
279
		$this->assertEquals( 'jetpack/block-with-existing-editor-style', $result->name );
280
		$this->assertEquals( 'custom-editor-style', $result->editor_style );
281
	}
282
}
283