|
| 1 | +<?php |
| 2 | +/** |
| 3 | + * Chart action buttons tests. |
| 4 | + * |
| 5 | + * @package Visualizer |
| 6 | + */ |
| 7 | +class Test_Visualizer_Frontend_Actions extends WP_UnitTestCase { |
| 8 | + |
| 9 | + /** |
| 10 | + * Render the shortcode of a chart that has the given action buttons setting. |
| 11 | + * |
| 12 | + * @param string[] $actions Saved action buttons. |
| 13 | + * |
| 14 | + * @return string |
| 15 | + */ |
| 16 | + private function render_chart_with_actions( $actions ) { |
| 17 | + $id = self::factory()->post->create( |
| 18 | + array( |
| 19 | + 'post_type' => Visualizer_Plugin::CPT_VISUALIZER, |
| 20 | + 'post_content' => serialize( array( array( 'a', 1 ) ) ), |
| 21 | + ) |
| 22 | + ); |
| 23 | + update_post_meta( $id, Visualizer_Plugin::CF_CHART_TYPE, 'pie' ); |
| 24 | + update_post_meta( $id, Visualizer_Plugin::CF_SERIES, array( array( 'label' => 'x', 'type' => 'string' ), array( 'label' => 'y', 'type' => 'number' ) ) ); |
| 25 | + update_post_meta( $id, Visualizer_Plugin::CF_SETTINGS, array( 'actions' => $actions ) ); |
| 26 | + |
| 27 | + return do_shortcode( '[visualizer id="' . $id . '"]' ); |
| 28 | + } |
| 29 | + |
| 30 | + /** |
| 31 | + * The part after the action name stays inside its attribute. |
| 32 | + */ |
| 33 | + public function test_action_mime_cannot_add_an_attribute() { |
| 34 | + $markup = $this->render_chart_with_actions( array( 'print;x" onfocus="alert(1)' ) ); |
| 35 | + |
| 36 | + $this->assertStringContainsString( 'data-visualizer-mime="x" onfocus="alert(1)"', $markup ); |
| 37 | + } |
| 38 | + |
| 39 | + /** |
| 40 | + * An action that the plugin does not define gets no button. |
| 41 | + */ |
| 42 | + public function test_unknown_action_gets_no_button() { |
| 43 | + $markup = $this->render_chart_with_actions( array( 'x" onfocus="alert(1)', 'csv;application/csv' ) ); |
| 44 | + |
| 45 | + $this->assertStringNotContainsString( 'onfocus', $markup ); |
| 46 | + $this->assertStringContainsString( 'data-visualizer-type="csv"', $markup ); |
| 47 | + } |
| 48 | + |
| 49 | + /** |
| 50 | + * The key and the title of an action that a filter adds stay inside their attributes. |
| 51 | + */ |
| 52 | + public function test_filtered_action_key_and_title_cannot_add_an_attribute() { |
| 53 | + add_filter( |
| 54 | + 'visualizer_action_buttons', |
| 55 | + function ( $actions ) { |
| 56 | + $actions['share" data-x="1'] = array( |
| 57 | + 'label' => 'Share', |
| 58 | + 'title' => 'Share" onfocus="alert(1)', |
| 59 | + ); |
| 60 | + return $actions; |
| 61 | + } |
| 62 | + ); |
| 63 | + |
| 64 | + $markup = $this->render_chart_with_actions( array( 'share" data-x="1' ) ); |
| 65 | + |
| 66 | + $this->assertStringContainsString( 'data-visualizer-type="share" data-x="1"', $markup ); |
| 67 | + $this->assertStringContainsString( 'title="Share" onfocus="alert(1)"', $markup ); |
| 68 | + } |
| 69 | +} |
0 commit comments