<commit_before><?php

namespace Oro\Bundle\UserBundle\Tests\Functional\API;

use Oro\Bundle\TestFrameworkBundle\Test\WebTestCase;
use Oro\Bundle\TestFrameworkBundle\Test\ToolsAPI;
use Oro\Bundle\TestFrameworkBundle\Test\Client;

/**
 * @outputBuffering enabled
 * @db_isolation
 */
class RestUsersApiTest extends WebTestCase
{

    protected $client = null;

    public function setUp()
    {
        $this->client = static::createClient(array(), ToolsAPI::generateWsseHeader());
    }

    /**
     * @return array
     */
    public function testApiCreateUser()
    {
        $request = array(
            "profile" => array (
                "username" => 'user_' . mt_rand(),
                "email" => 'test_'  . mt_rand() . '@test.com',
                "enabled" => 'true',
                "plainPassword" => '<PASSWORD>',
                "firstName" => "firstName",
                "lastName" => "lastName",
                "rolesCollection" => array("1")
            )
        );
        $this->client->request('POST', 'http://localhost/api/rest/latest/profile', $request);
        $result = $this->client->getResponse();
        $this->assertJsonResponse($result, 201);

        return $request;
    }

    /**
     * @depends testApiCreateUser
     * @param  string $request
     * @return int
     */
    public function testApiUpdateUser($request)
    {
        //get user id
        $this->client->request('GET', 'http://localhost/api/rest/latest/profiles?limit=100');
        $result = $this->client->getResponse();
        $this->assertJsonResponse($result, 200);
        $result = json_decode($result->getContent(), true);
        $userId = $this->assertEqualsUser($request, $result);
        //update profile
        $request['profile']['username'] .= '_Updated';
        unset($request['profile']['plainPassword']);
        $this->client->request('PUT', 'http://localhost/api/rest/latest/profiles' . '/' . $userId, $request);
        $result = $this->client->getResponse();
        $this->assertJsonResponse($result, 204);
        //open user by id
        $this->client->request('GET', 'http://localhost/api/rest/latest/profiles' . '/' . $userId);
        $result = $this->client->getResponse();
        $this->assertJsonResponse($result, 200);

        $result = json_decode($result->getContent(), true);
        //compare result
        $this->assertEquals($request['profile']['username'], $result['username']);

        return $userId;
    }

    /**
     * @depends testApiUpdateUser
     * @param int $userId
     */
    public function testApiDeleteUser($userId)
    {
        $this->client->request('DELETE', 'http://localhost/api/rest/latest/profiles' . '/' . $userId);
        $result = $this->client->getResponse();
        $this->assertJsonResponse($result, 204);
        $this->client->request('GET', 'http://localhost/api/rest/latest/profiles' . '/' . $userId);
        $result = $this->client->getResponse();
        $this->assertJsonResponse($result, 404);
    }

    /**
     * Test API response status
     *
     * @param string $response
     * @param int    $statusCode
     */
    protected function assertJsonResponse($response, $statusCode = 201)
    {
        $this->assertEquals(
            $statusCode,
            $response->getStatusCode(),
            $response->getContent()
        );
    }

    /**
     * Check created user
     *
     * @return int
     * @param  array $result
     * @param  array $request
     */
    protected function assertEqualsUser($request, $result)
    {
        $flag = 1;
        foreach ($result as $key => $object) {
            foreach ($request as $user) {
                if ($user['username'] == $result[$key]['username']) {
                    $flag = 0;
                    $userId = $result[$key]['id'];
                    break 2;
                }
            }
        }
        $this->assertEquals(0, $flag);

        return $userId;
    }
}
<commit_msg>BAP-746: Fix tests<commit_after><?php

namespace Oro\Bundle\UserBundle\Tests\Functional\API;

use Oro\Bundle\TestFrameworkBundle\Test\WebTestCase;
use Oro\Bundle\TestFrameworkBundle\Test\ToolsAPI;
use Oro\Bundle\TestFrameworkBundle\Test\Client;

/**
 * @outputBuffering enabled
 * @db_isolation
 */
class RestUsersApiTest extends WebTestCase
{

    protected $client = null;

    public function setUp()
    {
        $this->client = static::createClient(array(), ToolsAPI::generateWsseHeader());
    }

    /**
     * @return array
     */
    public function testApiCreateUser()
    {
        $request = array(
            "profile" => array (
                "username" => 'user_' . mt_rand(),
                "email" => 'test_'  . mt_rand() . '@test.<EMAIL>',
                "enabled" => '1',
                "plainPassword" => '<PASSWORD>',
                "firstName" => "firstName",
                "lastName" => "lastName",
                "rolesCollection" => array("1")
            )
        );
        $this->client->request('POST', 'http://localhost/api/rest/latest/profile', $request);
        $result = $this->client->getResponse();
        $this->assertJsonResponse($result, 201);

        return $request;
    }

    /**
     * @depends testApiCreateUser
     * @param  string $request
     * @return int
     */
    public function testApiUpdateUser($request)
    {
        //get user id
        $this->client->request('GET', 'http://localhost/api/rest/latest/profiles?limit=100');
        $result = $this->client->getResponse();
        $this->assertJsonResponse($result, 200);
        $result = json_decode($result->getContent(), true);
        $userId = $this->assertEqualsUser($request, $result);
        //update profile
        $request['profile']['username'] .= '_Updated';
        unset($request['profile']['plainPassword']);
        $this->client->request('PUT', 'http://localhost/api/rest/latest/profiles' . '/' . $userId, $request);
        $result = $this->client->getResponse();
        $this->assertJsonResponse($result, 204);
        //open user by id
        $this->client->request('GET', 'http://localhost/api/rest/latest/profiles' . '/' . $userId);
        $result = $this->client->getResponse();
        $this->assertJsonResponse($result, 200);

        $result = json_decode($result->getContent(), true);
        //compare result
        $this->assertEquals($request['profile']['username'], $result['username']);

        return $userId;
    }

    /**
     * @depends testApiUpdateUser
     * @param int $userId
     */
    public function testApiDeleteUser($userId)
    {
        $this->client->request('DELETE', 'http://localhost/api/rest/latest/profiles' . '/' . $userId);
        $result = $this->client->getResponse();
        $this->assertJsonResponse($result, 204);
        $this->client->request('GET', 'http://localhost/api/rest/latest/profiles' . '/' . $userId);
        $result = $this->client->getResponse();
        $this->assertJsonResponse($result, 404);
    }

    /**
     * Test API response status
     *
     * @param string $response
     * @param int    $statusCode
     */
    protected function assertJsonResponse($response, $statusCode = 201)
    {
        $this->assertEquals(
            $statusCode,
            $response->getStatusCode(),
            $response->getContent()
        );
    }

    /**
     * Check created user
     *
     * @return int
     * @param  array $result
     * @param  array $request
     */
    protected function assertEqualsUser($request, $result)
    {
        $flag = 1;
        foreach ($result as $key => $object) {
            foreach ($request as $user) {
                if ($user['username'] == $result[$key]['username']) {
                    $flag = 0;
                    $userId = $result[$key]['id'];
                    break 2;
                }
            }
        }
        $this->assertEquals(0, $flag);

        return $userId;
    }
}

{'fraction_non_alphanumeric': 0.16519099418163422, 'fraction_numerical': 0.007842145206172527, 'mean_word_length': 2.1577476038338657, 'pattern_counts': {'":': 0, '<': 8, '<?xml version=': 0, '>': 102, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '5757157', 'n_tokens_mistral': 2290, 'n_tokens_neox': 2217, 'n_words': 398}