<commit_before><?php
namespace Bolt\Tests\Twig;

use Bolt\Legacy\Content;
use Bolt\Tests\BoltUnitTest;
use Bolt\Twig\TwigExtension;
use Symfony\Component\HttpFoundation\Request;

/**
 * Class to test src/Library.
 *
 * @author <NAME> <<EMAIL>>
 * @author <NAME> <<EMAIL>>
 */
class TwigExtensionTest extends BoltUnitTest
{
    public function testTwigInterface()
    {
        $app = $this->getApp();
        $handlers = $this->getTwigHandlers($app);
        $twig = new TwigExtension($app, $handlers, false);
        $this->assertGreaterThan(0, $twig->getFunctions());
        $this->assertGreaterThan(0, $twig->getFilters());
        $this->assertGreaterThan(0, $twig->getTests());
        $this->assertEquals('Bolt', $twig->getName());
    }

    public function testGetGlobals()
    {
        $app = $this->getApp();
        $request = Request::createFromGlobals();
        $app['request'] = $request;
        $app['request_stack']->push($request);
        $handlers = $this->getTwigHandlers($app);
        $twig = new TwigExtension($app, $handlers, false);

        $response = $twig->getGlobals();
        $this->assertArrayHasKey('bolt_name', $response);
        $this->assertArrayHasKey('bolt_version', $response);
        $this->assertArrayHasKey('bolt_stable', $response);
        $this->assertArrayHasKey('frontend', $response);
        $this->assertArrayHasKey('backend', $response);
        $this->assertArrayHasKey('async', $response);
        $this->assertArrayHasKey('paths', $response);
        $this->assertArrayHasKey('theme', $response);
        $this->assertArrayHasKey('user', $response);
        $this->assertArrayHasKey('users', $response);
        $this->assertArrayHasKey('config', $response);
        $this->assertNotNull($response['config']);
        $this->assertNotNull($response['users']);
    }

    public function testGetGlobalsSafe()
    {
        $app = $this->getApp();
        $request = Request::createFromGlobals();
        $app['request'] = $request;
        $app['request_stack']->push($request);
        $handlers = $this->getTwigHandlers($app);
        $twig = new TwigExtension($app, $handlers, true);

        $result = $twig->getGlobals();
        $this->assertArrayHasKey('config', $result);
        $this->assertNull($result['config']);
        $this->assertNull($result['users']);
    }

    public function testGetGlobalsExceptionalExceptionIsExceptional()
    {
        $app = $this->getApp();

        $users = $this->getMock('Bolt\Users', ['getCurrentUser'], [$app]);
        $users
            ->expects($this->atLeastOnce())
            ->method('getCurrentUser')
            ->will($this->throwException(new \Exception()));
        $app['users'] = $users;
        $request = Request::createFromGlobals();
        $app['request'] = $request;
        $app['request_stack']->push($request);
        $handlers = $this->getTwigHandlers($app);
        $twig = new TwigExtension($app, $handlers, false);

        $result = $twig->getGlobals();
        $this->assertArrayHasKey('user', $result);
        $this->assertArrayHasKey('users', $result);
        $this->assertNull($result['user']);
        $this->assertNull($result['users']);
    }

    public function testGetTokenParsers()
    {
        $app = $this->getApp();
        $handlers = $this->getTwigHandlers($app);
        $twig = new TwigExtension($app, $handlers, false);

        $result = $twig->getTokenParsers();
        $this->assertInstanceOf('Bolt\Twig\SetcontentTokenParser', $result[0]);
    }

    public function testGetTokenParsersSafe()
    {
        $app = $this->getApp();
        $handlers = $this->getTwigHandlers($app);
        $twig = new TwigExtension($app, $handlers, true);

        $result = $twig->getTokenParsers();
        $this->assertSame([], $result);
    }

    /*
     * Handlers below
     */

    public function testAddData()
    {
        $app = $this->getApp();
        $handlers = $this->getTwigHandlers($app);
        $handlers['admin'] = $this->getMockHandler('AdminHandler', 'addData');
        $twig = new TwigExtension($app, $handlers, true);

        $twig->addData(null, null);
    }

    public function testBuid()
    {
        $app = $this->getApp();
        $handlers = $this->getTwigHandlers($app);
        $handlers['admin'] = $this->getMockHandler('AdminHandler', 'buid');
        $twig = new TwigExtension($app, $handlers, true);

        $twig->buid();
    }

    public function testCacheHash()
    {
        $app = $this->getApp();
        $handlers = $this->getTwigHandlers($app);
        $handlers['html'] = $this->getMockHandler('HtmlHandler', 'cacheHash');
        $twig = new TwigExtension($app, $handlers, true);

        $twig->cacheHash(null);
    }

    public function testCountWidgets()
    {
        $app = $this->getApp();
        $handlers = $this->getTwigHandlers($app);
        $handlers['widget'] = $this->getMockHandler('WidgetHandler', 'countWidgets');
        $twig = new TwigExtension($app, $handlers, true);

        $twig->countWidgets(null, null);
    }

    public function testCurrent()
    {
        $app = $this->getApp();
        $handlers = $this->getTwigHandlers($app);
        $handlers['record'] = $this->getMockHandler('RecordHandler', 'current');
        $twig = new TwigExtension($app, $handlers, true);

        $twig->current(null);
    }

