<commit_before><?php
/**
 * /tests/AppBundle/Controller/UserControllerTest.php
 *
 * @author  TLe, <NAME> <<EMAIL>>
 */
namespace AppBundle\Controller;

use App\Entity\User;
use App\Tests\WebTestCase;
use App\Utils\JSON;
use Symfony\Component\HttpFoundation\Response;

/**
 * Class UserControllerTest
 *
 * @category    Tests
 * @package     App\Controller
 * @author      TLe, <NAME> <<EMAIL>>
 */
class UserControllerTest extends WebTestCase
{
    /**
     * @dataProvider dataProviderTestThatOnlyAdminUsersCanListUsers
     *
     * @param   string  $username
     * @param   string  $password
     * @param   integer $expectedStatus
     */
    public function testThatOnlyAdminUsersCanListUsers($username, $password, $expectedStatus)
    {
        // Create request
        $client = $this->getClient($username, $password);
        $client->request('GET', '/user');

        // Check that HTTP status code is correct
        $this->assertEquals(
            $expectedStatus,
            $client->getResponse()->getStatusCode(),
            'HTTP status code was not expected for /user request\n' . $client->getResponse()
        );

        // Get response data
        $responseData = JSON::decode($client->getResponse()->getContent());

        // Forbidden request - not admin user
        if ($expectedStatus === Response::HTTP_FORBIDDEN) {
            $attributes = ['message', 'status', 'code'];

            foreach ($attributes as $attribute) {
                $this->assertObjectHasAttribute(
                    $attribute,
                    $responseData,
                    'Response did not contain expected attribute'
                );
            }

            // Get response object keys
            $keys = array_keys(get_object_vars($responseData));

            $this->assertEquals(
                sort($attributes),
                sort($keys),
                'Response contains keys that are not expected'
            );

            $this->assertEquals('Access denied.', $responseData->message);
            $this->assertEquals('403', $responseData->status);
            $this->assertEquals('0', $responseData->code);
        } else { // Otherwise check that response has correct output
            $this->assertTrue(is_array($responseData), 'Response did not return array of users.');
            $this->assertEquals(5, count($responseData), 'Response did not contain expected number of users.');
        }
    }

    /**
     * @dataProvider dataProviderTestThatOnlyAdminUsersCanListUsers
     *
     * @param   string  $username
     * @param   string  $password
     * @param   integer $expectedStatus
     * @param   string  $id
     */
    public function testThatOnlyAdminUsersCanGetSingleUserData($username, $password, $expectedStatus, $id)
    {
        // Create request
        $client = $this->getClient($username, $password);
        $client->request('GET', '/user/' . $id);

        // Check that HTTP status code is correct
        $this->assertEquals(
            $expectedStatus,
            $client->getResponse()->getStatusCode(),
            'HTTP status code was not expected for /user request\n' . $client->getResponse()
        );

        // Get response data
        $responseData = JSON::decode($client->getResponse()->getContent());

        // Forbidden request - not admin user
        if ($expectedStatus === Response::HTTP_FORBIDDEN) {
            $attributes = ['message', 'status', 'code'];

            foreach ($attributes as $attribute) {
                $this->assertObjectHasAttribute(
                    $attribute,
                    $responseData,
                    'Response did not contain expected attribute'
                );
            }

            // Get response object keys
            $keys = array_keys(get_object_vars($responseData));

            $this->assertEquals(
                sort($attributes),
                sort($keys),
                'Response contains keys that are not expected'
            );

            $this->assertEquals('Access denied.', $responseData->message);
            $this->assertEquals('403', $responseData->status);
            $this->assertEquals('0', $responseData->code);
        } else { // Otherwise check that response has correct output
            $attributes = ['id', 'username', 'firstname', 'surname', 'email', 'createdAt', 'updatedAt'];

            foreach ($attributes as $attribute) {
                $this->assertObjectHasAttribute(
                    $attribute,
                    $responseData,
                    'Response did not contain expected attribute'
                );
            }

            // Get response object keys
            $keys = array_keys(get_object_vars($responseData));

            $this->assertEquals(
                sort($attributes),
                sort($keys),
                'Response contains keys that are not expected'
            );
        }
    }

