<commit_before><?php

namespace Vierwd\VierwdSmarty\Tests\Unit\View;

use Vierwd\VierwdSmarty\View\SmartyView;
use TYPO3\CMS\Core\Tests\UnitTestCase;
use TYPO3\CMS\Extbase\Mvc\Web\Request as WebRequest;
use TYPO3\CMS\Extbase\Mvc\Controller\ControllerContext;

class SmartyViewTest extends UnitTestCase {

	// function getAccessibleMock($originalClassName, $methods = [], array $arguments = [], $mockClassName = '', $callOriginalConstructor = true, $callOriginalClone = true, $callAutoload = true);

	/**
     * Helper to build mock controller context needed to test expandGenericPathPattern.
     *
     * @param string $packageKey
     * @param string $subPackageKey
     * @param string $controllerName
     * @param string $format
     * @return ControllerContext
     */
    protected function setupMockControllerContext($packageKey, $subPackageKey, $controllerName, $action, $format) {
        $controllerObjectName = "TYPO3\\$packageKey\\" . ($subPackageKey != $subPackageKey . '\\' ? : '') . 'Controller\\' . $controllerName . 'Controller';
        $mockRequest = $this->getMock(WebRequest::class);
        $mockRequest->expects($this->any())->method('getControllerExtensionKey')->will($this->returnValue('vierwd_smarty'));
        $mockRequest->expects($this->any())->method('getControllerPackageKey')->will($this->returnValue($packageKey));
        $mockRequest->expects($this->any())->method('getControllerSubPackageKey')->will($this->returnValue($subPackageKey));
        $mockRequest->expects($this->any())->method('getControllerName')->will($this->returnValue($controllerName));
        $mockRequest->expects($this->any())->method('getControllerActionName')->will($this->returnValue($action));
        $mockRequest->expects($this->any())->method('getControllerObjectName')->will($this->returnValue($controllerObjectName));
        $mockRequest->expects($this->any())->method('getFormat')->will($this->returnValue($format));

        $mockControllerContext = $this->getMock(ControllerContext::class, ['getRequest'], [], '', false);
        $mockControllerContext->expects($this->any())->method('getRequest')->will($this->returnValue($mockRequest));

        return $mockControllerContext;
    }

	/**
	 * @test
	 */
	public function getTemplateRootPathsReturnsUserSpecifiedTemplatePaths() {
		/** @var SmartyView|\PHPUnit_Framework_MockObject_MockObject|\TYPO3\CMS\Core\Tests\AccessibleObjectInterface $templateView */
		$templateView = $this->getAccessibleMock(SmartyView::class, ['dummy'], [], '', false);
		$templateView->setTemplateRootPaths(['/foo/bar']);
		$expected = ['/foo/bar'];
		$actual = $templateView->_call('getTemplateRootPaths');
		$this->assertEquals($expected, $actual, 'A set template root path was not returned correctly.');
	}

	/**
	 * @test
	 */
	public function calculateTemplatePath() {
		$mockControllerContext = $this->setupMockControllerContext('MyPackage', null, 'My', 'action', 'tpl');

		$templateView = $this->getAccessibleMock(SmartyView::class, null, [], '', false);
		$templateView->_set('controllerContext', $mockControllerContext);

		$expected = 'vierwd_smarty/Resources/Private/Templates';
		$actual = $templateView->_call('getTemplateRootPaths');
		$this->assertInternalType('array', $actual);
		$this->assertStringEndsWith($expected, $actual[0]);
	}

	/**
	 * @test
	 */
	public function canRenderFindsTemplate() {
		$mockControllerContext = $this->setupMockControllerContext('MyPackage', null, 'Controller', 'action', 'html');
		$templateView = $this->getAccessibleMock(SmartyView::class, null, [], '', false);
		$templateView->setControllerContext($mockControllerContext);
		$templateView->setTemplateRootPaths([
			'EXT:vierwd_smarty/Tests/Unit/Fixtures/Templates',
		]);

		$this->assertTrue($templateView->canRender($mockControllerContext));

		$mockControllerContext = $this->setupMockControllerContext('MyPackage', null, 'Controller', 'action', 'tpl');
		$templateView = $this->getAccessibleMock(SmartyView::class, null, [], '', false);
		$templateView->setControllerContext($mockControllerContext);
		$templateView->setTemplateRootPaths([
			'EXT:vierwd_smarty/Tests/Unit/Fixtures/Templates',
		]);

		$this->assertTrue($templateView->canRender($mockControllerContext));
	}

	/**
	 * @test
	 */
	public function renderTemplate() {
		$mockControllerContext = $this->setupMockControllerContext('MyPackage', null, 'Controller', 'action', 'tpl');
		$templateView = $this->getAccessibleMock(SmartyView::class, null, [], '', false);
		$templateView->setControllerContext($mockControllerContext);
		$templateView->setTemplateRootPaths([
			'EXT:vierwd_smarty/Tests/Unit/Fixtures/Templates',
		]);

		$templateView->initializeView();

		$templateView->assign('templateVariable', 'test-string');
		$this->assertEquals('test-string', $templateView->render());

		// assert escaping works
		$templateView->assign('templateVariable', '<b>test</b>');
		$this->assertNotEquals('<b>test</b>', $templateView->render());
	}

