| 1 | <?php |
| 2 | /** |
| 3 | * Test the `WP_Privacy_Requests_Table` class. |
| 4 | * |
| 5 | * @package WordPress\UnitTests |
| 6 | * |
| 7 | * @since 5.0.3 |
| 8 | */ |
| 9 | |
| 10 | /** |
| 11 | * Tests_Admin_WpPrivacyRequestsTable class. |
| 12 | * |
| 13 | * @group admin |
| 14 | * @group privacy |
| 15 | * |
| 16 | * @since 5.0.3 |
| 17 | */ |
| 18 | class Tests_Admin_WpPrivacyRequestsTable extends WP_UnitTestCase { |
| 19 | /** |
| 20 | * Get instance for mocked class. |
| 21 | * |
| 22 | * @since 5.0.3 |
| 23 | * |
| 24 | * @return PHPUnit_Framework_MockObject_MockObject|WP_Privacy_Requests_Table $instance Mocked class instance. |
| 25 | */ |
| 26 | public function get_mocked_class_instance() { |
| 27 | if ( version_compare( PHP_VERSION, '5.3', '<' ) ) { |
| 28 | $this->markTestSkipped( 'ReflectionMethod::setAccessible is only available in PHP 5.3+' ); |
| 29 | } |
| 30 | |
| 31 | $args = array( |
| 32 | 'plural' => 'privacy_requests', |
| 33 | 'singular' => 'privacy_request', |
| 34 | 'screen' => 'export_personal_data', |
| 35 | ); |
| 36 | |
| 37 | $instance = $this |
| 38 | ->getMockBuilder( 'WP_Privacy_Requests_Table' ) |
| 39 | ->setConstructorArgs( array( $args ) ) |
| 40 | ->getMockForAbstractClass(); |
| 41 | |
| 42 | $reflection = new ReflectionClass( $instance ); |
| 43 | |
| 44 | // Set the request type as 'export_personal_data'. |
| 45 | $reflection_property = $reflection->getProperty( 'request_type' ); |
| 46 | $reflection_property->setAccessible( true ); |
| 47 | $reflection_property->setValue( $instance, 'export_personal_data' ); |
| 48 | |
| 49 | // Set the post type as 'user_request'. |
| 50 | $reflection_property = $reflection->getProperty( 'post_type' ); |
| 51 | $reflection_property->setAccessible( true ); |
| 52 | $reflection_property->setValue( $instance, 'user_request' ); |
| 53 | |
| 54 | return $instance; |
| 55 | } |
| 56 | |
| 57 | /** |
| 58 | * Test columns should be sortable. |
| 59 | * |
| 60 | * @since 5.0.3 |
| 61 | * |
| 62 | * @param string|null $order Order. |
| 63 | * @param string|null $orderby Order by. |
| 64 | * @param string|null $search Search term. |
| 65 | * @param string $expected Expected in SQL query. |
| 66 | |
| 67 | * @dataProvider data_test_columns_should_be_sortable |
| 68 | * @covers WP_Privacy_Requests_Table::prepare_items() |
| 69 | * @ticket 43960 |
| 70 | */ |
| 71 | public function test_columns_should_be_sortable( $order, $orderby, $search, $expected ) { |
| 72 | global $wpdb; |
| 73 | |
| 74 | $table = $this->get_mocked_class_instance(); |
| 75 | $this->sql = ''; |
| 76 | |
| 77 | $_REQUEST['order'] = $order; |
| 78 | $_REQUEST['orderby'] = $orderby; |
| 79 | $_REQUEST['s'] = $search; |
| 80 | |
| 81 | add_filter( 'posts_request', array( $this, 'filter_posts_request' ) ); |
| 82 | $table->prepare_items(); |
| 83 | remove_filter( 'posts_request', array( $this, 'filter_posts_request' ) ); |
| 84 | |
| 85 | unset( $_REQUEST['order'] ); |
| 86 | unset( $_REQUEST['orderby'] ); |
| 87 | unset( $_REQUEST['s'] ); |
| 88 | |
| 89 | $this->assertContains( "ORDER BY {$wpdb->posts}.{$expected}", $this->sql ); |
| 90 | } |
| 91 | |
| 92 | /** |
| 93 | * Filter to grab the complete SQL query. |
| 94 | * |
| 95 | * @since 5.0.3 |
| 96 | * |
| 97 | * @param string $request The complete SQL query. |
| 98 | * @return string $request The complete SQL query. |
| 99 | */ |
| 100 | public function filter_posts_request( $request ) { |
| 101 | $this->sql = $request; |
| 102 | return $request; |
| 103 | } |
| 104 | |
| 105 | /** |
| 106 | * Data provider for `test_columns_should_be_sortable()`. |
| 107 | * |
| 108 | * @since 5.0.3 |
| 109 | * |
| 110 | * @return array { |
| 111 | * @type array { |
| 112 | * @type string|null Order. |
| 113 | * @type string|null Order by. |
| 114 | * @type string|null Search term. |
| 115 | * @type string Expected in SQL query. |
| 116 | * } |
| 117 | * } |
| 118 | */ |
| 119 | public function data_test_columns_should_be_sortable() { |
| 120 | return array( |
| 121 | // Default order (ID) DESC. |
| 122 | array( |
| 123 | 'order' => null, |
| 124 | 'orderby' => null, |
| 125 | 's' => null, |
| 126 | 'expected' => 'post_date DESC', |
| 127 | ), |
| 128 | // Default order (ID) DESC. |
| 129 | array( |
| 130 | 'order' => '', |
| 131 | 'orderby' => '', |
| 132 | 's' => '', |
| 133 | 'expected' => 'post_date DESC', |
| 134 | ), |
| 135 | // Order by requester (post_title) ASC. |
| 136 | array( |
| 137 | 'order' => 'ASC', |
| 138 | 'orderby' => 'requester', |
| 139 | 's' => '', |
| 140 | 'expected' => 'post_title ASC', |
| 141 | ), |
| 142 | // Order by requester (post_title) DESC. |
| 143 | array( |
| 144 | 'order' => 'DESC', |
| 145 | 'orderby' => 'requester', |
| 146 | 's' => null, |
| 147 | 'expected' => 'post_title DESC', |
| 148 | ), |
| 149 | // Order by requested (post_date) ASC. |
| 150 | array( |
| 151 | 'order' => 'ASC', |
| 152 | 'orderby' => 'requested', |
| 153 | 's' => null, |
| 154 | 'expected' => 'post_date ASC', |
| 155 | ), |
| 156 | // Order by requested (post_date) DESC. |
| 157 | array( |
| 158 | 'order' => 'DESC', |
| 159 | 'orderby' => 'requested', |
| 160 | 's' => null, |
| 161 | 'expected' => 'post_date DESC', |
| 162 | ), |
| 163 | // Search and order by relevance. |
| 164 | array( |
| 165 | 'order' => null, |
| 166 | 'orderby' => null, |
| 167 | 's' => 'foo', |
| 168 | 'expected' => 'post_title LIKE', |
| 169 | ), |
| 170 | // Search and order by requester (post_title) ASC. |
| 171 | array( |
| 172 | 'order' => 'ASC', |
| 173 | 'orderby' => 'requester', |
| 174 | 's' => 'foo', |
| 175 | 'expected' => 'post_title ASC', |
| 176 | ), |
| 177 | // Search and order by requested (post_date) ASC. |
| 178 | array( |
| 179 | 'order' => 'ASC', |
| 180 | 'orderby' => 'requested', |
| 181 | 's' => 'foo', |
| 182 | 'expected' => 'post_date ASC', |
| 183 | ), |
| 184 | ); |
| 185 | } |
| 186 | } |