Completed
Push — try/jetpack-stories-block-mobi... ( d8c065...8184fb )
by
unknown
74:50 queued 66:11
created

Test_Blocks   A

Complexity

Total Complexity 13

Size/Duplication

Total Lines 200
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 1

Importance

Changes 0
Metric Value
dl 0
loc 200
rs 10
c 0
b 0
f 0
wmc 13
lcom 1
cbo 1

13 Methods

Rating   Name   Duplication   Size   Complexity  
A setUp() 0 6 1
A tearDown() 0 6 1
A test_block_classes() 0 20 1
A test_block_classes_invalid_align() 0 6 1
A test_is_amp_request() 0 8 1
A test_is_not_amp_request() 0 3 1
A test_remove_extension_prefix() 0 5 1
A get_extension_name_provider() 0 16 1
A test_is_extension_registered() 0 6 1
A test_returns_false_if_core_wp_version_less_than_minimum() 0 10 1
A test_returns_true_if_core_wp_version_greater_or_equal_to_minimum() 0 10 1
A test_jetpack_register_block_twice() 0 4 1
A test_jetpack_register_block_without_jetpack() 0 4 1
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 Automattic\Jetpack\Blocks;
13
use PHPUnit\Framework\TestCase;
14
15
/**
16
 * Class Test_Blocks
17
 */