	/**
	 * Smarty 3.1.30 had a bug with array_shift in loops.
	 * @see https://github.com/smarty-php/smarty/issues/291
	 * @test
	 */
	public function checkSimpleTemplateLogic() {
		$mockControllerContext = $this->setupMockControllerContext('MyPackage', null, 'Controller', 'action', 'tpl');
		$templateView = $this->getAccessibleMock(SmartyView::class, null, [], '', false);
		$templateView->setControllerContext($mockControllerContext);
		$templateView->setTemplateRootPaths([
			'EXT:vierwd_smarty/Tests/Unit/Fixtures/Templates',
		]);

		$templateView->initializeView();

		$this->assertEquals('44321', $templateView->render('TemplateLogic.tpl'));
	}

	/**
	 * @test
	 */
	public function checkTemplateWhitespaceIsStripped() {
		$mockControllerContext = $this->setupMockControllerContext('MyPackage', null, 'Controller', 'action', 'tpl');
		$templateView = $this->getAccessibleMock(SmartyView::class, null, [], '', false);
		$templateView->setControllerContext($mockControllerContext);
		$templateView->setTemplateRootPaths([
			'EXT:vierwd_smarty/Tests/Unit/Fixtures/Templates',
		]);

		$templateView->initializeView();

		$this->assertEquals('LineBreaks and trailing spacesmultiple breaks', $templateView->render('Whitespace.tpl'));
	}

