<commit_before><?php

namespace Common;

use ForkCMS\App\AppKernel;
use ForkCMS\App\BaseModel;
use SpoonDatabase;
use Symfony\Bundle\FrameworkBundle\Test\WebTestCase as BaseWebTestCase;
use Symfony\Component\FileSystem\Filesystem;
use Symfony\Bundle\FrameworkBundle\Client;
use Symfony\Component\DomCrawler\Form;
use Symfony\Component\DomCrawler\Crawler;
use Backend\Core\Engine\Authentication;

/**
 * WebTestCase is the base class for functional tests.
 */
abstract class WebTestCase extends BaseWebTestCase
{
    /**
     * Attempts to guess the kernel location.
     *
     * When the Kernel is located, the file is required.
     *
     * @todo Remove this when Fork has no custom Kernel class anymore
     *
     * @throws \RuntimeException
     *
     * @return string The Kernel class name
     */
    protected static function getKernelClass(): string
    {
        return AppKernel::class;
    }

    /**
     * Creates a Client.
     *
     * @param array $options An array of options to pass to the createKernel class
     * @param array $server An array of server parameters
     *
     * @return Client A Client instance
     */
    protected static function createClient(array $options = [], array $server = []): Client
    {
        if (null !== static::$kernel) {
            static::$kernel->shutdown();
        }

        $client = parent::createClient($options, $server);
        static::$kernel = $client->getKernel();
        BaseModel::setContainer(static::$kernel->getContainer());

        return $client;
    }

    /**
     * Fully empties the test database
     *
     * @param SpoonDatabase $database
     */
    protected function emptyTestDatabase(SpoonDatabase $database)
    {
        foreach ($database->getTables() as $table) {
            $database->execute(
                'SET FOREIGN_KEY_CHECKS = 0; DROP TABLE IF EXISTS ' . $table . '; SET FOREIGN_KEY_CHECKS = 1;'
            );
        }
    }

    /**
     * Executes sql in the database
     *
     * @param SpoonDatabase $database
     * @param string $sql
     */
    protected function importSQL(SpoonDatabase $database, string $sql)
    {
        $database->execute(trim($sql));
    }

    protected function loadFixtures(Client $client, array $fixtureClasses = [])
    {
        $database = $client->getContainer()->get('database');

        // make sure our database has a clean state (freshly installed Fork)
        $this->emptyTestDatabase($database);
        $kernelDir = $client->getContainer()->getParameter('kernel.project_dir') . '/app';
        $this->importSQL(
            $client->getContainer()->get('database'),
            file_get_contents($kernelDir . '/../tests/data/test_db.sql')
        );

        // load all the fixtures
        foreach ($fixtureClasses as $class) {
            $fixture = new $class();
            $fixture->load($database);
        }
    }

    /**
     * Copies the parameters.yml file to a backup version
     *
     * @param string $kernelDir
     * @param Filesystem $filesystem
     */
    protected function backupParametersFile(Filesystem $filesystem, string $kernelDir)
    {
        if ($filesystem->exists($kernelDir . '/config/parameters.yml')) {
            $filesystem->copy(
                $kernelDir . '/config/parameters.yml',
                $kernelDir . '/config/parameters.yml~backup'
            );
        }
        if ($filesystem->exists($kernelDir . '/cache/test')) {
            $filesystem->remove($kernelDir . '/cache/test');
        }
    }

    /**
     * Puts the backed up parameters.yml file back
     *
     * @param string $kernelDir
     * @param Filesystem $filesystem
     */
    protected function putParametersFileBack(Filesystem $filesystem, string $kernelDir)
    {
        if ($filesystem->exists($kernelDir . '/config/parameters.yml~backup')) {
            $filesystem->copy(
                $kernelDir . '/config/parameters.yml~backup',
                $kernelDir . '/config/parameters.yml',
                true
            );
            $filesystem->remove($kernelDir . '/config/parameters.yml~backup');
        }
        if ($filesystem->exists($kernelDir . '/cache/test')) {
            $filesystem->remove($kernelDir . '/cache/test');
        }
    }

