'table', 'filters' => array(), 'sort' => array( 'field' => 'title', 'direction' => 'asc', ), 'perPage' => 20, 'fields' => array( 'author', 'status' ), 'titleField' => 'title', ); /** * The default layouts shared by all entities. * * @var array */ const DEFAULT_LAYOUTS = array( 'table' => array(), 'grid' => array(), 'list' => array(), ); /** * The default view list for an entity with no specific provider. * * @var array */ const DEFAULT_VIEW_LIST = array( array( 'title' => 'All items', 'slug' => 'all', ), ); /** * The default form configuration shared by all entities. * * @var array */ const DEFAULT_FORM = array(); /** * Tears down each test. */ public function tear_down() { remove_all_filters( 'get_entity_view_config_posttype_unregistered_cpt' ); remove_all_filters( 'get_entity_view_config_custom_kind_custom_name' ); parent::tear_down(); } /** * Returns the form field with the given id from a view configuration. * * @param array $config The view configuration. * @param string $id The form field id. * @return array|null The form field, or null when absent. */ private function get_form_field( $config, $id ) { foreach ( $config['form']['fields'] as $field ) { if ( is_array( $field ) && isset( $field['id'] ) && $id === $field['id'] ) { return $field; } } return null; } /** * The `status` and `discussion` groups of the default post type form declare * their panel summary explicitly, for built-in and custom post types alike. * * @dataProvider data_default_form_post_types * * @param string $post_type The post type. */ public function test_default_form_groups_declare_summary( $post_type ) { if ( ! post_type_exists( $post_type ) ) { register_post_type( $post_type ); } $config = gutenberg_get_entity_view_config( 'postType', $post_type ); foreach ( array( 'status', 'discussion' ) as $group ) { $field = $this->get_form_field( $config, $group ); $this->assertNotNull( $field, "The `{$group}` group is present." ); $this->assertNotEmpty( $field['children'], "The `{$group}` group keeps its children." ); $this->assertSame( array( 'type' => 'panel', 'summary' => $group, ), $field['layout'], "The `{$group}` group summary is explicit." ); } if ( 'page' !== $post_type && 'post' !== $post_type ) { unregister_post_type( $post_type ); } } /** * Data provider. * * @return array[] */ public function data_default_form_post_types() { return array( 'page' => array( 'page' ), 'post' => array( 'post' ), 'custom post type' => array( 'summaries_cpt' ), ); } /** * Post types with their own form do not receive the default form groups. * * @dataProvider data_post_types_with_own_form * * @param string $post_type The post type. */ public function test_own_form_post_types_do_not_get_default_groups( $post_type ) { $config = gutenberg_get_entity_view_config( 'postType', $post_type ); $this->assertNull( $this->get_form_field( $config, 'status' ) ); $this->assertNull( $this->get_form_field( $config, 'discussion' ) ); } /** * Data provider. * * @return array[] */ public function data_post_types_with_own_form() { return array( 'attachment' => array( 'attachment' ), 'wp_block' => array( 'wp_block' ), 'wp_template' => array( 'wp_template' ), 'wp_template_part' => array( 'wp_template_part' ), ); } /** * The `excerpt` row shows the field's placeholder when the excerpt is * empty, for every post type whose form lists the field. * * @dataProvider data_post_types_with_excerpt_in_form * * @param string $post_type The post type. */ public function test_excerpt_layout_shows_placeholder_if_empty( $post_type ) { if ( ! post_type_exists( $post_type ) ) { register_post_type( $post_type ); } $config = gutenberg_get_entity_view_config( 'postType', $post_type ); $this->assertSame( array( 'type' => 'panel', 'labelPosition' => 'top', 'showPlaceholderIfEmpty' => true, ), $this->get_form_field( $config, 'excerpt' )['layout'] ); if ( ! in_array( $post_type, array( 'page', 'post', 'wp_block' ), true ) ) { unregister_post_type( $post_type ); } } /** * Data provider. * * @return array[] */ public function data_post_types_with_excerpt_in_form() { return array( 'page' => array( 'page' ), 'post' => array( 'post' ), 'wp_block' => array( 'wp_block' ), 'custom post type' => array( 'excerpt_cpt' ), ); } /** * Post types whose own form does not list the `excerpt` field do not get * one appended by the `excerpt` layout patch. * * @dataProvider data_post_types_without_excerpt_in_form * * @param string $post_type The post type. */ public function test_excerpt_layout_is_not_appended_to_own_forms( $post_type ) { $config = gutenberg_get_entity_view_config( 'postType', $post_type ); $this->assertNull( $this->get_form_field( $config, 'excerpt' ) ); } /** * Data provider. * * @return array[] */ public function data_post_types_without_excerpt_in_form() { return array( 'attachment' => array( 'attachment' ), 'wp_template' => array( 'wp_template' ), 'wp_template_part' => array( 'wp_template_part' ), ); } /** * The `description` row of templates shows the field's placeholder when * the description is empty, like the excerpt of posts and patterns. */ public function test_wp_template_description_layout_shows_placeholder_if_empty() { $config = gutenberg_get_entity_view_config( 'postType', 'wp_template' ); $this->assertSame( array( 'type' => 'panel', 'labelPosition' => 'top', 'showPlaceholderIfEmpty' => true, ), $this->get_form_field( $config, 'description' )['layout'] ); } /** * The `author` row shows the field's placeholder when the post has no * author, for every post type whose form lists the field. * * @dataProvider data_post_types_with_author_in_form * * @param string $post_type The post type. */ public function test_author_layout_shows_placeholder_if_empty( $post_type ) { if ( ! post_type_exists( $post_type ) ) { register_post_type( $post_type ); } $config = gutenberg_get_entity_view_config( 'postType', $post_type ); $this->assertSame( array( 'type' => 'panel', 'showPlaceholderIfEmpty' => true, ), $this->get_form_field( $config, 'author' )['layout'] ); if ( ! in_array( $post_type, array( 'page', 'post', 'attachment' ), true ) ) { unregister_post_type( $post_type ); } } /** * Data provider. * * @return array[] */ public function data_post_types_with_author_in_form() { return array( 'page' => array( 'page' ), 'post' => array( 'post' ), 'attachment' => array( 'attachment' ), 'custom post type' => array( 'summaries_cpt' ), ); } /** * The `date` panel of the default form is summarized by the * `publish_date` field, for every post type using that form. * * @dataProvider data_default_form_post_types * * @param string $post_type The post type. */ public function test_date_panel_is_summarized_by_publish_date( $post_type ) { if ( ! post_type_exists( $post_type ) ) { register_post_type( $post_type ); } $config = gutenberg_get_entity_view_config( 'postType', $post_type ); $this->assertSame( array( 'type' => 'panel', 'summary' => 'publish_date', ), $this->get_form_field( $config, 'date' )['layout'] ); if ( ! in_array( $post_type, array( 'page', 'post' ), true ) ) { unregister_post_type( $post_type ); } } /** * Post types providing their own form keep their `date` member, if any, * as it is, and get none appended. * * @dataProvider data_post_types_with_own_form * * @param string $post_type The post type. */ public function test_date_summary_is_not_applied_to_own_forms( $post_type ) { $config = gutenberg_get_entity_view_config( 'postType', $post_type ); $date = $this->get_form_field( $config, 'date' ); $this->assertTrue( null === $date || ! isset( $date['layout']['summary'] ) ); } /** * Post types whose own form does not list the `author` field do not get * one appended by the `author` layout patch. * * @dataProvider data_post_types_without_author_in_form * * @param string $post_type The post type. */ public function test_author_layout_is_not_appended_to_own_forms( $post_type ) { $config = gutenberg_get_entity_view_config( 'postType', $post_type ); $this->assertNotContains( 'author', $config['form']['fields'] ); $this->assertNull( $this->get_form_field( $config, 'author' ) ); } /** * Data provider. * * @return array[] */ public function data_post_types_without_author_in_form() { return array( 'wp_block' => array( 'wp_block' ), 'wp_template' => array( 'wp_template' ), 'wp_template_part' => array( 'wp_template_part' ), ); } /** * The `attachment` post type provides the form of the media editor: the * editable fields in a regular layout, then the rest in panels. */ public function test_attachment_form() { $config = gutenberg_get_entity_view_config( 'postType', 'attachment' ); $this->assertSame( array( 'type' => 'panel' ), $config['form']['layout'] ); $regular_layout = array( 'type' => 'regular', 'labelPosition' => 'top', ); $this->assertSame( array( array( 'id' => 'title', 'layout' => $regular_layout, ), array( 'id' => 'alt_text', 'layout' => $regular_layout, ), array( 'id' => 'caption', 'layout' => $regular_layout, ), array( 'id' => 'description', 'layout' => $regular_layout, ), 'date', array( 'id' => 'author', 'layout' => array( 'type' => 'panel', 'showPlaceholderIfEmpty' => true, ), ), 'filename', 'mime_type', 'filesize', 'media_dimensions', 'attached_to', ), $config['form']['fields'] ); } /** * The `wp_block` view list renames "My patterns" to "Site patterns" in place, * keeping its slug, and the table aligns the author column to the start. */ public function test_wp_block_site_patterns_view_and_author_alignment() { $config = gutenberg_get_entity_view_config( 'postType', 'wp_block' ); $this->assertSame( array( 'all-patterns', 'my-patterns' ), array_slice( array_column( $config['view_list'], 'slug' ), 0, 2 ) ); $this->assertSame( 'Site patterns', $config['view_list'][1]['title'] ); $this->assertSame( array( 'width' => '1%', 'align' => 'start', ), $config['default_layouts']['table']['layout']['styles']['author'] ); } /** * The default configuration exposes the documented shape for an unknown entity. */ public function test_default_config_for_unknown_entity() { $config = gutenberg_get_entity_view_config( 'custom_kind', 'custom_name' ); $this->assertIsArray( $config ); $this->assertSameSets( self::CONFIG_KEYS, array_keys( $config ) ); $this->assertSame( self::DEFAULT_VIEW, $config['default_view'] ); $this->assertSame( self::DEFAULT_LAYOUTS, $config['default_layouts'] ); $this->assertSame( self::DEFAULT_VIEW_LIST, $config['view_list'] ); $this->assertSame( self::DEFAULT_FORM, $config['form'] ); } /** * The base "all items" view falls back to a generic title for an unknown post type. */ public function test_view_list_falls_back_to_generic_all_items_title() { $config = gutenberg_get_entity_view_config( 'postType', 'does_not_exist' ); $this->assertCount( 1, $config['view_list'] ); $this->assertSame( 'all', $config['view_list'][0]['slug'] ); $this->assertSame( 'All items', $config['view_list'][0]['title'] ); } /** * For a registered post type, the "all items" view uses the post type's label. */ public function test_view_list_uses_post_type_all_items_label() { register_post_type( 'view_config_cpt', array( 'labels' => array( 'all_items' => 'All Custom Things', ), ) ); $config = gutenberg_get_entity_view_config( 'postType', 'view_config_cpt' ); $this->assertSame( 'All Custom Things', $config['view_list'][0]['title'] ); unregister_post_type( 'view_config_cpt' ); } /** * The dynamic filter receives the data container and the entity descriptor. */ public function test_filter_receives_data_and_entity() { $received_entity = null; $received_data = null; add_filter( 'get_entity_view_config_custom_kind_custom_name', function ( $data, $entity ) use ( &$received_entity, &$received_data ) { $received_entity = $entity; $received_data = $data; return $data; }, 10, 2 ); gutenberg_get_entity_view_config( 'custom_kind', 'custom_name' ); $this->assertInstanceOf( 'Gutenberg_View_Config_Data', $received_data ); $this->assertSame( array( 'kind' => 'custom_kind', 'name' => 'custom_name', ), $received_entity ); } /** * The dynamic filter name lowercases the entity kind and name. */ public function test_filter_hook_name_is_lowercased() { $called = false; add_filter( 'get_entity_view_config_posttype_unregistered_cpt', function ( $data ) use ( &$called ) { $called = true; return $data; } ); gutenberg_get_entity_view_config( 'postType', 'Unregistered_CPT' ); $this->assertTrue( $called ); $this->assertSame( 'get_entity_view_config_posttype_unregistered_cpt', gutenberg_get_entity_view_config_hook_name( 'postType', 'Unregistered_CPT' ) ); } /** * A filter can override configuration values through merge(). */ public function test_filter_data_is_merged() { add_filter( 'get_entity_view_config_custom_kind_custom_name', function ( $data ) { return $data->merge( array( 'default_view' => array( 'type' => 'grid' ) ), 1 ); } ); $config = gutenberg_get_entity_view_config( 'custom_kind', 'custom_name' ); $this->assertSame( 'grid', $config['default_view']['type'] ); } /** * Successive filters share the same Gutenberg_View_Config_Data instance, so * their effects compose: a later filter can add a form field to a group that * an earlier one defined. */ public function test_filter_data_chain() { add_filter( 'get_entity_view_config_custom_kind_custom_name', function ( $data ) { return $data->replace( array( 'form' => array( 'fields' => array( array( 'id' => 'discussion', 'children' => array( 'comment_status' ), ), ), ), ), 1 ); }, 9 ); add_filter( 'get_entity_view_config_custom_kind_custom_name', function ( $data ) { return $data->merge( array( 'form' => array( 'fields' => array( array( 'id' => 'discussion', 'children' => array( 'ping_status' ), ), ), ), ), 1 ); }, 11 ); $config = gutenberg_get_entity_view_config( 'custom_kind', 'custom_name' ); $this->assertSame( array( 'comment_status', 'ping_status' ), $config['form']['fields'][0]['children'] ); } /** * A filter that returns its own off-shape container has the result normalized: * undocumented keys are dropped and dropped documented keys are backfilled * from the defaults. */ public function test_filter_data_normalized() { $this->setExpectedIncorrectUsage( 'Gutenberg_View_Config_Data::set' ); add_filter( 'get_entity_view_config_custom_kind_custom_name', function ( $data ) { return $data->set( array( 'default_view' => array( 'type' => 'grid' ), 'not_a_real_key' => 'nope', ), 1 ); }, 10, 2 ); $config = gutenberg_get_entity_view_config( 'custom_kind', 'custom_name' ); // Undocumented key dropped; shape is exactly the documented keys. $this->assertSameSets( self::CONFIG_KEYS, array_keys( $config ) ); // The container's own value is respected. $this->assertSame( array( 'type' => 'grid' ), $config['default_view'] ); // Documented keys the container omitted are backfilled from the defaults. $this->assertSame( self::DEFAULT_LAYOUTS, $config['default_layouts'] ); $this->assertSame( self::DEFAULT_VIEW_LIST, $config['view_list'] ); $this->assertSame( self::DEFAULT_FORM, $config['form'] ); } /** * A documented key dropped through a null patch value is backfilled from * the defaults, so a null never reaches the response. */ public function test_filter_data_backfilled_if_null() { add_filter( 'get_entity_view_config_custom_kind_custom_name', function ( $data ) { return $data->merge( array( 'default_view' => null ), 1 ); } ); $config = gutenberg_get_entity_view_config( 'custom_kind', 'custom_name' ); $this->assertSame( self::DEFAULT_VIEW, $config['default_view'] ); } public function test_filter_default_view_merge_fields() { add_filter( 'get_entity_view_config_custom_kind_custom_name', function ( $data ) { return $data->merge( array( 'default_view' => array( 'fields' => array( 'title', 'author' ), ), ), 1 ); } ); $config = gutenberg_get_entity_view_config( 'custom_kind', 'custom_name' ); $this->assertSame( array( 'author', 'status', 'title' ), $config['default_view']['fields'] ); } public function test_filter_default_view_replace_fields() { add_filter( 'get_entity_view_config_custom_kind_custom_name', function ( $data ) { return $data->replace( array( 'default_view' => array( 'fields' => array( 'title' ), ), ), 1 ); } ); $config = gutenberg_get_entity_view_config( 'custom_kind', 'custom_name' ); $this->assertSame( array( 'title' ), $config['default_view']['fields'] ); } public function test_filter_default_view_remove_fields() { add_filter( 'get_entity_view_config_custom_kind_custom_name', function ( $data ) { $data->merge( array( 'default_view' => array( 'fields' => null, ), ), 1 ); $data->merge( array( 'default_view' => array( 'fields' => array( 'author' ), ), ), 1 ); return $data; } ); $config = gutenberg_get_entity_view_config( 'custom_kind', 'custom_name' ); $this->assertSame( array( 'author' ), $config['default_view']['fields'] ); } public function test_filter_view_list_add_view() { add_filter( 'get_entity_view_config_custom_kind_custom_name', function ( $data ) { return $data->merge( array( 'view_list' => array( array( 'slug' => 'my_view', 'title' => 'My View', ), ), ), 1 ); } ); $config = gutenberg_get_entity_view_config( 'custom_kind', 'custom_name' ); $this->assertSame( array( array( 'title' => 'All items', 'slug' => 'all', ), array( 'slug' => 'my_view', 'title' => 'My View', ), ), $config['view_list'] ); } }