Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.
Common duplication problems, and corresponding solutions are:
| 1 | <?php // phpcs:ignore WordPress.Files.FileName.InvalidClassFileName |
||
| 19 | class Test_Blocks extends TestCase { |
||
| 20 | use \Yoast\PHPUnitPolyfills\Polyfills\AssertStringContains; |
||
| 21 | |||
| 22 | /** |
||
| 23 | * Test block name. |
||
| 24 | * |
||
| 25 | * @var string |
||
| 26 | */ |
||
| 27 | public $block_name = 'jetpack/apple'; |
||
| 28 | |||
| 29 | /** |
||
| 30 | * Setup runs before each test. |
||
| 31 | * |
||
| 32 | * @before |
||
| 33 | */ |
||
| 34 | public function set_up() { |
||
| 39 | |||
| 40 | /** |
||
| 41 | * Teardown runs after each test. |
||
| 42 | * |
||
| 43 | * @after |
||
| 44 | */ |
||
| 45 | public function tear_down() { |
||
| 50 | |||
| 51 | /** |
||
| 52 | * Test the different inputs and matching output for Classes. |
||
| 53 | * |
||
| 54 | * @since 9.0.0 |
||
| 55 | * |
||
| 56 | * @covers Automattic\Jetpack\Blocks::classes |
||
| 57 | */ |
||
| 58 | public function test_block_classes() { |
||
| 78 | |||
| 79 | /** |
||
| 80 | * Test for invalid alignment values. |
||
| 81 | * |
||
| 82 | * @since 9.0.0 |
||
| 83 | * |
||
| 84 | * @covers Automattic\Jetpack\Blocks::classes |
||
| 85 | */ |
||
| 86 | public function test_block_classes_invalid_align() { |
||
| 92 | |||
| 93 | /** |
||
| 94 | * Test whether we can detect an AMP view. |
||
| 95 | * |
||
| 96 | * @since 9.0.0 |
||
| 97 | * |
||
| 98 | * @covers Automattic\Jetpack\Blocks::is_amp_request |
||
| 99 | */ |
||
| 100 | public function test_is_amp_request() { |
||
| 108 | |||
| 109 | /** |
||
| 110 | * Test whether we can detect an AMP view. |
||
| 111 | * |
||
| 112 | * @since 9.0.0 |
||
| 113 | * |
||
| 114 | * @covers Automattic\Jetpack\Blocks::is_amp_request |
||
| 115 | */ |
||
| 116 | public function test_is_not_amp_request() { |
||
| 119 | |||
| 120 | /** |
||
| 121 | * Test WordPress and Gutenberg version requirements. |
||
| 122 | * |
||
| 123 | * @covers Automattic\Jetpack\Blocks::is_gutenberg_version_available |
||
| 124 | */ |
||
| 125 | public function test_returns_false_if_core_wp_version_less_than_minimum() { |
||
| 135 | |||
| 136 | /** |
||
| 137 | * Test WordPress and Gutenberg version requirements. |
||
| 138 | * |
||
| 139 | * @covers Automattic\Jetpack\Blocks::is_gutenberg_version_available |
||
| 140 | */ |
||
| 141 | public function test_returns_true_if_core_wp_version_greater_or_equal_to_minimum() { |
||
| 151 | |||
| 152 | /** |
||
| 153 | * Testing removing the Jetpack prefix from a block slug. |
||
| 154 | * |
||
| 155 | * @covers Automattic\Jetpack\Blocks::remove_extension_prefix |
||
| 156 | * |
||
| 157 | * @dataProvider get_extension_name_provider |
||
| 158 | * |
||
| 159 | * @param string $extension_slug Block / Extension name. |
||
| 160 | * @param string $expected_short_slug Extension name without Jetpack prefix. |
||
| 161 | */ |
||
| 162 | public function test_remove_extension_prefix( $extension_slug, $expected_short_slug ) { |
||
| 167 | |||
| 168 | /** |
||
| 169 | * Get different possible block names. |
||
| 170 | * |
||
| 171 | * Data provider for test_remove_extension_prefix. |
||
| 172 | */ |
||
| 173 | public function get_extension_name_provider() { |
||
| 189 | |||
| 190 | /** |
||
| 191 | * Test to ensure that an extension is returned as registered. |
||
| 192 | * |
||
| 193 | * @covers Automattic\Jetpack\Blocks::is_registered |
||
| 194 | */ |
||
| 195 | public function test_is_extension_registered() { |
||
| 201 | |||
| 202 | /** |
||
| 203 | * Ensure blocks cannot be registered twice. |
||
| 204 | * |
||
| 205 | * @covers Automattic\Jetpack\Blocks::jetpack_register_block |
||
| 206 | */ |
||
| 207 | public function test_jetpack_register_block_twice() { |
||
| 211 | |||
| 212 | /** |
||
| 213 | * Test to ensure blocks without a Jetpack prefix are registered, but with a jetpack prefix. |
||
| 214 | * |
||
| 215 | * @expectedIncorrectUsage Automattic\Jetpack\Blocks::jetpack_register_block |
||
| 216 | * @covers Automattic\Jetpack\Blocks::jetpack_register_block |
||
| 217 | */ |
||
| 218 | public function test_jetpack_register_block_without_jetpack() { |
||
| 222 | |||
| 223 | /** |
||
| 224 | * Test that we can detect an FSE theme. |
||
| 225 | * |
||
| 226 | * @since 9.8.0 |
||
| 227 | * |
||
| 228 | * @covers Automattic\Jetpack\Blocks::is_standalone_block |
||
| 229 | */ |
||
| 230 | public function test_is_not_fse_theme() { |
||
| 233 | |||
| 234 | /** |
||
| 235 | * Test that we can detect an FSE theme using the provided filter. |
||
| 236 | * |
||
| 237 | * @since 9.8.0 |
||
| 238 | * |
||
| 239 | * @covers Automattic\Jetpack\Blocks::is_standalone_block |
||
| 240 | */ |
||
| 241 | public function test_is_fse_theme_via_filter() { |
||
| 249 | |||
| 250 | /** |
||
| 251 | * Test that we can detect an FSE theme using the provided core function, gutenberg_is_fse_theme. |
||
| 252 | * |
||
| 253 | * @since 9.8.0 |
||
| 254 | * |
||
| 255 | * @covers Automattic\Jetpack\Blocks::is_standalone_block |
||
| 256 | */ |
||
| 257 | public function test_is_fse_theme_via_core_function() { |
||
| 262 | |||
| 263 | /** |
||
| 264 | * Test that by default we are not running in a Jetpack plugin context. |
||
| 265 | * |
||
| 266 | * @since 9.6.0 |
||
| 267 | * |
||
| 268 | * @covers Automattic\Jetpack\Blocks::is_standalone_block |
||
| 269 | */ |
||
| 270 | public function test_is_standalone_block() { |
||
| 273 | |||
| 274 | /** |
||
| 275 | * Test that we are running in a Jetpack plugin context, and not |
||
| 276 | * as a standalone block. |
||
| 277 | * |
||
| 278 | * @since 9.6.0 |
||
| 279 | * |
||
| 280 | * @covers Automattic\Jetpack\Blocks::is_standalone_block |
||
| 281 | */ |
||
| 282 | public function test_is_not_standalone_block() { |
||
| 290 | |||
| 291 | /** |
||
| 292 | * Test to ensure registering a Jetpack block does not add in an editor style dependency, |
||
| 293 | * when the Jetpack_Gutenberg class is not available. |
||
| 294 | * |
||
| 295 | * @since 9.6.0 |
||
| 296 | * |
||
| 297 | * @covers Automattic\Jetpack\Blocks::jetpack_register_block |
||
| 298 | */ |
||
| 299 | public function test_jetpack_register_block_without_editor_style() { |
||
| 304 | |||
| 305 | /** |
||
| 306 | * Test to ensure registering a Jetpack block adds in an editor style dependency, |
||
| 307 | * when the Jetpack_Gutenberg class is available. |
||
| 308 | * |
||
| 309 | * @since 9.6.0 |
||
| 310 | * |
||
| 311 | * @covers Automattic\Jetpack\Blocks::jetpack_register_block |
||
| 312 | */ |
||
| 313 | View Code Duplication | public function test_jetpack_register_block_with_editor_style() { |
|
| 324 | |||
| 325 | /** |
||
| 326 | * Test to ensure registering a Jetpack block does not override an existing dependency, |
||
| 327 | * when the Jetpack_Gutenberg class is available. |
||
| 328 | * |
||
| 329 | * @since 9.6.0 |
||
| 330 | * |
||
| 331 | * @covers Automattic\Jetpack\Blocks::jetpack_register_block |
||
| 332 | */ |
||
| 333 | View Code Duplication | public function test_jetpack_register_block_with_existing_editor_style() { |
|
| 348 | } |
||
| 349 |