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

use Bolt\Tests\Controller\ControllerUnitTest;
use Symfony\Component\HttpFoundation\Request;

/**
 * Class to test correct operation of src/Controller/Backend/Records.
 *
 * @author <NAME> <<EMAIL>>
 * @author <NAME> <<EMAIL>>
 **/
class RecordsTest extends ControllerUnitTest
{
    public function testDelete()
    {
        $this->setRequest(Request::create('/bolt/deletecontent/pages/4'));
        $response = $this->controller()->delete($this->getRequest(), 'pages', 4);

        // This one should fail for permissions
        $this->assertEquals('/bolt/overview/pages', $response->getTargetUrl());
        $err = $this->getFlashBag()->get('error');
        $this->assertRegExp('/denied/', $err[0]);

        $users = $this->getMock('Bolt\Users', ['isAllowed'], [$this->getApp()]);
        $users->expects($this->any())
            ->method('isAllowed')
            ->will($this->returnValue(true));
        $this->setService('users', $users);

        // This one should get killed by the anti CSRF check
        $response = $this->controller()->delete($this->getRequest(), 'pages', 4);
        $this->assertEquals('/bolt/overview/pages', $response->getTargetUrl());
        $err = $this->getFlashBag()->get('info');
        $this->assertRegExp('/could not be deleted/', $err[0]);

        $authentication = $this->getMock('Bolt\AccessControl\Authentication', ['checkAntiCSRFToken'], [$this->getApp()]);
        $authentication->expects($this->any())
            ->method('checkAntiCSRFToken')
            ->will($this->returnValue(true));
        $this->setService('authentication', $authentication);

        $response = $this->controller()->delete($this->getRequest(), 'pages', 4);
        $this->assertEquals('/bolt/overview/pages', $response->getTargetUrl());
        $err = $this->getFlashBag()->get('info');
        $this->assertRegExp('/has been deleted/', $err[0]);
    }

    public function testEditGet()
    {
        // First test will fail permission so we check we are kicked back to the dashboard
        $this->setRequest(Request::create('/bolt/editcontent/pages/4'));
        $response = $this->controller()->edit($this->getRequest(), 'pages', 4);
        $this->assertEquals('/bolt', $response->getTargetUrl());

        // Since we're the test user we won't automatically have permission to edit.
        $users = $this->getMock('Bolt\Users', ['isAllowed'], [$this->getApp()]);
        $users->expects($this->any())
            ->method('isAllowed')
            ->will($this->returnValue(true));
        $this->setService('users', $users);

        $this->setRequest(Request::create('/bolt/editcontent/pages/4'));
        $response = $this->controller()->edit($this->getRequest(), 'pages', 4);
        $context = $response->getContext();
        $this->assertEquals('Pages', $context['context']['contenttype']['name']);
        $this->assertInstanceOf('Bolt\Content', $context['context']['content']);

        // Test creation
        $this->setRequest(Request::create('/bolt/editcontent/pages'));
        $response = $this->controller()->edit($this->getRequest(), 'pages', null);
        $context = $response->getContext();
        $this->assertEquals('Pages', $context['context']['contenttype']['name']);
        $this->assertInstanceOf('Bolt\Content', $context['context']['content']);
        $this->assertNull($context['context']['content']->id);

        // Test that non-existent throws a redirect
        $this->setRequest(Request::create('/bolt/editcontent/pages/310'));
        $response = $this->controller()->edit($this->getRequest(), 'pages', 310);
        $this->assertEquals(302, $response->getStatusCode());
    }

    public function testEditDuplicate()
    {
        // Since we're the test user we won't automatically have permission to edit.
        $users = $this->getMock('Bolt\Users', ['isAllowed'], [$this->getApp()]);
        $users->expects($this->any())
            ->method('isAllowed')
            ->will($this->returnValue(true));
        $this->setService('users', $users);

        $this->setRequest(Request::create('/bolt/editcontent/pages/4', 'GET', ['duplicate' => true]));
        $original = $this->getService('storage')->getContent('pages/4');
        $response = $this->controller()->edit($this->getRequest(), 'pages', 4);
        $context = $response->getContext();

        // Check that correct fields are equal in new object
        $new = $context['context']['content'];
        $this->assertEquals($new['body'], $original['body']);
        $this->assertEquals($new['title'], $original['title']);
        $this->assertEquals($new['teaser'], $original['teaser']);

        // Check that some have been cleared.
        $this->assertEquals('', $new['id']);
        $this->assertEquals('', $new['slug']);
        $this->assertEquals('', $new['ownerid']);
    }

