| 1 |
<?php |
| 2 |
|
| 3 |
namespace Extendify\Tests\Integration\QuickEdit\Schemas; |
| 4 |
|
| 5 |
use Extendify\QuickEdit\Schemas\Paragraph; |
| 6 |
use WP_UnitTestCase; |
| 7 |
|
| 8 |
class ParagraphTest extends WP_UnitTestCase |
| 9 |
{ |
| 10 |
public function test_fields_returns_text_then_alignment() |
| 11 |
{ |
| 12 |
$fields = (new Paragraph())->fields(); |
| 13 |
|
| 14 |
$this->assertCount(2, $fields); |
| 15 |
|
| 16 |
$this->assertSame('content', $fields[0]['key']); |
| 17 |
$this->assertSame('text', $fields[0]['control']); |
| 18 |
|
| 19 |
$this->assertSame('align', $fields[1]['key']); |
| 20 |
$this->assertSame('alignment', $fields[1]['control']); |
| 21 |
|
| 22 |
$alignValues = array_column($fields[1]['options'], 'value'); |
| 23 |
$this->assertSame(['left', 'center', 'right'], $alignValues); |
| 24 |
} |
| 25 |
|
| 26 |
public function test_apply_content_rewrites_inner_p_preserving_p_attributes() |
| 27 |
{ |
| 28 |
$block = $this->paragraphBlock('<p class="has-large-font-size">old</p>'); |
| 29 |
|
| 30 |
$result = (new Paragraph())->apply($block, 'content', 'new'); |
| 31 |
|
| 32 |
$this->assertSame('<p class="has-large-font-size">new</p>', $result['innerHTML']); |
| 33 |
$this->assertSame([$result['innerHTML']], $result['innerContent']); |
| 34 |
} |
| 35 |
|
| 36 |
public function test_apply_content_synthesizes_p_when_none_exists() |
| 37 |
{ |
| 38 |
$block = $this->paragraphBlock(''); |
| 39 |
|
| 40 |
$result = (new Paragraph())->apply($block, 'content', 'hello'); |
| 41 |
|
| 42 |
$this->assertSame('<p>hello</p>', $result['innerHTML']); |
| 43 |
$this->assertSame(['<p>hello</p>'], $result['innerContent']); |
| 44 |
} |
| 45 |
|
| 46 |
public function test_apply_content_runs_through_wp_kses_post() |
| 47 |
{ |
| 48 |
$block = $this->paragraphBlock('<p>old</p>'); |
| 49 |
|
| 50 |
$result = (new Paragraph())->apply($block, 'content', 'safe <script>bad()</script>'); |
| 51 |
|
| 52 |
// wp_kses_post strips <script>; bare text + tag name leak through but the call is unsafe-script-free. |
| 53 |
$this->assertStringNotContainsString('<script>', $result['innerHTML']); |
| 54 |
} |
| 55 |
|
| 56 |
public function test_apply_content_treats_regex_backreferences_in_user_text_as_literal() |
| 57 |
{ |
| 58 |
$inputs = ['Try $1 for size', 'Use \\1 here', '$0 free', 'Mixed $1 and \\2']; |
| 59 |
|
| 60 |
foreach ($inputs as $input) { |
| 61 |
$block = $this->paragraphBlock('<p>old</p>'); |
| 62 |
$result = (new Paragraph())->apply($block, 'content', $input); |
| 63 |
|
| 64 |
$this->assertSame('<p>' . $input . '</p>', $result['innerHTML'], "input: {$input}"); |
| 65 |
} |
| 66 |
} |
| 67 |
|
| 68 |
public function test_apply_align_sets_attr_and_adds_alignment_class() |
| 69 |
{ |
| 70 |
$block = $this->paragraphBlock('<p>x</p>'); |
| 71 |
|
| 72 |
$result = (new Paragraph())->apply($block, 'align', 'center'); |
| 73 |
|
| 74 |
$this->assertSame('center', $result['attrs']['align']); |
| 75 |
$this->assertStringContainsString('has-text-align-center', $result['innerHTML']); |
| 76 |
} |
| 77 |
|
| 78 |
public function test_apply_align_with_invalid_value_removes_alignment() |
| 79 |
{ |
| 80 |
$block = $this->paragraphBlock('<p class="has-text-align-right">x</p>'); |
| 81 |
$block['attrs']['align'] = 'right'; |
| 82 |
|
| 83 |
$result = (new Paragraph())->apply($block, 'align', 'sideways'); |
| 84 |
|
| 85 |
$this->assertArrayNotHasKey('align', $result['attrs']); |
| 86 |
$this->assertStringNotContainsString('has-text-align-', $result['innerHTML']); |
| 87 |
} |
| 88 |
|
| 89 |
public function test_apply_align_replaces_existing_alignment_class() |
| 90 |
{ |
| 91 |
$block = $this->paragraphBlock('<p class="has-text-align-left other">x</p>'); |
| 92 |
|
| 93 |
$result = (new Paragraph())->apply($block, 'align', 'right'); |
| 94 |
|
| 95 |
$this->assertStringContainsString('has-text-align-right', $result['innerHTML']); |
| 96 |
$this->assertStringNotContainsString('has-text-align-left', $result['innerHTML']); |
| 97 |
$this->assertStringContainsString('other', $result['innerHTML']); |
| 98 |
} |
| 99 |
|
| 100 |
public function test_apply_with_unknown_field_returns_block_unchanged() |
| 101 |
{ |
| 102 |
$block = $this->paragraphBlock('<p>x</p>'); |
| 103 |
|
| 104 |
$this->assertSame($block, (new Paragraph())->apply($block, 'bogus', 'y')); |
| 105 |
} |
| 106 |
|
| 107 |
private function paragraphBlock(string $innerHTML): array |
| 108 |
{ |
| 109 |
return [ |
| 110 |
'blockName' => 'core/paragraph', |
| 111 |
'attrs' => [], |
| 112 |
'innerBlocks' => [], |
| 113 |
'innerHTML' => $innerHTML, |
| 114 |
'innerContent' => [$innerHTML], |
| 115 |
]; |
| 116 |
} |
| 117 |
} |
| 118 |
|