18
class Test_Blocks extends TestCase {
19
	/**
20
	 * Test block name.
21
	 *
22
	 * @var string
23
	 */
24
	public $block_name = 'jetpack/apple';
25
26
	/**
27
	 * Setup runs before each test.
28
	 */
29
	public function setUp() {
30
		parent::setUp();
31
32
		// Register a test block.
33
		Blocks::jetpack_register_block( $this->block_name );
34
	}
35
36
	/**
37
	 * Teardown runs after each test.
38
	 */
39
	public function tearDown() {
40
		parent::tearDown();
41
42
		// Unregister the test Jetpack block we may have created for our tests.
43
		unregister_block_type( $this->block_name );
44
	}
45
46
	/**
47
	 * Test the different inputs and matching output for Classes.
48
	 *
49
	 * @since 9.0.0
50
	 *
51
	 * @covers Automattic\Jetpack\Blocks::classes
52
	 */
53
	public function test_block_classes() {
54
		$block_name = 'foo';
55
		$attr       = array(
56
			'bar'       => 'baz',
57
			'align'     => 'wide',
58
			'className' => 'editorclass',
59
		);
60
		$extra      = array( 'extraclass' );
61
62
		$block_classes = Blocks::classes( $block_name, $attr, $extra );
63
64
		$this->assertContains( 'wp-block-jetpack-foo', $block_classes ); // a general class is created from the block name.
65
		$this->assertNotContains( 'bar', $block_classes ); // The extra 'bar' attribute should be dropped.
66
		$this->assertNotContains( 'baz', $block_classes ); // The extra 'baz' attribute should be dropped.
67
		$this->assertNotContains( 'align ', $block_classes ); // The align attribute should only be used to create a new attribute.
68
		$this->assertNotContains( 'className', $block_classes ); // The className attribute should be dropped, only the editorclass value should remain.
69
		$this->assertContains( 'alignwide', $block_classes ); // an alignment class is created.
70
		$this->assertContains( 'editorclass', $block_classes ); // className classes are passed.
71
		$this->assertContains( 'extraclass', $block_classes ); // Extra class remains.
72
	}
73
74
	/**
75
	 * Test for invalid alignment values.
76
	 *
77
	 * @since 9.0.0
78
	 *
79
	 * @covers Automattic\Jetpack\Blocks::classes
80
	 */
81
	public function test_block_classes_invalid_align() {
82
		$attr          = array( 'align' => 'test' );
83
		$block_classes = Blocks::classes( 'test', $attr );
84
85
		$this->assertNotContains( 'aligntest', $block_classes );
86
	}
87
88
	/**
89
	 * Test whether we can detect an AMP view.
90
	 *
91
	 * @since 9.0.0
92
	 *
93
	 * @covers Automattic\Jetpack\Blocks::is_amp_request
94
	 */
95
	public function test_is_amp_request() {
96
		add_filter( 'jetpack_is_amp_request', '__return_true' );
97
		try {
98
			$this->assertTrue( Blocks::is_amp_request() );
99
		} finally {
100
			remove_filter( 'jetpack_is_amp_request', '__return_true' );
101
		}
102
	}
103
104
	/**
105
	 * Test whether we can detect an AMP view.
106
	 *
107
	 * @since 9.0.0
108
	 *
109
	 * @covers Automattic\Jetpack\Blocks::is_amp_request
110
	 */
111
	public function test_is_not_amp_request() {
112
		$this->assertFalse( Blocks::is_amp_request() );
113
	}
114
115
	/**
116
	 * Test WordPress and Gutenberg version requirements.
117
	 *
118
	 * @covers Automattic\Jetpack\Blocks::is_gutenberg_version_available
119
	 */
120
	public function test_returns_false_if_core_wp_version_less_than_minimum() {
121
		$version_gated = Blocks::is_gutenberg_version_available(
122
			array(
123
				'wp'        => '999999',
124
				'gutenberg' => '999999',
125
			),
126
			'gated_block'
127
		);
128
		$this->assertFalse( false, $version_gated );
129
	}
130
131
	/**
132
	 * Test WordPress and Gutenberg version requirements.
133
	 *
134
	 * @covers Automattic\Jetpack\Blocks::is_gutenberg_version_available
135
	 */
136
	public function test_returns_true_if_core_wp_version_greater_or_equal_to_minimum() {
137
		$version_gated = Blocks::is_gutenberg_version_available(
138
			array(
139
				'wp'        => '1',
140
				'gutenberg' => '999999',
141
			),
142
			'ungated_block'
143
		);
144
		$this->assertTrue( true, $version_gated );
145
	}
146
147
	/**
148
	 * Testing removing the Jetpack prefix from a block slug.
149
	 *
150
	 * @covers Automattic\Jetpack\Blocks::remove_extension_prefix
151
	 *
152
	 * @dataProvider get_extension_name_provider
153
	 *
154
	 * @param string $extension_slug      Block / Extension name.
155
	 * @param string $expected_short_slug Extension name without Jetpack prefix.
156
	 */
157
	public function test_remove_extension_prefix( $extension_slug, $expected_short_slug ) {
158
		$short_slug = Blocks::remove_extension_prefix( $extension_slug );
159
160
		$this->assertEquals( $expected_short_slug, $short_slug );
161
	}
162
163
	/**
164
	 * Get different possible block names.
165
	 *
166
	 * Data provider for test_remove_extension_prefix.
167
	 */
168
	public function get_extension_name_provider() {
169
		return array(
170
			'not_jetpack'    => array(
171
				'woocommerce/product-best-sellers',
172
				'woocommerce/product-best-sellers',
173
			),
174
			'jetpack_dash'   => array(
175
				'jetpack/shortlinks',
176
				'shortlinks',
177
			),
178
			'jetpack_hyphen' => array(
179
				'jetpack-shortlinks',
180
				'shortlinks',
181
			),
182
		);
183
	}
184
185
	/**
186
	 * Test to ensure that an extension is returned as registered.
187
	 *
188
	 * @covers Automattic\Jetpack\Blocks::is_registered
189
	 */
190
	public function test_is_extension_registered() {
191
		// Test for the block that is registered for all tests here.
192
		$this->assertTrue( Blocks::is_registered( $this->block_name ) );
193
		// Test for a non-existing block.
194
		$this->assertFalse( Blocks::is_registered( 'foo/bar' ) );
195
	}
196
197
	/**
198
	 * Ensure blocks cannot be registered twice.
199
	 *
200
	 * @covers Automattic\Jetpack\Blocks::jetpack_register_block
201
	 */
202
	public function test_jetpack_register_block_twice() {
203
		$result = Blocks::jetpack_register_block( $this->block_name );
204
		$this->assertFalse( $result );
205
	}
206
207
	/**
208
	 * Test to ensure blocks without a Jetpack prefix are registered, but with a jetpack prefix.
209
	 *
210
	 * @expectedIncorrectUsage Automattic\Jetpack\Blocks::jetpack_register_block
211
	 * @covers Automattic\Jetpack\Blocks::jetpack_register_block
212
	 */
213
	public function test_jetpack_register_block_without_jetpack() {
214
		$result = Blocks::jetpack_register_block( 'doing-it-wrong' );
215
		$this->assertEquals( 'jetpack/doing-it-wrong', $result->name );
216
	}
217
}
218