<commit_before><?php

namespace Ilios\CoreBundle\Tests\Controller;

use FOS\RestBundle\Util\Codes;

/**
 * Discipline controller Test.
 * @package Ilios\CoreBundle\Test\Controller;
 */
class DisciplineControllerTest extends AbstractControllerTest
{
    /**
     * @return array|string
     */
    protected function getFixtures()
    {
        return [
            'Ilios\CoreBundle\Tests\Fixture\LoadDisciplineData',
            'Ilios\CoreBundle\Tests\Fixture\LoadSchoolData',
            'Ilios\CoreBundle\Tests\Fixture\LoadCourseData',
            'Ilios\CoreBundle\Tests\Fixture\LoadProgramYearData',
            'Ilios\CoreBundle\Tests\Fixture\LoadSessionData'
        ];
    }

    /**
     * @return array|string
     */
    protected function getPrivateFields()
    {
        return [
        ];
    }

    public function testGetDiscipline()
    {
        $discipline = $this->container
            ->get('ilioscore.dataloader.discipline')
            ->getOne()
        ;

        $this->createJsonRequest(
            'GET',
            $this->getUrl(
                'get_disciplines',
                ['id' => $discipline['id']]
            )
        );

        $response = $this->client->getResponse();

        $this->assertJsonResponse($response, Codes::HTTP_OK);
        $this->assertEquals(
            $this->mockSerialize($discipline),
            json_decode($response->getContent(), true)['disciplines'][0]
        );
    }

    public function testGetAllDisciplines()
    {
        $this->createJsonRequest('GET', $this->getUrl('cget_disciplines'));
        $response = $this->client->getResponse();

        $this->assertJsonResponse($response, Codes::HTTP_OK);
        $this->assertEquals(
            $this->mockSerialize(
                $this->container
                    ->get('ilioscore.dataloader.discipline')
                    ->getAll()
            ),
            json_decode($response->getContent(), true)['disciplines']
        );
    }

    public function testPostDiscipline()
    {
        $data = $this->container->get('ilioscore.dataloader.discipline')
            ->create();
        $postData = $data;
        //unset any parameters which should not be POSTed
        unset($postData['id']);

        $this->createJsonRequest(
            'POST',
            $this->getUrl('post_disciplines'),
            json_encode(['discipline' => $postData])
        );

        $response = $this->client->getResponse();
        $headers  = [];

        $this->assertEquals(Codes::HTTP_CREATED, $response->getStatusCode(), $response->getContent());
        $this->assertEquals(
            $data,
            json_decode($response->getContent(), true)['disciplines'][0],
            $response->getContent()
        );
    }

    public function testPostBadDiscipline()
    {
        $invalidDiscipline = $this->container
            ->get('ilioscore.dataloader.discipline')
            ->createInvalid()
        ;

        $this->createJsonRequest(
            'POST',
            $this->getUrl('post_disciplines'),
            json_encode(['discipline' => $invalidDiscipline])
        );

        $response = $this->client->getResponse();
        $this->assertEquals(Codes::HTTP_BAD_REQUEST, $response->getStatusCode());
    }

    public function testPutDiscipline()
    {
        $data = $this->container
            ->get('ilioscore.dataloader.discipline')
            ->getOne();

        $postData = $data;
        //unset any parameters which should not be POSTed
        unset($postData['id']);

        $this->createJsonRequest(
            'PUT',
            $this->getUrl(
                'put_disciplines',
                ['id' => $data['id']]
            ),
            json_encode(['discipline' => $postData])
        );

        $response = $this->client->getResponse();
        $this->assertJsonResponse($response, Codes::HTTP_OK);
        $this->assertEquals(
            $this->mockSerialize($data),
            json_decode($response->getContent(), true)['discipline']
        );
    }

    public function testDeleteDiscipline()
    {
        $discipline = $this->container
            ->get('ilioscore.dataloader.discipline')
            ->getOne()
        ;

        $this->client->request(
            'DELETE',
            $this->getUrl(
                'delete_disciplines',
                ['id' => $discipline['id']]
            )
        );

        $response = $this->client->getResponse();
        $this->assertEquals(Codes::HTTP_NO_CONTENT, $response->getStatusCode());
        $this->client->request(
            'GET',
            $this->getUrl(
                'get_disciplines',
                ['id' => $discipline['id']]
            )
        );

        $response = $this->client->getResponse();
        $this->assertEquals(Codes::HTTP_NOT_FOUND, $response->getStatusCode());
    }

    public function testDisciplineNotFound()
    {
        $this->createJsonRequest(
            'GET',
            $this->getUrl('get_disciplines', ['id' => '0'])
        );

        $response = $this->client->getResponse();
        $this->assertJsonResponse($response, Codes::HTTP_NOT_FOUND);
    }
}<commit_msg>added auth user to discipline controller test.<commit_after><?php

namespace Ilios\CoreBundle\Tests\Controller;

use FOS\RestBundle\Util\Codes;

/**
 * Discipline controller Test.
 * @package Ilios\CoreBundle\Test\Controller;
 */
