|
| 1 | +<?php |
| 2 | +/** |
| 3 | + * Stored block-style sanitizers. |
| 4 | + * |
| 5 | + * @package Stackable |
| 6 | + */ |
| 7 | + |
| 8 | +use Brain\Monkey\Functions; |
| 9 | + |
| 10 | +class BlockDefaultsSanitizeTest extends Stackable_TestCase { |
| 11 | + |
| 12 | + protected function set_up() { |
| 13 | + parent::set_up(); |
| 14 | + Functions\when( 'wp_check_invalid_utf8' )->returnArg( 1 ); |
| 15 | + Functions\when( 'parse_blocks' )->justReturn( array() ); |
| 16 | + Functions\when( 'serialize_blocks' )->justReturn( '' ); |
| 17 | + $this->require_plugin_file( 'src/deprecated/block-defaults/custom-block-styles.php' ); |
| 18 | + } |
| 19 | + |
| 20 | + private function styles() { |
| 21 | + return new Stackable_Custom_Block_Styles(); |
| 22 | + } |
| 23 | + |
| 24 | + public function test_sanitize_block_name_rejects_core_blocks() { |
| 25 | + $styles = $this->styles(); |
| 26 | + $this->assertSame( '', $styles->sanitize_block_name( 'core/paragraph' ) ); |
| 27 | + $this->assertSame( 'stackable/heading', $styles->sanitize_block_name( 'stackable/heading' ) ); |
| 28 | + } |
| 29 | + |
| 30 | + public function test_sanitize_style_slug_uses_sanitize_title() { |
| 31 | + $this->assertSame( 'my-style', $this->styles()->sanitize_style_slug( 'My Style' ) ); |
| 32 | + } |
| 33 | + |
| 34 | + public function test_sanitize_array_setting_rejects_non_array() { |
| 35 | + $this->assertSame( array(), $this->styles()->sanitize_array_setting( 'nope' ) ); |
| 36 | + } |
| 37 | + |
| 38 | + public function test_sanitize_stored_block_styles_drops_invalid_blocks() { |
| 39 | + $styles = $this->styles(); |
| 40 | + $stored = array( |
| 41 | + array( |
| 42 | + 'block' => 'core/paragraph', |
| 43 | + 'styles' => array( |
| 44 | + array( |
| 45 | + 'slug' => 'plain', |
| 46 | + 'name' => 'Plain', |
| 47 | + 'data' => '{"attributes":{"text":"<script>x</script>"},"innerBlocks":[]}', |
| 48 | + 'save' => '', |
| 49 | + ), |
| 50 | + ), |
| 51 | + ), |
| 52 | + array( |
| 53 | + 'block' => 'stackable/heading', |
| 54 | + 'styles' => array( |
| 55 | + array( |
| 56 | + 'slug' => 'hero', |
| 57 | + 'name' => 'Hero', |
| 58 | + 'data' => '{"attributes":{"text":"Hello <script>x</script>"},"innerBlocks":[]}', |
| 59 | + 'save' => '', |
| 60 | + ), |
| 61 | + ), |
| 62 | + ), |
| 63 | + ); |
| 64 | + $out = $styles->sanitize_stored_block_styles( $stored ); |
| 65 | + $this->assertCount( 1, $out ); |
| 66 | + $this->assertSame( 'stackable/heading', $out[0]->block ); |
| 67 | + $this->assertSame( 'hero', $out[0]->styles[0]->slug ); |
| 68 | + $this->assertStringNotContainsString( '<script>', $out[0]->styles[0]->data ); |
| 69 | + } |
| 70 | +} |
0 commit comments