-
Notifications
You must be signed in to change notification settings - Fork 4.3k
/
block-navigation-block-hooks-test.php
160 lines (136 loc) · 4.34 KB
/
block-navigation-block-hooks-test.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
<?php
/**
* Navigation block block hooks tests.
*
* @package WordPress
* @subpackage Blocks
*/
/**
* Tests for the Navigation block.
*
* @group blocks
*/
class Block_Navigation_Block_Hooks_Test extends WP_UnitTestCase {
/**
* Original markup.
*
* @var string
*/
protected static $original_markup;
/**
* Post object.
*
* @var object
*/
protected static $navigation_post;
/**
* Setup method.
*/
public static function wpSetUpBeforeClass() {
//self::$original_markup = '<!-- wp:navigation-link {"label":"News & About","type":"page","id":2,"url":"http://localhost:8888/?page_id=2","kind":"post-type"} /-->';
self::$navigation_post = self::factory()->post->create_and_get(
array(
'post_type' => 'wp_navigation',
'post_title' => 'Navigation Menu',
'post_content' => 'Original content',
)
);
}
/**
* Tear down each test method.
*/
public function tear_down() {
$registry = WP_Block_Type_Registry::get_instance();
if ( $registry->is_registered( 'tests/my-block' ) ) {
$registry->unregister( 'tests/my-block' );
}
parent::tear_down();
}
/**
* @covers ::gutenberg_block_core_navigation_update_ignore_hooked_blocks_meta
*/
public function test_block_core_navigation_update_ignore_hooked_blocks_meta_preserves_entities() {
if ( ! function_exists( 'set_ignored_hooked_blocks_metadata' ) ) {
$this->markTestSkipped( 'Test skipped on WordPress versions that do not included required Block Hooks functionality.' );
}
register_block_type(
'tests/my-block',
array(
'block_hooks' => array(
'core/navigation' => 'last_child',
),
)
);
$original_markup = '<!-- wp:navigation-link {"label":"News & About","type":"page","id":2,"url":"http://localhost:8888/?page_id=2","kind":"post-type"} /-->';
$post = new stdClass();
$post->ID = self::$navigation_post->ID;
$post->post_content = $original_markup;
$post = gutenberg_block_core_navigation_update_ignore_hooked_blocks_meta( $post );
// We expect the '&' character to be replaced with its unicode representation.
$expected_markup = str_replace( '&', '\u0026', $original_markup );
$this->assertSame(
$expected_markup,
$post->post_content,
'Post content did not match expected markup with entities escaped.'
);
$this->assertSame(
array( 'tests/my-block' ),
json_decode( get_post_meta( self::$navigation_post->ID, '_wp_ignored_hooked_blocks', true ), true ),
'Block was not added to ignored hooked blocks metadata.'
);
}
/**
* @covers ::gutenberg_block_core_navigation_update_ignore_hooked_blocks_meta
*/
public function test_block_core_navigation_dont_modify_no_post_id() {
if ( ! function_exists( 'set_ignored_hooked_blocks_metadata' ) ) {
$this->markTestSkipped( 'Test skipped on WordPress versions that do not included required Block Hooks functionality.' );
}
register_block_type(
'tests/my-block',
array(
'block_hooks' => array(
'core/navigation' => 'last_child',
),
)
);
$original_markup = '<!-- wp:navigation-link {"label":"News","type":"page","id":2,"url":"http://localhost:8888/?page_id=2","kind":"post-type"} /-->';
$post = new stdClass();
$post->post_content = $original_markup;
$post = gutenberg_block_core_navigation_update_ignore_hooked_blocks_meta( $post );
$this->assertSame(
$original_markup,
$post->post_content,
'Post content did not match the original markup.'
);
}
/**
* @covers ::gutenberg_block_core_navigation_update_ignore_hooked_blocks_meta
*/
public function test_block_core_navigation_retains_content_if_not_set() {
if ( ! function_exists( 'set_ignored_hooked_blocks_metadata' ) ) {
$this->markTestSkipped( 'Test skipped on WordPress versions that do not included required Block Hooks functionality.' );
}
register_block_type(
'tests/my-block',
array(
'block_hooks' => array(
'core/navigation' => 'last_child',
),
)
);
$post = new stdClass();
$post->ID = self::$navigation_post->ID;
$post->post_title = 'Navigation Menu with changes';
$post = gutenberg_block_core_navigation_update_ignore_hooked_blocks_meta( $post );
$this->assertSame(
'Navigation Menu with changes',
$post->post_title,
'Post title was changed.'
);
$this->assertFalse(
isset( $post->post_content ),
'Post content should not be set.'
);
}
}