From 5294c65267b27ac38cee113b95eb48aca1615d11 Mon Sep 17 00:00:00 2001 From: jjgrainger Date: Thu, 24 Oct 2024 23:29:58 +0100 Subject: [PATCH 1/5] create PostTypeRegistrar class --- src/PostType.php | 200 +----------------------- src/Registrars/PostTypeRegistrar.php | 221 +++++++++++++++++++++++++++ 2 files changed, 223 insertions(+), 198 deletions(-) create mode 100644 src/Registrars/PostTypeRegistrar.php diff --git a/src/PostType.php b/src/PostType.php index 1ba321d..75346eb 100644 --- a/src/PostType.php +++ b/src/PostType.php @@ -3,6 +3,7 @@ namespace PostTypes; use PostTypes\Columns; +use PostTypes\Registrars\PostTypeRegistrar; /** * PostType @@ -215,67 +216,7 @@ public function columns() */ public function register() { - // register the PostType - if (!post_type_exists($this->name)) { - add_action('init', [$this, 'registerPostType']); - } else { - add_filter('register_post_type_args', [$this, 'modifyPostType'], 10, 2); - } - - // register Taxonomies to the PostType - add_action('init', [$this, 'registerTaxonomies']); - - // modify filters on the admin edit screen - add_action('restrict_manage_posts', [$this, 'modifyFilters']); - - if (isset($this->columns)) { - // modify the admin edit columns. - add_filter("manage_{$this->name}_posts_columns", [$this, 'modifyColumns'], 10, 1); - - // populate custom columns - add_filter("manage_{$this->name}_posts_custom_column", [$this, 'populateColumns'], 10, 2); - - // run filter to make columns sortable. - add_filter('manage_edit-'.$this->name.'_sortable_columns', [$this, 'setSortableColumns']); - - // run action that sorts columns on request. - add_action('pre_get_posts', [$this, 'sortSortableColumns']); - } - } - - /** - * Register the PostType - * @return void - */ - public function registerPostType() - { - // create options for the PostType - $options = $this->createOptions(); - - // check that the post type doesn't already exist - if (!post_type_exists($this->name)) { - // register the post type - register_post_type($this->name, $options); - } - } - - /** - * Modify the existing Post Type. - * - * @return array - */ - public function modifyPostType(array $args, string $posttype) - { - if ($posttype !== $this->name) { - return $args; - } - - // create options for the PostType - $options = $this->createOptions(); - - $args = array_replace_recursive($args, $options); - - return $args; + (new PostTypeRegistrar($this))->register(); } /** @@ -380,73 +321,6 @@ public function createLabels() return array_replace_recursive($labels, $this->labels); } - /** - * Register Taxonomies to the PostType - * @return void - */ - public function registerTaxonomies() - { - if (!empty($this->taxonomies)) { - foreach ($this->taxonomies as $taxonomy) { - register_taxonomy_for_object_type($taxonomy, $this->name); - } - } - } - - /** - * Modify and display filters on the admin edit screen - * @param string $posttype The current screen post type - * @return void - */ - public function modifyFilters($posttype) - { - // first check we are working with the this PostType - if ($posttype === $this->name) { - // calculate what filters to add - $filters = $this->getFilters(); - - foreach ($filters as $taxonomy) { - // if the taxonomy doesn't exist, ignore it - if (!taxonomy_exists($taxonomy)) { - continue; - } - - // If the taxonomy is not registered to the post type, continue. - if (!is_object_in_taxonomy($this->name, $taxonomy)) { - continue; - } - - // get the taxonomy object - $tax = get_taxonomy($taxonomy); - - // start the html for the filter dropdown - $selected = null; - - if (isset($_GET[$taxonomy])) { - $selected = sanitize_title($_GET[$taxonomy]); - } - - $dropdown_args = [ - 'name' => $taxonomy, - 'value_field' => 'slug', - 'taxonomy' => $tax->name, - 'show_option_all' => $tax->labels->all_items, - 'hierarchical' => $tax->hierarchical, - 'selected' => $selected, - 'orderby' => 'name', - 'hide_empty' => 0, - 'show_count' => 0, - ]; - - // Output screen reader label. - echo ''; - - // Output dropdown for taxonomy. - wp_dropdown_categories($dropdown_args); - } - } - } - /** * Calculate the filters for the PostType * @return array @@ -470,74 +344,4 @@ public function getFilters() return $filters; } - - /** - * Modify the columns for the PostType - * @param array $columns Default WordPress columns - * @return array The modified columns - */ - public function modifyColumns($columns) - { - $columns = $this->columns->modifyColumns($columns); - - return $columns; - } - - /** - * Populate custom columns for the PostType - * @param string $column The column slug - * @param int $post_id The post ID - */ - public function populateColumns($column, $post_id) - { - if (isset($this->columns->populate[$column])) { - call_user_func_array($this->columns()->populate[$column], [$column, $post_id]); - } - } - - /** - * Make custom columns sortable - * @param array $columns Default WordPress sortable columns - */ - public function setSortableColumns($columns) - { - if (!empty($this->columns()->sortable)) { - $columns = array_merge($columns, $this->columns()->sortable); - } - - return $columns; - } - - /** - * Set query to sort custom columns - * @param WP_Query $query - */ - public function sortSortableColumns($query) - { - // don't modify the query if we're not in the post type admin - if (!is_admin() || $query->get('post_type') !== $this->name) { - return; - } - - $orderby = $query->get('orderby'); - - // if the sorting a custom column - if ($this->columns()->isSortable($orderby)) { - // get the custom column options - $meta = $this->columns()->sortableMeta($orderby); - - // determine type of ordering - if (is_string($meta) or !$meta[1]) { - $meta_key = $meta; - $meta_value = 'meta_value'; - } else { - $meta_key = $meta[0]; - $meta_value = 'meta_value_num'; - } - - // set the custom order - $query->set('meta_key', $meta_key); - $query->set('orderby', $meta_value); - } - } } diff --git a/src/Registrars/PostTypeRegistrar.php b/src/Registrars/PostTypeRegistrar.php new file mode 100644 index 0000000..fb3186b --- /dev/null +++ b/src/Registrars/PostTypeRegistrar.php @@ -0,0 +1,221 @@ +posttype = $posttype; + } + + public function register() + { + // Get the PostType name. + $name = $this->posttype->name; + + // Register the PostType + if (!post_type_exists($name)) { + add_action('init', [$this, 'registerPostType'], 10); + } else { + add_filter('register_post_type_args', [$this, 'modifyPostType'], 10, 2); + } + + // register Taxonomies to the PostType + add_action('init', [$this, 'registerTaxonomies'], 10); + + // modify filters on the admin edit screen + add_action('restrict_manage_posts', [$this, 'modifyFilters'], 10, 1); + + if (isset($this->columns)) { + // modify the admin edit columns. + add_filter('manage_' . $name . '_posts_columns', [$this, 'modifyColumns'], 10, 1); + + // populate custom columns + add_filter('manage_' . $name . '_posts_custom_column', [$this, 'populateColumns'], 10, 2); + + // run filter to make columns sortable. + add_filter('manage_edit-' . $name . '_sortable_columns', [$this, 'setSortableColumns'], 10, 1); + + // run action that sorts columns on request. + add_action('pre_get_posts', [$this, 'sortSortableColumns'], 10, 1); + } + } + + /** + * Register the PostType + * @return void + */ + public function registerPostType() + { + // create options for the PostType + $options = $this->posttype->createOptions(); + + // check that the post type doesn't already exist + if (!post_type_exists($this->posttype->name)) { + // register the post type + register_post_type($this->posttype->name, $options); + } + } + + /** + * Modify the existing Post Type. + * + * @return array + */ + public function modifyPostType(array $args, string $posttype) + { + if ($posttype !== $this->posttype->name) { + return $args; + } + + // create options for the PostType + $options = $this->posttype->createOptions(); + + return array_replace_recursive($args, $options); + } + + /** + * Register Taxonomies to the PostType + * @return void + */ + public function registerTaxonomies() + { + if (empty($this->taxonomies)) { + return; + } + + foreach ($this->posttype->taxonomies as $taxonomy) { + register_taxonomy_for_object_type($taxonomy, $this->posttype->name); + } + } + + /** + * Modify and display filters on the admin edit screen + * @param string $posttype The current screen post type + * @return void + */ + public function modifyFilters($posttype) + { + // first check we are working with the this PostType + if ($posttype === $this->posttype->name) { + // calculate what filters to add + $filters = $this->posttype->getFilters(); + + foreach ($filters as $taxonomy) { + // if the taxonomy doesn't exist, ignore it + if (!taxonomy_exists($taxonomy)) { + continue; + } + + // If the taxonomy is not registered to the post type, continue. + if (!is_object_in_taxonomy($this->posttype->name, $taxonomy)) { + continue; + } + + // get the taxonomy object + $tax = get_taxonomy($taxonomy); + + // start the html for the filter dropdown + $selected = null; + + if (isset($_GET[$taxonomy])) { + $selected = sanitize_title($_GET[$taxonomy]); + } + + $dropdown_args = [ + 'name' => $taxonomy, + 'value_field' => 'slug', + 'taxonomy' => $tax->name, + 'show_option_all' => $tax->labels->all_items, + 'hierarchical' => $tax->hierarchical, + 'selected' => $selected, + 'orderby' => 'name', + 'hide_empty' => 0, + 'show_count' => 0, + ]; + + // Output screen reader label. + sprintf( + '', + $taxonomy, + $tax->labels->filter_by_item + ); + + // Output dropdown for taxonomy. + wp_dropdown_categories($dropdown_args); + } + } + } + + /** + * Modify the columns for the PostType + * @param array $columns Default WordPress columns + * @return array The modified columns + */ + public function modifyColumns($columns) + { + return $this->posttype->columns->modifyColumns($columns); + } + /** + * Populate custom columns for the PostType + * @param string $column The column slug + * @param int $post_id The post ID + */ + public function populateColumns($column, $post_id) + { + if (isset($this->columns->populate[$column])) { + call_user_func_array($this->posttype->columns()->populate[$column], [$column, $post_id]); + } + } + + /** + * Make custom columns sortable + * @param array $columns Default WordPress sortable columns + */ + public function setSortableColumns($columns) + { + if (!empty($this->posttype->columns()->sortable)) { + $columns = array_merge($columns, $this->posttype->columns()->sortable); + } + + return $columns; + } + + /** + * Set query to sort custom columns + * @param WP_Query $query + */ + public function sortSortableColumns($query) + { + // don't modify the query if we're not in the post type admin + if (!is_admin() || $query->get('post_type') !== $this->posttype->name) { + return; + } + + $orderby = $query->get('orderby'); + + // if the sorting a custom column + if ($this->posttype->columns()->isSortable($orderby)) { + // get the custom column options + $meta = $this->posttype->columns()->sortableMeta($orderby); + + // determine type of ordering + if (is_string($meta) or !$meta[1]) { + $meta_key = $meta; + $meta_value = 'meta_value'; + } else { + $meta_key = $meta[0]; + $meta_value = 'meta_value_num'; + } + + // set the custom order + $query->set('meta_key', $meta_key); + $query->set('orderby', $meta_value); + } + } +} From 7e950e78a04fa22bbe88c4f7dcc3f45e7c785ca8 Mon Sep 17 00:00:00 2001 From: jjgrainger Date: Thu, 24 Oct 2024 23:47:31 +0100 Subject: [PATCH 2/5] add PostTypeRegistrar tests --- .editorconfig | 8 ++++++++ src/Registrars/PostTypeRegistrar.php | 6 +++--- tests/Registrars/PostTypeRegistrarTest.php | 24 ++++++++++++++++++++++ 3 files changed, 35 insertions(+), 3 deletions(-) create mode 100644 .editorconfig create mode 100644 tests/Registrars/PostTypeRegistrarTest.php diff --git a/.editorconfig b/.editorconfig new file mode 100644 index 0000000..0763a70 --- /dev/null +++ b/.editorconfig @@ -0,0 +1,8 @@ +root = true + +[*] +charset = utf-8 +end_of_line = lf +insert_final_newline = true +trim_trailing_whitespace = true +indent_style = space diff --git a/src/Registrars/PostTypeRegistrar.php b/src/Registrars/PostTypeRegistrar.php index fb3186b..898e41f 100644 --- a/src/Registrars/PostTypeRegistrar.php +++ b/src/Registrars/PostTypeRegistrar.php @@ -31,7 +31,7 @@ public function register() // modify filters on the admin edit screen add_action('restrict_manage_posts', [$this, 'modifyFilters'], 10, 1); - if (isset($this->columns)) { + if (isset($this->posttype->columns)) { // modify the admin edit columns. add_filter('manage_' . $name . '_posts_columns', [$this, 'modifyColumns'], 10, 1); @@ -85,7 +85,7 @@ public function modifyPostType(array $args, string $posttype) */ public function registerTaxonomies() { - if (empty($this->taxonomies)) { + if (empty($this->posttype->taxonomies)) { return; } @@ -168,7 +168,7 @@ public function modifyColumns($columns) */ public function populateColumns($column, $post_id) { - if (isset($this->columns->populate[$column])) { + if (isset($this->posttype->columns->populate[$column])) { call_user_func_array($this->posttype->columns()->populate[$column], [$column, $post_id]); } } diff --git a/tests/Registrars/PostTypeRegistrarTest.php b/tests/Registrars/PostTypeRegistrarTest.php new file mode 100644 index 0000000..e6a64bd --- /dev/null +++ b/tests/Registrars/PostTypeRegistrarTest.php @@ -0,0 +1,24 @@ + false, + ]); + + $registrar = new PostTypeRegistrar($posttype); + + $options = $registrar->modifyPostType([ + 'public' => true, + ], 'post'); + + $this->assertEquals(false, $options['public']); + } +} From acac67566d57b2fe2c3f05beff3f562c735488a3 Mon Sep 17 00:00:00 2001 From: jjgrainger Date: Fri, 25 Oct 2024 00:27:31 +0100 Subject: [PATCH 3/5] create TaxonomyRegistrar class --- src/Registrars/TaxonomyRegistrar.php | 145 +++++++++++++++++++++++++++ src/Taxonomy.php | 123 +---------------------- 2 files changed, 147 insertions(+), 121 deletions(-) create mode 100644 src/Registrars/TaxonomyRegistrar.php diff --git a/src/Registrars/TaxonomyRegistrar.php b/src/Registrars/TaxonomyRegistrar.php new file mode 100644 index 0000000..d6da048 --- /dev/null +++ b/src/Registrars/TaxonomyRegistrar.php @@ -0,0 +1,145 @@ +taxonomy = $taxonomy; + } + + public function register() + { + // Get the Taxonomy name. + $name = $this->taxonomy->name; + + // Register the taxonomy, set priority to 9 so taxonomies are registered before PostTypes + add_action('init', [$this, 'registerTaxonomy'], 9); + + // Assign taxonomy to post type objects + add_action('init', [$this, 'registerTaxonomyToObjects'], 10); + + if (isset($this->taxonomy->columns)) { + // Modify the columns for the Taxonomy + add_filter("manage_edit-' . $name . '_columns", [$this, 'modifyColumns']); + + // populate the columns for the Taxonomy + add_filter('manage_' . $name . '_custom_column', [$this, 'populateColumns'], 10, 3); + + // set custom sortable columns + add_filter('manage_edit-' . $name . '_sortable_columns', [$this, 'setSortableColumns']); + + // run action that sorts columns on request + add_action('parse_term_query', [$this, 'sortSortableColumns']); + } + } + + /** + * Register the Taxonomy to WordPress + * @return void + */ + public function registerTaxonomy() + { + // Get the existing taxonomy options if it exists. + $options = (taxonomy_exists($this->taxonomy->name)) ? (array) get_taxonomy($this->taxonomy->name) : []; + + // create options for the Taxonomy. + $options = array_replace_recursive($options, $this->taxonomy->createOptions()); + + // register the Taxonomy with WordPress. + register_taxonomy($this->taxonomy->name, null, $options); + } + + /** + * Register the Taxonomy to PostTypes + * @return void + */ + public function registerTaxonomyToObjects() + { + // register Taxonomy to each of the PostTypes assigned + if (empty($this->taxonomy->posttypes)) { + return; + } + + foreach ($this->taxonomy->posttypes as $posttype) { + register_taxonomy_for_object_type($this->taxonomy->name, $posttype); + } + } + + /** + * Modify the columns for the Taxonomy + * @param array $columns The WordPress default columns + * @return array + */ + public function modifyColumns($columns) + { + return $this->taxonomy->columns->modifyColumns($columns); + } + + /** + * Populate custom columns for the Taxonomy + * @param string $content + * @param string $column + * @param int $term_id + */ + public function populateColumns($content, $column, $term_id) + { + if (isset($this->taxonomy->columns->populate[$column])) { + $content = call_user_func_array( + $this->taxonomy->columns()->populate[$column], + [$content, $column, $term_id] + ); + } + + return $content; + } + + /** + * Make custom columns sortable + * @param array $columns Default WordPress sortable columns + */ + public function setSortableColumns($columns) + { + if (!empty($this->taxonomy->columns()->sortable)) { + $columns = array_merge($columns, $this->taxonomy->columns()->sortable); + } + + return $columns; + } + + /** + * Set query to sort custom columns + * @param WP_Term_Query $query + */ + public function sortSortableColumns($query) + { + // don't modify the query if we're not in the post type admin + if (!is_admin() || !in_array($this->taxonomy->name, $query->query_vars['taxonomy'] ?? [])) { + return; + } + + // check the orderby is a custom ordering + if (isset($_GET['orderby']) && array_key_exists($_GET['orderby'], $this->taxonomy->columns()->sortable)) { + // get the custom sorting options + $meta = $this->taxonomy->columns()->sortable[$_GET['orderby']]; + + // check ordering is not numeric + if (is_string($meta)) { + $meta_key = $meta; + $orderby = 'meta_value'; + } else { + $meta_key = $meta[0]; + $orderby = 'meta_value_num'; + } + + // set the sort order + $query->query_vars['orderby'] = $orderby; + $query->query_vars['meta_key'] = $meta_key; + } + } +} diff --git a/src/Taxonomy.php b/src/Taxonomy.php index 1e27f74..a0acde5 100644 --- a/src/Taxonomy.php +++ b/src/Taxonomy.php @@ -3,6 +3,7 @@ namespace PostTypes; use PostTypes\Columns; +use PostTypes\Registrars\TaxonomyRegistrar; /** * Taxonomy @@ -162,56 +163,7 @@ public function columns() */ public function register() { - // register the taxonomy, set priority to 9 - // so taxonomies are registered before PostTypes - add_action('init', [$this, 'registerTaxonomy'], 9); - - // assign taxonomy to post type objects - add_action('init', [$this, 'registerTaxonomyToObjects']); - - if (isset($this->columns)) { - // modify the columns for the Taxonomy - add_filter("manage_edit-{$this->name}_columns", [$this, 'modifyColumns']); - - // populate the columns for the Taxonomy - add_filter("manage_{$this->name}_custom_column", [$this, 'populateColumns'], 10, 3); - - // set custom sortable columns - add_filter("manage_edit-{$this->name}_sortable_columns", [$this, 'setSortableColumns']); - - // run action that sorts columns on request - add_action('parse_term_query', [$this, 'sortSortableColumns']); - } - } - - /** - * Register the Taxonomy to WordPress - * @return void - */ - public function registerTaxonomy() - { - // Get the existing taxonomy options if it exists. - $options = (taxonomy_exists($this->name)) ? (array) get_taxonomy($this->name) : []; - - // create options for the Taxonomy. - $options = array_replace_recursive($options, $this->createOptions()); - - // register the Taxonomy with WordPress. - register_taxonomy($this->name, null, $options); - } - - /** - * Register the Taxonomy to PostTypes - * @return void - */ - public function registerTaxonomyToObjects() - { - // register Taxonomy to each of the PostTypes assigned - if (!empty($this->posttypes)) { - foreach ($this->posttypes as $posttype) { - register_taxonomy_for_object_type($this->name, $posttype); - } - } + (new TaxonomyRegistrar($this))->register(); } /** @@ -310,75 +262,4 @@ public function createLabels() return array_replace($labels, $this->labels); } - - /** - * Modify the columns for the Taxonomy - * @param array $columns The WordPress default columns - * @return array - */ - public function modifyColumns($columns) - { - $columns = $this->columns->modifyColumns($columns); - - return $columns; - } - - /** - * Populate custom columns for the Taxonomy - * @param string $content - * @param string $column - * @param int $term_id - */ - public function populateColumns($content, $column, $term_id) - { - if (isset($this->columns->populate[$column])) { - $content = call_user_func_array($this->columns()->populate[$column], [$content, $column, $term_id]); - } - - return $content; - } - - /** - * Make custom columns sortable - * @param array $columns Default WordPress sortable columns - */ - public function setSortableColumns($columns) - { - if (!empty($this->columns()->sortable)) { - $columns = array_merge($columns, $this->columns()->sortable); - } - - return $columns; - } - - /** - * Set query to sort custom columns - * @param WP_Term_Query $query - */ - public function sortSortableColumns($query) - { - // don't modify the query if we're not in the post type admin - if (!is_admin() || !in_array($this->name, $query->query_vars['taxonomy'] ?? [])) { - return; - } - - // check the orderby is a custom ordering - if (isset($_GET['orderby']) && array_key_exists($_GET['orderby'], $this->columns()->sortable)) { - // get the custom sorting options - $meta = $this->columns()->sortable[$_GET['orderby']]; - - // check ordering is not numeric - if (is_string($meta)) { - $meta_key = $meta; - $orderby = 'meta_value'; - } else { - $meta_key = $meta[0]; - $orderby = 'meta_value_num'; - } - - // set the sort order - $query->query_vars['orderby'] = $orderby; - $query->query_vars['meta_key'] = $meta_key; - } - } } From e37ce1d65468daafddf7a2223bff214765b7a854 Mon Sep 17 00:00:00 2001 From: jjgrainger Date: Fri, 25 Oct 2024 00:27:47 +0100 Subject: [PATCH 4/5] add TaxonomyRegistrarTest --- tests/Registrars/TaxonomyRegistrarTest.php | 17 +++++++++++++++++ 1 file changed, 17 insertions(+) create mode 100644 tests/Registrars/TaxonomyRegistrarTest.php diff --git a/tests/Registrars/TaxonomyRegistrarTest.php b/tests/Registrars/TaxonomyRegistrarTest.php new file mode 100644 index 0000000..3a2494b --- /dev/null +++ b/tests/Registrars/TaxonomyRegistrarTest.php @@ -0,0 +1,17 @@ +assertInstanceOf(TaxonomyRegistrar::class, $registrar); + } +} From 5b812fd7d70daf04e2fbfbb31954accf6e3dfdb1 Mon Sep 17 00:00:00 2001 From: jjgrainger Date: Sat, 16 Nov 2024 11:32:38 +0000 Subject: [PATCH 5/5] improve test coverage --- tests/ColumnsTest.php | 54 ++++++++++++++++++++++++++++++++++++++++-- tests/PostTypeTest.php | 38 +++++++++++++++++++++++++++++ 2 files changed, 90 insertions(+), 2 deletions(-) diff --git a/tests/ColumnsTest.php b/tests/ColumnsTest.php index 9959b67..ce2b217 100644 --- a/tests/ColumnsTest.php +++ b/tests/ColumnsTest.php @@ -32,10 +32,10 @@ public function canSetColumns() } /** @test */ - public function canAddColumns() + public function canAddColumnsWithArray() { $columns = [ - 'genre' => 'Genre' + 'genre' => 'Genre', ]; $this->columns->add($columns); @@ -43,6 +43,22 @@ public function canAddColumns() $this->assertEquals($this->columns->add, $columns); } + /** @test */ + public function canAddColumnsWithArgs() + { + $this->columns->add('genre', 'Genre'); + + // Auto generated label1 + $this->columns->add('price'); + + $expected = [ + 'genre' => 'Genre', + 'price' => 'Price', + ]; + + $this->assertEquals($this->columns->add, $expected); + } + /** @test */ public function canHideColumns() { @@ -209,4 +225,38 @@ public function canModifyColumns() $this->assertEquals($output, $expected); } + + /** @test */ + public function canIdentifySortableColumns() + { + $columns = [ + 'rating' => ['_rating', true], + 'price' => '_price', + 'sortable' => ['sortable'], + ]; + + $this->columns->sortable($columns); + + $this->assertTrue($this->columns->isSortable('_rating')); + $this->assertTrue($this->columns->isSortable('_price')); + $this->assertTrue($this->columns->isSortable('sortable')); + $this->assertFalse($this->columns->isSortable('not_a_column')); + } + + /** @test */ + public function returnsCorrectSortableMetaKey() + { + $columns = [ + 'rating' => ['_rating', true], + 'price' => '_price', + 'column' => ['sortable'], + ]; + + $this->columns->sortable($columns); + + $this->assertEquals($this->columns->sortableMeta('rating'), ['_rating', true]); + $this->assertEquals($this->columns->sortableMeta('_price'), '_price'); + $this->assertEquals($this->columns->sortableMeta('sortable'), ['sortable']); + $this->assertEquals($this->columns->sortableMeta('not_a_column'), ''); + } } diff --git a/tests/PostTypeTest.php b/tests/PostTypeTest.php index 1769bcc..2e3bd91 100644 --- a/tests/PostTypeTest.php +++ b/tests/PostTypeTest.php @@ -159,6 +159,17 @@ public function namesCreatedFromName() $this->assertEquals($this->books->slug, 'books'); } + /** @test */ + public function smartNamesCreatedFromName() + { + $story = new PostType('story'); + + $this->assertEquals($story->name, 'story'); + $this->assertEquals($story->singular, 'Story'); + $this->assertEquals($story->plural, 'Stories'); + $this->assertEquals($story->slug, 'stories'); + } + /** @test */ public function passedNamesAreUsed() { @@ -197,6 +208,33 @@ public function defaultOptionsUsedIfNotSet() $this->assertEquals($options, $defaults); } + /** @test */ + public function optionsGeneratedCorrectly() + { + // Set custom options + $this->books->options([ + 'public' => false, + ]); + + // Set option with helper method + $this->books->icon('dashicon-book-alt'); + + // generated options + $options = $this->books->createOptions(); + + // expected options + $expected = [ + 'public' => false, + 'labels' => $this->books->createLabels(), + 'menu_icon' => $this->books->icon, + 'rewrite' => [ + 'slug' => $this->books->slug + ] + ]; + + $this->assertEquals($options, $expected); + } + /** @test */ public function defaultLabelsAreGenerated() {