-
Notifications
You must be signed in to change notification settings - Fork 4k
/
class-wp-block-list-test.php
100 lines (82 loc) · 2.45 KB
/
class-wp-block-list-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
<?php
/**
* Test WP_Block_List class.
*
* @package Gutenberg
*/
/**
* This test file can be removed when plugin support requires WordPress 5.5.0+.
*
* @see https://core.trac.wordpress.org/ticket/49926
* @see https://core.trac.wordpress.org/changeset/48159
*/
class WP_Block_List_Test extends WP_UnitTestCase {
/**
* Fake block type registry.
*
* @var WP_Block_Type_Registry
*/
private $registry = null;
/**
* Set up each test method.
*/
public function setUp() {
parent::setUp();
$this->registry = new WP_Block_Type_Registry();
$this->registry->register( 'core/example', array() );
}
/**
* Tear down each test method.
*/
public function tearDown() {
parent::tearDown();
$this->registry = null;
}
function test_array_access() {
$parsed_blocks = parse_blocks( '<!-- wp:example /-->' );
$context = array();
$blocks = new WP_Block_List( $parsed_blocks, $context, $this->registry );
// Test "offsetExists".
$this->assertTrue( isset( $blocks[0] ) );
// Test "offsetGet".
$this->assertEquals( 'core/example', $blocks[0]->name );
// Test "offsetSet".
$parsed_blocks[0]['blockName'] = 'core/updated';
$blocks[0] = new WP_Block( $parsed_blocks[0], $context, $this->registry );
$this->assertEquals( 'core/updated', $blocks[0]->name );
// Test "offsetUnset".
unset( $blocks[0] );
$this->assertFalse( isset( $blocks[0] ) );
}
function test_iterable() {
$parsed_blocks = parse_blocks( '<!-- wp:example --><!-- wp:example /--><!-- /wp:example -->' );
$context = array();
$blocks = new WP_Block_List( $parsed_blocks, $context, $this->registry );
$assertions = 0;
foreach ( $blocks as $block ) {
$this->assertEquals( 'core/example', $block->name );
$assertions++;
foreach ( $block->inner_blocks as $inner_block ) {
$this->assertEquals( 'core/example', $inner_block->name );
$assertions++;
}
}
$blocks->rewind();
while ( $blocks->valid() ) {
$key = $blocks->key();
$block = $blocks->current();
$this->assertEquals( 0, $key );
$assertions++;
$this->assertEquals( 'core/example', $block->name );
$assertions++;
$blocks->next();
}
$this->assertEquals( 4, $assertions );
}
function test_countable() {
$parsed_blocks = parse_blocks( '<!-- wp:example /-->' );
$context = array();
$blocks = new WP_Block_List( $parsed_blocks, $context, $this->registry );
$this->assertEquals( 1, count( $blocks ) );
}
}