    public function testDecorateTT()
    {
        $app = $this->getApp();
        $handlers = $this->getTwigHandlers($app);<commit_msg>[Tests] Remove cache hash test<commit_after><?php
namespace Bolt\Tests\Twig;

use Bolt\Legacy\Content;
use Bolt\Tests\BoltUnitTest;
use Bolt\Twig\TwigExtension;
use Symfony\Component\HttpFoundation\Request;

/**
 * Class to test src/Library.
 *
 * @author <NAME> <<EMAIL>>
 * @author <NAME> <<EMAIL>>
 */
class TwigExtensionTest extends BoltUnitTest
{
    public function testTwigInterface()
    {
        $app = $this->getApp();
        $handlers = $this->getTwigHandlers($app);
        $twig = new TwigExtension($app, $handlers, false);
        $this->assertGreaterThan(0, $twig->getFunctions());
        $this->assertGreaterThan(0, $twig->getFilters());
        $this->assertGreaterThan(0, $twig->getTests());
        $this->assertEquals('Bolt', $twig->getName());
    }

    public function testGetGlobals()
    {
        $app = $this->getApp();
        $request = Request::createFromGlobals();
        $app['request'] = $request;
        $app['request_stack']->push($request);
        $handlers = $this->getTwigHandlers($app);
        $twig = new TwigExtension($app, $handlers, false);

        $response = $twig->getGlobals();
        $this->assertArrayHasKey('bolt_name', $response);
        $this->assertArrayHasKey('bolt_version', $response);
        $this->assertArrayHasKey('bolt_stable', $response);
        $this->assertArrayHasKey('frontend', $response);
        $this->assertArrayHasKey('backend', $response);
        $this->assertArrayHasKey('async', $response);
        $this->assertArrayHasKey('paths', $response);
        $this->assertArrayHasKey('theme', $response);
        $this->assertArrayHasKey('user', $response);
        $this->assertArrayHasKey('users', $response);
        $this->assertArrayHasKey('config', $response);
        $this->assertNotNull($response['config']);
        $this->assertNotNull($response['users']);
    }

    public function testGetGlobalsSafe()
    {
        $app = $this->getApp();
        $request = Request::createFromGlobals();
        $app['request'] = $request;
        $app['request_stack']->push($request);
        $handlers = $this->getTwigHandlers($app);
        $twig = new TwigExtension($app, $handlers, true);

        $result = $twig->getGlobals();
        $this->assertArrayHasKey('config', $result);
        $this->assertNull($result['config']);
        $this->assertNull($result['users']);
    }

    public function testGetGlobalsExceptionalExceptionIsExceptional()
    {
        $app = $this->getApp();

        $users = $this->getMock('Bolt\Users', ['getCurrentUser'], [$app]);
        $users
            ->expects($this->atLeastOnce())
            ->method('getCurrentUser')
            ->will($this->throwException(new \Exception()));
        $app['users'] = $users;
        $request = Request::createFromGlobals();
        $app['request'] = $request;
        $app['request_stack']->push($request);
        $handlers = $this->getTwigHandlers($app);
        $twig = new TwigExtension($app, $handlers, false);

        $result = $twig->getGlobals();
        $this->assertArrayHasKey('user', $result);
        $this->assertArrayHasKey('users', $result);
        $this->assertNull($result['user']);
        $this->assertNull($result['users']);
    }

    public function testGetTokenParsers()
    {
        $app = $this->getApp();
        $handlers = $this->getTwigHandlers($app);
        $twig = new TwigExtension($app, $handlers, false);

        $result = $twig->getTokenParsers();
        $this->assertInstanceOf('Bolt\Twig\SetcontentTokenParser', $result[0]);
    }

    public function testGetTokenParsersSafe()
    {
        $app = $this->getApp();
        $handlers = $this->getTwigHandlers($app);
        $twig = new TwigExtension($app, $handlers, true);

        $result = $twig->getTokenParsers();
        $this->assertSame([], $result);
    }

    /*
     * Handlers below
     */

    public function testAddData()
    {
        $app = $this->getApp();
        $handlers = $this->getTwigHandlers($app);
        $handlers['admin'] = $this->getMockHandler('AdminHandler', 'addData');
        $twig = new TwigExtension($app, $handlers, true);

        $twig->addData(null, null);
    }

    public function testBuid()
    {
        $app = $this->getApp();
        $handlers = $this->getTwigHandlers($app);
        $handlers['admin'] = $this->getMockHandler('AdminHandler', 'buid');
        $twig = new TwigExtension($app, $handlers, true);

        $twig->buid();
    }

    public function testCountWidgets()
    {
        $app = $this->getApp();
        $handlers = $this->getTwigHandlers($app);
        $handlers['widget'] = $this->getMockHandler('WidgetHandler', 'countWidgets');
        $twig = new TwigExtension($app, $handlers, true);

        $twig->countWidgets(null, null);
    }

    public function testCurrent()
    {
        $app = $this->getApp();
        $handlers = $this->getTwigHandlers($app);
        $handlers['record'] = $this->getMockHandler('RecordHandler', 'current');
        $twig = new TwigExtension($app, $handlers, true);

        $twig->current(null);
    }

    public function testDecorateTT()
    {
        $app = $this->getApp();
        $handlers = $this->getTwigHandlers($app);
{'fraction_non_alphanumeric': 0.17772156642308412, 'fraction_numerical': 0.0007494847292486415, 'mean_word_length': 2.9817232375979112, 'pattern_counts': {'":': 0, '<': 17, '<?xml version=': 0, '>': 167, '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': '4617957', 'n_tokens_mistral': 3340, 'n_tokens_neox': 3292, 'n_words': 527}