Skip to content

Commit

Permalink
Use $post argument in check_if_post_state_is_status (exists since 3.6…
Browse files Browse the repository at this point in the history
….0).
  • Loading branch information
Connor Jennings committed Dec 2, 2019
1 parent 3eb2acd commit 459a7e7
Show file tree
Hide file tree
Showing 2 changed files with 165 additions and 4 deletions.
8 changes: 4 additions & 4 deletions modules/custom-status/custom-status.php
Original file line number Diff line number Diff line change
Expand Up @@ -749,16 +749,16 @@ function _filter_manage_posts_custom_column( $column_name ) {
*
* @param array $post_states An array of post display states.
*/
function check_if_post_state_is_status($post_states) {
function check_if_post_state_is_status( $post_states, $post ) {

global $post;
$statuses = get_post_status_object(get_post_status($post->ID));
$statuses = get_post_status_object( get_post_status( $post->ID ) );
foreach ( $post_states as $state ) {
if ( $state !== $statuses->label ) {
echo '<span class="show"></span>';
}
}
return $post_states;

return $post_states;
}

/**
Expand Down
161 changes: 161 additions & 0 deletions tests/test-edit-flow-custom-status.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,45 @@ class WP_Test_Edit_Flow_Custom_Status extends WP_UnitTestCase {

protected static $admin_user_id;
protected static $EF_Custom_Status;
protected static $table_posts = array();

/**
* @var WP_Posts_List_Table
*/
protected $table;

/**
* @var \Walker_Nav_Menu The instance of the walker.
*/
public $walker;

public static function wpSetUpBeforeClass( $factory ) {
self::$admin_user_id = $factory->user->create( array( 'role' => 'administrator' ) );

self::$EF_Custom_Status = new EF_Custom_Status();
self::$EF_Custom_Status->install();
self::$EF_Custom_Status->init();

$post_statuses = ['draft', 'publish', 'future', 'pitch'];

foreach ( $post_statuses as $index => $status ) {
$args = array(
'post_type' => 'post',
'post_title' => sprintf( 'A Post %d', $index ),
'post_status' => $status
);

if ( $status === 'future' ) {
$future_date = strftime( "%Y-%m-%d %H:%M:%S" , strtotime( '+1 day' ) );

$args = array_merge(
$args,
array( 'post_date' => $future_date )
);
}

self::$table_posts[ $status ] = $factory->post->create_and_get($args);
}
}

public static function wpTearDownAfterClass() {
Expand All @@ -21,6 +53,12 @@ public static function wpTearDownAfterClass() {
function setUp() {
parent::setUp();

$this->table = _get_list_table( 'WP_Posts_List_Table', array( 'screen' => 'edit' ) );

/** Walker_Nav_Menu class */
require_once ABSPATH . 'wp-admin/includes/class-walker-nav-menu-checklist.php';
$this->walker = new Walker_Nav_Menu_Checklist();

global $pagenow;
$pagenow = 'post.php';
}
Expand Down Expand Up @@ -332,4 +370,127 @@ public function test_fix_get_sample_permalink_should_respect_hierarchy_of_publis
$this->assertSame( home_url() . '/publish-parent-page/%pagename%/', $actual[0] );
$this->assertSame( 'child-page', $actual[1] );
}

/**
* Post status should be hidden from the post title on "Posts" table when post is "Draft"
*/
public function test_post_state_is_not_shown_when_draft() {
$output = $this->_test_list_hierarchical_page( array(
'paged' => 1,
'posts_per_page' => 1,
'post_status' => 'draft'
) );

$this->assertContains( self::$table_posts[ 'draft' ]->post_title, $output );
$this->assertNotContains( '<span class="show"></span>', $output );
}

/**
* Post status should be hidden from the post title on "Posts" table when post is "Scheduled"
*/
public function test_post_state_is_not_shown_when_scheduled() {
$output = $this->_test_list_hierarchical_page( array(
'paged' => 1,
'posts_per_page' => 1,
'post_status' => 'future'
) );

$this->assertContains( self::$table_posts[ 'future' ]->post_title, $output );
$this->assertNotContains( '<span class="show"></span>', $output );
}

/**
* Post status should be hidden from the post title on "Posts" table when post is "Published"
*/
public function test_post_state_is_not_shown_when_published() {
$output = $this->_test_list_hierarchical_page( array(
'paged' => 1,
'posts_per_page' => 1,
'post_status' => 'publish'
) );

$this->assertContains( self::$table_posts[ 'publish' ]->post_title, $output );
$this->assertNotContains( '<span class="show"></span>', $output );
}

/**
* Post status should be hidden from the post title on "Posts" table when post is "Pitch"
*/
public function test_post_state_is_not_shown_when_pitch() {
$output = $this->_test_list_hierarchical_page( array(
'paged' => 1,
'posts_per_page' => 1,
'post_status' => 'pitch'
) );

$this->assertContains( self::$table_posts[ 'pitch' ]->post_title, $output );
$this->assertNotContains( '<span class="show"></span>', $output );
}

public function test_walker_nav_menu_checklist_title() {
$expected = '';
$post_id = $this->factory->post->create();
$post_title = get_the_title( $post_id );

$item = array(
'ID' => $post_id,
'object_id' => $post_id,
'title' => $post_title,
'menu_item_parent' => null,
'object' => null,
'type' => 'post',
'url' => '',
'attr_title' => '',
'classes' => array(),
'target' => '_blank',
'xfn' => '',
'current' => false,
);

$args = array(
'before' => '',
'after' => '',
'link_before' => '',
'link_after' => '',
);

$this->walker->start_el( $expected, (object) $item, 0, (object) $args );

$this->assertStringStartsWith( "<li><label class=\"menu-item-title\"><input type=\"checkbox\" class=\"menu-item-checkbox\" name=\"menu-item[-1][menu-item-object-id]\" value=\"8\" /> $post_title</label>", $expected );
}

/**
* Helper function to test the output of a page which uses `WP_Posts_List_Table`.
*
* @param array $args Query args for the list of posts.
*/
protected function _test_list_hierarchical_page( array $args ) {
$matches = array();
$_REQUEST['paged'] = $args['paged'];
$GLOBALS['per_page'] = $args['posts_per_page'];
$args = array_merge(
array(
'post_type' => 'post',
),
$args
);
// Mimic the behaviour of `wp_edit_posts_query()`:
if ( ! isset( $args['orderby'] ) ) {
$args['orderby'] = 'menu_order title';
$args['order'] = 'asc';
$args['posts_per_page'] = -1;
$args['posts_per_archive_page'] = -1;
}
$posts = new WP_Query( $args );
ob_start();
$this->table->set_hierarchical_display( true );
$this->table->display_rows( $posts->posts );
$output = ob_get_clean();
// Clean up.
unset( $_REQUEST['paged'] );
unset( $GLOBALS['per_page'] );
preg_match_all( '|<tr[^>]*>|', $output, $matches );

return $output;
}
}

0 comments on commit 459a7e7

Please sign in to comment.