|
| 1 | +<?php |
| 2 | + |
| 3 | +/** |
| 4 | + * Unit tests covering WP_Block_Processor functionality. |
| 5 | + * |
| 6 | + * @package WordPress |
| 7 | + * @subpackage HTML-API |
| 8 | + * |
| 9 | + * @since {WP_VERSION} |
| 10 | + * |
| 11 | + * @group block-scanner |
| 12 | + * |
| 13 | + * @coversDefaultClass WP_Block_Processor |
| 14 | + */ |
| 15 | +class Tests_Blocks_BlockScanner_WP_Block_Processor extends WP_UnitTestCase { |
| 16 | + public function test_creates_class_instance() { |
| 17 | + $processor = WP_Block_Processor::create( '' ); |
| 18 | + |
| 19 | + $this->assertInstanceOf( 'WP_Block_Processor', $processor ); |
| 20 | + } |
| 21 | + |
| 22 | + public function test_get_breadcrumbs() { |
| 23 | + $processor = WP_Block_Processor::create( '<!-- wp:top --><!-- wp:inside /--><!-- /wp:top -->' ); |
| 24 | + |
| 25 | + $this->assertTrue( |
| 26 | + $processor->next_delimiter(), |
| 27 | + 'Should have found the opening "top" delimiter but found nothing.' |
| 28 | + ); |
| 29 | + |
| 30 | + $this->assertSame( |
| 31 | + array( 'core/top' ), |
| 32 | + $processor->get_breadcrumbs(), |
| 33 | + 'Should have found only the single opening delimiter.' |
| 34 | + ); |
| 35 | + |
| 36 | + $processor->next_delimiter(); |
| 37 | + $this->assertSame( |
| 38 | + array( 'core/top', 'core/inside' ), |
| 39 | + $processor->get_breadcrumbs(), |
| 40 | + 'Should have detected the nesting structure of the blocks.' |
| 41 | + ); |
| 42 | + } |
| 43 | + |
| 44 | + public function test_get_depth() { |
| 45 | + // Create a deeply-nested stack of blocks. |
| 46 | + $html = ''; |
| 47 | + $max_depth = 10; |
| 48 | + |
| 49 | + for ( $i = 0; $i < $max_depth; $i++ ) { |
| 50 | + $html .= "<!-- wp:ladder {\"level\":{$i}} -->\n"; |
| 51 | + } |
| 52 | + |
| 53 | + for ( $i = 0; $i < $max_depth; $i++ ) { |
| 54 | + $html .= "<!-- /wp:ladder -->\n"; |
| 55 | + } |
| 56 | + |
| 57 | + $processor = WP_Block_Processor::create( $html ); |
| 58 | + $n = new NumberFormatter( 'en-US', NumberFormatter::ORDINAL ); |
| 59 | + |
| 60 | + for ( $i = 0; $i < $max_depth; $i++ ) { |
| 61 | + $this->assertTrue( |
| 62 | + $processor->next_delimiter(), |
| 63 | + "Should have found {$n->format( $i + 1 )} opening delimiter: check test setup." |
| 64 | + ); |
| 65 | + |
| 66 | + $this->assertSame( |
| 67 | + $i + 1, |
| 68 | + $processor->get_depth(), |
| 69 | + "Should have identified the proper depth of the {$n->format( $i + 1 )} opening delimiter." |
| 70 | + ); |
| 71 | + } |
| 72 | + |
| 73 | + for ( $i = 0; $i < $max_depth; $i++ ) { |
| 74 | + $this->assertTrue( |
| 75 | + $processor->next_delimiter(), |
| 76 | + "Should have found {$n->format( $i + 1 )} closing delimiter: check test setup." |
| 77 | + ); |
| 78 | + |
| 79 | + $this->assertSame( |
| 80 | + $max_depth - $i - 1, |
| 81 | + $processor->get_depth(), |
| 82 | + "Should have identified the proper depth of the {$n->format( $i + 1 )} closing delimiter." |
| 83 | + ); |
| 84 | + } |
| 85 | + } |
| 86 | + |
| 87 | + public function test_builds_block() { |
| 88 | + $cover_block = [ 'blockName' => 'core/cover', 'attrs' => [], 'innerHTML' => '<img>', 'innerContent' => [ '<img>' ] ]; |
| 89 | + $heading_block = [ 'blockName' => 'core/heading', 'attrs' => [ 'level' => 2 ], 'innerHTML' => '<h2>Testing works!</h2>', 'innerContent' => [ '<h2>Testing works!</h2>' ] ]; |
| 90 | + $paragraph_block = [ 'blockName' => 'core/paragraph', 'attrs' => [], 'innerHTML' => '<p>Who knew?</p>', 'innerContent' => [ '<p>Who knew?</p>' ] ]; |
| 91 | + $group_block = [ 'blockName' => 'core/group', 'attrs' => [], 'innerHTML' => '', 'innerBlocks' => [ $heading_block, $paragraph_block ], 'innerContent' => [ null, null ] ]; |
| 92 | + $blocks = [ $cover_block, $group_block ]; |
| 93 | + $html = serialize_blocks( $blocks ); |
| 94 | + |
| 95 | + $processor = WP_Block_Processor::create( $html ); |
| 96 | + |
| 97 | + $extracted = array(); |
| 98 | + while ( $processor->next_delimiter() ) { |
| 99 | + $extracted[] = $processor->extract_block(); |
| 100 | + } |
| 101 | + |
| 102 | + $this->assertSame( |
| 103 | + $blocks, |
| 104 | + $extracted, |
| 105 | + 'Should have extracted a block matching the input group block.' |
| 106 | + ); |
| 107 | + } |
| 108 | +} |
0 commit comments