/tests/wpunit/NodeByUriTest.php

https://github.com/wp-graphql/wp-graphql · PHP · 524 lines · 394 code · 99 blank · 31 comment · 0 complexity · f6689969ea1a6dbb724a217206b013f6 MD5 · raw file

  1. <?php
  2. class NodeByUriTest extends \Codeception\TestCase\WPTestCase {
  3. public $post;
  4. public $page;
  5. public $user;
  6. public $tag;
  7. public $category;
  8. public $custom_type;
  9. public $custom_taxonomy;
  10. public function setUp(): void {
  11. WPGraphQL::clear_schema();
  12. register_post_type('custom_type', [
  13. 'show_in_graphql' => true,
  14. 'graphql_single_name' => 'CustomType',
  15. 'graphql_plural_name' => 'CustomTypes',
  16. 'public' => true,
  17. ]);
  18. register_taxonomy( 'custom_tax', 'custom_type', [
  19. 'show_in_graphql' => true,
  20. 'graphql_single_name' => 'CustomTax',
  21. 'graphql_plural_name' => 'CustomTaxes',
  22. ]);
  23. $this->set_permalink_structure( '/%year%/%monthnum%/%day%/%postname%/' );
  24. $this->user = $this->factory()->user->create([
  25. 'role' => 'administrator',
  26. ]);
  27. $this->tag = $this->factory()->term->create([
  28. 'taxonomy' => 'post_tag',
  29. ]);
  30. $this->category = $this->factory()->term->create([
  31. 'taxonomy' => 'category',
  32. ]);
  33. $this->custom_taxonomy = $this->factory()->term->create([
  34. 'taxonomy' => 'custom_tax',
  35. ]);
  36. $this->post = $this->factory()->post->create( [
  37. 'post_type' => 'post',
  38. 'post_status' => 'publish',
  39. 'post_title' => 'Test',
  40. 'post_author' => $this->user,
  41. ] );
  42. $this->page = $this->factory()->post->create( [
  43. 'post_type' => 'page',
  44. 'post_status' => 'publish',
  45. 'post_title' => 'Test Page',
  46. 'post_author' => $this->user
  47. ] );
  48. $this->custom_type = $this->factory()->post->create( [
  49. 'post_type' => 'custom_type',
  50. 'post_status' => 'publish',
  51. 'post_title' => 'Test Page',
  52. 'post_author' => $this->user
  53. ] );
  54. parent::setUp();
  55. }
  56. public function tearDown(): void {
  57. unregister_post_type( 'custom_type' );
  58. WPGraphQL::clear_schema();
  59. $this->set_permalink_structure( '/%year%/%monthnum%/%day%/%postname%/' );
  60. parent::tearDown();
  61. wp_delete_post( $this->post );
  62. wp_delete_post( $this->page );
  63. wp_delete_post( $this->custom_post_type );
  64. wp_delete_term( $this->tag, 'post_tag' );
  65. wp_delete_term( $this->category, 'category' );
  66. wp_delete_term( $this->custom_taxonomy, 'custom_tax' );
  67. wp_delete_user( $this->user );
  68. }
  69. public function set_permalink_structure( $structure = '' ) {
  70. global $wp_rewrite;
  71. $wp_rewrite->init();
  72. $wp_rewrite->set_permalink_structure( $structure );
  73. $wp_rewrite->flush_rules( true );
  74. }
  75. /**
  76. * Get a Post by it's permalink
  77. * @throws Exception
  78. */
  79. public function testPostByUri() {
  80. $query = '
  81. query GET_NODE_BY_URI( $uri: String! ) {
  82. nodeByUri( uri: $uri ) {
  83. __typename
  84. ...on Post {
  85. postId
  86. }
  87. }
  88. }
  89. ';
  90. codecept_debug( get_permalink( $this->post ) );
  91. $actual = graphql([
  92. 'query' => $query,
  93. 'variables' => [
  94. 'uri' => get_permalink( $this->post ),
  95. ],
  96. ]);
  97. codecept_debug( $actual );
  98. $this->assertArrayNotHasKey( 'Errors', $actual );
  99. $this->assertSame( ucfirst( get_post_type_object( 'post' )->graphql_single_name ), $actual['data']['nodeByUri']['__typename'] );
  100. $this->assertSame( $this->post, $actual['data']['nodeByUri']['postId'] );
  101. $this->set_permalink_structure( '' );
  102. $actual = graphql([
  103. 'query' => $query,
  104. 'variables' => [
  105. 'uri' => get_permalink( $this->post ),
  106. ],
  107. ]);
  108. codecept_debug( get_permalink( $this->post ) );
  109. codecept_debug( $actual );
  110. $this->assertArrayNotHasKey( 'Errors', $actual );
  111. $this->assertSame( ucfirst( get_post_type_object( 'post' )->graphql_single_name ), $actual['data']['nodeByUri']['__typename'] );
  112. $this->assertSame( $this->post, $actual['data']['nodeByUri']['postId'] );
  113. }
  114. /**
  115. * @throws Exception
  116. */
  117. function testPageByUri() {
  118. $query = '
  119. query GET_NODE_BY_URI( $uri: String! ) {
  120. nodeByUri( uri: $uri ) {
  121. __typename
  122. ...on Page {
  123. pageId
  124. }
  125. }
  126. }
  127. ';
  128. $actual = graphql([
  129. 'query' => $query,
  130. 'variables' => [
  131. 'uri' => get_permalink( $this->page ),
  132. ],
  133. ]);
  134. codecept_debug( $actual );
  135. $this->assertArrayNotHasKey( 'Errors', $actual );
  136. $this->assertSame( ucfirst( get_post_type_object( 'page' )->graphql_single_name ), $actual['data']['nodeByUri']['__typename'] );
  137. $this->assertSame( $this->page, $actual['data']['nodeByUri']['pageId'] );
  138. $this->set_permalink_structure( '' );
  139. $actual = graphql([
  140. 'query' => $query,
  141. 'variables' => [
  142. 'uri' => get_permalink( $this->page ),
  143. ],
  144. ]);
  145. codecept_debug( get_permalink( $this->page ) );
  146. codecept_debug( $actual );
  147. $this->assertArrayNotHasKey( 'Errors', $actual );
  148. $this->assertSame( ucfirst( get_post_type_object( 'page' )->graphql_single_name ), $actual['data']['nodeByUri']['__typename'] );
  149. $this->assertSame( $this->page, $actual['data']['nodeByUri']['pageId'] );
  150. }
  151. /**
  152. * @throws Exception
  153. */
  154. function testCustomPostTypeByUri() {
  155. codecept_debug( get_post( $this->custom_type ) );
  156. $query = '
  157. query GET_NODE_BY_URI( $uri: String! ) {
  158. nodeByUri( uri: $uri ) {
  159. __typename
  160. ...on CustomType {
  161. customTypeId
  162. }
  163. }
  164. }
  165. ';
  166. flush_rewrite_rules( true );
  167. $actual = graphql([
  168. 'query' => $query,
  169. 'variables' => [
  170. 'uri' => get_permalink( $this->custom_type ),
  171. ],
  172. ]);
  173. codecept_debug( get_permalink( $this->custom_type ) );
  174. codecept_debug( $actual );
  175. $this->assertArrayNotHasKey( 'Errors', $actual );
  176. $this->assertSame( ucfirst( get_post_type_object( 'custom_type' )->graphql_single_name ), $actual['data']['nodeByUri']['__typename'] );
  177. $this->assertSame( $this->custom_type, $actual['data']['nodeByUri']['customTypeId'] );
  178. $this->set_permalink_structure( '' );
  179. $actual = graphql([
  180. 'query' => $query,
  181. 'variables' => [
  182. 'uri' => get_permalink( $this->custom_type ),
  183. ],
  184. ]);
  185. codecept_debug( get_permalink( $this->page ) );
  186. codecept_debug( $actual );
  187. $this->assertArrayNotHasKey( 'Errors', $actual );
  188. $this->assertSame( ucfirst( get_post_type_object( 'custom_type' )->graphql_single_name ), $actual['data']['nodeByUri']['__typename'] );
  189. $this->assertSame( $this->custom_type, $actual['data']['nodeByUri']['customTypeId'] );
  190. }
  191. /**
  192. * @throws Exception
  193. */
  194. function testCategoryByUri() {
  195. $query = '
  196. query GET_NODE_BY_URI( $uri: String! ) {
  197. nodeByUri( uri: $uri ) {
  198. __typename
  199. ...on Category {
  200. categoryId
  201. }
  202. }
  203. }
  204. ';
  205. codecept_debug( get_term_link( $this->category ) );
  206. $actual = graphql([
  207. 'query' => $query,
  208. 'variables' => [
  209. 'uri' => get_term_link( $this->category ),
  210. ],
  211. ]);
  212. codecept_debug( $actual );
  213. $this->assertArrayNotHasKey( 'Errors', $actual );
  214. $this->assertSame( ucfirst( get_taxonomy( 'category' )->graphql_single_name ), $actual['data']['nodeByUri']['__typename'] );
  215. $this->assertSame( $this->category, $actual['data']['nodeByUri']['categoryId'] );
  216. }
  217. /**
  218. * @throws Exception
  219. */
  220. function testTagByUri() {
  221. $query = '
  222. query GET_NODE_BY_URI( $uri: String! ) {
  223. nodeByUri( uri: $uri ) {
  224. __typename
  225. ...on Tag {
  226. tagId
  227. }
  228. }
  229. }
  230. ';
  231. $actual = graphql([
  232. 'query' => $query,
  233. 'variables' => [
  234. 'uri' => get_term_link( $this->tag ),
  235. ],
  236. ]);
  237. codecept_debug( $actual );
  238. $this->assertArrayNotHasKey( 'Errors', $actual );
  239. $this->assertSame( ucfirst( get_taxonomy( 'post_tag' )->graphql_single_name ), $actual['data']['nodeByUri']['__typename'] );
  240. $this->assertSame( $this->tag, $actual['data']['nodeByUri']['tagId'] );
  241. }
  242. /**
  243. * @throws Exception
  244. */
  245. function testCustomTaxTermByUri() {
  246. $query = '
  247. query GET_NODE_BY_URI( $uri: String! ) {
  248. nodeByUri( uri: $uri ) {
  249. __typename
  250. ...on CustomTax {
  251. customTaxId
  252. }
  253. }
  254. }
  255. ';
  256. $actual = graphql([
  257. 'query' => $query,
  258. 'variables' => [
  259. 'uri' => get_term_link( $this->custom_taxonomy ),
  260. ],
  261. ]);
  262. codecept_debug( $actual );
  263. $this->assertArrayNotHasKey( 'Errors', $actual );
  264. $this->assertSame( ucfirst( get_taxonomy( 'custom_tax' )->graphql_single_name ), $actual['data']['nodeByUri']['__typename'] );
  265. $this->assertSame( $this->custom_taxonomy, $actual['data']['nodeByUri']['customTaxId'] );
  266. }
  267. /**
  268. * @throws Exception
  269. */
  270. public function testHomePageByUri() {
  271. $title = 'Home Test' . uniqid();
  272. $post_id = $this->factory()->post->create([
  273. 'post_type' => 'page',
  274. 'post_status' => 'publish',
  275. 'post_title' => $title
  276. ]);
  277. $query = '
  278. {
  279. nodeByUri(uri: "/") {
  280. __typename
  281. uri
  282. ... on Page {
  283. title
  284. isPostsPage
  285. isFrontPage
  286. }
  287. ... on ContentType {
  288. name
  289. isPostsPage
  290. isFrontPage
  291. }
  292. }
  293. }
  294. ';
  295. update_option( 'page_on_front', 0 );
  296. update_option( 'page_for_posts', 0 );
  297. update_option( 'show_on_front', 0 );
  298. $actual = graphql([ 'query' => $query ]);
  299. codecept_debug( $actual );
  300. // When the page_on_front, page_for_posts and show_on_front are all not set, the `/` uri should return
  301. // the post ContentType as the homepage node
  302. $this->assertArrayNotHasKey( 'errors', $actual );
  303. $this->assertNotNull( $actual['data']['nodeByUri'] );
  304. $this->assertSame( '/', $actual['data']['nodeByUri']['uri'] );
  305. $this->assertSame( 'ContentType', $actual['data']['nodeByUri']['__typename'] );
  306. $this->assertTrue( $actual['data']['nodeByUri']['isPostsPage'] );
  307. $this->assertTrue( $actual['data']['nodeByUri']['isFrontPage'] );
  308. // if the "show_on_front" is set to page, but no page is specifically set, the
  309. // homepage should still be the Post ContentType
  310. update_option( 'show_on_front', 'page' );
  311. $actual = graphql([ 'query' => $query ]);
  312. $this->assertArrayNotHasKey( 'errors', $actual );
  313. $this->assertNotNull( $actual['data']['nodeByUri'] );
  314. $this->assertSame( '/', $actual['data']['nodeByUri']['uri'] );
  315. $this->assertSame( 'ContentType', $actual['data']['nodeByUri']['__typename'] );
  316. $this->assertTrue( $actual['data']['nodeByUri']['isPostsPage'] );
  317. $this->assertTrue( $actual['data']['nodeByUri']['isFrontPage'] );
  318. // If the "show_on_front" and "page_on_front" value are both set,
  319. // the node should be the Page that is set
  320. update_option( 'page_on_front', $post_id );
  321. $actual = graphql([ 'query' => $query ]);
  322. codecept_debug( $actual );
  323. $this->assertSame( $title, $actual['data']['nodeByUri']['title'] );
  324. $this->assertSame( 'Page', $actual['data']['nodeByUri']['__typename'] );
  325. $this->assertTrue( $actual['data']['nodeByUri']['isFrontPage'] );
  326. $this->assertFalse( $actual['data']['nodeByUri']['isPostsPage'] );
  327. }
  328. /**
  329. * @throws Exception
  330. */
  331. public function testHierarchicalCptNodesByUri() {
  332. register_post_type( 'test_hierarchical', [
  333. 'public' => true,
  334. 'publicly_queryable' => true,
  335. 'show_ui' => true,
  336. 'show_in_menu' => true,
  337. 'query_var' => true,
  338. 'rewrite' => [
  339. 'slug' => 'test_hierarchical',
  340. 'with_front' => false
  341. ],
  342. 'capability_type' => 'page',
  343. 'has_archive' => false,
  344. 'hierarchical' => true,
  345. 'menu_position' => null,
  346. 'supports' => array( 'title', 'editor', 'author', 'thumbnail', 'excerpt', 'page-attributes' ),
  347. 'show_in_rest' => true,
  348. 'rest_base' => 'test-hierarchical',
  349. 'show_in_graphql' => true,
  350. 'graphql_single_name' => 'testHierarchical',
  351. 'graphql_plural_name' => 'testHierarchicals',
  352. ]);
  353. flush_rewrite_rules( true );
  354. $parent = $this->factory()->post->create([
  355. 'post_type' => 'test_hierarchical',
  356. 'post_title' => 'test',
  357. 'post_content' => 'test',
  358. 'post_status' => 'publish',
  359. ]);
  360. $child = $this->factory()->post->create([
  361. 'post_type' => 'test_hierarchical',
  362. 'post_title' => 'child',
  363. 'post_content' => 'child',
  364. 'post_parent' => $parent,
  365. 'post_status' => 'publish',
  366. ]);
  367. $query = '
  368. {
  369. testHierarchicals {
  370. nodes {
  371. id
  372. databaseId
  373. title
  374. uri
  375. }
  376. }
  377. }
  378. ';
  379. $actual = graphql(['query' => $query]);
  380. codecept_debug( $actual );
  381. codecept_debug( parse_url( get_permalink( $child ), PHP_URL_PATH ) );
  382. $this->assertArrayNotHasKey( 'errors', $actual );
  383. $database_ids = wp_list_pluck( $actual['data']['testHierarchicals']['nodes'], 'databaseId' );
  384. codecept_debug( $database_ids );
  385. $this->assertTrue( in_array( $child, $database_ids, true ) );
  386. $this->assertTrue( in_array( $parent, $database_ids, true ) );
  387. $query = '
  388. query NodeByUri( $uri: String! ) {
  389. nodeByUri( uri: $uri ) {
  390. uri
  391. __typename
  392. ...on DatabaseIdentifier {
  393. databaseId
  394. }
  395. }
  396. }
  397. ';
  398. $child_uri = parse_url( get_permalink( $child ), PHP_URL_PATH );
  399. codecept_debug( $child_uri );
  400. $actual = graphql([
  401. 'query' => $query,
  402. 'variables' => [
  403. 'uri' => $child_uri,
  404. ],
  405. ]);
  406. codecept_debug( $actual );
  407. $this->assertArrayNotHasKey( 'errors', $actual );
  408. $this->assertSame( $child_uri, $actual['data']['nodeByUri']['uri'], 'Makes sure the uri of the node matches the uri queried with' );
  409. $this->assertSame( 'TestHierarchical', $actual['data']['nodeByUri']['__typename'] );
  410. $this->assertSame( $child, $actual['data']['nodeByUri']['databaseId'] );
  411. codecept_debug( $actual );
  412. $parent_uri = parse_url( get_permalink( $parent ), PHP_URL_PATH );
  413. $actual = graphql([
  414. 'query' => $query,
  415. 'variables' => [
  416. 'uri' => $parent_uri,
  417. ],
  418. ]);
  419. codecept_debug( $actual );
  420. $this->assertArrayNotHasKey( 'errors', $actual );
  421. $this->assertSame( $parent_uri, $actual['data']['nodeByUri']['uri'], 'Makes sure the uri of the node matches the uri queried with' );
  422. $this->assertSame( 'TestHierarchical', $actual['data']['nodeByUri']['__typename'] );
  423. $this->assertSame( $parent, $actual['data']['nodeByUri']['databaseId'] );
  424. }
  425. }