<commit_before><?php
namespace Bolt\Tests\Controller\Async;

use Bolt\Controller\Zone;
use Bolt\Response\BoltResponse;
use Bolt\Tests\Controller\ControllerUnitTest;
use GuzzleHttp\Exception\RequestException;
use Symfony\Component\HttpFoundation\JsonResponse;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\HttpKernel\Event\GetResponseEvent;
use Symfony\Component\HttpKernel\HttpKernelInterface;
use Symfony\Component\HttpKernel\KernelEvents;

/**
 * Class to test correct operation of src/Controller/Async/General.
 *
 * @author <NAME> <<EMAIL>>
 **/
class GeneralTest extends ControllerUnitTest
{
    /**
     * @covers \Bolt\Controller\Zone::get
     * @covers \Bolt\Controller\Zone::isAsync
     */
    public function testControllerZone()
    {
        $app = $this->getApp();
        $this->allowLogin($app);
        $this->setRequest(Request::create('/async'));
        $request = $this->getRequest();

        $kernel = $this->getMock('Symfony\\Component\\HttpKernel\\HttpKernelInterface');
        $app['dispatcher']->dispatch(KernelEvents::REQUEST, new GetResponseEvent($kernel, $request, HttpKernelInterface::MASTER_REQUEST));

        $this->assertEquals('async', Zone::get($request));
        $this->assertTrue(Zone::isAsync($request));
    }

    public function testAsyncBaseRoute()
    {
        $app = $this->getApp();
        $this->allowLogin($app);
        $this->setRequest(Request::create('/async'));

        $response = $this->controller()->async();

        $this->assertJson($response->getContent());
        $this->assertEquals('["OK"]', $response->getContent());
    }

    public function testChangeLogRecord()
    {
        $this->setRequest(Request::create('/async/changelog/page/1'));

        $response = $this->controller()->changeLogRecord('page', 1);

        $this->assertTrue($response instanceof BoltResponse);
        $this->assertSame('components/panel-change-record.twig', $response->getTemplateName());
        $this->assertSame(Response::HTTP_OK, $response->getStatusCode());
    }
    
    public function testDashboardNewsWithInvalidRequest()
    {
        $this->setRequest(Request::create('/async/dashboardnews'));
        $app = $this->getApp();
        $testGuzzle = $this->getMock('GuzzleHttp\Client', ['get'], []);
        
        $guzzleInterface = $this->getMock('GuzzleHttp\Message\RequestInterface');
        $testGuzzle->expects($this->at(0))->method("get")->will($this->throwException(new RequestException('Mock Fail', $guzzleInterface)));
        $app['guzzle.client'] = $testGuzzle;
        
        $logger = $this->getMock('Bolt\Logger\Manager', ['info', 'critical'], [$app]);
        
        $logger->expects($this->at(1))
            ->method('critical')
            ->with($this->stringContains('Error occurred'));
        $app['logger.system'] = $logger;
        $response = $this->controller()->dashboardNews($this->getRequest());
    }
    
    public function testDashboardNewsWithInvalidJson()
    {
        $this->setRequest(Request::create('/async/dashboardnews'));
        $app = $this->getApp();
        $testGuzzle = $this->getMock('GuzzleHttp\Client', ['get'], []);
        $testRequest = $this->getMock('GuzzleHttp\Message', ['getBody']);
        $testRequest->expects($this->any())
                    ->method('getBody')
                    ->will($this->returnValue('invalidstring'));
        $testGuzzle->expects($this->any())
                    ->method('get')
                    ->will($this->returnValue($testRequest));
        $app['guzzle.client'] = $testGuzzle;
        
        $logger = $this->getMock('Bolt\Logger\Manager', ['info', 'error'], [$app]);
        
        $logger->expects($this->at(1))
            ->method('error')
            ->with($this->stringContains('Invalid JSON'));
        $app['logger.system'] = $logger;
        $response = $this->controller()->dashboardNews($this->getRequest());
    }
    
