1

I'm trying to test a method from the dev version of the Allow a content type only once (Only One) module that have a t() call inside:

<?php

namespace Drupal\onlyone\Utility;

class InternalFunctions {


  public static function getLanguageLabel($language) {
    switch ($language) {
      case 'und':
      case '':
        // If the language is empty then is Undetermined.
        return t('Not specified');

      case 'zxx':
        return t('Not applicable');

      default:
        return ucfirst($language);
    }
  }

}

With the following Unit test:

<?php

namespace Drupal\Tests\onlyone\Unit;

use Drupal\onlyone\Utility\InternalFunctions;
use Drupal\Tests\UnitTestCase;


class InternalFunctionsTest extends UnitTestCase {


  public function testGetLanguageLabel($language_code, $expected) {
    $this->assertEquals($expected, InternalFunctions::getLanguageLabel($language_code));
  }

  public function providerGetLanguageLabel() {
    $tests[] = ['', 'Not specified'];
    $tests[] = ['und', 'Not specified'];
    $tests[] = ['zxx', 'Not applicable'];
    $tests[] = ['es', 'Es'];
    $tests[] = ['en', 'En'];

    return $tests;
  }
}

and when I run the test I receive these errors:

1) Drupal\Tests\onlyone\Unit\InternalFunctionsTest::testGetLanguageLabel with data set #0 ('', 'Not specified')
Error: Call to undefined function Drupal\onlyone\Utility\t()

/Users/adriancid/Sites/drupal84-test/modules/drupal.org/onlyone/src/Utility/InternalFunctions.php:26
/Users/adriancid/Sites/drupal84-test/modules/drupal.org/onlyone/tests/src/Unit/InternalFunctionsTest.php:30

2) Drupal\Tests\onlyone\Unit\InternalFunctionsTest::testGetLanguageLabel with data set #1 ('und', 'Not specified')
Error: Call to undefined function Drupal\onlyone\Utility\t()

/Users/adriancid/Sites/drupal84-test/modules/drupal.org/onlyone/src/Utility/InternalFunctions.php:26
/Users/adriancid/Sites/drupal84-test/modules/drupal.org/onlyone/tests/src/Unit/InternalFunctionsTest.php:30

3) Drupal\Tests\onlyone\Unit\InternalFunctionsTest::testGetLanguageLabel with data set #2 ('zxx', 'Not applicable')
Error: Call to undefined function Drupal\onlyone\Utility\t()

But the t() function is always defined inside Drupal. If I remove the t() calls inside the method the test works fine.

I found in Unit testing more complicated Drupal classes that:

... global functions do not exist in the testing environment.

And a method to made this works, but I find that this is complicate things just to use a test and method with the t() function inside.

So, there is not a simple method to test a class method that have a t() call inside?

  • 2
    Don't have time for a full answer, but you can create a shadow function in your test namespace to define t(). TimeTest in core does something similar. – mpdonadio Mar 13 '18 at 18:30
1

You should not be using t() inside classes. The string translation trait should be included instead (if it is not already part of a parent class). Then you will use:

$this->t()

Then you can use the answer I provided here as to how to do unit tests with string translation trait: How to unit test a class which uses a trait?

  • The method is static I can't use $this inside. – Adrian Cid Almaguer Mar 14 '18 at 11:37
  • Ahh I missed that. Then the method you found is the correct way to go about it. – Jaypan Mar 14 '18 at 11:48
  • I think that you should keep your answer here as a guide. Please don't remove it. – Adrian Cid Almaguer Mar 14 '18 at 13:24
1

Thanks to @mpdonadio I have one way to solve this:

In the test file you need to add a namespace with the same namespace where is the method that you're trying to test and then define the function.

namespace Drupal\onlyone\Utility;

/**
 * Shadow t() system call.
 *
 * @param string $string
 *   A string containing the English text to translate.
 *
 * @return string
 */
function t($string) {
  return $string;
}

Maybe there are another method because I'm still wondering how is possible that in the Webform module a test named testToString() don't use this approach and works for a method with a t() call inside: WebformArrayHelper::toString().

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.