PluginProbe
Extendify / 3.1.0
Extendify v3.1.0
3.2.1 3.2.0 3.1.6 3.1.5 3.1.4 3.1.3 3.1.2 3.1.1 3.1.0 3.0.6 3.0.5 3.0.4 trunk 0.1.0 0.10.0 0.10.1 0.10.2 0.11.0 0.11.1 0.2.0 0.3.0 0.3.1 0.4.0 0.5.0 0.6.0 All 127 releases
extendify / tests / Integration / QuickEdit / Schemas / HeadingTest.php

HeadingTest.php in Extendify 3.1.0, at tests/Integration/QuickEdit/Schemas/HeadingTest.php

109 lines 3.9 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2
3 namespace Extendify\Tests\Integration\QuickEdit\Schemas;
4
5 use Extendify\QuickEdit\Schemas\Heading;
6 use WP_UnitTestCase;
7
8 class HeadingTest extends WP_UnitTestCase
9 {
10 public function test_fields_returns_content_then_alignment_no_level()
11 {
12 $fields = (new Heading())->fields();
13
14 $this->assertCount(2, $fields, 'level is intentionally not exposed (SEO/a11y)');
15 $this->assertSame('content', $fields[0]['key']);
16 $this->assertSame('align', $fields[1]['key']);
17 }
18
19 public function test_apply_content_preserves_existing_heading_level()
20 {
21 $block = $this->headingBlock('<h3 class="x">old</h3>', ['level' => 3]);
22
23 $result = (new Heading())->apply($block, 'content', 'new');
24
25 $this->assertSame('<h3 class="x">new</h3>', $result['innerHTML']);
26 }
27
28 public function test_apply_content_synthesizes_h_tag_from_attrs_level_when_no_existing_tag()
29 {
30 $block = $this->headingBlock('', ['level' => 4]);
31
32 $result = (new Heading())->apply($block, 'content', 'hi');
33
34 $this->assertSame('<h4>hi</h4>', $result['innerHTML']);
35 }
36
37 public function test_apply_content_clamps_synthesized_level_to_1_6()
38 {
39 $block = $this->headingBlock('', ['level' => 99]);
40 $this->assertSame('<h6>x</h6>', (new Heading())->apply($block, 'content', 'x')['innerHTML']);
41
42 $block = $this->headingBlock('', ['level' => 0]);
43 $this->assertSame('<h1>y</h1>', (new Heading())->apply($block, 'content', 'y')['innerHTML']);
44
45 $block = $this->headingBlock('', []);
46 $this->assertSame('<h2>z</h2>', (new Heading())->apply($block, 'content', 'z')['innerHTML'], 'defaults to h2');
47 }
48
49 public function test_apply_content_treats_regex_backreferences_in_user_text_as_literal()
50 {
51 $inputs = ['Try $1 for size', 'Use \\1 here', '$0 free', 'Mixed $1 and \\2'];
52
53 foreach ($inputs as $input) {
54 $block = $this->headingBlock('<h2>old</h2>', ['level' => 2]);
55 $result = (new Heading())->apply($block, 'content', $input);
56
57 $this->assertSame('<h2>' . $input . '</h2>', $result['innerHTML'], "input: {$input}");
58 }
59 }
60
61 public function test_apply_content_runs_through_wp_kses_post()
62 {
63 $block = $this->headingBlock('<h2>old</h2>', ['level' => 2]);
64 $result = (new Heading())->apply($block, 'content', 'hi<script>alert(1)</script>');
65
66 $this->assertStringContainsString('hi', $result['innerHTML']);
67 $this->assertStringNotContainsString('<script', $result['innerHTML']);
68 }
69
70 public function test_apply_align_uses_textAlign_attr_not_align()
71 {
72 $block = $this->headingBlock('<h2>x</h2>');
73
74 $result = (new Heading())->apply($block, 'align', 'center');
75
76 $this->assertSame('center', $result['attrs']['textAlign']);
77 $this->assertArrayNotHasKey('align', $result['attrs']);
78 $this->assertStringContainsString('has-text-align-center', $result['innerHTML']);
79 }
80
81 public function test_apply_align_with_invalid_value_clears_textAlign()
82 {
83 $block = $this->headingBlock('<h2 class="has-text-align-left">x</h2>', ['textAlign' => 'left']);
84
85 $result = (new Heading())->apply($block, 'align', 'diagonal');
86
87 $this->assertArrayNotHasKey('textAlign', $result['attrs']);
88 $this->assertStringNotContainsString('has-text-align-', $result['innerHTML']);
89 }
90
91 public function test_apply_with_unknown_field_returns_block_unchanged()
92 {
93 $block = $this->headingBlock('<h2>x</h2>');
94
95 $this->assertSame($block, (new Heading())->apply($block, 'level', 4));
96 }
97
98 private function headingBlock(string $innerHTML, array $attrs = []): array
99 {
100 return [
101 'blockName' => 'core/heading',
102 'attrs' => $attrs,
103 'innerBlocks' => [],
104 'innerHTML' => $innerHTML,
105 'innerContent' => [$innerHTML],
106 ];
107 }
108 }
109