TranslationTest.php

Same filename in this branch
  1. 11.x core/modules/field/tests/src/Kernel/TranslationTest.php
Same filename and directory in other branches
  1. 9 core/modules/field/tests/src/Kernel/TranslationTest.php
  2. 9 core/tests/Drupal/Tests/Core/Annotation/TranslationTest.php
  3. 8.9.x core/modules/field/tests/src/Kernel/TranslationTest.php
  4. 8.9.x core/tests/Drupal/Tests/Core/Annotation/TranslationTest.php
  5. 10 core/modules/field/tests/src/Kernel/TranslationTest.php
  6. 10 core/tests/Drupal/Tests/Core/Annotation/TranslationTest.php

Namespace

Drupal\Tests\Core\Annotation

File

core/tests/Drupal/Tests/Core/Annotation/TranslationTest.php

View source
<?php

declare (strict_types=1);
namespace Drupal\Tests\Core\Annotation;

use Drupal\Core\Annotation\Translation;
use Drupal\Core\DependencyInjection\ContainerBuilder;
use Drupal\Tests\UnitTestCase;
use Drupal\TestTools\Random;

/**
 * @coversDefaultClass \Drupal\Core\Annotation\Translation
 * @group Annotation
 */
class TranslationTest extends UnitTestCase {
    
    /**
     * The translation manager used for testing.
     *
     * @var \Drupal\Core\StringTranslation\TranslationInterface|\PHPUnit\Framework\MockObject\MockObject
     */
    protected $translationManager;
    
    /**
     * {@inheritdoc}
     */
    protected function setUp() : void {
        parent::setUp();
        $this->translationManager = $this->getStringTranslationStub();
    }
    
    /**
     * @covers ::get
     *
     * @dataProvider providerTestGet
     */
    public function testGet(array $values, $expected) : void {
        $container = new ContainerBuilder();
        $container->set('string_translation', $this->translationManager);
        \Drupal::setContainer($container);
        $annotation = new Translation($values);
        $this->assertSame($expected, (string) $annotation->get());
    }
    
    /**
     * Provides data to self::testGet().
     */
    public static function providerTestGet() {
        $data = [];
        $data[] = [
            [
                'value' => 'Foo',
            ],
            'Foo',
        ];
        $random = Random::machineName();
        $random_html_entity = '&' . $random;
        $data[] = [
            [
                'value' => 'Foo @bar @baz %qux',
                'arguments' => [
                    '@bar' => $random,
                    '@baz' => $random_html_entity,
                    '%qux' => $random_html_entity,
                ],
                'context' => Random::machineName(),
            ],
            'Foo ' . $random . ' &amp;' . $random . ' <em class="placeholder">&amp;' . $random . '</em>',
        ];
        return $data;
    }

}

Classes

Title Deprecated Summary
TranslationTest @coversDefaultClass \Drupal\Core\Annotation\Translation @group Annotation

Buggy or inaccurate documentation? Please file an issue. Need support? Need help programming? Connect with the Drupal community.