user = $this->factory->user->create( array( 'role' => 'administrator', ) ); $this->contributor = $this->factory->user->create( array( 'role' => 'contributor', ) ); // Create 2 product attributes with terms. $this->attr_color = ProductHelper::create_attribute( 'color', array( 'red', 'yellow', 'blue' ) ); $this->attr_size = ProductHelper::create_attribute( 'size', array( 'small', 'medium', 'large', 'xlarge' ) ); } /** * Test getting attribute terms. * * @since 3.6.0 */ public function test_get_terms() { wp_set_current_user( $this->user ); $request = new WP_REST_Request( 'GET', $this->endpoint . '/products/attributes/' . $this->attr_color['attribute_id'] . '/terms' ); $response = $this->server->dispatch( $request ); $data = $response->get_data(); $this->assertEquals( 200, $response->get_status() ); $this->assertEquals( 3, count( $data ) ); $term = $data[0]; $this->assertArrayHasKey( 'attribute', $term ); $attribute = $term['attribute']; $this->assertArrayHasKey( 'id', $attribute ); $this->assertArrayHasKey( 'name', $attribute ); $this->assertArrayHasKey( 'slug', $attribute ); } /** * Test getting invalid attribute terms. * * @since 3.6.0 */ public function test_get_invalid_attribute_terms() { wp_set_current_user( $this->user ); $request = new WP_REST_Request( 'GET', $this->endpoint . '/products/attributes/99999/terms' ); $response = $this->server->dispatch( $request ); $this->assertEquals( 404, $response->get_status() ); } /** * Test un-authorized getting attribute terms. * * @since 3.6.0 */ public function test_get_unauthed_attribute_terms() { $request = new WP_REST_Request( 'GET', $this->endpoint . '/products/attributes/' . $this->attr_size['attribute_id'] . '/terms' ); $response = $this->server->dispatch( $request ); $this->assertEquals( 401, $response->get_status() ); } /** * Test getting attribute terms as contributor. * * @since 3.6.0 */ public function test_get_attribute_terms_contributor() { wp_set_current_user( $this->contributor ); $request = new WP_REST_Request( 'GET', $this->endpoint . '/products/attributes/' . $this->attr_size['attribute_id'] . '/terms' ); $response = $this->server->dispatch( $request ); $data = $response->get_data(); $this->assertEquals( 200, $response->get_status() ); $this->assertEquals( 4, count( $data ) ); } }