<commit_before><?php

namespace ForkCMS\Bundle\InstallerBundle\Tests\Controller;

use Common\WebTestCase;
use Symfony\Bundle\FrameworkBundle\Client;
use Symfony\Component\DomCrawler\Crawler;
use Symfony\Component\Filesystem\Filesystem;
use Symfony\Component\HttpFoundation\Response;

/**
 * @group installer
 */
class InstallerControllerTest extends WebTestCase
{
    /** @var string */
    private $kernelDir;

    protected function setUp(): void
    {
        parent::setUp();

        $this->kernelDir = $this->getProvidedData()[0]->getContainer()->getParameter('kernel.project_dir') . '/app';
    }

    protected function onNotSuccessfulTest($e): void
    {
        // put back our parameters file
        $this->putParametersFileBack(new Filesystem(), $this->kernelDir);

        parent::onNotSuccessfulTest($e);
    }

    public function testNoStepActionAction(): void
    {
        $client = static::createClient(['environment' => 'test_install']);

        $client->request('GET', '/install');
        $client->followRedirect();

        // we should be redirected to the first step
        self::assertEquals(Response::HTTP_FOUND, $client->getResponse()->getStatusCode());
        $this->assertCurrentUrlEndsWith($client, '/install/1');
    }

    public function testInstallationProcess(Client $client): void
    {
        $container = $client->getContainer();
        $filesystem = new Filesystem();
        $installDatabaseConfig = [
            'install_database[databaseHostname]' => $container->getParameter('database.host'),
            'install_database[databasePort]' => $container->getParameter('database.port'),
            'install_database[databaseName]' => $container->getParameter('database.name') . '_test',
            'install_database[databaseUsername]' => $container->getParameter('database.user'),
            'install_database[databasePassword]' => $container->getParameter('database.password'),
        ];

        // make sure we have a clean slate and our parameters file is backed up
        $this->emptyTestDatabase($container->get('database'));

        // recreate the client with the empty database because we need this in our installer checks

        $this->backupParametersFile($filesystem, $this->kernelDir);
        $client = static::createClient(['environment' => 'test_install']);

        $crawler = $client->request('GET', '/install/2');
        $crawler = $this->runTroughStep2($crawler, $client);
        $crawler = $this->runTroughStep3($crawler, $client);
        $crawler = $this->runTroughStep4($crawler, $client, $installDatabaseConfig);
        $this->runTroughStep5($crawler, $client);

        // put back our parameters file
        $this->putParametersFileBack($filesystem, $this->kernelDir);
    }

    private function runTroughStep2(Crawler $crawler, Client $client): Crawler
    {
        $form = $crawler->selectButton('Next')->form();
        $form['install_languages[languages][0]']->tick();
        $form['install_languages[languages][1]']->tick();
        $form['install_languages[languages][2]']->tick();
        $client->submit(
            $form,
            [
                'install_languages[language_type]' => 'multiple',
                'install_languages[default_language]' => 'en',
            ]
        );

        $crawler = $client->followRedirect();

        // we should be redirected to step 3
        self::assertEquals(
            200,
            $client->getResponse()->getStatusCode()
        );
        self::assertStringEndsWith(
            '/install/3',
            $client->getHistory()->current()->getUri()
        );

        return $crawler;
    }

    private function runTroughStep3(Crawler $crawler, Client $client): Crawler
    {
        $form = $crawler->selectButton('Next')->form();
        $form['install_modules[modules][9]']->tick();
        $form['install_modules[modules][10]']->tick();
        $form['install_modules[modules][11]']->tick();
        $form['install_modules[modules][12]']->tick();
        $form['install_modules[modules][13]']->tick();
        $form['install_modules[modules][14]']->tick();
        $form['install_modules[modules][15]']->tick();
        $form['install_modules[modules][16]']->tick();
        $form['install_modules[modules][17]']->tick();
        $client->submit($form, []);
        $crawler = $client->followRedirect();

        // we should be redirected to step 4
        self::assertEquals(
            200,
            $client->getResponse()->getStatusCode()
        );
        self::assertStringEndsWith(
            '/install/4',
            $client->getHistory()->current()->getUri()
        );

        return $crawler;
    }

    private function runTroughStep4(Crawler $crawler, Client $client, array $installDatabaseConfig): Crawler
    {
        // first submit with incorrect data
        $form = $crawler->selectButton('Next')->form();
        $crawler = $client->submit($form, []);
        self::assertGreaterThan(
            0,
            $crawler->filter('div.errorMessage:contains("Problem with database credentials")')->count()
        );

        // submit with correct database credentials
        $form = $crawler->selectButton('Next')->form();
        $client->submit(
            $form,
            $installDatabaseConfig
        );
        $crawler = $client->followRedirect();

        // we should be redirected to step 5
        self::assertEquals(
            200,
            $client->getResponse()->getStatusCode()
        );
        self::assertStringEndsWith(
            '/install/5',
            $client->getHistory()->current()->getUri()
        );

        return $crawler;
    }

