<?php

namespace Tests\unit\workflow\engine\methods\cases;

use Illuminate\Foundation\Testing\DatabaseTransactions;
use ProcessMaker\Model\User;
use RBAC;
use Tests\TestCase;


class ProxyNewCasesListTest extends TestCase
{
    use DatabaseTransactions;

    /**
     * This sets the initial parameters for each test.
     */
    public function setUp(): void
    {
        parent::setUp(); // TODO: Change the autogenerated stub
        $this->settingUserLogged();
    }

    /**
     * This starts a valid user in session with the appropriate permissions.
     * @global object $RBAC
     */
    private function settingUserLogged()
    {
        global $RBAC;

        $user = User::where('USR_ID', '=', 1)
            ->get()
            ->first();

        $_SESSION['USER_LOGGED'] = $user['USR_UID'];

        $RBAC = RBAC::getSingleton(PATH_DATA, session_id());
        $RBAC->initRBAC();
        $RBAC->loadUserRolePermission('PROCESSMAKER', $_SESSION['USER_LOGGED']);
    }

    /**
     * It tests the result contains an error
     *
     * @test
     */
    public function it_should_test_there_is_an_error_in_the_proxy_new_cases_list_file()
    {
        // Turn on output buffering
        ob_start();
        // Call the tested file
        require_once PATH_METHODS . 'cases/proxyNewCasesList.php';
        // Return the contents of the output buffer
        $outputBuffer = ob_get_contents();
        // Clean the output buffer and turn off output buffering
        ob_end_clean();
        // Parse JSON
        $result = json_decode($outputBuffer, true);
        // This asserts there is an error in the output
        $this->assertNotEmpty($result);
        $this->assertArrayHasKey('error', $result);
    }

    /**
     * It tests the result contains an empty "search" field
     *
     * @test
     */
    public function it_should_test_the_response_of_the_proxy_new_cases_list_file()
    {
        $_REQUEST["paged"] = '';
        $_REQUEST['count'] = '';
        $_REQUEST["category"] = '';
        $_REQUEST["process"] = '';
        $_REQUEST["search"] = 'fsfaefwa';
        $_REQUEST["filter"] = '';
        $_REQUEST["dateFrom"] = '';
        $_REQUEST["dateTo"] = '';
        $_REQUEST["start"] = '';
        $_REQUEST["limit"] = '';
        $_REQUEST['sort'] = 'ASC';
        $_REQUEST["dir"] = '';
        $_REQUEST["action"] = 'todo';
        $_REQUEST["user"] = '';
        $_REQUEST["list"] = 'inbox';
        $_REQUEST["filterStatus"] = '';
        $_REQUEST['openApplicationUid'] = '';

        // Turn on output buffering
        ob_start();
        // Call the tested file
        require_once PATH_METHODS . 'cases/proxyNewCasesList.php';
        // Return the contents of the output buffer
        $outputBuffer = ob_get_contents();
        // Clean the output buffer and turn off output buffering
        ob_end_clean();
        // Parse JSON
        $result = json_decode($outputBuffer, true);
        // This asserts that the search parameter has an empty value
        if (!empty($result)) {
            $this->assertArrayHasKey('search', $result);
        } else {
            $this->assertNull($result);
        }
    }
}