    public function testEditCSRF()
    {
        $users = $this->getMock('Bolt\Users', ['isAllowed', 'checkAntiCSRFToken'], [$this->getApp()]);
        $users->expects($this->any())
            ->method('isAllowed')
            ->will($this->returnValue(true));
        $users->expects($this->any())
            ->method('checkAntiCSRFToken')
            ->will($this->returnValue(false));

        $this->setService('users', $users);

        $this->setRequest(Request::create('/bolt/editcontent/showcases/3', 'POST'));
        $this->setExpectedException('Symfony\Component\HttpKernel\Exception\HttpException', 'Something went wrong');
        $this->controller()->edit($this->getRequest(), 'showcases', 3);
    }

    public function testEditPermissions()
    {
        $authentication = $this->getMock('Bolt\AccessControl\Authentication', ['checkAntiCSRFToken'], [$this->getApp()]);
        $authentication->expects($this->any())
            ->method('checkAntiCSRFToken')
            ->will($this->returnValue(true));
        $this->setService('authentication', $authentication);

        $users = $this->getMock('Bolt\Users', ['isAllowed'], [$this->getApp()]);
        $users->expects($this->at(0))
            ->method('isAllowed')
            ->will($this->returnValue(false));
        $this->setService('users', $users);

        // We should get kicked here because we dont have permissions to edit this
        $this->setRequest(Request::create('/bolt/editcontent/showcases/3', 'POST'));
        $response = $this->controller()->edit($this->getRequest(), 'showcases', 3);
        $this->assertEquals('/bolt', $response->getTargetUrl());
    }

    public function testEditPost()
    {
        $authentication = $this->getMock('Bolt\AccessControl\Authentication', ['checkAntiCSRFToken'], [$this->getApp()]);
        $authentication->expects($this->any())
            ->method('checkAntiCSRFToken')
            ->will($this->returnValue(true));
        $this->setService('authentication', $authentication);

        $users = $this->getMock('Bolt\Users', ['isAllowed'], [$this->getApp()]);
        $users->expects($this->any())
            ->method('isAllowed')
            ->will($this->returnValue(true));
        $this->setService('users', $users);

        $this->setRequest(Request::create('/bolt/editcontent/showcases/3', 'POST', ['floatfield' => 1.2]));
        //$original = $this->getService('storage')->getContent('showcases/3');
        $response = $this->controller()->edit($this->getRequest(), 'showcases', 3);
        $this->assertEquals('/bolt/overview/showcases', $response->getTargetUrl());
    }

