| 3995 | public function test_generated_permalink_template_and_slug_for_non_viewable_posts() { |
| 3996 | |
| 3997 | $post_type = 'private-post'; |
| 3998 | |
| 3999 | register_post_type( |
| 4000 | $post_type, |
| 4001 | array( |
| 4002 | 'label' => 'Private Posts', |
| 4003 | 'supports' => array( 'title', 'editor', 'author' ), |
| 4004 | 'show_in_rest' => true, |
| 4005 | 'publicly_queryable' => false, |
| 4006 | 'public' => true, |
| 4007 | 'rest_base' => 'private-post', |
| 4008 | ) |
| 4009 | ); |
| 4010 | |
| 4011 | create_initial_rest_routes(); |
| 4012 | |
| 4013 | $this->generated_permalink_template_and_slug_tests( $post_type ); |
| 4014 | |
| 4015 | _unregister_post_type( 'private-post' ); |
| 4016 | |
| 4017 | } |
| 4018 | |
| 4019 | public function test_generated_permalink_template_and_slug_for_non_public_posts() { |
| 4020 | |
| 4021 | $post_type = 'private-post'; |
| 4022 | |
| 4023 | register_post_type( |
| 4024 | $post_type, |
| 4025 | array( |
| 4026 | 'label' => 'Private Posts', |
| 4027 | 'supports' => array( 'title', 'editor', 'author' ), |
| 4028 | 'show_in_rest' => true, |
| 4029 | 'public' => false, |
| 4030 | 'rest_base' => 'private-post', |
| 4031 | ) |
| 4032 | ); |
| 4033 | |
| 4034 | create_initial_rest_routes(); |
| 4035 | |
| 4036 | $this->generated_permalink_template_and_slug_tests( $post_type ); |
| 4037 | |
| 4038 | _unregister_post_type( 'private-post' ); |
| 4039 | |
| 4040 | } |
| 4041 | |
| 4042 | public function test_generated_permalink_template_and_slug_for_edit_posts() { |
| 4043 | |
| 4044 | $this->generated_permalink_template_and_slug_tests( 'post' ); |
| 4045 | } |
| 4046 | |
| 4047 | public function generated_permalink_template_and_slug_tests( $post_type ) { |
| 4048 | |
| 4049 | $permalink_structure = '%postname%'; |
| 4050 | $expected_permalink_template = trailingslashit( home_url( $permalink_structure ) ); |
| 4051 | $this->set_permalink_structure( "/$permalink_structure/" ); |
| 4052 | |
| 4053 | $post_type_obj = get_post_type_object( $post_type ); |
| 4054 | |
| 4055 | wp_set_current_user( self::$editor_id ); |
| 4056 | |
| 4057 | $post_id = $this->factory->post->create( |
| 4058 | array( |
| 4059 | 'post_title' => 'Permalink Template', |
| 4060 | 'post_type' => $post_type, |
| 4061 | ) |
| 4062 | ); |
| 4063 | |
| 4064 | $post_endpoint_route = sprintf( '/wp/v2/%s/%d', $post_type_obj->rest_base, $post_id ); |
| 4065 | |
| 4066 | $request = new WP_REST_Request( 'GET', $post_endpoint_route ); |
| 4067 | $request->set_param( 'context', 'edit' ); |
| 4068 | $response = rest_get_server()->dispatch( $request ); |
| 4069 | $this->check_get_post_response( $response, 'edit' ); |
| 4070 | $read_data = $response->get_data(); |
| 4071 | |
| 4072 | |
| 4073 | |
| 4074 | if ( is_post_type_viewable( $post_type_obj ) && $post_type_obj->public ) { |
| 4075 | |
| 4076 | $this->assertArrayHasKey( 'permalink_template', $read_data ); |
| 4077 | $this->assertArrayHasKey( 'generated_slug', $read_data ); |
| 4078 | |
| 4079 | $this->assertEquals( $expected_permalink_template, $read_data['permalink_template'] ); |
| 4080 | $this->assertEquals( 'permalink-template', $read_data['generated_slug'] ); |
| 4081 | } else { |
| 4082 | |
| 4083 | $this->assertArrayNotHasKey( 'permalink_template', $read_data ); |
| 4084 | $this->assertArrayNotHasKey( 'generated_slug', $read_data ); |
| 4085 | } |
| 4086 | |
| 4087 | $request = new WP_REST_Request( 'POST', $post_endpoint_route ); |
| 4088 | $params = array( |
| 4089 | 'title' => 'Permalink Template Slug', |
| 4090 | ); |
| 4091 | $request->set_body_params( $params ); |
| 4092 | $response = rest_get_server()->dispatch( $request ); |
| 4093 | $updated_data = $response->get_data(); |
| 4094 | |
| 4095 | if ( is_post_type_viewable( $post_type_obj ) && $post_type_obj->public ) { |
| 4096 | |
| 4097 | $this->assertArrayHasKey( 'permalink_template', $updated_data ); |
| 4098 | $this->assertArrayHasKey( 'generated_slug', $updated_data ); |
| 4099 | $this->assertArrayHasKey( 'slug', $updated_data ); |
| 4100 | |
| 4101 | $this->assertEquals( $expected_permalink_template, $updated_data['permalink_template'] ); |
| 4102 | $this->assertEquals( 'permalink-template-slug', $updated_data['generated_slug'] ); |
| 4103 | $this->assertEquals( 'permalink-template', $updated_data['slug'] ); |
| 4104 | } else { |
| 4105 | |
| 4106 | $this->assertArrayNotHasKey( 'permalink_template', $updated_data ); |
| 4107 | $this->assertArrayNotHasKey( 'generated_slug', $updated_data ); |
| 4108 | } |
| 4109 | |
| 4110 | $request = new WP_REST_Request( 'GET', $post_endpoint_route ); |
| 4111 | $response = rest_get_server()->dispatch( $request ); |
| 4112 | $this->check_get_post_response( $response, 'view' ); |
| 4113 | $read_data = $response->get_data(); |
| 4114 | |
| 4115 | $this->assertArrayNotHasKey( 'permalink_template', $read_data ); |
| 4116 | $this->assertArrayNotHasKey( 'generated_slug', $read_data ); |
| 4117 | } |
| 4118 | |