Make WordPress Core

Ticket #16502: 16502-tests.diff

File 16502-tests.diff, 4.4 KB (added by costdev, 3 years ago)

PHPUnit tests.

  • tests/phpunit/tests/admin/wpPostsListTable.php

    diff --git a/tests/phpunit/tests/admin/wpPostsListTable.php b/tests/phpunit/tests/admin/wpPostsListTable.php
    index c209179870..11b4b4a078 100644
    a b class Tests_Admin_wpPostsListTable extends WP_UnitTestCase { 
    341341                $this->assertSame( $expected, $actual );
    342342        }
    343343
     344        /**
     345         * Tests that `WP_Posts_List_Table::handle_row_actions()` conditionally renders
     346         * Quick Edit markup depending on the presence of the 'inline hide-if-no-js' action.
     347         *
     348         * @ticket 16502
     349         *
     350         * @covers WP_Posts_List_Table::handle_row_actions
     351         *
     352         * @dataProvider data_post_types
     353         *
     354         * @param bool   $has_inline_action Whether the 'inline hide-if-no-js' should be present.
     355         * @param bool   $expected          Whether the Quick Edit markup should be present.
     356         * @param string $post_type         The post type to test.
     357         * @param bool   $hierarchical      Optional. Whether the post type is hierarchical.
     358         *                                  For custom post types only.
     359         *                                  If hierarchical, the 'page_row_actions' hook is used,
     360         *                                  otherwise 'post_row_actions'.
     361         *                                  Default false.
     362         */
     363        public function test_handle_row_actions_should_conditionally_render_quick_edit_markup( $has_inline_action, $expected, $post_type, $hierarchical = false ) {
     364                $hook = 'page' === $post_type || $hierarchical ? 'page_row_actions' : 'post_row_actions';
     365                add_filter(
     366                        $hook,
     367                        static function( $actions ) use ( $has_inline_action ) {
     368                                if ( $has_inline_action ) {
     369                                        $actions['inline hide-if-no-js'] = 'Should render';
     370                                } else {
     371                                        unset( $actions['inline hide-if-no-js'] );
     372                                }
     373                                return $actions;
     374                        }
     375                );
     376
     377                if ( str_contains( $post_type, 'cpt' ) ) {
     378                        register_post_type(
     379                                $post_type,
     380                                array(
     381                                        'labels'       => array( 'name' => $post_type ),
     382                                        'hierarchical' => $hierarchical,
     383                                )
     384                        );
     385                }
     386
     387                $post = self::factory()->post->create_and_get(
     388                        array(
     389                                'post_title' => 'Post Title',
     390                                'post_type'  => $post_type,
     391                        )
     392                );
     393
     394                $handle_row_actions = new ReflectionMethod( $this->table, 'handle_row_actions' );
     395
     396                $handle_row_actions->setAccessible( true );
     397                $actual = $handle_row_actions->invokeArgs( $this->table, array( $post, 'primary', 'primary' ) );
     398                $handle_row_actions->setAccessible( false );
     399
     400                if ( $expected ) {
     401                        $this->assertStringContainsString( 'inline hide-if-no-js', $actual );
     402                } else {
     403                        $this->assertStringNotContainsString( 'inline hide-if-no-js', $actual );
     404                }
     405        }
     406
     407        /**
     408         * Data provider.
     409         *
     410         * @return array[]
     411         */
     412        public function data_post_types() {
     413                return array(
     414                        'a post with the "inline hide-if-no-js" action'    => array(
     415                                'has_inline_action' => true,
     416                                'expected'          => true,
     417                                'post_type'         => 'post',
     418                        ),
     419                        'a page with the "inline hide-if-no-js" action'    => array(
     420                                'has_inline_action' => true,
     421                                'expected'          => true,
     422                                'post_type'         => 'page',
     423                        ),
     424                        'a hierarchical custom post type with the "inline hide-if-no-js" action' => array(
     425                                'has_inline_action' => true,
     426                                'expected'          => true,
     427                                'post_type'         => 'my_cpt',
     428                                'hierarchical'      => true,
     429                        ),
     430                        'a non-hierarchical custom post type with the "inline hide-if-no-js" action' => array(
     431                                'has_inline_action' => true,
     432                                'expected'          => true,
     433                                'post_type'         => 'my_cpt',
     434                                'hierarchical'      => false,
     435                        ),
     436                        'a post without the "inline hide-if-no-js" action' => array(
     437                                'has_inline_action' => false,
     438                                'expected'          => false,
     439                                'post_type'         => 'post',
     440                        ),
     441                        'a page without the "inline hide-if-no-js" action' => array(
     442                                'has_inline_action' => false,
     443                                'expected'          => false,
     444                                'post_type'         => 'page',
     445                        ),
     446                        'a hierarchical custom post type without the "inline hide-if-no-js" action' => array(
     447                                'has_inline_action' => false,
     448                                'expected'          => false,
     449                                'post_type'         => 'my_cpt',
     450                                'hierarchical'      => true,
     451                        ),
     452                        'a non-hierarchical custom post type without the "inline hide-if-no-js" action' => array(
     453                                'has_inline_action' => false,
     454                                'expected'          => false,
     455                                'post_type'         => 'my_cpt',
     456                                'hierarchical'      => false,
     457                        ),
     458                );
     459        }
     460
    344461}