    /**
     * Data provider method for 'testThatOnlyAdminUsersCanListUsers' test
     *
     * @return array
     */
    public function dataProviderTestThatOnlyAdminUsersCanListUsers()
    {
        // Fetch users and pick random of those
        $users = $this->getContainer()->get('app.services.user')->find();
        $key = array_rand($users, 1);

        /** @var User $user */
        $user = $users[$key];

        return [
            ['john', 'doe', Response::HTTP_FORBIDDEN, $user->getId()],
            ['<EMAIL>', 'doe', Response::HTTP_FORBIDDEN, $user->getId()],<commit_msg>Refactored test to use correct service.<commit_after><?php
/**
 * /tests/AppBundle/Controller/UserControllerTest.php
 *
 * @author  TLe, <NAME> <<EMAIL>>
 */
namespace AppBundle\Controller;

use App\Entity\User;
use App\Tests\WebTestCase;
use App\Utils\JSON;
use Symfony\Component\HttpFoundation\Response;

/**
 * Class UserControllerTest
 *
 * @category    Tests
 * @package     App\Controller
 * @author      TLe, <NAME> <<EMAIL>>
 */
class UserControllerTest extends WebTestCase
{
    /**
     * @dataProvider dataProviderTestThatOnlyAdminUsersCanListUsers
     *
     * @param   string  $username
     * @param   string  $password
     * @param   integer $expectedStatus
     */
    public function testThatOnlyAdminUsersCanListUsers($username, $password, $expectedStatus)
    {
        // Create request
        $client = $this->getClient($username, $password);
        $client->request('GET', '/user');

        // Check that HTTP status code is correct
        $this->assertEquals(
            $expectedStatus,
            $client->getResponse()->getStatusCode(),
            'HTTP status code was not expected for /user request\n' . $client->getResponse()
        );

        // Get response data
        $responseData = JSON::decode($client->getResponse()->getContent());

        // Forbidden request - not admin user
        if ($expectedStatus === Response::HTTP_FORBIDDEN) {
            $attributes = ['message', 'status', 'code'];

            foreach ($attributes as $attribute) {
                $this->assertObjectHasAttribute(
                    $attribute,
                    $responseData,
                    'Response did not contain expected attribute'
                );
            }

            // Get response object keys
            $keys = array_keys(get_object_vars($responseData));

            $this->assertEquals(
                sort($attributes),
                sort($keys),
                'Response contains keys that are not expected'
            );

            $this->assertEquals('Access denied.', $responseData->message);
            $this->assertEquals('403', $responseData->status);
            $this->assertEquals('0', $responseData->code);
        } else { // Otherwise check that response has correct output
            $this->assertTrue(is_array($responseData), 'Response did not return array of users.');
            $this->assertEquals(5, count($responseData), 'Response did not contain expected number of users.');
        }
    }

    /**
     * @dataProvider dataProviderTestThatOnlyAdminUsersCanListUsers
     *
     * @param   string  $username
     * @param   string  $password
     * @param   integer $expectedStatus
     * @param   string  $id
     */
    public function testThatOnlyAdminUsersCanGetSingleUserData($username, $password, $expectedStatus, $id)
    {
        // Create request
        $client = $this->getClient($username, $password);
        $client->request('GET', '/user/' . $id);

        // Check that HTTP status code is correct
        $this->assertEquals(
            $expectedStatus,
            $client->getResponse()->getStatusCode(),
            'HTTP status code was not expected for /user request\n' . $client->getResponse()
        );

        // Get response data
        $responseData = JSON::decode($client->getResponse()->getContent());

        // Forbidden request - not admin user
        if ($expectedStatus === Response::HTTP_FORBIDDEN) {
            $attributes = ['message', 'status', 'code'];

            foreach ($attributes as $attribute) {
                $this->assertObjectHasAttribute(
                    $attribute,
                    $responseData,
                    'Response did not contain expected attribute'
                );
            }

            // Get response object keys
            $keys = array_keys(get_object_vars($responseData));

            $this->assertEquals(
                sort($attributes),
                sort($keys),
                'Response contains keys that are not expected'
            );

            $this->assertEquals('Access denied.', $responseData->message);
            $this->assertEquals('403', $responseData->status);
            $this->assertEquals('0', $responseData->code);
        } else { // Otherwise check that response has correct output
            $attributes = ['id', 'username', 'firstname', 'surname', 'email', 'createdAt', 'updatedAt'];

            foreach ($attributes as $attribute) {
                $this->assertObjectHasAttribute(
                    $attribute,
                    $responseData,
                    'Response did not contain expected attribute'
                );
            }

            // Get response object keys
            $keys = array_keys(get_object_vars($responseData));

            $this->assertEquals(
                sort($attributes),
                sort($keys),
                'Response contains keys that are not expected'
            );
        }
    }

    /**
     * Data provider method for 'testThatOnlyAdminUsersCanListUsers' test
     *
     * @return array
     */
    public function dataProviderTestThatOnlyAdminUsersCanListUsers()
    {
        // Fetch users and pick random of those
        $users = $this->getContainer()->get('app.services.rest.user')->find();
        $key = array_rand($users, 1);

        /** @var User $user */
        $user = $users[$key];

        return [
            ['john', 'doe', Response::HTTP_FORBIDDEN, $user->getId()],
            ['<EMAIL>', 'doe', Response::HTTP_FORBIDDEN, $user->getId()],
{'fraction_non_alphanumeric': 0.12298259850329096, 'fraction_numerical': 0.0018032639076728879, 'mean_word_length': 2.0010822510822512, 'pattern_counts': {'":': 0, '<': 19, '<?xml version=': 0, '>': 99, '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': '7144052', 'n_tokens_mistral': 2778, 'n_tokens_neox': 2687, 'n_words': 674}