PageRenderTime 44ms CodeModel.GetById 15ms RepoModel.GetById 0ms app.codeStats 0ms

/tests/phpunit/includes/testcase-rest-post-type-controller.php

https://gitlab.com/morganestes/wordpress-develop
PHP | 315 lines | 246 code | 47 blank | 22 comment | 52 complexity | 23664725407a422c1e64b268541b4657 MD5 | raw file
  1. <?php
  2. abstract class WP_Test_REST_Post_Type_Controller_Testcase extends WP_Test_REST_Controller_Testcase {
  3. protected function check_post_data( $post, $data, $context, $links ) {
  4. $post_type_obj = get_post_type_object( $post->post_type );
  5. // Standard fields
  6. $this->assertEquals( $post->ID, $data['id'] );
  7. $this->assertEquals( $post->post_name, $data['slug'] );
  8. $this->assertEquals( get_permalink( $post->ID ), $data['link'] );
  9. if ( '0000-00-00 00:00:00' === $post->post_date_gmt ) {
  10. $post_date_gmt = date( 'Y-m-d H:i:s', strtotime( $post->post_date ) - ( get_option( 'gmt_offset' ) * 3600 ) );
  11. $this->assertEquals( mysql_to_rfc3339( $post_date_gmt ), $data['date_gmt'] );
  12. } else {
  13. $this->assertEquals( mysql_to_rfc3339( $post->post_date_gmt ), $data['date_gmt'] );
  14. }
  15. $this->assertEquals( mysql_to_rfc3339( $post->post_date ), $data['date'] );
  16. if ( '0000-00-00 00:00:00' === $post->post_modified_gmt ) {
  17. $post_modified_gmt = date( 'Y-m-d H:i:s', strtotime( $post->post_modified ) - ( get_option( 'gmt_offset' ) * 3600 ) );
  18. $this->assertEquals( mysql_to_rfc3339( $post_modified_gmt ), $data['modified_gmt'] );
  19. } else {
  20. $this->assertEquals( mysql_to_rfc3339( $post->post_modified_gmt ), $data['modified_gmt'] );
  21. }
  22. $this->assertEquals( mysql_to_rfc3339( $post->post_modified ), $data['modified'] );
  23. // author
  24. if ( post_type_supports( $post->post_type, 'author' ) ) {
  25. $this->assertEquals( $post->post_author, $data['author'] );
  26. } else {
  27. $this->assertEmpty( $data['author'] );
  28. }
  29. // post_parent
  30. if ( $post_type_obj->hierarchical ) {
  31. $this->assertArrayHasKey( 'parent', $data );
  32. if ( $post->post_parent ) {
  33. if ( is_int( $data['parent'] ) ) {
  34. $this->assertEquals( $post->post_parent, $data['parent'] );
  35. } else {
  36. $this->assertEquals( $post->post_parent, $data['parent']['id'] );
  37. $this->check_get_post_response( $data['parent'], get_post( $data['parent']['id'] ), 'view-parent' );
  38. }
  39. } else {
  40. $this->assertEmpty( $data['parent'] );
  41. }
  42. } else {
  43. $this->assertFalse( isset( $data['parent'] ) );
  44. }
  45. // page attributes
  46. if ( $post_type_obj->hierarchical && post_type_supports( $post->post_type, 'page-attributes' ) ) {
  47. $this->assertEquals( $post->menu_order, $data['menu_order'] );
  48. } else {
  49. $this->assertFalse( isset( $data['menu_order'] ) );
  50. }
  51. // Comments
  52. if ( post_type_supports( $post->post_type, 'comments' ) ) {
  53. $this->assertEquals( $post->comment_status, $data['comment_status'] );
  54. $this->assertEquals( $post->ping_status, $data['ping_status'] );
  55. } else {
  56. $this->assertFalse( isset( $data['comment_status'] ) );
  57. $this->assertFalse( isset( $data['ping_status'] ) );
  58. }
  59. if ( 'post' === $post->post_type ) {
  60. $this->assertEquals( is_sticky( $post->ID ), $data['sticky'] );
  61. }
  62. if ( 'post' === $post->post_type && 'edit' === $context ) {
  63. $this->assertEquals( $post->post_password, $data['password'] );
  64. }
  65. if ( 'page' === $post->post_type ) {
  66. $this->assertEquals( get_page_template_slug( $post->ID ), $data['template'] );
  67. }
  68. if ( post_type_supports( $post->post_type, 'thumbnail' ) ) {
  69. $this->assertEquals( (int) get_post_thumbnail_id( $post->ID ), $data['featured_media'] );
  70. } else {
  71. $this->assertFalse( isset( $data['featured_media'] ) );
  72. }
  73. // Check post format.
  74. if ( post_type_supports( $post->post_type, 'post-formats' ) ) {
  75. $post_format = get_post_format( $post->ID );
  76. if ( empty( $post_format ) ) {
  77. $this->assertEquals( 'standard', $data['format'] );
  78. } else {
  79. $this->assertEquals( get_post_format( $post->ID ), $data['format'] );
  80. }
  81. } else {
  82. $this->assertFalse( isset( $data['format'] ) );
  83. }
  84. // Check filtered values.
  85. if ( post_type_supports( $post->post_type, 'title' ) ) {
  86. add_filter( 'protected_title_format', array( $this, 'protected_title_format' ) );
  87. $this->assertEquals( get_the_title( $post->ID ), $data['title']['rendered'] );
  88. remove_filter( 'protected_title_format', array( $this, 'protected_title_format' ) );
  89. if ( 'edit' === $context ) {
  90. $this->assertEquals( $post->post_title, $data['title']['raw'] );
  91. } else {
  92. $this->assertFalse( isset( $data['title']['raw'] ) );
  93. }
  94. } else {
  95. $this->assertFalse( isset( $data['title'] ) );
  96. }
  97. if ( post_type_supports( $post->post_type, 'editor' ) ) {
  98. // TODO: apply content filter for more accurate testing.
  99. if ( ! $post->post_password ) {
  100. $this->assertEquals( wpautop( $post->post_content ), $data['content']['rendered'] );
  101. }
  102. if ( 'edit' === $context ) {
  103. $this->assertEquals( $post->post_content, $data['content']['raw'] );
  104. } else {
  105. $this->assertFalse( isset( $data['content']['raw'] ) );
  106. }
  107. } else {
  108. $this->assertFalse( isset( $data['content'] ) );
  109. }
  110. if ( post_type_supports( $post->post_type, 'excerpt' ) ) {
  111. if ( empty( $post->post_password ) ) {
  112. // TODO: apply excerpt filter for more accurate testing.
  113. $this->assertEquals( wpautop( $post->post_excerpt ), $data['excerpt']['rendered'] );
  114. } else {
  115. // TODO: better testing for excerpts for password protected posts.
  116. }
  117. if ( 'edit' === $context ) {
  118. $this->assertEquals( $post->post_excerpt, $data['excerpt']['raw'] );
  119. } else {
  120. $this->assertFalse( isset( $data['excerpt']['raw'] ) );
  121. }
  122. } else {
  123. $this->assertFalse( isset( $data['excerpt'] ) );
  124. }
  125. $this->assertEquals( $post->post_status, $data['status'] );
  126. $this->assertEquals( $post->guid, $data['guid']['rendered'] );
  127. if ( 'edit' === $context ) {
  128. $this->assertEquals( $post->guid, $data['guid']['raw'] );
  129. }
  130. $taxonomies = wp_list_filter( get_object_taxonomies( $post->post_type, 'objects' ), array( 'show_in_rest' => true ) );
  131. foreach ( $taxonomies as $taxonomy ) {
  132. $this->assertTrue( isset( $data[ $taxonomy->rest_base ] ) );
  133. $terms = wp_get_object_terms( $post->ID, $taxonomy->name, array( 'fields' => 'ids' ) );
  134. sort( $terms );
  135. sort( $data[ $taxonomy->rest_base ] );
  136. $this->assertEquals( $terms, $data[ $taxonomy->rest_base ] );
  137. }
  138. // test links
  139. if ( $links ) {
  140. $links = test_rest_expand_compact_links( $links );
  141. $post_type = get_post_type_object( $data['type'] );
  142. $this->assertEquals( $links['self'][0]['href'], rest_url( 'wp/v2/' . $post_type->rest_base . '/' . $data['id'] ) );
  143. $this->assertEquals( $links['collection'][0]['href'], rest_url( 'wp/v2/' . $post_type->rest_base ) );
  144. $this->assertEquals( $links['about'][0]['href'], rest_url( 'wp/v2/types/' . $data['type'] ) );
  145. if ( post_type_supports( $post->post_type, 'author' ) && $data['author'] ) {
  146. $this->assertEquals( $links['author'][0]['href'], rest_url( 'wp/v2/users/' . $data['author'] ) );
  147. }
  148. if ( post_type_supports( $post->post_type, 'comments' ) ) {
  149. $this->assertEquals( $links['replies'][0]['href'], add_query_arg( 'post', $data['id'], rest_url( 'wp/v2/comments' ) ) );
  150. }
  151. if ( post_type_supports( $post->post_type, 'revisions' ) ) {
  152. $this->assertEquals( $links['version-history'][0]['href'], rest_url( 'wp/v2/' . $post_type->rest_base . '/' . $data['id'] . '/revisions' ) );
  153. }
  154. if ( $post_type->hierarchical && ! empty( $data['parent'] ) ) {
  155. $this->assertEquals( $links['up'][0]['href'], rest_url( 'wp/v2/' . $post_type->rest_base . '/' . $data['parent'] ) );
  156. }
  157. if ( ! in_array( $data['type'], array( 'attachment', 'nav_menu_item', 'revision' ), true ) ) {
  158. $this->assertEquals( $links['https://api.w.org/attachment'][0]['href'], add_query_arg( 'parent', $data['id'], rest_url( 'wp/v2/media' ) ) );
  159. }
  160. if ( ! empty( $data['featured_media'] ) ) {
  161. $this->assertEquals( $links['https://api.w.org/featuredmedia'][0]['href'], rest_url( 'wp/v2/media/' . $data['featured_media'] ) );
  162. }
  163. $num = 0;
  164. foreach ( $taxonomies as $key => $taxonomy ) {
  165. $this->assertEquals( $taxonomy->name, $links['https://api.w.org/term'][ $num ]['attributes']['taxonomy'] );
  166. $this->assertEquals( add_query_arg( 'post', $data['id'], rest_url( 'wp/v2/' . $taxonomy->rest_base ) ), $links['https://api.w.org/term'][ $num ]['href'] );
  167. $num++;
  168. }
  169. }
  170. }
  171. protected function check_get_posts_response( $response, $context = 'view' ) {
  172. $this->assertNotWPError( $response );
  173. $response = rest_ensure_response( $response );
  174. $this->assertEquals( 200, $response->get_status() );
  175. $headers = $response->get_headers();
  176. $this->assertArrayHasKey( 'X-WP-Total', $headers );
  177. $this->assertArrayHasKey( 'X-WP-TotalPages', $headers );
  178. $all_data = $response->get_data();
  179. foreach ( $all_data as $data ) {
  180. $post = get_post( $data['id'] );
  181. // as the links for the post are "response_links" format in the data array we have to pull them
  182. // out and parse them.
  183. $links = $data['_links'];
  184. foreach ( $links as &$links_array ) {
  185. foreach ( $links_array as &$link ) {
  186. $attributes = array_diff_key(
  187. $link, array(
  188. 'href' => 1,
  189. 'name' => 1,
  190. )
  191. );
  192. $link = array_diff_key( $link, $attributes );
  193. $link['attributes'] = $attributes;
  194. }
  195. }
  196. $this->check_post_data( $post, $data, $context, $links );
  197. }
  198. }
  199. protected function check_get_post_response( $response, $context = 'view' ) {
  200. $this->assertNotWPError( $response );
  201. $response = rest_ensure_response( $response );
  202. $this->assertEquals( 200, $response->get_status() );
  203. $data = $response->get_data();
  204. $post = get_post( $data['id'] );
  205. $this->check_post_data( $post, $data, $context, $response->get_links() );
  206. }
  207. protected function check_create_post_response( $response ) {
  208. $this->assertNotWPError( $response );
  209. $response = rest_ensure_response( $response );
  210. $this->assertEquals( 201, $response->get_status() );
  211. $headers = $response->get_headers();
  212. $this->assertArrayHasKey( 'Location', $headers );
  213. $data = $response->get_data();
  214. $post = get_post( $data['id'] );
  215. $this->check_post_data( $post, $data, 'edit', $response->get_links() );
  216. }
  217. protected function check_update_post_response( $response ) {
  218. $this->assertNotWPError( $response );
  219. $response = rest_ensure_response( $response );
  220. $this->assertEquals( 200, $response->get_status() );
  221. $headers = $response->get_headers();
  222. $this->assertArrayNotHasKey( 'Location', $headers );
  223. $data = $response->get_data();
  224. $post = get_post( $data['id'] );
  225. $this->check_post_data( $post, $data, 'edit', $response->get_links() );
  226. }
  227. protected function set_post_data( $args = array() ) {
  228. $defaults = array(
  229. 'title' => 'Post Title',
  230. 'content' => 'Post content',
  231. 'excerpt' => 'Post excerpt',
  232. 'name' => 'test',
  233. 'status' => 'publish',
  234. 'author' => get_current_user_id(),
  235. 'type' => 'post',
  236. );
  237. return wp_parse_args( $args, $defaults );
  238. }
  239. protected function set_raw_post_data( $args = array() ) {
  240. return wp_parse_args(
  241. $args, $this->set_post_data(
  242. array(
  243. 'title' => array(
  244. 'raw' => 'Post Title',
  245. ),
  246. 'content' => array(
  247. 'raw' => 'Post content',
  248. ),
  249. 'excerpt' => array(
  250. 'raw' => 'Post excerpt',
  251. ),
  252. )
  253. )
  254. );
  255. }
  256. /**
  257. * Overwrite the default protected title format.
  258. *
  259. * By default WordPress will show password protected posts with a title of
  260. * "Protected: %s", as the REST API communicates the protected status of a post
  261. * in a machine readable format, we remove the "Protected: " prefix.
  262. *
  263. * @return string
  264. */
  265. public function protected_title_format() {
  266. return '%s';
  267. }
  268. }