| | 1 | <?php |
| | 2 | /** |
| | 3 | * Test the `WP_Privacy_Requests_Table` class. |
| | 4 | * |
| | 5 | * @package WordPress\UnitTests |
| | 6 | * |
| | 7 | * @since 4.9.8 |
| | 8 | */ |
| | 9 | |
| | 10 | /** |
| | 11 | * Tests_Admin_WpPrivacyRequestsTable class. |
| | 12 | * |
| | 13 | * @group admin |
| | 14 | * @group privacy |
| | 15 | * |
| | 16 | * @since 4.9.8 |
| | 17 | */ |
| | 18 | class Tests_Admin_WpPrivacyRequestsTable extends WP_UnitTestCase { |
| | 19 | /** |
| | 20 | * Get instance for mocked class. |
| | 21 | * |
| | 22 | * @since 4.9.8 |
| | 23 | * |
| | 24 | * @return PHPUnit_Framework_MockObject_MockObject|WP_Privacy_Requests_Table $instance Mocked class instance. |
| | 25 | */ |
| | 26 | public function get_mocked_class_instance() { |
| | 27 | $GLOBALS['hook_suffix'] = 'mocked'; |
| | 28 | $instance = $this->getMockForAbstractClass( 'WP_Privacy_Requests_Table' ); |
| | 29 | unset( $GLOBALS['hook_suffix'] ); |
| | 30 | return $instance; |
| | 31 | } |
| | 32 | |
| | 33 | /** |
| | 34 | * Test columns should be sortable. |
| | 35 | * |
| | 36 | * @since 4.9.8 |
| | 37 | * @dataProvider data_test_columns_should_be_sortable |
| | 38 | * @covers WP_Privacy_Requests_Table::prepare_items() |
| | 39 | */ |
| | 40 | public function test_columns_should_be_sortable( $order, $orderby, $search, $expected ) { |
| | 41 | global $wpdb; |
| | 42 | |
| | 43 | $table = $this->get_mocked_class_instance(); |
| | 44 | $this->sql = ''; |
| | 45 | |
| | 46 | $_REQUEST['order'] = $order; |
| | 47 | $_REQUEST['orderby'] = $orderby; |
| | 48 | $_REQUEST['s'] = $search; |
| | 49 | |
| | 50 | add_filter( 'posts_request', array( $this, 'filter_posts_request' ) ); |
| | 51 | $table->prepare_items(); |
| | 52 | remove_filter( 'posts_request', array( $this, 'filter_posts_request' ) ); |
| | 53 | |
| | 54 | unset( $_REQUEST['order'] ); |
| | 55 | unset( $_REQUEST['orderby'] ); |
| | 56 | unset( $_REQUEST['s'] ); |
| | 57 | |
| | 58 | $this->assertContains( "ORDER BY {$wpdb->posts}.{$expected}", $this->sql ); |
| | 59 | } |
| | 60 | |
| | 61 | /** |
| | 62 | * Filter to grab the complete SQL query. |
| | 63 | * |
| | 64 | * @since 4.9.8 |
| | 65 | * |
| | 66 | * @param string $request The complete SQL query. |
| | 67 | */ |
| | 68 | public function filter_posts_request( $request ) { |
| | 69 | $this->sql = $request; |
| | 70 | return $request; |
| | 71 | } |
| | 72 | |
| | 73 | /** |
| | 74 | * Data provider for `test_columns_should_be_sortable()`. |
| | 75 | * |
| | 76 | * @since 4.9.8 |
| | 77 | * |
| | 78 | * @return array { |
| | 79 | * @type array { |
| | 80 | * @type string Order. |
| | 81 | * @type string Order by. |
| | 82 | * @type string Search term. |
| | 83 | * @type string Expected in SQL query. |
| | 84 | * } |
| | 85 | * } |
| | 86 | */ |
| | 87 | public function data_test_columns_should_be_sortable() { |
| | 88 | return array( |
| | 89 | // Default order (ID) DESC. |
| | 90 | array( |
| | 91 | 'order' => null, |
| | 92 | 'orderby' => null, |
| | 93 | 's' => null, |
| | 94 | 'expected' => 'post_date DESC', |
| | 95 | ), |
| | 96 | // Default order (ID) DESC. |
| | 97 | array( |
| | 98 | 'order' => '', |
| | 99 | 'orderby' => '', |
| | 100 | 's' => '', |
| | 101 | 'expected' => 'post_date DESC', |
| | 102 | ), |
| | 103 | // Order by requester (post_title) ASC. |
| | 104 | array( |
| | 105 | 'order' => 'ASC', |
| | 106 | 'orderby' => 'requester', |
| | 107 | 's' => '', |
| | 108 | 'expected' => 'post_title ASC', |
| | 109 | ), |
| | 110 | // Order by requester (post_title) DESC. |
| | 111 | array( |
| | 112 | 'order' => 'DESC', |
| | 113 | 'orderby' => 'requester', |
| | 114 | 's' => null, |
| | 115 | 'expected' => 'post_title DESC', |
| | 116 | ), |
| | 117 | // Order by requested (post_date) ASC. |
| | 118 | array( |
| | 119 | 'order' => 'ASC', |
| | 120 | 'orderby' => 'requested', |
| | 121 | 's' => null, |
| | 122 | 'expected' => 'post_date ASC', |
| | 123 | ), |
| | 124 | // Order by requested (post_date) DESC. |
| | 125 | array( |
| | 126 | 'order' => 'DESC', |
| | 127 | 'orderby' => 'requested', |
| | 128 | 's' => null, |
| | 129 | 'expected' => 'post_date DESC', |
| | 130 | ), |
| | 131 | // Search and order by relevance. |
| | 132 | array( |
| | 133 | 'order' => null, |
| | 134 | 'orderby' => null, |
| | 135 | 's' => 'foo', |
| | 136 | 'expected' => 'post_title LIKE', |
| | 137 | ), |
| | 138 | // Search and order by requester (post_title) ASC. |
| | 139 | array( |
| | 140 | 'order' => 'ASC', |
| | 141 | 'orderby' => 'requester', |
| | 142 | 's' => 'foo', |
| | 143 | 'expected' => 'post_title ASC', |
| | 144 | ), |
| | 145 | // Search and order by requested (post_date) ASC. |
| | 146 | array( |
| | 147 | 'order' => 'ASC', |
| | 148 | 'orderby' => 'requested', |
| | 149 | 's' => 'foo', |
| | 150 | 'expected' => 'post_date ASC', |
| | 151 | ), |
| | 152 | ); |
| | 153 | } |
| | 154 | } |