    private function runTroughStep5(Crawler $crawler, Client $client): Crawler
    {
        $form = $crawler->selectButton('Finish installation')->form();
        $client->submit(
            $form,
            [
                'install_login[email]' => '<EMAIL>',
                'install_login[password][first]' => 'password',
                'install_login[password][second]' => 'password',
            ]
        );
        $crawler = $client->followRedirect();

        // we should be redirected to step 6
        self::assertEquals(
            200,
            $client->getResponse()->getStatusCode()
        );
        self::assertStringEndsWith(
            '/install/6',
            $client->getHistory()->current()->getUri()
        );
        self::assertGreaterThan(
            0,
            $crawler->filter('h2:contains("Installation complete")')->count()
        );

        return $crawler;
    }<commit_msg>Clean up installer test<commit_after><?php

namespace ForkCMS\Bundle\InstallerBundle\Tests\Controller;

use Common\WebTestCase;
use Symfony\Bundle\FrameworkBundle\Client;
use Symfony\Component\Filesystem\Filesystem;
use Symfony\Component\HttpFoundation\Response;

/**
 * @group installer
 */
class InstallerControllerTest extends WebTestCase
{
    /** @var string */
    private $kernelDir;

    protected function setUp(): void
    {
        parent::setUp();

        $this->kernelDir = $this->getProvidedData()[0]->getContainer()->getParameter('kernel.project_dir') . '/app';
    }

    protected function onNotSuccessfulTest($e): void
    {
        // put back our parameters file
        $this->putParametersFileBack(new Filesystem(), $this->kernelDir);

        parent::onNotSuccessfulTest($e);
    }

    public function testNoStepActionAction(): void
    {
        $client = static::createClient(['environment' => 'test_install']);

        $client->request('GET', '/install');
        $client->followRedirect();

        // we should be redirected to the first step
        self::assertEquals(Response::HTTP_FOUND, $client->getResponse()->getStatusCode());
        $this->assertCurrentUrlEndsWith($client, '/install/1');
    }

    public function testInstallationProcess(Client $client): void
    {
        $container = $client->getContainer();
        $filesystem = new Filesystem();
        $installDatabaseConfig = [
            'install_database[databaseHostname]' => $container->getParameter('database.host'),
            'install_database[databasePort]' => $container->getParameter('database.port'),
            'install_database[databaseName]' => $container->getParameter('database.name') . '_test',
            'install_database[databaseUsername]' => $container->getParameter('database.user'),
            'install_database[databasePassword]' => $container->getParameter('database.password'),
        ];

        // make sure we have a clean slate and our parameters file is backed up
        $this->emptyTestDatabase($container->get('database'));

        // recreate the client with the empty database because we need this in our installer checks

        $this->backupParametersFile($filesystem, $this->kernelDir);
        $client = static::createClient(['environment' => 'test_install']);

        $this->assertGetsRedirected($client, '/install', '/install/2');
        $this->runTroughStep2($client);
        $this->runTroughStep3($client);
        $this->runTroughStep4($client, $installDatabaseConfig);
        $this->runTroughStep5($client);

        // put back our parameters file
        $this->putParametersFileBack($filesystem, $this->kernelDir);
    }

    private function runTroughStep2(Client $client): void
    {
        $this->assertCurrentUrlEndsWith($client, '/install/2');

        $form = $this->getFormForSubmitButton($client, 'Next');
        $form['install_languages[languages][0]']->tick();
        $form['install_languages[languages][1]']->tick();
        $form['install_languages[languages][2]']->tick();
        $this->submitForm(
            $client,
            $form,
            [
                'install_languages[language_type]' => 'multiple',
                'install_languages[default_language]' => 'en',
            ]
        );

        // we should be redirected to step 3
        $this->assertIs200($client);
        $this->assertCurrentUrlEndsWith($client, '/install/3');
    }

    private function runTroughStep3(Client $client): void
    {
        $form = $this->getFormForSubmitButton($client, 'Next');
        $form['install_modules[modules][9]']->tick();
        $form['install_modules[modules][10]']->tick();
        $form['install_modules[modules][11]']->tick();
        $form['install_modules[modules][12]']->tick();
        $form['install_modules[modules][13]']->tick();
        $form['install_modules[modules][14]']->tick();
        $form['install_modules[modules][15]']->tick();
        $form['install_modules[modules][16]']->tick();
        $form['install_modules[modules][17]']->tick();
        $this->submitForm($client, $form);

        // we should be redirected to step 4
        $this->assertIs200($client);
        $this->assertCurrentUrlEndsWith($client, '/install/4');
    }

    private function runTroughStep4(Client $client, array $installDatabaseConfig): void
    {
        // first submit with incorrect data
        $form = $this->getFormForSubmitButton($client, 'Next');
        $this->submitForm($client, $form);
        self::assertGreaterThan(
            0,
            $client->getCrawler()->filter('div.errorMessage:contains("Problem with database credentials")')->count()
        );

        // submit with correct database credentials
        $form = $this->getFormForSubmitButton($client, 'Next');
        $this->submitForm($client, $form, $installDatabaseConfig);

        // we should be redirected to step 5
        $this->assertIs200($client);
        $this->assertCurrentUrlEndsWith($client, '/install/5');
    }

    private function runTroughStep5(Client $client): void
    {
        $form = $this->getFormForSubmitButton($client, 'Finish installation');
        $this->submitForm(
            $client,
            $form,
            [
                'install_login[email]' => '<EMAIL>',
                'install_login[password][first]' => 'password',
                'install_login[password][second]' => 'password',
            ]
        );

        // we should be redirected to step 6
        $this->assertIs200($client);
        $this->assertCurrentUrlEndsWith($client, '/install/6');
        self::assertGreaterThan(
            0,
            $client->getCrawler()->filter('h2:contains("Installation complete")')->count()
        );
    }
{'fraction_non_alphanumeric': 0.15410154668394202, 'fraction_numerical': 0.008826625637703457, 'mean_word_length': 2.752658766332422, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 177, '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': '5047045', 'n_tokens_mistral': 3426, 'n_tokens_neox': 3312, 'n_words': 580}