getPageByPath.php 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320
  1. <?php
  2. /**
  3. * @group post
  4. */
  5. class Tests_Post_GetPageByPath extends WP_UnitTestCase {
  6. /**
  7. * @ticket 15665
  8. */
  9. public function test_get_page_by_path_priority() {
  10. global $wpdb;
  11. $attachment = self::factory()->post->create_and_get(
  12. array(
  13. 'post_title' => 'some-page',
  14. 'post_type' => 'attachment',
  15. )
  16. );
  17. $page = self::factory()->post->create_and_get(
  18. array(
  19. 'post_title' => 'some-page',
  20. 'post_type' => 'page',
  21. )
  22. );
  23. $other_att = self::factory()->post->create_and_get(
  24. array(
  25. 'post_title' => 'some-other-page',
  26. 'post_type' => 'attachment',
  27. )
  28. );
  29. $wpdb->update( $wpdb->posts, array( 'post_name' => 'some-page' ), array( 'ID' => $page->ID ) );
  30. clean_post_cache( $page->ID );
  31. $page = get_post( $page->ID );
  32. $this->assertSame( 'some-page', $attachment->post_name );
  33. $this->assertSame( 'some-page', $page->post_name );
  34. // get_page_by_path() should return a post of the requested type before returning an attachment.
  35. $this->assertEquals( $page, get_page_by_path( 'some-page' ) );
  36. // Make sure get_page_by_path() will still select an attachment when a post of the requested type doesn't exist.
  37. $this->assertEquals( $other_att, get_page_by_path( 'some-other-page' ) );
  38. }
  39. public function test_should_match_top_level_page() {
  40. $page = self::factory()->post->create(
  41. array(
  42. 'post_type' => 'page',
  43. 'post_name' => 'foo',
  44. )
  45. );
  46. $found = get_page_by_path( 'foo' );
  47. $this->assertSame( $page, $found->ID );
  48. }
  49. public function test_should_obey_post_type() {
  50. register_post_type( 'wptests_pt' );
  51. $page = self::factory()->post->create(
  52. array(
  53. 'post_type' => 'wptests_pt',
  54. 'post_name' => 'foo',
  55. )
  56. );
  57. $found = get_page_by_path( 'foo' );
  58. $this->assertNull( $found );
  59. $found = get_page_by_path( 'foo', OBJECT, 'wptests_pt' );
  60. $this->assertSame( $page, $found->ID );
  61. }
  62. public function test_should_match_nested_page() {
  63. $p1 = self::factory()->post->create(
  64. array(
  65. 'post_type' => 'page',
  66. 'post_name' => 'foo',
  67. )
  68. );
  69. $p2 = self::factory()->post->create(
  70. array(
  71. 'post_type' => 'page',
  72. 'post_name' => 'bar',
  73. 'post_parent' => $p1,
  74. )
  75. );
  76. $p3 = self::factory()->post->create(
  77. array(
  78. 'post_type' => 'page',
  79. 'post_name' => 'baz',
  80. 'post_parent' => $p2,
  81. )
  82. );
  83. $found = get_page_by_path( 'foo/bar/baz' );
  84. $this->assertSame( $p3, $found->ID );
  85. }
  86. public function test_should_not_make_partial_match() {
  87. $p1 = self::factory()->post->create(
  88. array(
  89. 'post_type' => 'page',
  90. 'post_name' => 'foo',
  91. )
  92. );
  93. $p2 = self::factory()->post->create(
  94. array(
  95. 'post_type' => 'page',
  96. 'post_name' => 'bar',
  97. 'post_parent' => $p1,
  98. )
  99. );
  100. $p3 = self::factory()->post->create(
  101. array(
  102. 'post_type' => 'page',
  103. 'post_name' => 'baz',
  104. 'post_parent' => $p2,
  105. )
  106. );
  107. $found = get_page_by_path( 'bar/baz' );
  108. $this->assertNull( $found );
  109. }
  110. public function test_should_not_match_parts_out_of_order() {
  111. $p1 = self::factory()->post->create(
  112. array(
  113. 'post_type' => 'page',
  114. 'post_name' => 'foo',
  115. )
  116. );
  117. $p2 = self::factory()->post->create(
  118. array(
  119. 'post_type' => 'page',
  120. 'post_name' => 'bar',
  121. 'post_parent' => $p1,
  122. )
  123. );
  124. $p3 = self::factory()->post->create(
  125. array(
  126. 'post_type' => 'page',
  127. 'post_name' => 'baz',
  128. 'post_parent' => $p2,
  129. )
  130. );
  131. $found = get_page_by_path( 'bar/foo/baz' );
  132. $this->assertNull( $found );
  133. }
  134. /**
  135. * @ticket 36711
  136. */
  137. public function test_should_hit_cache() {
  138. global $wpdb;
  139. $page = self::factory()->post->create(
  140. array(
  141. 'post_type' => 'page',
  142. 'post_name' => 'foo',
  143. )
  144. );
  145. // Prime cache.
  146. $found = get_page_by_path( 'foo' );
  147. $this->assertSame( $page, $found->ID );
  148. $num_queries = $wpdb->num_queries;
  149. $found = get_page_by_path( 'foo' );
  150. $this->assertSame( $page, $found->ID );
  151. $this->assertSame( $num_queries, $wpdb->num_queries );
  152. }
  153. /**
  154. * @ticket 36711
  155. */
  156. public function test_bad_path_should_be_cached() {
  157. global $wpdb;
  158. // Prime cache.
  159. $found = get_page_by_path( 'foo' );
  160. $this->assertNull( $found );
  161. $num_queries = $wpdb->num_queries;
  162. $found = get_page_by_path( 'foo' );
  163. $this->assertNull( $found );
  164. $this->assertSame( $num_queries, $wpdb->num_queries );
  165. }
  166. /**
  167. * @ticket 36711
  168. */
  169. public function test_bad_path_served_from_cache_should_not_fall_back_on_current_post() {
  170. global $wpdb, $post;
  171. // Fake the global.
  172. $post = self::factory()->post->create_and_get();
  173. // Prime cache.
  174. $found = get_page_by_path( 'foo' );
  175. $this->assertNull( $found );
  176. $num_queries = $wpdb->num_queries;
  177. $found = get_page_by_path( 'foo' );
  178. $this->assertNull( $found );
  179. $this->assertSame( $num_queries, $wpdb->num_queries );
  180. unset( $post );
  181. }
  182. /**
  183. * @ticket 36711
  184. */
  185. public function test_cache_should_not_match_post_in_different_post_type_with_same_path() {
  186. global $wpdb;
  187. register_post_type( 'wptests_pt' );
  188. $p1 = self::factory()->post->create(
  189. array(
  190. 'post_type' => 'page',
  191. 'post_name' => 'foo',
  192. )
  193. );
  194. $p2 = self::factory()->post->create(
  195. array(
  196. 'post_type' => 'wptests_pt',
  197. 'post_name' => 'foo',
  198. )
  199. );
  200. // Prime cache for the page.
  201. $found = get_page_by_path( 'foo' );
  202. $this->assertSame( $p1, $found->ID );
  203. $num_queries = $wpdb->num_queries;
  204. $found = get_page_by_path( 'foo', OBJECT, 'wptests_pt' );
  205. $this->assertSame( $p2, $found->ID );
  206. $num_queries++;
  207. $this->assertSame( $num_queries, $wpdb->num_queries );
  208. }
  209. /**
  210. * @ticket 36711
  211. */
  212. public function test_cache_should_be_invalidated_when_post_name_is_edited() {
  213. global $wpdb;
  214. $page = self::factory()->post->create(
  215. array(
  216. 'post_type' => 'page',
  217. 'post_name' => 'foo',
  218. )
  219. );
  220. // Prime cache.
  221. $found = get_page_by_path( 'foo' );
  222. $this->assertSame( $page, $found->ID );
  223. wp_update_post(
  224. array(
  225. 'ID' => $page,
  226. 'post_name' => 'bar',
  227. )
  228. );
  229. $num_queries = $wpdb->num_queries;
  230. $found = get_page_by_path( 'bar' );
  231. $this->assertSame( $page, $found->ID );
  232. $num_queries++;
  233. $this->assertSame( $num_queries, $wpdb->num_queries );
  234. }
  235. /**
  236. * @ticket 37611
  237. */
  238. public function test_output_param_should_be_obeyed_for_cached_value() {
  239. $page = self::factory()->post->create(
  240. array(
  241. 'post_type' => 'page',
  242. 'post_name' => 'foo',
  243. )
  244. );
  245. // Prime cache.
  246. $found = get_page_by_path( 'foo' );
  247. $this->assertSame( $page, $found->ID );
  248. $object = get_page_by_path( 'foo', OBJECT );
  249. $this->assertInternalType( 'object', $object );
  250. $this->assertSame( $page, $object->ID );
  251. $array_n = get_page_by_path( 'foo', ARRAY_N );
  252. $this->assertInternalType( 'array', $array_n );
  253. $this->assertSame( $page, $array_n[0] );
  254. $array_a = get_page_by_path( 'foo', ARRAY_A );
  255. $this->assertInternalType( 'array', $array_a );
  256. $this->assertSame( $page, $array_a['ID'] );
  257. }
  258. }