-
Notifications
You must be signed in to change notification settings - Fork 4.8k
Expand file tree
/
Copy pathclass-block-context-test.php
More file actions
134 lines (115 loc) · 3.22 KB
/
class-block-context-test.php
File metadata and controls
134 lines (115 loc) · 3.22 KB
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
<?php
/**
* Test block context.
*
* @package Gutenberg
*/
class Block_Context_Test extends WP_UnitTestCase {
/**
* Registered block names.
*
* @var string[]
*/
private $registered_block_names = array();
/**
* Sets up each test method.
*/
public function setUp() {
global $post;
parent::setUp();
$args = array(
'post_content' => 'example',
'post_excerpt' => '',
);
$post = $this->factory()->post->create_and_get( $args );
setup_postdata( $post );
}
/**
* Tear down each test method.
*/
public function tearDown() {
parent::tearDown();
while ( ! empty( $this->registered_block_names ) ) {
$block_name = array_pop( $this->registered_block_names );
unregister_block_type( $block_name );
}
}
/**
* Registers a block type.
*
* @param string|WP_Block_Type $name Block type name including namespace, or alternatively a
* complete WP_Block_Type instance. In case a WP_Block_Type
* is provided, the $args parameter will be ignored.
* @param array $args {
* Optional. Array of block type arguments. Any arguments may be defined, however the
* ones described below are supported by default. Default empty array.
*
* @type callable $render_callback Callback used to render blocks of this block type.
* }
*/
protected function register_block_type( $name, $args ) {
register_block_type( $name, $args );
$this->registered_block_names[] = $name;
}
/**
* Tests that a block which provides context makes that context available to
* its inner blocks.
*/
function test_provides_block_context() {
$provided_context = array();
$this->register_block_type(
'gutenberg/test-context-provider',
array(
'attributes' => array(
'contextWithAssigned' => array(
'type' => 'number',
),
'contextWithDefault' => array(
'type' => 'number',
'default' => 0,
),
'contextWithoutDefault' => array(
'type' => 'number',
),
'contextNotRequested' => array(
'type' => 'number',
),
),
'providesContext' => array(
'gutenberg/contextWithAssigned' => 'contextWithAssigned',
'gutenberg/contextWithDefault' => 'contextWithDefault',
'gutenberg/contextWithoutDefault' => 'contextWithoutDefault',
'gutenberg/contextNotRequested' => 'contextNotRequested',
),
)
);
$this->register_block_type(
'gutenberg/test-context-consumer',
array(
'context' => array(
'gutenberg/contextWithDefault',
'gutenberg/contextWithAssigned',
'gutenberg/contextWithoutDefault',
),
'render_callback' => function() use ( &$provided_context ) {
global $_experimental_block;
$provided_context[] = $_experimental_block->context;
return '';
},
)
);
$parsed_blocks = parse_blocks(
'<!-- wp:gutenberg/test-context-provider {"contextWithAssigned":10} -->' .
'<!-- wp:gutenberg/test-context-consumer /-->' .
'<!-- /wp:gutenberg/test-context-provider -->'
);
render_block( $parsed_blocks[0] );
$this->assertEquals(
array(
'gutenberg/contextWithDefault' => 0,
'gutenberg/contextWithAssigned' => 10,
),
$provided_context[0]
);
}
}