-
Notifications
You must be signed in to change notification settings - Fork 4k
/
class-block-type-registry-test.php
153 lines (131 loc) · 3.69 KB
/
class-block-type-registry-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
<?php
/**
* WP_Block_Type_Registry Tests
*
* @package Gutenberg
*/
/**
* Tests for WP_Block_Type_Registry
*/
class Block_Type_Registry_Test extends WP_UnitTestCase {
/**
* Dummy block type registry.
*
* @var WP_Block_Type_Registry
*/
private $registry = null;
function setUp() {
parent::setUp();
$this->registry = new WP_Block_Type_Registry();
}
function tearDown() {
parent::tearDown();
$this->registry = null;
}
/**
* Should reject numbers
*
* @expectedIncorrectUsage WP_Block_Type_Registry::register
*/
function test_invalid_non_string_names() {
$result = $this->registry->register( 1, array() );
$this->assertFalse( $result );
}
/**
* Should reject blocks without a namespace
*
* @expectedIncorrectUsage WP_Block_Type_Registry::register
*/
function test_invalid_names_without_namespace() {
$result = $this->registry->register( 'paragraph', array() );
$this->assertFalse( $result );
}
/**
* Should reject blocks with invalid characters
*
* @expectedIncorrectUsage WP_Block_Type_Registry::register
*/
function test_invalid_characters() {
$result = $this->registry->register( 'still/_doing_it_wrong', array() );
$this->assertFalse( $result );
}
/**
* Should reject blocks with uppercase characters
*
* @expectedIncorrectUsage WP_Block_Type_Registry::register
*/
function test_uppercase_characters() {
$result = $this->registry->register( 'Core/Paragraph', array() );
$this->assertFalse( $result );
}
/**
* Should accept valid block names
*/
function test_register_block_type() {
$name = 'core/paragraph';
$settings = array(
'icon' => 'editor-paragraph',
);
$block_type = $this->registry->register( $name, $settings );
$this->assertEquals( $name, $block_type->name );
$this->assertEquals( $settings['icon'], $block_type->icon );
$this->assertEquals( $block_type, $this->registry->get_registered( $name ) );
}
/**
* Should fail to re-register the same block
*
* @expectedIncorrectUsage WP_Block_Type_Registry::register
*/
function test_register_block_type_twice() {
$name = 'core/paragraph';
$settings = array(
'icon' => 'editor-paragraph',
);
$result = $this->registry->register( $name, $settings );
$this->assertNotFalse( $result );
$result = $this->registry->register( $name, $settings );
$this->assertFalse( $result );
}
/**
* Should accept a WP_Block_Type instance
*/
function test_register_block_type_instance() {
$block_type = new WP_Dummy_Block_Type( 'core/dummy' );
$result = $this->registry->register( $block_type );
$this->assertSame( $block_type, $result );
}
/**
* Unregistering should fail if a block is not registered
*
* @expectedIncorrectUsage WP_Block_Type_Registry::unregister
*/
function test_unregister_not_registered_block() {
$result = $this->registry->unregister( 'core/unregistered' );
$this->assertFalse( $result );
}
/**
* Should unregister existing blocks
*/
function test_unregister_block_type() {
$name = 'core/paragraph';
$settings = array(
'icon' => 'editor-paragraph',
);
$this->registry->register( $name, $settings );
$block_type = $this->registry->unregister( $name );
$this->assertEquals( $name, $block_type->name );
$this->assertEquals( $settings['icon'], $block_type->icon );
$this->assertFalse( $this->registry->is_registered( $name ) );
}
function test_get_all_registered() {
$names = array( 'core/paragraph', 'core/image', 'core/blockquote' );
$settings = array(
'icon' => 'random',
);
foreach ( $names as $name ) {
$this->registry->register( $name, $settings );
}
$registered = $this->registry->get_all_registered();
$this->assertEqualSets( $names, array_keys( $registered ) );
}
}