diff --git a/classes/Visualizer/Gutenberg/Block.php b/classes/Visualizer/Gutenberg/Block.php index a9c5d667..d5ae3e51 100644 --- a/classes/Visualizer/Gutenberg/Block.php +++ b/classes/Visualizer/Gutenberg/Block.php @@ -330,7 +330,7 @@ public function get_visualizer_data( $post ) { $data['visualizer-data'] = Visualizer_Module::sanitize_chart_data( apply_filters( Visualizer_Plugin::FILTER_GET_CHART_DATA, - Visualizer_Module::decode_content( html_entity_decode( get_post_field( 'post_content', $post_id, 'raw' ) ) ), + Visualizer_Module::can_read_chart_rows( $post_id ) ? Visualizer_Module::decode_content( html_entity_decode( get_post_field( 'post_content', $post_id, 'raw' ) ) ) : array(), $post_id, $data['visualizer-chart-type'] ) diff --git a/classes/Visualizer/Module.php b/classes/Visualizer/Module.php index 6acba5e3..8be0865f 100644 --- a/classes/Visualizer/Module.php +++ b/classes/Visualizer/Module.php @@ -878,6 +878,60 @@ public static function maybe_decode_content( $value ) { return is_serialized( $value ) ? self::decode_content( $value ) : $value; } + /** + * Tells whether the current user can read the stored rows of a chart. + * + * A post-type chart of the Pro plugin stores post titles and meta values of posts in any status. + * A published chart shows its rows to everyone who can see it. The rows of an unpublished chart are for + * users who can read every post of the type: the post type has an admin screen, and the user can edit + * its posts of other users and read its private posts. This is the rule the Pro plugin has for the query. + * + * @param int $chart_id The chart ID, or the ID of a chart revision. + * @return bool + */ + public static function can_read_chart_rows( $chart_id ) { + if ( self::has_public_rows( $chart_id ) ) { + return true; + } + + $filter_config = get_post_meta( $chart_id, Visualizer_Plugin::CF_FILTER_CONFIG, true ); + $post_type = is_array( $filter_config ) && isset( $filter_config['type'] ) && is_string( $filter_config['type'] ) ? get_post_type_object( $filter_config['type'] ) : null; + return $post_type instanceof WP_Post_Type + && $post_type->show_ui + && current_user_can( $post_type->cap->edit_others_posts ) + && current_user_can( $post_type->cap->read_private_posts ); + } + + /** + * Tells whether everyone who can see a chart can read its stored rows: the chart has no post-type query, or it is published. + * + * @param int $chart_id The chart ID, or the ID of a chart revision. + * @return bool + */ + public static function has_public_rows( $chart_id ) { + $parent_id = wp_is_post_revision( $chart_id ); + return empty( get_post_meta( $chart_id, Visualizer_Plugin::CF_FILTER_CONFIG, true ) ) || 'publish' === get_post_status( $parent_id ? $parent_id : $chart_id ); + } + + /** + * Deletes the saved image of a chart whose rows are not public. + * + * The image shows the rows, and each visitor can read a file in the uploads folder. + * + * @param int $chart_id The chart ID. A revision has no image of its own. + * @return void + */ + public static function delete_image_of_private_rows( $chart_id ) { + if ( wp_is_post_revision( $chart_id ) || self::has_public_rows( $chart_id ) ) { + return; + } + $image_id = self::get_chart_image_id( $chart_id ); + if ( $image_id ) { + wp_delete_attachment( $image_id, true ); + } + delete_post_meta( $chart_id, Visualizer_Plugin::CF_CHART_IMAGE ); + } + /** * Gets the chart content after common manipulations. * @@ -885,9 +939,14 @@ public static function maybe_decode_content( $value ) { * @param string $type The chart type passed to the data filter. * @param bool $run_filter Whether to run the chart data filter. * @param bool $sanitize Whether to prepare the cells for HTML-capable renderers; exports and editors need the stored values. - * @return mixed The chart data. + * @return mixed The chart data; an empty array when the current user cannot read the stored rows. */ public static function get_chart_data( $chart, $type, $run_filter = true, $sanitize = true ) { + // A chart object that is not a saved post has no ID and no saved post-type query. + if ( isset( $chart->ID ) && ! self::can_read_chart_rows( $chart->ID ) ) { + return array(); + } + // change HTML entities $post_content = html_entity_decode( htmlentities( $chart->post_content ) ); $post_content = preg_replace_callback( diff --git a/classes/Visualizer/Module/Chart.php b/classes/Visualizer/Module/Chart.php index f6b79b34..c6ed9c83 100644 --- a/classes/Visualizer/Module/Chart.php +++ b/classes/Visualizer/Module/Chart.php @@ -1345,6 +1345,14 @@ public function uploadData() { do_action( 'themeisle_log_event', Visualizer_Plugin::NAME, sprintf( 'Uploading data for chart %d with POST = %s and GET = %s', $chart_id, print_r( $_POST, true ), print_r( $_GET, true ) ), 'debug', __FILE__, __LINE__ ); + // A new data source removes the post-type query of the chart, and the stored rows stay when it gives no data. + // A user who cannot read those rows does not get them in an editor, so that user cannot replace them or the query. + if ( $can_die && ! self::can_read_chart_rows( $chart_id ) ) { + $render = new Visualizer_Render_Page_Update( array( 'message' => esc_html__( 'You cannot change the data of this chart, because it comes from posts that you cannot read.', 'visualizer' ) ) ); + $render->render(); + ( defined( 'WP_TESTS_DOMAIN' ) && function_exists( 'tests_add_filter' ) ) ? wp_die() : exit(); + } + if ( ! isset( $_POST['vz-import-time'] ) ) { apply_filters( 'visualizer_pro_remove_schedule', $chart_id ); } @@ -1780,7 +1788,7 @@ public function saveFilter() { * * @param string $base64_img Chart image. * @param int $chart_id Chart ID. - * @param bool $save_attachment Save attachment. + * @param bool $save_attachment Save attachment. A chart whose rows are not public gets no image. * @return int Attachment ID, or 0 when no attachment was saved. */ public function save_chart_image( $base64_img, $chart_id, $save_attachment = true ) { @@ -1792,7 +1800,7 @@ public function save_chart_image( $base64_img, $chart_id, $save_attachment = tru wp_delete_attachment( $old_attachment_id, true ); } - if ( ! $save_attachment ) { + if ( ! $save_attachment || ! self::has_public_rows( $chart_id ) ) { // The chart has no image now. A chart copied by an older version can still point to the image of its source chart. delete_post_meta( $chart_id, Visualizer_Plugin::CF_CHART_IMAGE ); return 0; diff --git a/classes/Visualizer/Module/Setup.php b/classes/Visualizer/Module/Setup.php index e19292c3..e89b943e 100644 --- a/classes/Visualizer/Module/Setup.php +++ b/classes/Visualizer/Module/Setup.php @@ -69,6 +69,11 @@ public function __construct( Visualizer_Plugin $plugin ) { $this->_addAction( 'action_scheduler_ensure_recurring_actions', 'ensure_refresh_db_action' ); $this->_addFilter( 'visualizer_schedule_refresh_chart', 'refresh_db_for_chart', 10, 3 ); $this->_addFilter( 'is_protected_meta', 'protect_chart_meta', 10, 3 ); + $this->_addFilter( 'wp_get_revision_ui_diff', 'remove_unreadable_rows_from_revision_diff', 10, 3 ); + $this->_addFilter( Visualizer_Plugin::FILTER_GET_CHART_SETTINGS, 'remove_image_of_unreadable_rows', 10, 2 ); + $this->_addAction( 'transition_post_status', 'delete_image_after_status_change', null, 10, 3 ); + $this->_addAction( 'added_post_meta', 'delete_image_after_query_change', null, 10, 3 ); + $this->_addAction( 'updated_post_meta', 'delete_image_after_query_change', null, 10, 3 ); $this->_addFilter( 'map_meta_cap', 'deny_json_headers_meta_caps', 10, 4 ); $this->_addFilter( 'wxr_export_skip_postmeta', 'skip_json_headers_in_export', 10, 2 ); @@ -314,6 +319,74 @@ public function adminInit() { } + /** + * Removes the chart content from a revision comparison for a user who cannot read the rows of the chart. + * + * WordPress compares the revisions of each post that its user can edit, and the content of a chart is its rows. + * + * @param array $fields The compared fields. + * @param WP_Post|false $compare_from The older revision, or false for none. + * @param WP_Post $compare_to The newer revision, or the post. + * @return array + */ + public function remove_unreadable_rows_from_revision_diff( $fields, $compare_from, $compare_to ) { + $parent_id = wp_is_post_revision( $compare_to ); + if ( Visualizer_Plugin::CPT_VISUALIZER !== get_post_type( $parent_id ? $parent_id : $compare_to ) ) { + return $fields; + } + // The older revision can hold rows of a post-type query that the newer one no longer has. + if ( self::can_read_chart_rows( $compare_to->ID ) && ( ! $compare_from instanceof WP_Post || self::can_read_chart_rows( $compare_from->ID ) ) ) { + return $fields; + } + return array_values( wp_list_filter( $fields, array( 'id' => 'post_content' ), 'NOT' ) ); + } + + /** + * Removes the rendered chart image from the settings for a user who cannot read the rows of the chart. + * + * The chart editor saves a picture of the chart in the `chart-img` setting, and the picture shows the rows. + * + * @param mixed $settings The chart settings. + * @param int $chart_id The chart ID. + * @return mixed + */ + public function remove_image_of_unreadable_rows( $settings, $chart_id ) { + if ( is_array( $settings ) && isset( $settings['chart-img'] ) && ! self::can_read_chart_rows( $chart_id ) ) { + unset( $settings['chart-img'] ); + } + return $settings; + } + + /** + * Deletes the saved image of a chart that is no longer published, when its rows are not public. + * + * @param string $new_status The new status. + * @param string $old_status The old status. + * @param WP_Post $post The post. + * @return void + */ + public function delete_image_after_status_change( $new_status, $old_status, $post ) { + if ( Visualizer_Plugin::CPT_VISUALIZER === $post->post_type ) { + self::delete_image_of_private_rows( $post->ID ); + } + } + + /** + * Deletes the saved image of an unpublished chart that gets a post-type query. + * + * A restored revision brings the query and the image in any order. + * + * @param int $meta_id The meta ID. + * @param int $chart_id The post ID. + * @param string $meta_key The meta key. + * @return void + */ + public function delete_image_after_query_change( $meta_id, $chart_id, $meta_key ) { + if ( in_array( $meta_key, array( Visualizer_Plugin::CF_FILTER_CONFIG, Visualizer_Plugin::CF_CHART_IMAGE ), true ) ) { + self::delete_image_of_private_rows( $chart_id ); + } + } + /** * Marks the chart meta as protected. * diff --git a/classes/Visualizer/Module/Upgrade.php b/classes/Visualizer/Module/Upgrade.php index 1dcf3b9e..39c8b7c9 100644 --- a/classes/Visualizer/Module/Upgrade.php +++ b/classes/Visualizer/Module/Upgrade.php @@ -24,6 +24,12 @@ public static function upgrade() { $upgraded = true; } + // Once after each update: an earlier version keeps such an image, and no version number marks the first one that does not. + if ( version_compare( $last_version, Visualizer_Plugin::VERSION, '<' ) ) { + self::deleteImagesOfPrivateRows(); + $upgraded = true; + } + if ( ! $upgraded ) { return; } @@ -34,6 +40,33 @@ public static function upgrade() { update_option( 'visualizer-upgraded-from', $last_version ); } + /** + * Deletes the images that an earlier version saved for unpublished charts with a post-type query. + * + * @return void + */ + private static function deleteImagesOfPrivateRows() { + $chart_ids = get_posts( + array( + 'post_type' => Visualizer_Plugin::CPT_VISUALIZER, + 'post_status' => array_values( array_diff( get_post_stati(), array( 'publish' ) ) ), + 'fields' => 'ids', + 'posts_per_page' => -1, + 'meta_query' => array( + array( + 'key' => Visualizer_Plugin::CF_FILTER_CONFIG, + 'compare' => 'EXISTS', + ), + array( + 'key' => Visualizer_Plugin::CF_CHART_IMAGE, + 'compare' => 'EXISTS', + ), + ), + ) + ); + array_map( array( 'Visualizer_Module', 'delete_image_of_private_rows' ), $chart_ids ); + } + /** * All 'dataTable' and 'table' charts to become 'tabular'. * All charts that do not have a library, to get the default library. diff --git a/tests/test-chart-rows-permissions.php b/tests/test-chart-rows-permissions.php new file mode 100644 index 00000000..c2725f9f --- /dev/null +++ b/tests/test-chart-rows-permissions.php @@ -0,0 +1,530 @@ +author_id = $this->factory->user->create( array( 'role' => 'author' ) ); + $this->editor_id = $this->factory->user->create( array( 'role' => 'editor' ) ); + $this->chart_id = $this->factory->post->create( + array( + 'post_type' => Visualizer_Plugin::CPT_VISUALIZER, + 'post_status' => 'draft', + 'post_author' => $this->author_id, + 'post_content' => serialize( array( array( self::PRIVATE_CELL, 1 ) ) ), + ) + ); + update_post_meta( $this->chart_id, Visualizer_Plugin::CF_CHART_TYPE, 'line' ); + update_post_meta( $this->chart_id, Visualizer_Plugin::CF_CHART_LIBRARY, 'ChartJS' ); + // The settings that the chart editor reads without a default. + $font = array( + 'fontName' => '', + 'fontFamily' => '', + 'fontSize' => '', + ); + update_post_meta( + $this->chart_id, + Visualizer_Plugin::CF_SETTINGS, + array( + 'legend' => array( 'labels' => $font ), + 'chart-img' => 'data:image/png;base64,' . self::PRIVATE_IMAGE, + 'tooltip' => array( 'intersect' => '' ), + 'xAxes' => array( 'scaleLabel' => $font ), + 'yAxes' => array( 'scaleLabel' => $font ), + ) + ); + update_post_meta( + $this->chart_id, + Visualizer_Plugin::CF_SERIES, + array( + array( + 'label' => 'x', + 'type' => 'string', + ), + array( + 'label' => 'y', + 'type' => 'number', + ), + ) + ); + update_post_meta( $this->chart_id, Visualizer_Plugin::CF_FILTER_CONFIG, array( 'type' => 'post' ) ); + + // The library gives the chart data to this script. Its admin page registers it. + if ( ! wp_script_is( 'visualizer-library', 'registered' ) ) { + wp_register_script( 'visualizer-library', 'https://example.com/library.js', array(), '1', true ); + } + } + + /** + * The readers of the stored rows. + * + * @return array The name of the method that returns what a reader gives to the current user. + */ + public function readers() { + return array( + 'block editor field' => array( 'read_block_field' ), + 'get-chart ability' => array( 'read_ability' ), + 'export' => array( 'read_export' ), + 'shortcode' => array( 'read_shortcode' ), + 'chart editor' => array( 'read_chart_editor' ), + 'chart library' => array( 'read_library' ), + ); + } + + /** + * A reader gives the rows of the unpublished chart to the Editor, and not to the Author who owns the chart. + * After the chart is published, the Author gets them too. + * + * @dataProvider readers + * + * @param string $reader The method that reads. + */ + public function test_reader_gives_unpublished_rows_only_to_a_user_who_passes_the_rule( $reader ) { + wp_set_current_user( $this->editor_id ); + $this->assertStringContainsString( self::PRIVATE_CELL, $this->$reader() ); + + wp_set_current_user( $this->author_id ); + $this->assertStringNotContainsString( self::PRIVATE_CELL, $this->$reader() ); + + wp_publish_post( $this->chart_id ); + $this->assertStringContainsString( self::PRIVATE_CELL, $this->$reader() ); + } + + /** + * The rule holds for a chart without a usable post type, and does not touch a chart without a post-type query. + */ + public function test_rule_needs_a_post_type_with_an_admin_screen() { + wp_set_current_user( $this->editor_id ); + + update_post_meta( $this->chart_id, Visualizer_Plugin::CF_FILTER_CONFIG, array( 'type' => 'revision' ) ); + $this->assertFalse( Visualizer_Module::can_read_chart_rows( $this->chart_id ) ); + + update_post_meta( $this->chart_id, Visualizer_Plugin::CF_FILTER_CONFIG, 'broken' ); + $this->assertFalse( Visualizer_Module::can_read_chart_rows( $this->chart_id ) ); + + delete_post_meta( $this->chart_id, Visualizer_Plugin::CF_FILTER_CONFIG ); + wp_set_current_user( $this->author_id ); + $this->assertStringContainsString( self::PRIVATE_CELL, $this->read_block_field() ); + } + + /** + * A revision of a published chart follows the status of the chart. + */ + public function test_revision_follows_the_status_of_its_chart() { + wp_set_current_user( $this->author_id ); + $revision_id = $this->factory->post->create( + array( + 'post_type' => 'revision', + 'post_status' => 'inherit', + 'post_parent' => $this->chart_id, + 'post_name' => $this->chart_id . '-revision-v1', + ) + ); + // update_post_meta() writes to the chart, not to its revision. + update_metadata( 'post', $revision_id, Visualizer_Plugin::CF_FILTER_CONFIG, array( 'type' => 'post' ) ); + + $this->assertFalse( Visualizer_Module::can_read_chart_rows( $revision_id ) ); + + wp_publish_post( $this->chart_id ); + $this->assertTrue( Visualizer_Module::can_read_chart_rows( $revision_id ) ); + } + + /** + * The block editor field gives the rendered chart image only to a user who can read the rows. + */ + public function test_reader_gives_the_chart_image_only_to_a_user_who_passes_the_rule() { + wp_set_current_user( $this->editor_id ); + $this->assertStringContainsString( self::PRIVATE_IMAGE, $this->read_block_field() ); + + wp_set_current_user( $this->author_id ); + $this->assertStringNotContainsString( self::PRIVATE_IMAGE, $this->read_block_field() ); + } + + /** + * The revision comparison of WordPress gives the chart content only to a user who can read the rows. + */ + public function test_revision_comparison_gives_rows_only_to_a_user_who_passes_the_rule() { + wp_set_current_user( $this->editor_id ); + $this->assertStringContainsString( self::PRIVATE_CELL, $this->read_revision_comparison() ); + + wp_set_current_user( $this->author_id ); + $this->assertStringNotContainsString( self::PRIVATE_CELL, $this->read_revision_comparison() ); + } + + /** + * The comparison of two revisions does not give the rows of the older one after the chart got other data. + */ + public function test_revision_comparison_keeps_the_rows_of_an_older_revision() { + $older_id = $this->create_revision( '2020-01-01 00:00:00', get_post_field( 'post_content', $this->chart_id, 'raw' ) ); + $newer_id = $this->create_revision( '2021-01-01 00:00:00', 'other data' ); + // update_post_meta() writes to the chart, not to its revision. + update_metadata( 'post', $older_id, Visualizer_Plugin::CF_FILTER_CONFIG, array( 'type' => 'post' ) ); + + wp_set_current_user( $this->editor_id ); + $this->assertStringContainsString( self::PRIVATE_CELL, $this->read_revision_comparison( $older_id, $newer_id ) ); + + wp_set_current_user( $this->author_id ); + $this->assertStringNotContainsString( self::PRIVATE_CELL, $this->read_revision_comparison( $older_id, $newer_id ) ); + } + + /** + * Create a revision of the chart. + * + * @param string $date The date of the revision. + * @param string $content The content of the revision. + * @return int + */ + private function create_revision( $date, $content ) { + return $this->factory->post->create( + array( + 'post_type' => 'revision', + 'post_status' => 'inherit', + 'post_parent' => $this->chart_id, + 'post_content' => $content, + 'post_date' => $date, + ) + ); + } + + /** + * Compare two versions of the chart, as the revisions screen does through `wp_ajax_get_revision_diffs()`. + * + * @param int $compare_from The ID of the older revision, or 0 for none. + * @param int $compare_to The ID of the newer revision, or 0 for the chart. + * @return string + */ + private function read_revision_comparison( $compare_from = 0, $compare_to = 0 ) { + require_once ABSPATH . 'wp-admin/includes/revision.php'; + return (string) wp_json_encode( wp_get_revision_ui_diff( $this->chart_id, $compare_from, $compare_to ? $compare_to : $this->chart_id ) ); + } + + /** + * A manual editor does not have the rows that its user cannot read. Its content does not replace them, and + * no upload of that user removes the post-type query. The content of a user who can read them is saved. + */ + public function test_manual_editor_does_not_replace_rows_that_its_user_cannot_read() { + wp_set_current_user( $this->author_id ); + $saves = array( + array( "x,y\nstring,number\nEdited cell,2", '' ), + array( '', '' ), + // A request that names the post-type query as its source brings other data too. + array( "x,y\nstring,number\nEdited cell,2", Visualizer_Plugin::CF_SOURCE_FILTER ), + ); + foreach ( $saves as list( $chart_data, $source ) ) { + $this->save_text_editor( $chart_data, $source ); + $this->assertStringContainsString( self::PRIVATE_CELL, get_post_field( 'post_content', $this->chart_id, 'raw' ) ); + $this->assertSame( array( 'type' => 'post' ), get_post_meta( $this->chart_id, Visualizer_Plugin::CF_FILTER_CONFIG, true ) ); + $this->assertFalse( Visualizer_Module::can_read_chart_rows( $this->chart_id ) ); + } + + wp_set_current_user( $this->editor_id ); + $this->save_text_editor(); + $content = get_post_field( 'post_content', $this->chart_id, 'raw' ); + $this->assertStringContainsString( 'Edited cell', $content ); + $this->assertStringNotContainsString( self::PRIVATE_CELL, $content ); + } + + /** + * Save the text editor of the chart, as the current user. + * + * @param string $chart_data The content of the editor. + * @param string $source The data source that the request names. + */ + private function save_text_editor( $chart_data = "x,y\nstring,number\nEdited cell,2", $source = '' ) { + $_GET = array( + 'nonce' => wp_create_nonce( 'visualizer-upload-data' ), + 'chart' => $this->chart_id, + ); + // An earlier upload test can leave its file here, and a file comes before the editor content. + $_FILES = array(); + $_POST = array( + 'chart_data' => $chart_data, + 'chart_data_src' => $source, + 'editor-type' => 'text', + ); + ob_start(); + try { + $this->_handleAjax( Visualizer_Plugin::ACTION_UPLOAD_DATA ); + } catch ( WPAjaxDieContinueException $e ) { + // The handler ends the request after it answers. + } catch ( WPAjaxDieStopException $e ) { + // The handler ends the request after it answers. + } + ob_end_clean(); + } + + /** + * Read the REST field of the block editor. + * + * @return string + */ + private function read_block_field() { + return (string) wp_json_encode( Visualizer_Gutenberg_Block::get_instance()->get_visualizer_data( array( 'id' => $this->chart_id ) ) ); + } + + /** + * Read the chart through the get-chart ability. + * + * @return string + */ + private function read_ability() { + $module = Visualizer_Plugin::instance()->getModule( Visualizer_Module_Abilities::NAME ); + return (string) wp_json_encode( $module->executeGetChart( array( 'chart_id' => $this->chart_id ) ) ); + } + + /** + * Read the CSV export of the chart. + * + * @return string + */ + private function read_export() { + $module = Visualizer_Plugin::instance()->getModule( Visualizer_Module_Chart::NAME ); + return (string) wp_json_encode( $module->_getDataAs( $this->chart_id, 'csv' ) ); + } + + /** + * Render the shortcode of the chart and read the charts that the front end collected for the page scripts. + * + * @return string + */ + private function read_shortcode() { + do_shortcode( '[visualizer id="' . $this->chart_id . '"]' ); + + $charts = new ReflectionProperty( Visualizer_Module_Frontend::class, '_charts' ); + $charts->setAccessible( true ); + $module = Visualizer_Plugin::instance()->getModule( Visualizer_Module_Frontend::NAME ); + $data = (string) wp_json_encode( $charts->getValue( $module ) ); + $charts->setValue( $module, array() ); + return $data; + } + + /** + * Open the chart editor and read its page. + * + * @return string + */ + private function read_chart_editor() { + $_GET = array( + 'chart' => $this->chart_id, + 'tab' => 'settings', + 'nonce' => wp_create_nonce(), + ); + $_REQUEST = $_GET; + $this->_last_response = ''; + try { + $this->_handleAjax( Visualizer_Plugin::ACTION_EDIT_CHART ); + } catch ( WPAjaxDieContinueException $e ) { + // Expected after the page is printed. + unset( $e ); + } catch ( WPAjaxDieStopException $e ) { + unset( $e ); + } + return $this->_last_response . $this->script_data(); + } + + /** + * Render the chart library and read the chart data that it gives to the page scripts. + * + * @return string + */ + private function read_library() { + $_GET = array(); + ob_start(); + Visualizer_Plugin::instance()->getModule( Visualizer_Module_Admin::NAME )->renderLibraryPage(); + ob_end_clean(); + return $this->script_data(); + } + + /** + * Collect the data that the registered scripts get, and forget it for the next read. + * + * @return string + */ + private function script_data() { + $data = ''; + foreach ( wp_scripts()->registered as $script ) { + if ( ! empty( $script->extra['data'] ) ) { + $data .= $script->extra['data']; + unset( $script->extra['data'] ); + } + } + return $data; + } + + /** + * A visitor can read each image in the uploads folder, so an unpublished chart with a post-type query gets no saved image. + */ + public function test_image_is_not_saved_for_an_unpublished_chart() { + $module = Visualizer_Plugin::instance()->getModule( Visualizer_Module_Chart::NAME ); + $this->assertSame( 0, $module->save_chart_image( self::PNG, $this->chart_id ) ); + + $image_id = $this->save_image_of_published_chart(); + $this->assertSame( 200, $this->read_image_as_visitor( $image_id ) ); + } + + /** + * The saved image is deleted when the chart is no longer published. + */ + public function test_image_is_deleted_when_the_chart_is_unpublished() { + $image_id = $this->save_image_of_published_chart(); + $file = get_attached_file( $image_id ); + wp_update_post( + array( + 'ID' => $this->chart_id, + 'post_status' => 'draft', + ) + ); + + $this->assertSame( 404, $this->read_image_as_visitor( $image_id ) ); + $this->assertFileDoesNotExist( $file ); + wp_set_current_user( $this->author_id ); + $this->assertStringNotContainsString( 'chart_id, Visualizer_Plugin::CF_FILTER_CONFIG, '' ); + $image_id = Visualizer_Plugin::instance()->getModule( Visualizer_Module_Chart::NAME )->save_chart_image( self::PNG, $this->chart_id ); + $this->assertSame( 200, $this->read_image_as_visitor( $image_id ) ); + + if ( $query_first ) { + update_post_meta( $this->chart_id, Visualizer_Plugin::CF_FILTER_CONFIG, array( 'type' => 'post' ) ); + add_post_meta( $this->chart_id, Visualizer_Plugin::CF_CHART_IMAGE, $image_id ); + } else { + add_post_meta( $this->chart_id, Visualizer_Plugin::CF_CHART_IMAGE, $image_id ); + $this->assertSame( 200, $this->read_image_as_visitor( $image_id ) ); + update_post_meta( $this->chart_id, Visualizer_Plugin::CF_FILTER_CONFIG, array( 'type' => 'post' ) ); + } + $this->assertSame( 404, $this->read_image_as_visitor( $image_id ) ); + } + + /** + * The orders of the query and the image. + * + * @return array + */ + public function meta_orders() { + return array( + 'query first' => array( true ), + 'image first' => array( false ), + ); + } + + /** + * A query on a revision does not remove the image of its chart. + */ + public function test_query_of_a_revision_keeps_the_image_of_the_chart() { + delete_post_meta( $this->chart_id, Visualizer_Plugin::CF_FILTER_CONFIG ); + $image_id = Visualizer_Plugin::instance()->getModule( Visualizer_Module_Chart::NAME )->save_chart_image( self::PNG, $this->chart_id ); + update_post_meta( $this->chart_id, Visualizer_Plugin::CF_CHART_IMAGE, $image_id ); + + update_metadata( 'post', $this->create_revision( '2020-01-01 00:00:00', '' ), Visualizer_Plugin::CF_FILTER_CONFIG, array( 'type' => 'post' ) ); + $this->assertSame( $image_id, Visualizer_Module::get_chart_image_id( $this->chart_id ) ); + } + + /** + * The upgrade deletes the image that an earlier version saved for an unpublished chart, once for each version. + */ + public function test_upgrade_deletes_the_image_of_an_unpublished_chart() { + global $wpdb; + $image_id = $this->save_image_of_published_chart(); + // An earlier version kept the image after the status change. + $wpdb->update( $wpdb->posts, array( 'post_status' => 'draft' ), array( 'ID' => $this->chart_id ) ); + clean_post_cache( $this->chart_id ); + $this->assertSame( 200, $this->read_image_as_visitor( $image_id ) ); + + update_option( 'visualizer-upgraded', Visualizer_Plugin::VERSION ); + Visualizer_Module_Upgrade::upgrade(); + $this->assertSame( 200, $this->read_image_as_visitor( $image_id ) ); + + update_option( 'visualizer-upgraded', '4.0.0' ); + Visualizer_Module_Upgrade::upgrade(); + $this->assertSame( 404, $this->read_image_as_visitor( $image_id ) ); + $this->assertSame( Visualizer_Plugin::VERSION, get_option( 'visualizer-upgraded' ) ); + } + + /** + * Publish the chart and save its image. + * + * @return int The attachment ID. + */ + private function save_image_of_published_chart() { + wp_publish_post( $this->chart_id ); + $image_id = Visualizer_Plugin::instance()->getModule( Visualizer_Module_Chart::NAME )->save_chart_image( self::PNG, $this->chart_id ); + update_post_meta( $this->chart_id, Visualizer_Plugin::CF_CHART_IMAGE, $image_id ); + return $image_id; + } + + /** + * Read the image through the media REST route, as a visitor. + * + * @param int $image_id The attachment ID. + * @return int The HTTP status. + */ + private function read_image_as_visitor( $image_id ) { + wp_set_current_user( 0 ); + return rest_do_request( new WP_REST_Request( 'GET', '/wp/v2/media/' . $image_id ) )->get_status(); + } +}