|
| 1 | +<?php |
| 2 | +/** |
| 3 | + * Paragraph block rendering tests. |
| 4 | + * |
| 5 | + * @package WordPress |
| 6 | + * @subpackage Blocks |
| 7 | + */ |
| 8 | + |
| 9 | +/** |
| 10 | + * Tests for the Paragraph block. |
| 11 | + * |
| 12 | + * @group blocks |
| 13 | + */ |
| 14 | +class Render_Block_Paragraph_Test extends WP_UnitTestCase { |
| 15 | + |
| 16 | + |
| 17 | + /** |
| 18 | + * @covers ::gutenberg_block_core_paragraph_render |
| 19 | + * @dataProvider add_css_class_test_examples |
| 20 | + */ |
| 21 | + public function test_block_core_paragraph_render_appends_css_class_to_a_vanilla_element( $input, $expected_result ) { |
| 22 | + $actual = gutenberg_block_core_paragraph_render( array(), $input ); |
| 23 | + $this->assertEquals( $expected_result, $actual ); |
| 24 | + } |
| 25 | + |
| 26 | + public function add_css_class_test_examples() { |
| 27 | + return array( |
| 28 | + 'should add a class name to a vanilla p element' => array( |
| 29 | + '<p>Hello World</p>', |
| 30 | + '<p class="wp-block-paragraph">Hello World</p>', |
| 31 | + ), |
| 32 | + 'should not add a class name to a div element' => array( |
| 33 | + '<div>Hello World</div>', |
| 34 | + '<div>Hello World</div>', |
| 35 | + ), |
| 36 | + 'should not add a class name to a span element' => array( |
| 37 | + '<span>Hello World</span>', |
| 38 | + '<span>Hello World</span>', |
| 39 | + ), |
| 40 | + 'should not add a class name to a heading element' => array( |
| 41 | + '<h2>Hello World</h2>', |
| 42 | + '<h2>Hello World</h2>', |
| 43 | + ), |
| 44 | + 'should add a class name even when the class attribute is already defined' => array( |
| 45 | + '<p class="is-align-right">Hello World</p>', |
| 46 | + '<p class="is-align-right wp-block-paragraph">Hello World</p>', |
| 47 | + ), |
| 48 | + 'should handle single quotes' => array( |
| 49 | + "<p class='is-align-right'>Hello World</p>", |
| 50 | + '<p class="is-align-right wp-block-paragraph">Hello World</p>', |
| 51 | + ), |
| 52 | + 'should handle single quotes with double quotes inside' => array( |
| 53 | + "<p class='\" is-align-right'>Hello World</p>", |
| 54 | + '<p class="" is-align-right wp-block-paragraph">Hello World</p>', |
| 55 | + ), |
| 56 | + 'should not add a class name even when it is already defined' => array( |
| 57 | + '<p class="is-align-right wp-block-paragraph">Hello World</p>', |
| 58 | + '<p class="is-align-right wp-block-paragraph">Hello World</p>', |
| 59 | + ), |
| 60 | + 'should add a class name even when there are other HTML attributes present' => array( |
| 61 | + '<p style="display: block">Hello World</p>', |
| 62 | + '<p class="wp-block-paragraph" style="display: block">Hello World</p>', |
| 63 | + ), |
| 64 | + 'should add a class name even when the class attribute is already defined and has many entries' => array( |
| 65 | + '<p class="is-align-right custom classes">Hello World</p>', |
| 66 | + '<p class="is-align-right custom classes wp-block-paragraph">Hello World</p>', |
| 67 | + ), |
| 68 | + 'should not add a class name to a nested p' => array( |
| 69 | + '<p class="is-align-right custom classes"><p>Hello World</p></p>', |
| 70 | + '<p class="is-align-right custom classes wp-block-paragraph"><p>Hello World</p></p>', |
| 71 | + ), |
| 72 | + 'should not add a class name to a nested p when the parent has another attribute' => array( |
| 73 | + '<p style="display: block" class="is-align-right"><p>Hello World</p></p>', |
| 74 | + '<p style="display: block" class="is-align-right wp-block-paragraph"><p>Hello World</p></p>', |
| 75 | + ), |
| 76 | + 'should add a class name even when the class attribute is surrounded by other attributes' => array( |
| 77 | + '<p style="display: block" class="is-align-right" data-class="corner case!"><p>Hello World</p></p>', |
| 78 | + '<p style="display: block" class="is-align-right wp-block-paragraph" data-class="corner case!"><p>Hello World</p></p>', |
| 79 | + ), |
| 80 | + 'should add a class name without getting confused when there is a tricky data-class attribute present' => array( |
| 81 | + '<p data-class="corner case!" style="display: block" class="is-align-right"><p>Hello World</p></p>', |
| 82 | + '<p data-class="corner case!" style="display: block" class="is-align-right wp-block-paragraph"><p>Hello World</p></p>', |
| 83 | + ), |
| 84 | + ); |
| 85 | + } |
| 86 | +} |
0 commit comments