function ComponentTranslationTest::testEnumPropsCanBeTranslated

Test that components render enum props correctly with their translations.

File

core/tests/Drupal/KernelTests/Components/ComponentTranslationTest.php, line 66

Class

ComponentTranslationTest
Tests the component can be translated.

Namespace

Drupal\KernelTests\Components

Code

public function testEnumPropsCanBeTranslated() : void {
    $bannerString = $this->buildSourceString([
        'source' => 'Open in a new window',
        'context' => 'Banner link target',
    ]);
    $bannerString->save();
    $ctaString = $this->buildSourceString([
        'source' => 'Open in a new window',
        'context' => 'CTA link target',
    ]);
    $ctaString->save();
    $ctaEmptyString = $this->buildSourceString([
        'source' => 'Open in same window',
        'context' => 'CTA link target',
    ]);
    $ctaEmptyString->save();
    $this->createTranslation($bannerString, 'epa', [
        'translation' => 'Abre er bânnêh en una nueba bentana',
    ]);
    $this->createTranslation($ctaString, 'epa', [
        'translation' => 'Abre er CTA en una nueba bentana',
    ]);
    $this->createTranslation($ctaEmptyString, 'epa', [
        'translation' => 'Abre er CTA en la mîmma bentana',
    ]);
    $build = [
        'banner' => [
            '#type' => 'component',
            '#component' => 'sdc_test:my-banner',
            '#props' => [
                'heading' => 'I am a banner',
                'ctaText' => 'Click me',
                'ctaHref' => 'https://www.example.org',
                'ctaTarget' => '_blank',
            ],
        ],
        'cta' => [
            '#type' => 'component',
            '#component' => 'sdc_test:my-cta',
            '#props' => [
                'text' => 'Click me',
                'href' => 'https://www.example.org',
                'target' => '_blank',
            ],
        ],
        'cta_with_empty_enum' => [
            '#type' => 'component',
            '#component' => 'sdc_test:my-cta',
            '#props' => [
                'text' => 'Click me',
                'href' => 'https://www.example.org',
                'target' => '',
            ],
        ],
    ];
    \Drupal::state()->set('sdc_test_component', $build);
    $response = $this->request(Request::create('sdc-test-component'));
    $crawler = new Crawler($response->getContent());
    // Assert that even if the source is the same, the translations depend on
    // the enum context.
    $this->assertStringContainsString('Abre er bânnêh en una nueba bentana', $crawler->filter('#sdc-wrapper [data-component-id="sdc_test:my-banner"]')
        ->outerHtml());
    $this->assertStringContainsString('Abre er CTA en una nueba bentana', $crawler->filter('#sdc-wrapper a[data-component-id="sdc_test:my-cta"]:nth-of-type(1)')
        ->outerHtml());
    $this->assertStringContainsString('Abre er CTA en la mîmma bentana', $crawler->filter('#sdc-wrapper a[data-component-id="sdc_test:my-cta"]:nth-of-type(2)')
        ->outerHtml());
}

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