<commit_before><?php

/*
 * Bear CMS addon for Bear Framework
 * https://bearcms.com/
 * Copyright (c) 2016 Amplilabs Ltd.
 * Free to use under the MIT license.
 */

require __DIR__ . '/../vendor/autoload.php';

/**
 * 
 */
class BearFrameworkAddonTestCase extends PHPUnit_Framework_TestCase
{

    private $app = null;

    function getTestDir()
    {
        return sys_get_temp_dir() . '/unittests/' . uniqid() . '/';
    }

    function getApp($config = [], $createNew = false)
    {
        if ($this->app == null || $createNew) {
            $rootDir = $this->getTestDir();
            $this->app = new BearFramework\App();
            $this->app->filesystem->makeDir($rootDir . 'app/');
            $this->app->filesystem->makeDir($rootDir . 'data/');
            $this->app->filesystem->makeDir($rootDir . 'logs/');
            $this->app->filesystem->makeDir($rootDir . 'addons/');
            $this->app->config->handleErrors = false;

            $initialConfig = [
                'appDir' => $rootDir . 'app/',
                'dataDir' => $rootDir . 'data/',
                'logsDir' => $rootDir . 'logs/',
                'addonsDir' => realpath($rootDir . 'addons/')
            ];
            $config = array_merge($initialConfig, $config);
            foreach ($config as $key => $value) {
                $this->app->config->$key = $value;
            }

            $this->app->initialize();
            $this->app->request->base = 'http://example.com/www';
            $this->app->request->method = 'GET';

            $this->app->addons->add('bearcms/bearframework-addon', [
                'serverUrl' => 'http://dummy.bearcms.com/',
                'addonsDir' => realpath(__DIR__ . '/../addons/'),
                'language' => 'en'
            ]);
        }

        return $this->app;
    }

    function createDir($dir)
    {
        $this->app->filesystem->makeDir($dir);
    }

    function createFile($filename, $content)
    {
        $this->app->filesystem->makeFileDir($filename);
        file_put_contents($filename, $content);
    }

    function createSampleFile($filename, $type)
    {
        if ($type === 'png') {
            $this->createFile($filename, base64_decode('<KEY>'));
        } elseif ($type === 'jpg' || $type === 'jpeg') {
            $this->createFile($filename, base64_decode('/9j/4AAQSkZJRgABAQEASABIAAD/2wCEAAEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQE<KEY>UEQEAAAAAAAAAAAAAAAAAAABg/9oACAECAQE/AGv/xAAUEQEAAAAAAAAAAAAAAAAAAABg/9oACAEDAQE/AGv/2Q=='));
        } elseif ($type === 'gif') {
            $this->createFile($filename, base64_decode('R0lGODdhZABGAPAAAP8AAAAAACwAAAAAZABGAAACXISPqcvtD6OctNqLs968+w+G4kiW5omm6sq27gvH8kzX9o3n+s73/g8MCofEovGITCqXzKbzCY1Kp9Sq9YrNarfcrvcLDovH5LL5jE6r1+y2+w2Py+f0uv2Oz5cLADs='));
        } elseif ($type === 'webp') {
            $this->createFile($filename, base64_decode('UklGRlYAAABXRUJQVlA4IEoAAADQAwCdASpkAEYAAAAAJaQB2APwA/QACFiY02iY02iY02iY02iYywAA/v9vVv//8sPx/Unn/yxD///4npzeIqeV//EyAAAAAAAAAA=='));
        } elseif ($type === 'bmp') {
            $this->createFile($filename, base64_decode('Qk16AAAAAAAAAHYAAAAoAAAAAQAAAAEAAAABAAQAAAAAAAQAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAgAAAgAAAAICAAIAAAACAAIAAgIAAAICAgADAwMAAAAD/AAD/AAAA//8A/wAAAP8A/wD//wAA////APAAAAA='));
        } elseif ($type === 'broken') {
            $this->createFile($filename, base64_decode('broken'));
        }
    }

    function createUser()
    {
        $userID = uniqid();
        $userData = '{
    "id": "' . $userID . '",
    "registerTime": 1234567890,
    "lastLoginTime": 1234567890,
    "hashedPassword": "<PASSWORD>",
    "emails": [
        "<EMAIL>"
    ],
    "permissions": [
        "modifyContent",
        "managePages",
        "manageAppearance",
        "manageBlog",
        "manageFiles",
        "manageAddons",
        "manageAdministrators",
        "manageSettings",
        "viewAboutInformation"
    ]
}';
        $this->app->data->set([
            'key' => 'bearcms/users/user/' . md5($userID) . '.json',
            'body' => $userData
        ]);
        return $userID;
    }

    function loginUser($userID)
    {
        $sessionKey = str_repeat(uniqid(), 90 / strlen(uniqid()));
        \BearCMS\Internal\Cookies::setList(\BearCMS\Internal\Cookies::TYPE_SERVER, [['name' => '_s', 'value' => $sessionKey, 'expire' => time() + 86400]]);

        $this->app->data->set([
            'key' => '.temp/bearcms/userkeys/' . md5($sessionKey),
            'body' => $userID
        ]);
        return $sessionKey;
    }
    
    function createAndLoginUser(){
        $userID = $this->createUser();
        $this->loginUser($userID);
    }

}
<commit_msg>Unit tests update.<commit_after><?php

/*
 * Bear CMS addon for Bear Framework
 * https://bearcms.com/
 * Copyright (c) 2016 Amplilabs Ltd.
 * Free to use under the MIT license.
 */

require __DIR__ . '/../vendor/autoload.php';

/**
 * 
 */
class BearFrameworkAddonTestCase extends PHPUnit_Framework_TestCase
{

