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