    public function testEditPostAjax()
    {
        $authentication = $this->getMock('Bolt\AccessControl\Authentication', ['checkAntiCSRFToken'], [$this->getApp()]);
        $authentication->expects($this->any())
            ->method('checkAntiCSRFToken')
            ->will($this->returnValue(true));
        $this->setService('authentication', $authentication);

        // Since we're the test user we won't automatically have permission to edit.
        $users = $this->getMock('Bolt\Users', ['isAllowed'], [$this->getApp()]);
        $users->expects($this->any())
            ->method('isAllowed')
            ->will($this->returnValue(true));
        $this->setService('users', $users);

        $this->setRequest(Request::create('/bolt/editcontent/pages/4?returnto=ajax', 'POST'));<commit_msg>fix auth injection for RecordsTest mocks<commit_after><?php
namespace Bolt\Tests\Controller\Backend;

use Bolt\Tests\Controller\ControllerUnitTest;
use Symfony\Component\HttpFoundation\Request;

/**
 * Class to test correct operation of src/Controller/Backend/Records.
 *
 * @author <NAME> <<EMAIL>>
 * @author <NAME> <<EMAIL>>
 **/
class RecordsTest extends ControllerUnitTest
{
    public function testDelete()
    {
        $this->setRequest(Request::create('/bolt/deletecontent/pages/4'));
        $response = $this->controller()->delete($this->getRequest(), 'pages', 4);

        // This one should fail for permissions
        $this->assertEquals('/bolt/overview/pages', $response->getTargetUrl());
        $err = $this->getFlashBag()->get('error');
        $this->assertRegExp('/denied/', $err[0]);

        $users = $this->getMock('Bolt\Users', ['isAllowed'], [$this->getApp()]);
        $users->expects($this->any())
            ->method('isAllowed')
            ->will($this->returnValue(true));
        $this->setService('users', $users);

        // This one should get killed by the anti CSRF check
        $response = $this->controller()->delete($this->getRequest(), 'pages', 4);
        $this->assertEquals('/bolt/overview/pages', $response->getTargetUrl());
        $err = $this->getFlashBag()->get('info');
        $this->assertRegExp('/could not be deleted/', $err[0]);

        $app = $this->getApp();
        $authentication = $this->getMock(
            'Bolt\AccessControl\Authentication', 
            ['checkAntiCSRFToken'], 
            [  
                $app,
                $app['storage']->getRepository('Bolt\Storage\Entity\Authtoken')
            ]
        );
        $authentication->expects($this->any())
            ->method('checkAntiCSRFToken')
            ->will($this->returnValue(true));
        $this->setService('authentication', $authentication);

        $response = $this->controller()->delete($this->getRequest(), 'pages', 4);
        $this->assertEquals('/bolt/overview/pages', $response->getTargetUrl());
        $err = $this->getFlashBag()->get('info');
        $this->assertRegExp('/has been deleted/', $err[0]);
    }

    public function testEditGet()
    {
        // First test will fail permission so we check we are kicked back to the dashboard
        $this->setRequest(Request::create('/bolt/editcontent/pages/4'));
        $response = $this->controller()->edit($this->getRequest(), 'pages', 4);
        $this->assertEquals('/bolt', $response->getTargetUrl());

        // Since we're the test user we won't automatically have permission to edit.
        $users = $this->getMock('Bolt\Users', ['isAllowed'], [$this->getApp()]);
        $users->expects($this->any())
            ->method('isAllowed')
            ->will($this->returnValue(true));
        $this->setService('users', $users);

        $this->setRequest(Request::create('/bolt/editcontent/pages/4'));
        $response = $this->controller()->edit($this->getRequest(), 'pages', 4);
        $context = $response->getContext();
        $this->assertEquals('Pages', $context['context']['contenttype']['name']);
        $this->assertInstanceOf('Bolt\Content', $context['context']['content']);

        // Test creation
        $this->setRequest(Request::create('/bolt/editcontent/pages'));
        $response = $this->controller()->edit($this->getRequest(), 'pages', null);
        $context = $response->getContext();
        $this->assertEquals('Pages', $context['context']['contenttype']['name']);
        $this->assertInstanceOf('Bolt\Content', $context['context']['content']);
        $this->assertNull($context['context']['content']->id);

        // Test that non-existent throws a redirect
        $this->setRequest(Request::create('/bolt/editcontent/pages/310'));
        $response = $this->controller()->edit($this->getRequest(), 'pages', 310);
        $this->assertEquals(302, $response->getStatusCode());
    }

    public function testEditDuplicate()
    {
        // Since we're the test user we won't automatically have permission to edit.
        $users = $this->getMock('Bolt\Users', ['isAllowed'], [$this->getApp()]);
        $users->expects($this->any())
            ->method('isAllowed')
            ->will($this->returnValue(true));
        $this->setService('users', $users);

        $this->setRequest(Request::create('/bolt/editcontent/pages/4', 'GET', ['duplicate' => true]));
        $original = $this->getService('storage')->getContent('pages/4');
        $response = $this->controller()->edit($this->getRequest(), 'pages', 4);
        $context = $response->getContext();

        // Check that correct fields are equal in new object
        $new = $context['context']['content'];
        $this->assertEquals($new['body'], $original['body']);
        $this->assertEquals($new['title'], $original['title']);
        $this->assertEquals($new['teaser'], $original['teaser']);

        // Check that some have been cleared.
        $this->assertEquals('', $new['id']);
        $this->assertEquals('', $new['slug']);
        $this->assertEquals('', $new['ownerid']);
    }

    public function testEditCSRF()
    {
        $users = $this->getMock('Bolt\Users', ['isAllowed', 'checkAntiCSRFToken'], [$this->getApp()]);
        $users->expects($this->any())
            ->method('isAllowed')
            ->will($this->returnValue(true));
        $users->expects($this->any())
            ->method('checkAntiCSRFToken')
            ->will($this->returnValue(false));

        $this->setService('users', $users);

        $this->setRequest(Request::create('/bolt/editcontent/showcases/3', 'POST'));
        $this->setExpectedException('Symfony\Component\HttpKernel\Exception\HttpException', 'Something went wrong');
        $this->controller()->edit($this->getRequest(), 'showcases', 3);
    }

    public function testEditPermissions()
    {
        $app = $this->getApp();
        $authentication = $this->getMock(
            'Bolt\AccessControl\Authentication', 
            ['checkAntiCSRFToken'], 
            [  
                $app,
                $app['storage']->getRepository('Bolt\Storage\Entity\Authtoken')
            ]
        );
        $authentication->expects($this->any())
            ->method('checkAntiCSRFToken')
            ->will($this->returnValue(true));
        $this->setService('authentication', $authentication);

        $users = $this->getMock('Bolt\Users', ['isAllowed'], [$this->getApp()]);
        $users->expects($this->at(0))
            ->method('isAllowed')
            ->will($this->returnValue(false));
        $this->setService('users', $users);

        // We should get kicked here because we dont have permissions to edit this
        $this->setRequest(Request::create('/bolt/editcontent/showcases/3', 'POST'));
        $response = $this->controller()->edit($this->getRequest(), 'showcases', 3);
        $this->assertEquals('/bolt', $response->getTargetUrl());
    }

    public function testEditPost()
    {
        $app = $this->getApp();
        $authentication = $this->getMock(
            'Bolt\AccessControl\Authentication', 
            ['checkAntiCSRFToken'], 
            [  
                $app,
                $app['storage']->getRepository('Bolt\Storage\Entity\Authtoken')
            ]
        );
        $authentication->expects($this->any())
            ->method('checkAntiCSRFToken')
            ->will($this->returnValue(true));
        $this->setService('authentication', $authentication);

        $users = $this->getMock('Bolt\Users', ['isAllowed'], [$this->getApp()]);
        $users->expects($this->any())
            ->method('isAllowed')
            ->will($this->returnValue(true));
        $this->setService('users', $users);

        $this->setRequest(Request::create('/bolt/editcontent/showcases/3', 'POST', ['floatfield' => 1.2]));
        //$original = $this->getService('storage')->getContent('showcases/3');
        $response = $this->controller()->edit($this->getRequest(), 'showcases', 3);
        $this->assertEquals('/bolt/overview/showcases', $response->getTargetUrl());
    }

    public function testEditPostAjax()
    {
        $app = $this->getApp();
        $authentication = $this->getMock(
            'Bolt\AccessControl\Authentication', 
            ['checkAntiCSRFToken'], 
            [  
                $app,
                $app['storage']->getRepository('Bolt\Storage\Entity\Authtoken')
            ]
        );
        $authentication->expects($this->any())
            ->method('checkAntiCSRFToken')
            ->will($this->returnValue(true));
        $this->setService('authentication', $authentication);

        // Since we're the test user we won't automatically have permission to edit.
        $users = $this->getMock('Bolt\Users', ['isAllowed'], [$this->getApp()]);
        $users->expects($this->any())
            ->method('isAllowed')
            ->will($this->returnValue(true));
        $this->setService('users', $users);

        $this->setRequest(Request::create('/bolt/editcontent/pages/4?returnto=ajax', 'POST'));
{'fraction_non_alphanumeric': 0.19904504400851405, 'fraction_numerical': 0.003911867916930334, 'mean_word_length': 3.037157454714352, 'pattern_counts': {'":': 0, '<': 17, '<?xml version=': 0, '>': 381, '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': '6124500', 'n_tokens_mistral': 4914, 'n_tokens_neox': 4767, 'n_words': 782}