| 1 |
<?php |
| 2 |
|
| 3 |
namespace Extendify\Tests\Integration\QuickEdit\Schemas; |
| 4 |
|
| 5 |
use Extendify\QuickEdit\Schemas\NavigationLink; |
| 6 |
use WP_UnitTestCase; |
| 7 |
|
| 8 |
class NavigationLinkTest extends WP_UnitTestCase |
| 9 |
{ |
| 10 |
public function test_fields_returns_label_then_url() |
| 11 |
{ |
| 12 |
$fields = (new NavigationLink())->fields(); |
| 13 |
|
| 14 |
$this->assertCount(2, $fields); |
| 15 |
$this->assertSame('label', $fields[0]['key']); |
| 16 |
$this->assertSame('text', $fields[0]['control']); |
| 17 |
$this->assertSame('url', $fields[1]['key']); |
| 18 |
$this->assertSame('link', $fields[1]['control']); |
| 19 |
} |
| 20 |
|
| 21 |
public function test_apply_label_strips_tags_and_sets_attr() |
| 22 |
{ |
| 23 |
$block = $this->navLinkBlock(['label' => 'old']); |
| 24 |
|
| 25 |
$result = (new NavigationLink())->apply($block, 'label', ' <b>About</b> us '); |
| 26 |
|
| 27 |
$this->assertSame('About us', $result['attrs']['label']); |
| 28 |
} |
| 29 |
|
| 30 |
public function test_apply_label_with_non_string_becomes_empty() |
| 31 |
{ |
| 32 |
$block = $this->navLinkBlock(['label' => 'old']); |
| 33 |
|
| 34 |
$result = (new NavigationLink())->apply($block, 'label', null); |
| 35 |
|
| 36 |
$this->assertSame('', $result['attrs']['label']); |
| 37 |
} |
| 38 |
|
| 39 |
public function test_apply_url_sets_attr() |
| 40 |
{ |
| 41 |
$block = $this->navLinkBlock([]); |
| 42 |
|
| 43 |
$result = (new NavigationLink())->apply($block, 'url', 'https://example.test/about'); |
| 44 |
|
| 45 |
$this->assertSame('https://example.test/about', $result['attrs']['url']); |
| 46 |
} |
| 47 |
|
| 48 |
public function test_apply_url_strips_dangerous_schemes() |
| 49 |
{ |
| 50 |
foreach (['javascript:alert(1)', 'data:text/html,<script>x</script>', 'vbscript:msgbox(1)'] as $url) { |
| 51 |
$block = $this->navLinkBlock([]); |
| 52 |
// esc_url_raw rejects the scheme → empty → attr left unset. |
| 53 |
$result = (new NavigationLink())->apply($block, 'url', $url); |
| 54 |
|
| 55 |
$this->assertArrayNotHasKey('url', $result['attrs'], "url: {$url}"); |
| 56 |
} |
| 57 |
} |
| 58 |
|
| 59 |
public function test_apply_label_strips_script_tags() |
| 60 |
{ |
| 61 |
$block = $this->navLinkBlock([]); |
| 62 |
$result = (new NavigationLink())->apply($block, 'label', 'Home<script>alert(1)</script>'); |
| 63 |
|
| 64 |
$this->assertSame('Home', $result['attrs']['label']); |
| 65 |
} |
| 66 |
|
| 67 |
public function test_apply_url_empty_string_unsets_attr() |
| 68 |
{ |
| 69 |
$block = $this->navLinkBlock(['url' => 'https://example.test/old']); |
| 70 |
|
| 71 |
$result = (new NavigationLink())->apply($block, 'url', ''); |
| 72 |
|
| 73 |
$this->assertArrayNotHasKey('url', $result['attrs']); |
| 74 |
} |
| 75 |
|
| 76 |
public function test_apply_with_unknown_field_returns_block_unchanged() |
| 77 |
{ |
| 78 |
$block = $this->navLinkBlock(['label' => 'About']); |
| 79 |
|
| 80 |
$this->assertSame($block, (new NavigationLink())->apply($block, 'kind', 'post')); |
| 81 |
} |
| 82 |
|
| 83 |
private function navLinkBlock(array $attrs): array |
| 84 |
{ |
| 85 |
return [ |
| 86 |
'blockName' => 'core/navigation-link', |
| 87 |
'attrs' => $attrs, |
| 88 |
'innerBlocks' => [], |
| 89 |
'innerHTML' => '', |
| 90 |
'innerContent' => [], |
| 91 |
]; |
| 92 |
} |
| 93 |
} |
| 94 |
|