    private $app = null;

    function getTestDir()
    {
        return sys_get_temp_dir() . '/unittests/' . uniqid() . '/';
    }

    function getApp($config = [], $createNew = false)
    {
        if ($this->app == null || $createNew) {
            $rootDir = $this->getTestDir();
            $this->app = new BearFramework\App();
            $this->app->filesystem->makeDir($rootDir . 'app/');
            $this->app->filesystem->makeDir($rootDir . 'data/');
            $this->app->filesystem->makeDir($rootDir . 'logs/');
            $this->app->filesystem->makeDir($rootDir . 'addons/');
            $this->app->config->handleErrors = false;

            $initialConfig = [
                'appDir' => $rootDir . 'app/',
                'dataDir' => $rootDir . 'data/',
                'logsDir' => $rootDir . 'logs/',
                'addonsDir' => realpath($rootDir . 'addons/')
            ];
            $config = array_merge($initialConfig, $config);
            foreach ($config as $key => $value) {
                $this->app->config->$key = $value;
            }

            $this->app->initialize();
            $this->app->request->base = 'http://example.com/www';
            $this->app->request->method = 'GET';

            $this->app->addons->add('bearcms/bearframework-addon', [
                'serverUrl' => 'http://dummy.bearcms.com/',
                'siteID' => 'dummy1',
                'siteSecret' => 'dummy1',
                'addonsDir' => realpath(__DIR__ . '/../addons/'),
                'language' => 'en'
            ]);
        }

        return $this->app;
    }

    function createDir($dir)
    {
        $this->app->filesystem->makeDir($dir);
    }

    function createFile($filename, $content)
    {
        $this->app->filesystem->makeFileDir($filename);
        file_put_contents($filename, $content);
    }

    function createSampleFile($filename, $type)
    {
        if ($type === 'png') {
            $this->createFile($filename, base64_decode('iVBORw0KG<KEY>='));
        } elseif ($type === 'jpg' || $type === 'jpeg') {
            $this->createFile($filename, base64_decode('/9j/4AAQSkZJRgABAQEASABIAAD/2wCEAAEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAf/CABEIAEYAZAMBEQACEQEDEQH/xAAVAAEBAAAAAAAAAAAAAAAAAAAACf/aAAgBAQAAAACL4AAAAAAAAAAAAAAAAAAB/8QAFQEBAQAAAAAAAAAAAAAAAAAAAAn/2gAIAQIQAAAAlOAAAAAAAAAAAAAAAAAAAf/EABUBAQEAAAAAAAAAAAAAAAAAAAAK/9oACAEDEAAAAL+AAAAAAAAAAAAAAAAAAAD/xAAUEAEAAAAAAAAAAAAAAAAAAABg/9oACAEBAAE/AGv/xAAUEQEAAAAAAAAAAAAAAAAAAABg/9oACAECAQE/AGv/xAAUEQEAAAAAAAAAAAAAAAAAAABg/9oACAEDAQE/AGv/2Q=='));
        } elseif ($type === 'gif') {
            $this->createFile($filename, base64_decode('R0lGODdhZAB<KEY>'));
        } elseif ($type === 'webp') {
            $this->createFile($filename, base64_decode('UklGRlYAAABXRUJQVlA4IEoAAADQAwCdASpkAEYAAAAAJaQB2APwA/QACFiY02iY02iY02iY02iYywAA/v9vVv//8sPx/Unn/yxD///4npzeIqeV//EyAAAAAAAAAA=='));
        } elseif ($type === 'bmp') {
            $this->createFile($filename, base64_decode('Qk16AAAAAAAAAHYAAAAoAAAAAQAAAAEAAAABAAQAAAAAAAQAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAgAAAgAAAAICAAIAAAACAAIAAgIAAAICAgADAwMAAAAD/AAD/AAAA//8A/wAAAP8A/wD//wAA////APAAAAA='));
        } elseif ($type === 'broken') {
            $this->createFile($filename, base64_decode('broken'));
        }
    }

    function createUser()
    {
        $userID = uniqid();
        $userData = '{
    "id": "' . $userID . '",
    "registerTime": 1234567890,
    "lastLoginTime": 1234567890,
    "hashedPassword": "<PASSWORD>",
    "emails": [
        "<EMAIL>"
    ],
    "permissions": [
        "modifyContent",
        "managePages",
        "manageAppearance",
        "manageBlog",
        "manageFiles",
        "manageAddons",
        "manageAdministrators",
        "manageSettings",
        "viewAboutInformation"
    ]
}';
        $this->app->data->set([
            'key' => 'bearcms/users/user/' . md5($userID) . '.json',
            'body' => $userData
        ]);
        return $userID;
    }

    function loginUser($userID)
    {
        $sessionKey = str_repeat(uniqid(), 90 / strlen(uniqid()));
        \BearCMS\Internal\Cookies::setList(\BearCMS\Internal\Cookies::TYPE_SERVER, [['name' => '_s', 'value' => $sessionKey, 'expire' => time() + 86400]]);

        $this->app->data->set([
            'key' => '.temp/bearcms/userkeys/' . md5($sessionKey),
            'body' => $userID
        ]);
        return $sessionKey;
    }

    function createAndLoginUser()
    {
        $userID = $this->createUser();
        $this->loginUser($userID);
    }

}

{'fraction_non_alphanumeric': 0.16596660178260425, 'fraction_numerical': 0.017826042413687123, 'mean_word_length': 2.4678507992895202, 'pattern_counts': {'":': 12, '<': 13, '<?xml version=': 0, '>': 149, 'https://': 2, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 19, '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': '7515990', 'n_tokens_mistral': 3610, 'n_tokens_neox': 3399, 'n_words': 410}