Files
old-new-wiaas/backend/app/plugins/wiaas/tests/unit-tests/api/test-wiaas-rest-user-api.php

109 lines
3.3 KiB
PHP

<?php
/**
* Wiaas_Delivery_Process_Step_Test
*
* @package Wiaas
*/
class Wiass_REST_User_Api_Test extends Wiaas_Unit_Test_Case {
protected $server;
function setUp() {
parent::setUp();
/** @var WP_REST_Server $wp_rest_server */
global $wp_rest_server;
$this->server = $wp_rest_server = new \WP_REST_Server;
do_action( 'rest_api_init' );
}
/**
* @covers Wiass_REST_User_API::validate_token
*/
function test_validate_token_as_guest() {
wp_set_current_user(0);
$request = new WP_REST_Request( 'POST', '/wiaas/user/validate-token');
$response = $this->server->dispatch( $request );
$this->assertNotNull($response);
$this->assertInstanceOf('WP_REST_Response',$response);
$this->assertTrue($response->is_error());
$this->assertEquals($response->get_status(), 401);
$error_data = $response->as_error();
$this->assertEquals($error_data->get_error_message(), 'Sorry, you are not allowed to do that.');
}
/**
* @covers Wiass_REST_User_API::validate_token
*/
function test_validate_token_with_valid_user() {
wp_set_current_user(1);
$request = new WP_REST_Request( 'POST', '/wiaas/user/validate-token');
$response = $this->server->dispatch( $request );
$this->assertNotNull($response);
$this->assertInstanceOf('WP_REST_Response',$response);
$this->assertFalse($response->is_error());
$this->assertEquals($response->get_status(), 200);
$user_info = $response->get_data()['userInfo'];
$this->assertNotNull($user_info);
$this->assertTrue(is_array($user_info));
$this->assertArrayHasKey('wiaas_id_user', $user_info);
$this->assertArrayHasKey('wiaas_is_company_admin', $user_info);
$this->assertArrayHasKey('wiaas_user_full_name', $user_info);
$this->assertArrayHasKey('wiaas_user_type', $user_info);
$this->assertArrayHasKey('wiaas_username', $user_info);
$this->assertEquals($user_info['wiaas_id_user'], '1');
$this->assertEquals($user_info['wiaas_is_company_admin'], '1');
$this->assertEquals($user_info['wiaas_username'], 'admin');
}
/**
* @covers Wiass_REST_User_API::get_countries
*/
function test_get_countries_as_guest() {
wp_set_current_user(0);
$request = new WP_REST_Request( 'GET', '/wiaas/user/get-countries');
$response = $this->server->dispatch( $request );
$this->assertNotNull($response);
$this->assertInstanceOf('WP_REST_Response',$response);
$this->assertTrue($response->is_error());
$this->assertEquals($response->get_status(), 401);
$error_data = $response->as_error();
$this->assertEquals($error_data->get_error_message(), 'Sorry, you are not allowed to do that.');
}
/**
* @covers Wiass_REST_User_API::get_countries
*/
function test_get_countries() {
wp_set_current_user(1);
$request = new WP_REST_Request( 'GET', '/wiaas/user/get-countries');
$response = $this->server->dispatch( $request );
$this->assertNotNull($response);
$this->assertInstanceOf('WP_REST_Response',$response);
$this->assertFalse($response->is_error());
$this->assertEquals($response->get_status(), 200);
$data = $response->get_data();
$this->assertNotNull($data);
$this->assertTrue(is_array($data));
$country = $data[0];
$this->assertNotNull($country);
$this->assertTrue(is_array($country));
$this->assertArrayHasKey('code', $country);
$this->assertArrayHasKey('name', $country);
}
}