Completed
Push — update/add-csstidy-config-sett... ( 5d5261...a816cd )
by
unknown
548:09 queued 537:58
created

Repeat_Visitor_Block_Test   A

Complexity

Total Complexity 8

Size/Duplication

Total Lines 100
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 2

Importance

Changes 0
Metric Value
dl 0
loc 100
rs 10
c 0
b 0
f 0
wmc 8
lcom 1
cbo 2

5 Methods

Rating   Name   Duplication   Size   Complexity  
A setUp() 0 9 2
A tearDown() 0 11 3
A test_block_can_be_registered() 0 4 1
A test_server_side_rendering_based_on_serialized_fixtures_below_threshold() 0 9 1
A test_server_side_rendering_based_on_serialized_fixtures_above_threshold() 0 9 1
1
<?php // phpcs:ignore WordPress.Files.FileName.InvalidClassFileName
2
/**
3
 * Repeat Visitor 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/repeat-visitor/repeat-visitor.php';
12
13
/**
14
 * Include a test case that we can inherit from to make it easier to test against existing fixtures.
15
 */
16
require_once __DIR__ . '/class-block-fixture-testcase.php';
17
18
/**
19
 * Repeat Visitor Block tests.
20
 *
21
 * These tests primarily check that server rendered markup is not changing unexpectedly
22
 * when serialized fixtures are updated via the block's JS-based save behaviour.
23
 *
24
 * The goal is to catch when changes to serialized markup affects server rendering of the block.
25
 *
26
 * Because the Repeat Visitor block can render in two different states depending on the number
27
 * of times a visitor has visited the page, in this set of tests, we generate two different kinds
28
 * of server-rendered fixtures: a set where the visitor is below the visit threshold, and a set
29
 * where the visitor is above the visit threshold.
30
 */
31
class Repeat_Visitor_Block_Test extends \Jetpack_Block_Fixture_TestCase {
32
	/**
33
	 * A variable to track whether or not the block was already registered before the test was run.
34
	 *
35
	 * @access private
36
	 *
37
	 * @var boolean
38
	 */
39
	private $was_registered = false;
40
41
	/**
42
	 * A variable to track the current cookie value of repeat visits.
43
	 *
44
	 * @access private
45
	 *
46
	 * @var number
47
	 */
48
	private $original_visit_counter;
49
50
	/**
51
	 * The name of the block under test.
52
	 *
53
	 * @access private
54
	 *
55
	 * @var string
56
	 */
57
	const BLOCK_NAME = 'jetpack/repeat-visitor';
58
59
	/**
60
	 * Setup and ensure the block is registered before running the tests.
61
	 *
62
	 * @before
63
	 */
64
	public function setUp() {
65
		parent::setUp();
66
		$this->was_registered = \Automattic\Jetpack\Blocks::is_registered( self::BLOCK_NAME );
67
		\Automattic\Jetpack\Extensions\Repeat_Visitor\register_block();
68
69
		if ( isset( $_COOKIE['jp-visit-counter'] ) ) {
70
			$this->original_visit_counter = $_COOKIE['jp-visit-counter'];
71
		}
72
	}
73
74
	/**
75
	 * Teardown and unregister the block if it wasn't registered before running these tests.
76
	 *
77
	 * @after
78
	 */
79
	public function tearDown() {
80
		if ( ! $this->was_registered ) {
81
			unregister_block_type( self::BLOCK_NAME );
82
		}
83
84
		if ( isset( $this->original_visit_counter ) ) {
85
			$_COOKIE['jp-visit-counter'] = $this->original_visit_counter;
86
		}
87
88
		parent::tearDown();
89
	}
90
91
	/**
92
	 * Test that the block is registered, which means that it can be registered.
93
	 */
94
	public function test_block_can_be_registered() {
95
		$is_registered = \Automattic\Jetpack\Blocks::is_registered( self::BLOCK_NAME );
96
		$this->assertTrue( $is_registered );
97
	}
98
99
	/**
100
	 * Set the visit counter to zero, and test the serialized fixtures as though the visitor
101
	 * is below the Repeat Visitor block's threshold.
102
	 *
103
	 * This will generate server-rendered fixtures if they do not exist.
104
	 */
105
	public function test_server_side_rendering_based_on_serialized_fixtures_below_threshold() {
106
		$_COOKIE['jp-visit-counter'] = 0;
107
108
		$this->generate_server_side_rendering_based_on_serialized_fixtures(
109
			self::BLOCK_NAME,
110
			'repeat-visitor',
111
			'.server-rendered-below-threshold.html'
112
		);
113
	}
114
115
	/**
116
	 * Set the visit counter to zero, and test the serialized fixtures as though the visitor
117
	 * is above the Repeat Visitor block's threshold.
118
	 *
119
	 * This will generate server-rendered fixtures if they do not exist.
120
	 */
121
	public function test_server_side_rendering_based_on_serialized_fixtures_above_threshold() {
122
		$_COOKIE['jp-visit-counter'] = 999;
123
124
		$this->generate_server_side_rendering_based_on_serialized_fixtures(
125
			self::BLOCK_NAME,
126
			'repeat-visitor',
127
			'.server-rendered-above-threshold.html'
128
		);
129
	}
130
}
131