class DisciplineControllerTest extends AbstractControllerTest
{
    /**
     * @return array|string
     */
    protected function getFixtures()
    {
        $fixtures = parent::getFixtures();
        return array_merge($fixtures, [
            'Ilios\CoreBundle\Tests\Fixture\LoadDisciplineData',
            'Ilios\CoreBundle\Tests\Fixture\LoadSchoolData',
            'Ilios\CoreBundle\Tests\Fixture\LoadCourseData',
            'Ilios\CoreBundle\Tests\Fixture\LoadProgramYearData',
            'Ilios\CoreBundle\Tests\Fixture\LoadSessionData'
        ]);
    }

    /**
     * @return array|string
     */
    protected function getPrivateFields()
    {
        return [
        ];
    }

    public function testGetDiscipline()
    {
        $discipline = $this->container
            ->get('ilioscore.dataloader.discipline')
            ->getOne()
        ;

        $this->createJsonRequest(
            'GET',
            $this->getUrl(
                'get_disciplines',
                ['id' => $discipline['id']]
            ),
            null,
            $this->getAuthenticatedUserToken()
        );

        $response = $this->client->getResponse();

        $this->assertJsonResponse($response, Codes::HTTP_OK);
        $this->assertEquals(
            $this->mockSerialize($discipline),
            json_decode($response->getContent(), true)['disciplines'][0]
        );
    }

    public function testGetAllDisciplines()
    {
        $this->createJsonRequest('GET', $this->getUrl('cget_disciplines'), null, $this->getAuthenticatedUserToken());
        $response = $this->client->getResponse();

        $this->assertJsonResponse($response, Codes::HTTP_OK);
        $this->assertEquals(
            $this->mockSerialize(
                $this->container
                    ->get('ilioscore.dataloader.discipline')
                    ->getAll()
            ),
            json_decode($response->getContent(), true)['disciplines']
        );
    }

    public function testPostDiscipline()
    {
        $data = $this->container->get('ilioscore.dataloader.discipline')
            ->create();
        $postData = $data;
        //unset any parameters which should not be POSTed
        unset($postData['id']);

        $this->createJsonRequest(
            'POST',
            $this->getUrl('post_disciplines'),
            json_encode(['discipline' => $postData]),
            $this->getAuthenticatedUserToken()
        );

        $response = $this->client->getResponse();

        $this->assertEquals(Codes::HTTP_CREATED, $response->getStatusCode(), $response->getContent());
        $this->assertEquals(
            $data,
            json_decode($response->getContent(), true)['disciplines'][0],
            $response->getContent()
        );
    }

    public function testPostBadDiscipline()
    {
        $invalidDiscipline = $this->container
            ->get('ilioscore.dataloader.discipline')
            ->createInvalid()
        ;

        $this->createJsonRequest(
            'POST',
            $this->getUrl('post_disciplines'),
            json_encode(['discipline' => $invalidDiscipline]),
            $this->getAuthenticatedUserToken()
        );

        $response = $this->client->getResponse();
        $this->assertEquals(Codes::HTTP_BAD_REQUEST, $response->getStatusCode());
    }

    public function testPutDiscipline()
    {
        $data = $this->container
            ->get('ilioscore.dataloader.discipline')
            ->getOne();

        $postData = $data;
        //unset any parameters which should not be POSTed
        unset($postData['id']);

        $this->createJsonRequest(
            'PUT',
            $this->getUrl(
                'put_disciplines',
                ['id' => $data['id']]
            ),
            json_encode(['discipline' => $postData]),
            $this->getAuthenticatedUserToken()
        );

        $response = $this->client->getResponse();
        $this->assertJsonResponse($response, Codes::HTTP_OK);
        $this->assertEquals(
            $this->mockSerialize($data),
            json_decode($response->getContent(), true)['discipline']
        );
    }

    public function testDeleteDiscipline()
    {
        $discipline = $this->container
            ->get('ilioscore.dataloader.discipline')
            ->getOne()
        ;

        $this->createJsonRequest(
            'DELETE',
            $this->getUrl(
                'delete_disciplines',
                ['id' => $discipline['id']]
            ),
            null,
            $this->getAuthenticatedUserToken()
        );

        $response = $this->client->getResponse();
        $this->assertEquals(Codes::HTTP_NO_CONTENT, $response->getStatusCode());
        $this->createJsonRequest(
            'GET',
            $this->getUrl(
                'get_disciplines',
                ['id' => $discipline['id']]
            ),
            null,
            $this->getAuthenticatedUserToken()
        );

        $response = $this->client->getResponse();
        $this->assertEquals(Codes::HTTP_NOT_FOUND, $response->getStatusCode());
    }

    public function testDisciplineNotFound()
    {
        $this->createJsonRequest(
            'GET',
            $this->getUrl('get_disciplines', ['id' => '0']),
            null,
            $this->getAuthenticatedUserToken()
        );

        $response = $this->client->getResponse();
        $this->assertJsonResponse($response, Codes::HTTP_NOT_FOUND);
    }
}
{'fraction_non_alphanumeric': 0.1455114054451803, 'fraction_numerical': 0.0005518763796909492, 'mean_word_length': 1.969953564599836, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 179, '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': '3228127', 'n_tokens_mistral': 3096, 'n_tokens_neox': 2998, 'n_words': 401}