Skip to content

Commit

Permalink
refactor: category api test case
Browse files Browse the repository at this point in the history
  • Loading branch information
osmansufy committed Jan 13, 2025
1 parent a6001c6 commit 068ac08
Showing 1 changed file with 182 additions and 120 deletions.
302 changes: 182 additions & 120 deletions tests/php/src/ProductCategory/ProductCategoryApiTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,183 +5,245 @@
use WeDevs\Dokan\Test\DokanTestCase;

class ProductCategoryApiTest extends DokanTestCase {

/**
* The namespace of the REST route.
*
* @var string Dokan API Namespace
* @var string API namespace
*/
protected string $rest_base = 'product-categories';

/**
* Test that the endpoint exists
*
* @return void
* @var array Test categories
*/
public function test_endpoint_exists() {
$routes = $this->server->get_routes( $this->namespace );
$full_route = $this->get_route( $this->rest_base . '/' );
$this->assertArrayHasKey( $full_route, $routes );
}
protected array $categories = [];

/**
* Test that get categories permission callback works
* Set up test environment
*/
public function setUp(): void {
parent::setUp();

public function test_get_categories_permission_callback() {
$route = $this->get_route( $this->rest_base . '/' );
$request = new \WP_REST_Request( 'GET', $route );
$response = $this->server->dispatch( $request );
$this->assertEquals( 401, $response->get_status() );
// Create test categories with specific structure
$this->categories = $this->create_test_categories();
}

/**
* Test that get categories permission only for vendor
* Create test categories with parent-child relationships
*/

public function test_get_categories_permission_only_for_vendor() {
$route = $this->get_route( $this->rest_base . '/' );
$request = new \WP_REST_Request( 'GET', $route );
$seller_id = $this->seller_id1;
wp_set_current_user( $seller_id );
$response = $this->server->dispatch( $request );
$this->assertEquals( 200, $response->get_status() );
protected function create_test_categories(): array {
$categories = [];

// Create parent categories
$categories['parent1'] = $this->factory()->term->create(
[
'taxonomy' => 'product_cat',
'name' => 'Parent Category 1',
]
);

$categories['parent2'] = $this->factory()->term->create(
[
'taxonomy' => 'product_cat',
'name' => 'Parent Category 2',
]
);

// Create child categories
$categories['child1'] = $this->factory()->term->create(
[
'taxonomy' => 'product_cat',
'name' => 'Child Category 1',
'parent' => $categories['parent1'],
]
);

$categories['child2'] = $this->factory()->term->create(
[
'taxonomy' => 'product_cat',
'name' => 'Child Category 2',
'parent' => $categories['parent1'],
]
);

// Create additional categories for pagination tests
$categories['others'] = $this->factory()->term->create_many(
6, [
'taxonomy' => 'product_cat',
]
);

return $categories;
}

/**
* Test that get categories with pagination
* Helper method to make API requests
*/
protected function make_request( array $params = [] ): \WP_REST_Response {
$route = $this->get_route( $this->rest_base . '/' );
$request = new \WP_REST_Request( 'GET', $route );

public function test_get_categories_with_pagination() {
foreach ( $params as $key => $value ) {
$request->set_param( $key, $value );
}

// create 10 categories for testing with factory
$this->factory()->term->create_many( 10, [ 'taxonomy' => 'product_cat' ] );

$route = $this->get_route( $this->rest_base . '/' );
$request = new \WP_REST_Request( 'GET', $route );
$seller_id = $this->seller_id1;
wp_set_current_user( $seller_id );
$request->set_param( 'per_page', 5 );
$response = $this->server->dispatch( $request );
$this->assertEquals( 200, $response->get_status() );
$data = $response->get_data();
// Check if the response is an array and has 5 items
print_r( $data );
$this->assertIsArray( $data );
$this->assertCount( 5, $data );
return $this->server->dispatch( $request );
}



/**
* Test that get categories with search
* Helper method to authenticate as vendor
*/
protected function authenticate_as_vendor(): void {
wp_set_current_user( $this->seller_id1 );
}

public function test_get_categories_with_search() {

// create 10 categories for testing with factory
$this->factory()->term->create_many( 10, [ 'taxonomy' => 'product_cat' ] );
$route = $this->get_route( $this->rest_base . '/' );
$request = new \WP_REST_Request( 'GET', $route );
$seller_id = $this->seller_id1;
wp_set_current_user( $seller_id );
$request->set_param( 'search', 'term' );
/**
* @group endpoint
*/
public function test_endpoint_exists(): void {
$routes = $this->server->get_routes( $this->namespace );
$full_route = $this->get_route( $this->rest_base . '/' );
$this->assertArrayHasKey( $full_route, $routes );
}

$response = $this->server->dispatch( $request );
/**
* @group authentication
*/
public function test_requires_authentication(): void {
$response = $this->make_request();
$this->assertEquals( 401, $response->get_status() );
}

/**
* @group authentication
*/
public function test_vendor_can_access(): void {
$this->authenticate_as_vendor();
$response = $this->make_request();
$this->assertEquals( 200, $response->get_status() );
$data = $response->get_data();

// Check if the response is an array and includes the search term
$this->assertIsArray( $data );
print_r( $data[0]['name'] );
$this->assertStringContainsStringIgnoringCase( 'term', $data[0]['name'] );
}


/**
* Test that get categories with exclude
* @group pagination
*/
public function test_pagination(): void {
$this->authenticate_as_vendor();

// Test first page
$response = $this->make_request(
[
'per_page' => 5,
'page' => 1,
]
);
$data = $response->get_data();
$this->assertCount( 5, $data );

public function test_get_categories_with_exclude() {

// create 10 categories for testing with factory
$categories = $this->factory()->term->create_many( 10, [ 'taxonomy' => 'product_cat' ] );
$exclude = $categories[0];
// convert the exclude integer to string
$exclude = strval( $exclude );
$route = $this->get_route( $this->rest_base . '/' );
$request = new \WP_REST_Request( 'GET', $route );
$seller_id = $this->seller_id1;
wp_set_current_user( $seller_id );
$request->set_param( 'search', 'term' );
// Test second page
$response = $this->make_request(
[
'per_page' => 5,
'page' => 2,
]
);
$data = $response->get_data();
$this->assertNotEmpty( $data );

$response = $this->server->dispatch( $request );
$request->set_param( 'exclude', $exclude );
// Test pagination headers
$headers = $response->get_headers();
$this->assertArrayHasKey( 'X-WP-Total', $headers );
$this->assertArrayHasKey( 'X-WP-TotalPages', $headers );
}

$response = $this->server->dispatch( $request );
/**
* @group filtering
*/
public function test_search_filter(): void {
$this->authenticate_as_vendor();

$this->assertEquals( 200, $response->get_status() );
$response = $this->make_request( [ 'search' => 'Parent' ] );
$data = $response->get_data();
// Check if the response is an array and does not include the excluded category
$this->assertIsArray( $data );
$this->assertNotContains( $exclude, $data );

$this->assertNotEmpty( $data );
foreach ( $data as $category ) {
$this->assertStringContainsStringIgnoringCase( 'Parent', $category['name'] );
}
}

/**
* Test that get categories with include
* @group filtering
*/
public function test_exclude_filter(): void {
$this->authenticate_as_vendor();

public function test_get_categories_with_include() {

// create 10 categories for testing with factory
$categories = $this->factory()->term->create_many( 10, [ 'taxonomy' => 'product_cat' ] );
$include = $categories[0];
// convert the include integer to string
$include = strval( $include );
$route = $this->get_route( $this->rest_base . '/' );
$request = new \WP_REST_Request( 'GET', $route );
$seller_id = $this->seller_id1;
wp_set_current_user( $seller_id );
$request->set_param( 'search', 'term' );
$exclude_id = $this->categories['parent1'];
$response = $this->make_request( [ 'exclude' => (string) $exclude_id ] );
$data = $response->get_data();

$response = $this->server->dispatch( $request );
$request->set_param( 'include', $include );
$category_ids = wp_list_pluck( $data, 'id' );
$this->assertNotContains( $exclude_id, $category_ids );
}

$response = $this->server->dispatch( $request );
/**
* @group filtering
*/
public function test_include_filter(): void {
$this->authenticate_as_vendor();

$this->assertEquals( 200, $response->get_status() );
$include_id = $this->categories['parent1'];
$response = $this->make_request( [ 'include' => (string) $include_id ] );
$data = $response->get_data();

// Check if the response is an array and includes the included category
$this->assertIsArray( $data );
$this->assertCount( 1, $data );
$this->assertEquals( $include, $data[0]['id'] );
$this->assertEquals( $include_id, $data[0]['id'] );
}

/**
* Test that get categories with _fields
* @group fields
*/
public function test_fields_parameter(): void {
$this->authenticate_as_vendor();

public function test_get_categories_with_fields() {
$response = $this->make_request( [ '_fields' => 'id,name' ] );
$data = $response->get_data();

// create 10 categories for testing with factory
$categories = $this->factory()->term->create_many( 10, [ 'taxonomy' => 'product_cat' ] );
$route = $this->get_route( $this->rest_base . '/' );
$request = new \WP_REST_Request( 'GET', $route );
$seller_id = $this->seller_id1;
wp_set_current_user( $seller_id );
$request->set_param( '_fields', 'id,name' );
$this->assertNotEmpty( $data );
foreach ( $data as $category ) {
$this->assertArrayHasKey( 'id', $category );
$this->assertArrayHasKey( 'name', $category );
$this->assertArrayNotHasKey( 'description', $category );
$this->assertArrayNotHasKey( 'parent', $category );
}
}

$response = $this->server->dispatch( $request );
/**
* @group parent-child
*/
public function test_parent_parameter(): void {
$this->authenticate_as_vendor();

$this->assertEquals( 200, $response->get_status() );
$parent_id = $this->categories['parent1'];
$response = $this->make_request( [ 'parent' => $parent_id ] );
$data = $response->get_data();

// Check if the response is an array and includes only the id and name fields
$this->assertIsArray( $data );
$this->assertArrayHasKey( 'id', $data[0] );
$this->assertArrayHasKey( 'name', $data[0] );
$this->assertArrayNotHasKey( 'description', $data[0] );
$this->assertNotEmpty( $data );
foreach ( $data as $category ) {
$this->assertEquals( $parent_id, $category['parent'] );
}
}

/**
* Clean up after tests
*/
public function tearDown(): void {
// Delete test categories
foreach ( $this->categories as $category_id ) {
if ( is_array( $category_id ) ) {
foreach ( $category_id as $id ) {
wp_delete_term( $id, 'product_cat' );
}
} else {
wp_delete_term( $category_id, 'product_cat' );
}
}

parent::tearDown();
}
}

0 comments on commit 068ac08

Please sign in to comment.