wpPrivacyRequestsTable.php 4.5 KB

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