    public function testDashboardNewsWithVariable()
    {
        $app = $this->getApp();
        $app['cache']->clearCache();
        $this->setRequest(Request::create('/async/dashboardnews'));
        $app['config']->set('general/branding/news_variable', 'testing');
        
        $testGuzzle = $this->getMock('GuzzleHttp\Client', ['get'], []);
        $testRequest = $this->getMock('GuzzleHttp\Message', ['getBody']);
        $testRequest->expects($this->any())
                    ->method('getBody')
                    ->will($this->returnValue('{"testing":[{"item":"one"},{"item":"two"},{"item":"three"}]}'));
        $testGuzzle->expects($this->any())
                    ->method('get')
                    ->will($this->returnValue($testRequest));
        $app['guzzle.client'] = $testGuzzle;

        $response = $this->controller()->dashboardNews($this->getRequest());

        $context = $response->getContext();
        $this->assertEquals(['item' => 'one'], (array)$context['context']['information']);
    }
    

    public function testDashboardNews()
    {
        $this->setRequest(Request::create('/async/dashboardnews'));

        $response = $this->controller()->dashboardNews($this->getRequest());
        $this->assertTrue($response instanceof BoltResponse);
        $this->assertSame('components/panel-news.twig', $response->getTemplateName());
        $this->assertSame(Response::HTTP_OK, $response->getStatusCode());
    }

    public function testLastModified()
    {
        $this->setRequest(Request::create('/async/lastmodified/page/1'));

        $response = $this->controller()->lastModified('page', 1);

        $this->assertTrue($response instanceof BoltResponse);
        $this->assertSame('components/panel-lastmodified.twig', $response->getTemplateName());
        $this->assertSame(Response::HTTP_OK, $response->getStatusCode());
    }

