| 939 | /** |
| 940 | * Testing updated taxonomy schema and including category children. |
| 941 | * |
| 942 | * @ticket 39494 |
| 943 | */ |
| 944 | public function test_get_items_categories_by_term_ids_and_include_children_true() { |
| 945 | $id1 = $this->factory->post->create( array( 'post_status' => 'publish' ) ); |
| 946 | $id2 = $this->factory->post->create( array( 'post_status' => 'publish' ) ); |
| 947 | |
| 948 | $cat1 = wp_insert_term( 'My Parent Category', 'category' ); |
| 949 | $cat2 = wp_insert_term( 'My Child Category', 'category', array( 'parent' => $cat1['term_id'] ) ); |
| 950 | |
| 951 | wp_set_object_terms( $id1, array( $cat1['term_id'] ), 'category' ); |
| 952 | wp_set_object_terms( $id2, array( $cat2['term_id'] ), 'category' ); |
| 953 | |
| 954 | $request = new WP_REST_Request( 'GET', '/wp/v2/posts' ); |
| 955 | $request->set_param( 'categories_object', (object) array( |
| 956 | 'term_ids' => array( $cat1['term_id'] ), |
| 957 | 'include_children' => true, |
| 958 | ) ); |
| 959 | $response = rest_get_server()->dispatch( $request ); |
| 960 | $data = $response->get_data(); |
| 961 | |
| 962 | $this->assertCount( 2, $data ); |
| 963 | $this->assertEquals( $id1, $data[0]['id'] ); |
| 964 | $this->assertEquals( $id2, $data[1]['id'] ); |
| 965 | } |
| 966 | |
| 967 | /** |
| 968 | * Testing updated taxonomy schema without including category children. |
| 969 | * |
| 970 | * @ticket 39494 |
| 971 | */ |
| 972 | public function test_get_items_categories_by_term_ids_and_include_children_false() { |
| 973 | $id1 = $this->factory->post->create( array( 'post_status' => 'publish' ) ); |
| 974 | $id2 = $this->factory->post->create( array( 'post_status' => 'publish' ) ); |
| 975 | |
| 976 | $cat1 = wp_insert_term( 'My Parent Category', 'category' ); |
| 977 | $cat2 = wp_insert_term( 'My Child Category', 'category', array( 'parent' => $cat1['term_id'] ) ); |
| 978 | |
| 979 | wp_set_object_terms( $id1, array( $cat1['term_id'] ), 'category' ); |
| 980 | wp_set_object_terms( $id2, array( $cat2['term_id'] ), 'category' ); |
| 981 | |
| 982 | $request = new WP_REST_Request( 'GET', '/wp/v2/posts' ); |
| 983 | $request->set_param( 'categories_object', (object) array( |
| 984 | 'term_ids' => array( $cat1['term_id'] ), |
| 985 | 'include_children' => false, |
| 986 | ) ); |
| 987 | $response = rest_get_server()->dispatch( $request ); |
| 988 | $data = $response->get_data(); |
| 989 | |
| 990 | $this->assertCount( 1, $data ); |
| 991 | $this->assertEquals( $id1, $data[0]['id'] ); |
| 992 | } |
| 993 | |
| 994 | /** |
| 995 | * Testing updated taxonomy schema by searching for term slugs and including category children. |
| 996 | * |
| 997 | * @ticket 39494 |
| 998 | */ |
| 999 | public function test_get_items_categories_by_term_slugs_and_include_children_true() { |
| 1000 | $id1 = $this->factory->post->create( array( 'post_status' => 'publish' ) ); |
| 1001 | $id2 = $this->factory->post->create( array( 'post_status' => 'publish' ) ); |
| 1002 | |
| 1003 | $cat1 = wp_insert_term( 'My Parent Category', 'category' ); |
| 1004 | $cat2 = wp_insert_term( 'My Child Category', 'category', array( 'parent' => $cat1['term_id'] ) ); |
| 1005 | |
| 1006 | wp_set_object_terms( $id1, array( $cat1['term_id'] ), 'category' ); |
| 1007 | wp_set_object_terms( $id2, array( $cat2['term_id'] ), 'category' ); |
| 1008 | |
| 1009 | $cat1_slug = get_term_by( 'term_id', $cat1['term_id'], 'category' )->slug; |
| 1010 | |
| 1011 | $request = new WP_REST_Request( 'GET', '/wp/v2/posts' ); |
| 1012 | $request->set_param( 'categories_object', (object) array( |
| 1013 | 'term_slugs' => array( $cat1_slug ), |
| 1014 | 'include_children' => true, |
| 1015 | ) ); |
| 1016 | $response = rest_get_server()->dispatch( $request ); |
| 1017 | $data = $response->get_data(); |
| 1018 | |
| 1019 | $this->assertCount( 2, $data ); |
| 1020 | $this->assertEquals( $id1, $data[0]['id'] ); |
| 1021 | $this->assertEquals( $id2, $data[1]['id'] ); |
| 1022 | } |
| 1023 | |
| 1024 | /** |
| 1025 | * Testing updated taxonomy schema by searching for term slugs and not including category children. |
| 1026 | * |
| 1027 | * @ticket 39494 |
| 1028 | */ |
| 1029 | public function test_get_items_categories_by_term_slugs_and_include_children_false() { |
| 1030 | $id1 = $this->factory->post->create( array( 'post_status' => 'publish' ) ); |
| 1031 | $id2 = $this->factory->post->create( array( 'post_status' => 'publish' ) ); |
| 1032 | |
| 1033 | $cat1 = wp_insert_term( 'My Parent Category', 'category' ); |
| 1034 | $cat2 = wp_insert_term( 'My Child Category', 'category', array( 'parent' => $cat1['term_id'] ) ); |
| 1035 | |
| 1036 | wp_set_object_terms( $id1, array( $cat1['term_id'] ), 'category' ); |
| 1037 | wp_set_object_terms( $id2, array( $cat2['term_id'] ), 'category' ); |
| 1038 | |
| 1039 | $cat1_slug = get_term_by( 'term_id', $cat1['term_id'], 'category' )->slug; |
| 1040 | |
| 1041 | $request = new WP_REST_Request( 'GET', '/wp/v2/posts' ); |
| 1042 | $request->set_param( 'categories_object', (object) array( |
| 1043 | 'term_slugs' => array( $cat1_slug ), |
| 1044 | 'include_children' => false, |
| 1045 | ) ); |
| 1046 | $response = rest_get_server()->dispatch( $request ); |
| 1047 | $data = $response->get_data(); |
| 1048 | |
| 1049 | $this->assertCount( 1, $data ); |
| 1050 | $this->assertEquals( $id1, $data[0]['id'] ); |
| 1051 | } |
| 1052 | |