Completed
Push — update/business-hours-block-ph... ( 3401d5...d076d1 )
by
unknown
09:58 queued 10s
created

Story_Block_Test   A

Complexity

Total Complexity 10

Size/Duplication

Total Lines 127
Duplicated Lines 8.66 %

Coupling/Cohesion

Components 1
Dependencies 1

Importance

Changes 0
Metric Value
dl 11
loc 127
rs 10
c 0
b 0
f 0
wmc 10
lcom 1
cbo 1

5 Methods

Rating   Name   Duplication   Size   Complexity  
A set_up() 0 5 1
A tear_down() 0 6 2
A get_metadata() 0 20 2
A test_block_can_be_registered() 0 4 1
A test_server_side_rendering_based_on_serialized_fixtures() 11 52 4

How to fix   Duplicated Code   

Duplicated Code

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
2
/**
3
 * Story Block tests
4
 *
5
 * @package automattic/jetpack
6
 */
7
8
/**
9
 * Include the file containing the block's registration and render functions.
10
 */
11
require_once JETPACK__PLUGIN_DIR . 'extensions/blocks/story/story.php';
12
13
/**
14
 * Story Block tests.
15
 *
16
 * These tests primarily check that server rendered markup is not changing unexpectedly
17
 * when serialized fixtures are updated via the block's JS-based save behaviour.
18
 *
19
 * The goal is to catch when changes to serialized markup affects server rendering of the block.
20
 */
21
class Story_Block_Test extends \WP_UnitTestCase {
22
	/**
23
	 * A variable to track whether or not the block was already registered before the test was run.
24
	 *
25
	 * @access private
26
	 *
27
	 * @var boolean
28
	 */
29
	private $was_registered = false;
30
31
	/**
32
	 * Setup and ensure the block is registered before running the tests.
33
	 *
34
	 * @before
35
	 */
36
	public function set_up() {
37
		$this->was_registered = \Automattic\Jetpack\Blocks::is_registered( 'jetpack/story' );
38
		\Automattic\Jetpack\Extensions\Story\register_block();
39
		add_filter( 'get_post_metadata', array( $this, 'get_metadata' ), 10, 2 );
40
	}
41
42
	/**
43
	 * Teardown and unregister the block if it wasn't registered before running these tests.
44
	 *
45
	 * @after
46
	 */
47
	public function tear_down() {
48
		if ( ! $this->was_registered ) {
49
			unregister_block_type( 'jetpack/story' );
50
		}
51
		remove_filter( 'get_post_metadata', array( $this, 'get_attachment_metadata' ) );
52
	}
53
54
	/**
55
	 * Mock function to retrieve metadata about some post attachement
56
	 *
57
	 * @param mixed $metadata   Current metadata value.
58
	 * @param int   $object_id  ID of the object.
59
	 */
60
	public function get_metadata( $metadata, $object_id ) {
61
		// Attachment with id 14 represents the videopress media
62
		// in `extensions/blocks/story/test/fixtures/jetpack__story__story-with-videopress.html`.
63
		if ( 14 === $object_id ) {
64
			return array(
65
				array(
66
					'width'      => 320,
67
					'height'     => 640,
68
					'original'   => array(
69
						'url' => 'https://videos.files.wordpress.com/xxyyzz/videopress.mp4',
70
					),
71
					'videopress' => array(
72
						'description' => 'This is the video description',
73
						'poster'      => 'http://localhost/wp-includes/images/videopress_poster.png',
74
					),
75
				),
76
			);
77
		}
78
		return $metadata;
79
	}
80
81
	/**
82
	 * Test that the block is registered, which means that it can be registered.
83
	 */
84
	public function test_block_can_be_registered() {
85
		$is_registered = \Automattic\Jetpack\Blocks::is_registered( 'jetpack/story' );
86
		$this->assertTrue( $is_registered );
87
	}
88
89
	/**
90
	 * This test iterates over the block's serialized fixtures, and tests that the generated
91
	 * markup matches a fixture for the server rendered markup for the block.
92
	 *
93
	 * If no server rendered fixture can be found, then one is created.
94
	 */
95
	public function test_server_side_rendering_based_on_serialized_fixtures() {
96
		// phpcs:disable WordPress.WP.AlternativeFunctions
97
		$fixtures_path = 'extensions/blocks/story/test/fixtures/';
98
		$file_pattern  = '*.serialized.html';
99
		$files         = glob( JETPACK__PLUGIN_DIR . $fixtures_path . $file_pattern );
100
101
		$fail_messages = array();
102
103
		foreach ( $files as $file ) {
104
			$block_markup = trim( file_get_contents( $file ) );
105
106
			$parsed_blocks   = parse_blocks( $block_markup );
107
			$rendered_output = render_block( $parsed_blocks[0] );
108
109
			$target_markup_filename = str_replace( '.serialized.html', '.server-rendered.html', $file );
110
111
			// Create a server rendered fixture if one does not exist.
112 View Code Duplication
			if ( ! file_exists( $target_markup_filename ) ) {
113
				file_put_contents( $target_markup_filename, $rendered_output );
114
				$fail_messages[] =
115
					sprintf(
116
						"No server rendered fixture could be found for the %s block's %s fixture\n" .
117
						"A fixture file has been created in: %s\n",
118
						'jetpack/story',
119
						basename( $file ),
120
						$fixtures_path . basename( $target_markup_filename )
121
					);
122
			}
123
124
			$server_rendered_fixture = file_get_contents( $target_markup_filename );
125
			$this->assertEquals(
126
				$rendered_output,
127
				trim( $server_rendered_fixture ),
128
				sprintf(
129
					'The results of render_block for %s called with serialized markup from %s do not match ' .
130
					"the server-rendered fixture: %s\n",
131
					'jetpack/story',
132
					basename( $file ),
133
					basename( $target_markup_filename )
134
				)
135
			);
136
		}
137
138
		// Fail the test if any fixtures were missing, and report that fixtures have been generated.
139
		if ( ! empty( $fail_messages ) ) {
140
			$this->fail(
141
				implode( "\n", $fail_messages ) .
142
				"\nTry running this test again. Be sure to commit generated fixture files with any code changes."
143
			);
144
		}
145
		// phpcs:enable WordPress.WP.AlternativeFunctions
146
	}
147
}
148