    public function testLatestactivity()
    {
        $this->setRequest(Request::create('/async/latestactivity'));<commit_msg>[Tests] Update constructors for mocks<commit_after><?php
namespace Bolt\Tests\Controller\Async;

use Bolt\Controller\Zone;
use Bolt\Response\BoltResponse;
use Bolt\Tests\Controller\ControllerUnitTest;
use GuzzleHttp\Exception\RequestException;
use Symfony\Component\HttpFoundation\JsonResponse;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\HttpKernel\Event\GetResponseEvent;
use Symfony\Component\HttpKernel\HttpKernelInterface;
use Symfony\Component\HttpKernel\KernelEvents;

/**
 * Class to test correct operation of src/Controller/Async/General.
 *
 * @author <NAME> <<EMAIL>>
 **/
class GeneralTest extends ControllerUnitTest
{
    /**
     * @covers \Bolt\Controller\Zone::get
     * @covers \Bolt\Controller\Zone::isAsync
     */
    public function testControllerZone()
    {
        $app = $this->getApp();
        $this->allowLogin($app);
        $this->setRequest(Request::create('/async'));
        $request = $this->getRequest();

        $kernel = $this->getMock('Symfony\\Component\\HttpKernel\\HttpKernelInterface');
        $app['dispatcher']->dispatch(KernelEvents::REQUEST, new GetResponseEvent($kernel, $request, HttpKernelInterface::MASTER_REQUEST));

        $this->assertEquals('async', Zone::get($request));
        $this->assertTrue(Zone::isAsync($request));
    }

    public function testAsyncBaseRoute()
    {
        $app = $this->getApp();
        $this->allowLogin($app);
        $this->setRequest(Request::create('/async'));

        $response = $this->controller()->async();

        $this->assertJson($response->getContent());
        $this->assertEquals('["OK"]', $response->getContent());
    }

    public function testChangeLogRecord()
    {
        $this->setRequest(Request::create('/async/changelog/page/1'));

        $response = $this->controller()->changeLogRecord('page', 1);

        $this->assertTrue($response instanceof BoltResponse);
        $this->assertSame('components/panel-change-record.twig', $response->getTemplateName());
        $this->assertSame(Response::HTTP_OK, $response->getStatusCode());
    }

    public function testDashboardNewsWithInvalidRequest()
    {
        $this->setRequest(Request::create('/async/dashboardnews'));
        $app = $this->getApp();
        $testGuzzle = $this->getMock('GuzzleHttp\Client', ['get'], []);

        $guzzleInterface = $this->getMock('GuzzleHttp\Message\RequestInterface');
        $testGuzzle->expects($this->at(0))->method("get")->will($this->throwException(new RequestException('Mock Fail', $guzzleInterface)));
        $app['guzzle.client'] = $testGuzzle;

        $changeRepository = $app['storage']->getRepository('Bolt\Storage\Entity\LogChange');
        $systemRepository = $app['storage']->getRepository('Bolt\Storage\Entity\LogSystem');
        $logger = $this->getMock('Bolt\Logger\Manager', ['info', 'critical'], [$app, $changeRepository, $systemRepository]);

        $logger->expects($this->at(1))
            ->method('critical')
            ->with($this->stringContains('Error occurred'));
        $app['logger.system'] = $logger;
        $response = $this->controller()->dashboardNews($this->getRequest());
    }

    public function testDashboardNewsWithInvalidJson()
    {
        $this->setRequest(Request::create('/async/dashboardnews'));
        $app = $this->getApp();
        $testGuzzle = $this->getMock('GuzzleHttp\Client', ['get'], []);
        $testRequest = $this->getMock('GuzzleHttp\Message', ['getBody']);
        $testRequest->expects($this->any())
                    ->method('getBody')
                    ->will($this->returnValue('invalidstring'));
        $testGuzzle->expects($this->any())
                    ->method('get')
                    ->will($this->returnValue($testRequest));
        $app['guzzle.client'] = $testGuzzle;

        $changeRepository = $app['storage']->getRepository('Bolt\Storage\Entity\LogChange');
        $systemRepository = $app['storage']->getRepository('Bolt\Storage\Entity\LogSystem');
        $logger = $this->getMock('Bolt\Logger\Manager', ['info', 'error'], [$app, $changeRepository, $systemRepository]);

        $logger->expects($this->at(1))
            ->method('error')
            ->with($this->stringContains('Invalid JSON'));
        $app['logger.system'] = $logger;
        $response = $this->controller()->dashboardNews($this->getRequest());
    }

    public function testDashboardNewsWithVariable()
    {
        $app = $this->getApp();
        $app['cache']->clearCache();
        $this->setRequest(Request::create('/async/dashboardnews'));
        $app['config']->set('general/branding/news_variable', 'testing');

        $testGuzzle = $this->getMock('GuzzleHttp\Client', ['get'], []);
        $testRequest = $this->getMock('GuzzleHttp\Message', ['getBody']);
        $testRequest->expects($this->any())
                    ->method('getBody')
                    ->will($this->returnValue('{"testing":[{"item":"one"},{"item":"two"},{"item":"three"}]}'));
        $testGuzzle->expects($this->any())
                    ->method('get')
                    ->will($this->returnValue($testRequest));
        $app['guzzle.client'] = $testGuzzle;

        $response = $this->controller()->dashboardNews($this->getRequest());

        $context = $response->getContext();
        $this->assertEquals(['item' => 'one'], (array)$context['context']['information']);
    }


    public function testDashboardNews()
    {
        $this->setRequest(Request::create('/async/dashboardnews'));

        $response = $this->controller()->dashboardNews($this->getRequest());
        $this->assertTrue($response instanceof BoltResponse);
        $this->assertSame('components/panel-news.twig', $response->getTemplateName());
        $this->assertSame(Response::HTTP_OK, $response->getStatusCode());
    }

    public function testLastModified()
    {
        $this->setRequest(Request::create('/async/lastmodified/page/1'));

        $response = $this->controller()->lastModified('page', 1);

        $this->assertTrue($response instanceof BoltResponse);
        $this->assertSame('components/panel-lastmodified.twig', $response->getTemplateName());
        $this->assertSame(Response::HTTP_OK, $response->getStatusCode());
    }

    public function testLatestactivity()
    {
        $this->setRequest(Request::create('/async/latestactivity'));
{'fraction_non_alphanumeric': 0.18581354015188237, 'fraction_numerical': 0.0011310389400549362, 'mean_word_length': 3.4037709000355747, 'pattern_counts': {'":': 8, '<': 11, '<?xml version=': 0, '>': 225, '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': '4148097', 'n_tokens_mistral': 3479, 'n_tokens_neox': 3407, 'n_words': 435}