	/**
	 * @test
	 */
	public function testEmailLink() {
		$mockControllerContext = $this->setupMockControllerContext('MyPackage', null, 'Controller', 'action', 'tpl');
		$templateView = $this->getAccessibleMock(SmartyView::class, null, [], '', false);
		$templateView->setControllerContext($mockControllerContext);
		$templateView->setTemplateRootPaths([
			'EXT:vierwd_smarty/Tests/Unit/Fixtures/Templates',
		]);<commit_msg>Fixed TemplatePaths in tests<commit_after><?php

namespace Vierwd\VierwdSmarty\Tests\Unit\View;

use Vierwd\VierwdSmarty\View\SmartyView;
use TYPO3\CMS\Core\Tests\UnitTestCase;
use TYPO3\CMS\Core\Utility\GeneralUtility;
use TYPO3\CMS\Extbase\Mvc\Web\Request as WebRequest;
use TYPO3\CMS\Extbase\Mvc\Controller\ControllerContext;

class SmartyViewTest extends UnitTestCase {

	// function getAccessibleMock($originalClassName, $methods = [], array $arguments = [], $mockClassName = '', $callOriginalConstructor = true, $callOriginalClone = true, $callAutoload = true);

	/**
     * Helper to build mock controller context needed to test expandGenericPathPattern.
     *
     * @param string $packageKey
     * @param string $subPackageKey
     * @param string $controllerName
     * @param string $format
     * @return ControllerContext
     */
    protected function setupMockControllerContext($packageKey, $subPackageKey, $controllerName, $action, $format) {
        $controllerObjectName = "TYPO3\\$packageKey\\" . ($subPackageKey != $subPackageKey . '\\' ? : '') . 'Controller\\' . $controllerName . 'Controller';
        $mockRequest = $this->getMock(WebRequest::class);
        $mockRequest->expects($this->any())->method('getControllerExtensionKey')->will($this->returnValue('vierwd_smarty'));
        $mockRequest->expects($this->any())->method('getControllerPackageKey')->will($this->returnValue($packageKey));
        $mockRequest->expects($this->any())->method('getControllerSubPackageKey')->will($this->returnValue($subPackageKey));
        $mockRequest->expects($this->any())->method('getControllerName')->will($this->returnValue($controllerName));
        $mockRequest->expects($this->any())->method('getControllerActionName')->will($this->returnValue($action));
        $mockRequest->expects($this->any())->method('getControllerObjectName')->will($this->returnValue($controllerObjectName));
        $mockRequest->expects($this->any())->method('getFormat')->will($this->returnValue($format));

        $mockControllerContext = $this->getMock(ControllerContext::class, ['getRequest'], [], '', false);
        $mockControllerContext->expects($this->any())->method('getRequest')->will($this->returnValue($mockRequest));

        return $mockControllerContext;
    }

	/**
	 * @test
	 */
	public function getTemplateRootPathsReturnsUserSpecifiedTemplatePaths() {
		/** @var SmartyView|\PHPUnit_Framework_MockObject_MockObject|\TYPO3\CMS\Core\Tests\AccessibleObjectInterface $templateView */
		$templateView = $this->getAccessibleMock(SmartyView::class, ['dummy'], [], '', false);
		$templateView->setTemplateRootPaths(['/foo/bar']);
		$expected = ['/foo/bar'];
		$actual = $templateView->_call('getTemplateRootPaths');
		$this->assertEquals($expected, $actual, 'A set template root path was not returned correctly.');
	}

	/**
	 * @test
	 */
	public function calculateTemplatePath() {
		$mockControllerContext = $this->setupMockControllerContext('MyPackage', null, 'My', 'action', 'tpl');

		$templateView = $this->getAccessibleMock(SmartyView::class, null, [], '', false);
		$templateView->_set('controllerContext', $mockControllerContext);

		$expected = 'vierwd_smarty/Resources/Private/Templates';
		$actual = $templateView->_call('getTemplateRootPaths');
		$this->assertInternalType('array', $actual);
		$this->assertStringEndsWith($expected, $actual[0]);
	}

	/**
	 * @test
	 */
	public function canRenderFindsTemplate() {
		$mockControllerContext = $this->setupMockControllerContext('MyPackage', null, 'Controller', 'action', 'html');
		$templateView = $this->getAccessibleMock(SmartyView::class, null, [], '', false);
		$templateView->setControllerContext($mockControllerContext);
		$templateView->setTemplateRootPaths([
			GeneralUtility::getFileAbsFileName('EXT:vierwd_smarty/Tests/Unit/Fixtures/Templates'),
		]);

		$this->assertTrue($templateView->canRender($mockControllerContext));

		$mockControllerContext = $this->setupMockControllerContext('MyPackage', null, 'Controller', 'action', 'tpl');
		$templateView = $this->getAccessibleMock(SmartyView::class, null, [], '', false);
		$templateView->setControllerContext($mockControllerContext);
		$templateView->setTemplateRootPaths([
			GeneralUtility::getFileAbsFileName('EXT:vierwd_smarty/Tests/Unit/Fixtures/Templates'),
		]);

		$this->assertTrue($templateView->canRender($mockControllerContext));
	}

	/**
	 * @test
	 */
	public function renderTemplate() {
		$mockControllerContext = $this->setupMockControllerContext('MyPackage', null, 'Controller', 'action', 'tpl');
		$templateView = $this->getAccessibleMock(SmartyView::class, null, [], '', false);
		$templateView->setControllerContext($mockControllerContext);
		$templateView->setTemplateRootPaths([
			GeneralUtility::getFileAbsFileName('EXT:vierwd_smarty/Tests/Unit/Fixtures/Templates'),
		]);

		$templateView->initializeView();

		$templateView->assign('templateVariable', 'test-string');
		$this->assertEquals('test-string', $templateView->render());

		// assert escaping works
		$templateView->assign('templateVariable', '<b>test</b>');
		$this->assertNotEquals('<b>test</b>', $templateView->render());
	}

	/**
	 * Smarty 3.1.30 had a bug with array_shift in loops.
	 * @see https://github.com/smarty-php/smarty/issues/291
	 * @test
	 */
	public function checkSimpleTemplateLogic() {
		$mockControllerContext = $this->setupMockControllerContext('MyPackage', null, 'Controller', 'action', 'tpl');
		$templateView = $this->getAccessibleMock(SmartyView::class, null, [], '', false);
		$templateView->setControllerContext($mockControllerContext);
		$templateView->setTemplateRootPaths([
			GeneralUtility::getFileAbsFileName('EXT:vierwd_smarty/Tests/Unit/Fixtures/Templates'),
		]);

		$templateView->initializeView();

		$this->assertEquals('44321', $templateView->render('TemplateLogic.tpl'));
	}

	/**
	 * @test
	 */
	public function checkTemplateWhitespaceIsStripped() {
		$mockControllerContext = $this->setupMockControllerContext('MyPackage', null, 'Controller', 'action', 'tpl');
		$templateView = $this->getAccessibleMock(SmartyView::class, null, [], '', false);
		$templateView->setControllerContext($mockControllerContext);
		$templateView->setTemplateRootPaths([
			GeneralUtility::getFileAbsFileName('EXT:vierwd_smarty/Tests/Unit/Fixtures/Templates'),
		]);

		$templateView->initializeView();

		$this->assertEquals('LineBreaks and trailing spacesmultiple breaks', $templateView->render('Whitespace.tpl'));
	}

	/**
	 * @test
	 */
	public function testEmailLink() {
		$mockControllerContext = $this->setupMockControllerContext('MyPackage', null, 'Controller', 'action', 'tpl');
		$templateView = $this->getAccessibleMock(SmartyView::class, null, [], '', false);
		$templateView->setControllerContext($mockControllerContext);
		$templateView->setTemplateRootPaths([
			GeneralUtility::getFileAbsFileName('EXT:vierwd_smarty/Tests/Unit/Fixtures/Templates'),
		]);
{'fraction_non_alphanumeric': 0.17175966932388545, 'fraction_numerical': 0.002731030410392678, 'mean_word_length': 6.1687830687830685, 'pattern_counts': {'":': 0, '<': 13, '<?xml version=': 0, '>': 197, 'https://': 2, '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': '953174', 'n_tokens_mistral': 4104, 'n_tokens_neox': 3886, 'n_words': 539}