    protected function assertIs404(Client $client)
    {
        $client->followRedirect();
        self::assertEquals(
            404,
            $client->getResponse()->getStatusCode()
        );
        self::assertStringEndsWith(
            '404',
            $client->getHistory()->current()->getUri()
        );<commit_msg>404 checks in the tests now only need to look at the status code<commit_after><?php

namespace Common;

use ForkCMS\App\AppKernel;
use ForkCMS\App\BaseModel;
use SpoonDatabase;
use Symfony\Bundle\FrameworkBundle\Test\WebTestCase as BaseWebTestCase;
use Symfony\Component\FileSystem\Filesystem;
use Symfony\Bundle\FrameworkBundle\Client;
use Symfony\Component\DomCrawler\Form;
use Symfony\Component\DomCrawler\Crawler;
use Backend\Core\Engine\Authentication;

/**
 * WebTestCase is the base class for functional tests.
 */
abstract class WebTestCase extends BaseWebTestCase
{
    /**
     * Attempts to guess the kernel location.
     *
     * When the Kernel is located, the file is required.
     *
     * @todo Remove this when Fork has no custom Kernel class anymore
     *
     * @throws \RuntimeException
     *
     * @return string The Kernel class name
     */
    protected static function getKernelClass(): string
    {
        return AppKernel::class;
    }

    /**
     * Creates a Client.
     *
     * @param array $options An array of options to pass to the createKernel class
     * @param array $server An array of server parameters
     *
     * @return Client A Client instance
     */
    protected static function createClient(array $options = [], array $server = []): Client
    {
        if (null !== static::$kernel) {
            static::$kernel->shutdown();
        }

        $client = parent::createClient($options, $server);
        static::$kernel = $client->getKernel();
        BaseModel::setContainer(static::$kernel->getContainer());

        return $client;
    }

    /**
     * Fully empties the test database
     *
     * @param SpoonDatabase $database
     */
    protected function emptyTestDatabase(SpoonDatabase $database)
    {
        foreach ($database->getTables() as $table) {
            $database->execute(
                'SET FOREIGN_KEY_CHECKS = 0; DROP TABLE IF EXISTS ' . $table . '; SET FOREIGN_KEY_CHECKS = 1;'
            );
        }
    }

    /**
     * Executes sql in the database
     *
     * @param SpoonDatabase $database
     * @param string $sql
     */
    protected function importSQL(SpoonDatabase $database, string $sql)
    {
        $database->execute(trim($sql));
    }

    protected function loadFixtures(Client $client, array $fixtureClasses = [])
    {
        $database = $client->getContainer()->get('database');

        // make sure our database has a clean state (freshly installed Fork)
        $this->emptyTestDatabase($database);
        $kernelDir = $client->getContainer()->getParameter('kernel.project_dir') . '/app';
        $this->importSQL(
            $client->getContainer()->get('database'),
            file_get_contents($kernelDir . '/../tests/data/test_db.sql')
        );

        // load all the fixtures
        foreach ($fixtureClasses as $class) {
            $fixture = new $class();
            $fixture->load($database);
        }
    }

    /**
     * Copies the parameters.yml file to a backup version
     *
     * @param string $kernelDir
     * @param Filesystem $filesystem
     */
    protected function backupParametersFile(Filesystem $filesystem, string $kernelDir)
    {
        if ($filesystem->exists($kernelDir . '/config/parameters.yml')) {
            $filesystem->copy(
                $kernelDir . '/config/parameters.yml',
                $kernelDir . '/config/parameters.yml~backup'
            );
        }
        if ($filesystem->exists($kernelDir . '/cache/test')) {
            $filesystem->remove($kernelDir . '/cache/test');
        }
    }

    /**
     * Puts the backed up parameters.yml file back
     *
     * @param string $kernelDir
     * @param Filesystem $filesystem
     */
    protected function putParametersFileBack(Filesystem $filesystem, string $kernelDir)
    {
        if ($filesystem->exists($kernelDir . '/config/parameters.yml~backup')) {
            $filesystem->copy(
                $kernelDir . '/config/parameters.yml~backup',
                $kernelDir . '/config/parameters.yml',
                true
            );
            $filesystem->remove($kernelDir . '/config/parameters.yml~backup');
        }
        if ($filesystem->exists($kernelDir . '/cache/test')) {
            $filesystem->remove($kernelDir . '/cache/test');
        }
    }

    protected function assertIs404(Client $client)
    {
        self::assertEquals(
            404,
            $client->getResponse()->getStatusCode()
        );
{'fraction_non_alphanumeric': 0.12296983758700696, 'fraction_numerical': 0.0024306706441277206, 'mean_word_length': 2.250269299820467, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 59, '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': '2854134', 'n_tokens_mistral': 2500, 'n_tokens_neox': 2432, 'n_words': 589}