| 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 | 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( $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 4.9.8 |
| 61 | * @dataProvider data_test_columns_should_be_sortable |
| 62 | * @covers WP_Privacy_Requests_Table::prepare_items() |
| 63 | */ |
| 64 | public function test_columns_should_be_sortable( $order, $orderby, $search, $expected ) { |
| 65 | global $wpdb; |
| 66 | |
| 67 | $table = $this->get_mocked_class_instance(); |
| 68 | $this->sql = ''; |
| 69 | |
| 70 | $_REQUEST['order'] = $order; |
| 71 | $_REQUEST['orderby'] = $orderby; |
| 72 | $_REQUEST['s'] = $search; |
| 73 | |
| 74 | add_filter( 'posts_request', array( $this, 'filter_posts_request' ) ); |
| 75 | $table->prepare_items(); |
| 76 | remove_filter( 'posts_request', array( $this, 'filter_posts_request' ) ); |
| 77 | |
| 78 | unset( $_REQUEST['order'] ); |
| 79 | unset( $_REQUEST['orderby'] ); |
| 80 | unset( $_REQUEST['s'] ); |
| 81 | |
| 82 | $this->assertContains( "ORDER BY {$wpdb->posts}.{$expected}", $this->sql ); |
| 83 | } |
| 84 | |
| 85 | /** |
| 86 | * Filter to grab the complete SQL query. |
| 87 | * |
| 88 | * @since 4.9.8 |
| 89 | * |
| 90 | * @param string $request The complete SQL query. |
| 91 | */ |
| 92 | public function filter_posts_request( $request ) { |
| 93 | $this->sql = $request; |
| 94 | return $request; |
| 95 | } |
| 96 | |
| 97 | /** |
| 98 | * Data provider for `test_columns_should_be_sortable()`. |
| 99 | * |
| 100 | * @since 4.9.8 |
| 101 | * |
| 102 | * @return array { |
| 103 | * @type array { |
| 104 | * @type string Order. |
| 105 | * @type string Order by. |
| 106 | * @type string Search term. |
| 107 | * @type string Expected in SQL query. |
| 108 | * } |
| 109 | * } |
| 110 | */ |
| 111 | public function data_test_columns_should_be_sortable() { |
| 112 | return array( |
| 113 | // Default order (ID) DESC. |
| 114 | array( |
| 115 | 'order' => null, |
| 116 | 'orderby' => null, |
| 117 | 's' => null, |
| 118 | 'expected' => 'post_date DESC', |
| 119 | ), |
| 120 | // Default order (ID) DESC. |
| 121 | array( |
| 122 | 'order' => '', |
| 123 | 'orderby' => '', |
| 124 | 's' => '', |
| 125 | 'expected' => 'post_date DESC', |
| 126 | ), |
| 127 | // Order by requester (post_title) ASC. |
| 128 | array( |
| 129 | 'order' => 'ASC', |
| 130 | 'orderby' => 'requester', |
| 131 | 's' => '', |
| 132 | 'expected' => 'post_title ASC', |
| 133 | ), |
| 134 | // Order by requester (post_title) DESC. |
| 135 | array( |
| 136 | 'order' => 'DESC', |
| 137 | 'orderby' => 'requester', |
| 138 | 's' => null, |
| 139 | 'expected' => 'post_title DESC', |
| 140 | ), |
| 141 | // Order by requested (post_date) ASC. |
| 142 | array( |
| 143 | 'order' => 'ASC', |
| 144 | 'orderby' => 'requested', |
| 145 | 's' => null, |
| 146 | 'expected' => 'post_date ASC', |
| 147 | ), |
| 148 | // Order by requested (post_date) DESC. |
| 149 | array( |
| 150 | 'order' => 'DESC', |
| 151 | 'orderby' => 'requested', |
| 152 | 's' => null, |
| 153 | 'expected' => 'post_date DESC', |
| 154 | ), |
| 155 | // Search and order by relevance. |
| 156 | array( |
| 157 | 'order' => null, |
| 158 | 'orderby' => null, |
| 159 | 's' => 'foo', |
| 160 | 'expected' => 'post_title LIKE', |
| 161 | ), |
| 162 | // Search and order by requester (post_title) ASC. |
| 163 | array( |
| 164 | 'order' => 'ASC', |
| 165 | 'orderby' => 'requester', |
| 166 | 's' => 'foo', |
| 167 | 'expected' => 'post_title ASC', |
| 168 | ), |
| 169 | // Search and order by requested (post_date) ASC. |
| 170 | array( |
| 171 | 'order' => 'ASC', |
| 172 | 'orderby' => 'requested', |
| 173 | 's' => 'foo', |
| 174 | 'expected' => 'post_date ASC', |
| 175 | ), |
| 176 | ); |
| 177 | } |
| 178 | } |