Search code examples
testingphpunittypo3functional-testingtypo3-7.6.x

How to use the LocalizationUtility in test context


I am trying to test a controller action which returns an localized error message. I am Using the static TYPO3\CMS\Extbase\Utility\LocalizationUtility::translate method for the localized error messages. It works when I call the action through my browser. But it'll always return NULL when the controller action is called under a test context. Seems like the localang.xml isn't properly loaded...

Do I need to do some preparation work in order to use the LocalizationUtility ?

My phpunit call:

/var/www/html/vendor/bin/phpunit -c /var/www/html/vendor/typo3/cms/typo3/sysext/core/Build/FunctionalTests.xml /var/www/html/typo3_app/typo3conf/ext/some_ext/Tests/Functional/Controller/SomeControllerTest.php

For reference, my test class:

<?php
namespace SomeExt\Tests\Functional\Controller;

use SomeExt\Controller\SomeController;

use Nimut\TestingFramework\TestCase\FunctionalTestCase;
use TYPO3\CMS\Core\Utility\GeneralUtility;


/**
 * Test class
 *
 */

class SomeControllerTest extends FunctionalTestCase
{
    /**
     * @var SomeController
     */
    protected $controller;

    /**
     * @var \TYPO3\CMS\Extbase\Mvc\Request
     */
    protected $request;

    /**
     * @var \TYPO3\CMS\Fluid\View\TemplateView
     */
    protected $view;

    /**
     * @var array
     */
    protected $settings;

    public function setUp() {
        parent::setUp();

        $OM = GeneralUtility::makeInstance(\TYPO3\CMS\Extbase\Object\ObjectManager::class);

        $this->controller = $OM->get(SomeController::class);
        $this->prepareController();

    }

    public function prepareController() {
        $this->view = $this->getMock(\TYPO3\CMS\Fluid\View\TemplateView::class);
        $this->controller->setView($this->view);
    }


    /**
     * @test
     */
    public function submitFormActionFailure() {

        $submitData = [
           ['some_field1', null],
           ['some_field2', null],
           ['some_field3', null],
           ['some_field4', null]
        ];

        $failRetval = [
            'success' => false,
            'errors' => [
                [
                    'field' => 'field1',
                    'message' => 'This field is mandatory'
                ],
                [
                    'field' => 'field2',
                    'message' => 'This field is mandatory'
                ],
                [
                    'field' => 'field3',
                    'message' => 'This field is mandatory'
                ],
                [
                    'field' => 'field4',
                    'message' => 'This field is mandatory'
                ]
            ]
        ];

        $request = $this->getMock(\TYPO3\CMS\Extbase\Mvc\Request::class);
        $request->method('getArgument')
            ->will(
                $this->returnValueMap($submitData)
            );

        $this->controller->setRequest($request);

        /**
         * will always fail because message is always null
         *
         * assigned value:
         * [
         *  'success' => false,
         *  'errors' => [
         *      [
         *          'field' => 'field1',
         *          'message' => NULL
         *      ],
         *      [
         *          'field' => 'field2',
         *          'message' => NULL
         *      ],
         *      [
         *          'field' => 'field3',
         *          'message' => NULL
         *      ],
         *      [
         *          'field' => 'field4',
         *          'message' => NULL
         *      ]
         *  ]
         * ]
         */
        $this->view->expects($this->at(0))
            ->method('assign')
            ->with('retval', $failRetval);

        $this->controller->submitFormAction();

    }
}

Solution

  • In this case all you need to do is to add your extension key to the $testExtensionsToLoad list, which ensures your translation files are loaded as usual.