Skip to content

Commit

Permalink
Update countries rest api and add testcase.
Browse files Browse the repository at this point in the history
  • Loading branch information
Aunshon committed Jan 21, 2025
1 parent 12e6c1c commit a20b1e4
Show file tree
Hide file tree
Showing 2 changed files with 100 additions and 70 deletions.
87 changes: 17 additions & 70 deletions includes/REST/DokanDataCountriesController.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@
use WC_REST_Data_Countries_Controller;
use WP_Error;
use WP_REST_Request;
use WP_REST_Response;

class DokanDataCountriesController extends WC_REST_Data_Countries_Controller {

Expand All @@ -24,65 +23,14 @@ class DokanDataCountriesController extends WC_REST_Data_Countries_Controller {
protected $rest_base = 'data/countries';

/**
* Register routes.
*
* @since 3.5.0
*/
public function register_routes() {
parent::register_routes();
}

/**
* Return the list of states for all countries.
*
* @since 3.5.0
* @param WP_REST_Request $request Request data.
* @return WP_Error|WP_REST_Response
*/
public function get_items( $request ) {
$this->set_woocommerce_rest_check_permissions();

return parent::get_items( $request );
}

/**
* Return the list of states for a given country.
*
* @since 3.5.0
* @param WP_REST_Request $request Request data.
* @return WP_Error|WP_REST_Response
*/
public function get_item( $request ) {
$this->set_woocommerce_rest_check_permissions();
return parent::get_item( $request );
}

/**
* Check if a given request has access to read an item.
*
* @param WP_REST_Request $request Full details about the request.
* @return WP_Error|boolean
*/
public function get_item_permissions_check( $request ) {
$this->set_woocommerce_rest_check_permissions();
$item_permission = $this->get_items_permissions_check( $request );

if ( is_wp_error( $item_permission ) ) {
return $item_permission;
}

return parent::get_item_permissions_check( $request );
}

/**
* Check if a given request has access to read items.
* Check the permission of the request for dokan.
*
* @since DOKAN_SINCE
*
* @param WP_REST_Request $request Full details about the request.
* @return WP_Error|boolean
*/
public function get_items_permissions_check( $request ) {
public function check_dokan_permission( $request ) {
// phpcs:ignore WordPress.WP.Capabilities.Unknown
if ( current_user_can( dokan_admin_menu_capability() ) || current_user_can( 'dokandar' ) ) {
return true;
Expand All @@ -93,31 +41,30 @@ public function get_items_permissions_check( $request ) {
__( 'You are not allowed to do this action.', 'dokan-lite' ),
[
'status' => rest_authorization_required_code(),
]
]
);
}

private function set_woocommerce_rest_check_permissions() {
add_filter( 'woocommerce_rest_check_permissions', [ $this, 'add_subscriptions_read_permission_to_vendors' ], 10, 4 );
/**
* Check if a given request has access to read an item.
*
* @param WP_REST_Request $request Full details about the request.
* @return WP_Error|boolean
*/
public function get_item_permissions_check( $request ) {
return $this->check_dokan_permission( $request );
}

/**
* Add permissions.
* Check if a given request has access to read items.
*
* @since DOKAN_PRO_SINCE
* @since DOKAN_SINCE
*
* @param $permission
* @param $context
* @param $object_id
* @param $obj
* @param WP_REST_Request $request Full details about the request.
*
* @return true
* @return WP_Error|boolean
*/
public function add_subscriptions_read_permission_to_vendors( $permission, $context, $object_id, $obj ) {
if ( 'read' === $context ) {
return true;
}

return $permission;
public function get_items_permissions_check( $request ) {
return $this->check_dokan_permission( $request );
}
}
83 changes: 83 additions & 0 deletions tests/php/src/REST/DokanDataCountriesControllerTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,83 @@
<?php

namespace WeDevs\Dokan\Test\REST;

use WeDevs\Dokan\Test\DokanTestCase;

class DokanDataCountriesControllerTest extends DokanTestCase {

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

/**
* Test that the endpoint exist.
*/
public function test_if_get_all_continents_api_exists() {
$routes = $this->server->get_routes( $this->namespace );
$full_route = $this->get_route( $this->rest_base );

$this->assertArrayHasKey( $full_route, $routes );
}

/**
* Test that the endpoint exist.
*/
public function test_if_get_a_single_continent_api_exists() {
$routes = $this->server->get_routes( $this->namespace );
$full_route = $this->get_route( $this->rest_base . '/(?P<location>[\w-]+)' );

$this->assertArrayHasKey( $full_route, $routes );
}

public function test_that_we_can_get_all_continents() {
wp_set_current_user( $this->seller_id1 );

$response = $this->get_request( $this->rest_base );

$this->assertEquals( 200, $response->get_status() );

$data = $response->get_data();

$this->assertIsArray( $data );
$this->assertArrayHasKey( 'code', $data[0] );
$this->assertArrayHasKey( 'name', $data[0] );
$this->assertArrayHasKey( 'states', $data[0] );
}

public function test_that_we_can_get_single_continent_item_by_code() {
wp_set_current_user( $this->seller_id1 );

$response = $this->get_request( $this->rest_base . '/BD' );

$this->assertEquals( 200, $response->get_status() );

$data = $response->get_data();

$this->assertIsArray( $data );

$this->assertArrayHasKey( 'code', $data );
$this->assertEquals( 'BD', $data['code'] );

$this->assertArrayHasKey( 'name', $data );
$this->assertEquals( 'Bangladesh', $data['name'] );

$this->assertArrayHasKey( 'states', $data );
$this->assertIsArray( $data['states'] );

$found = array_filter(
$data['states'], function ( $state ) {
return $state['code'] === 'BD-13';
}
);
$this->assertEquals( 1, count( $found ) );

$found = reset( $found );
$this->assertArrayHasKey( 'name', $found );
$this->assertEquals( 'Dhaka', $found['name'] );
$this->assertEquals( 'BD-13', $found['code'] );
}
}

0 comments on commit a20b1e4

Please sign in to comment.