Verified Commit b401495d authored by Lee Rowlands's avatar Lee Rowlands
Browse files

Issue #3528998 by grimreaper, larowlan, pdureau, xjm, wim leers, catch,...

Issue #3528998 by grimreaper, larowlan, pdureau, xjm, wim leers, catch, penyaskito: Follow-up: SDC `enum` props should have translatable labels: use `meta:enum`

(cherry picked from commit 166350f2)
parent 9873ee64
Loading
Loading
Loading
Loading
Loading
+23 −53
Original line number Diff line number Diff line
@@ -5,6 +5,7 @@
use Drupal\Core\Extension\ExtensionLifecycle;
use Drupal\Core\StringTranslation\StringTranslationTrait;
use Drupal\Core\Render\Component\Exception\InvalidComponentException;
use Drupal\Core\StringTranslation\TranslatableMarkup;

/**
 * Component metadata.
@@ -168,17 +169,29 @@ private function parseSchemaInfo(array $metadata_info): ?array {
        throw new InvalidComponentException('The schema for the %s in the component metadata is invalid. Arbitrary additional properties are not allowed.');
      }
      $schema['additionalProperties'] = FALSE;
      // All props should also support "object" this allows deferring rendering
      // in Twig to the render pipeline.
      $schema_props = $metadata_info['props'];
      foreach ($schema_props['properties'] ?? [] as $name => $prop_schema) {
        $type = $prop_schema['type'] ?? '';
        if (isset($prop_schema['enum'], $prop_schema['meta:enum'])) {
          $enum_keys_diff = array_diff($prop_schema['enum'], array_keys($prop_schema['meta:enum']));
          if (!empty($enum_keys_diff)) {
            throw new InvalidComponentException(sprintf('The values for the %s prop enum in component %s must be defined in meta:enum.', $name, $this->id));
          }
      foreach ($schema['properties'] ?? [] as $name => $prop_schema) {
        if (isset($prop_schema['enum'])) {
          // Ensure all enum values are also in meta:enum.
          $enum = array_combine($prop_schema['enum'], $prop_schema['enum']);
          $prop_schema['meta:enum'] = array_replace($enum, $prop_schema['meta:enum'] ?? []);

          // Remove meta:enum values which are not in enum.
          $prop_schema['meta:enum'] = array_intersect_key($prop_schema['meta:enum'], $enum);

          // Make meta:enum label translatable.
          $translation_context = $prop_schema['x-translation-context'] ?? '';
          $prop_schema['meta:enum'] = array_map(
            // @phpcs:ignore Drupal.Semantics.FunctionT.NotLiteralString
            fn($label) => new TranslatableMarkup((string) $label, [], ['context' => $translation_context]),
            $prop_schema['meta:enum']
          );

          $schema['properties'][$name] = $prop_schema;
        }

        // All props should also support "object" this allows deferring
        // rendering in Twig to the render pipeline.
        $type = $prop_schema['type'] ?? '';
        $schema['properties'][$name]['type'] = array_unique([
          ...(array) $type,
          'object',
@@ -209,14 +222,6 @@ public function getThumbnailPath(): string {
   *   The normalized value object.
   */
  public function normalize(): array {
    $meta = [];
    if (!empty($this->schema['properties'])) {
      foreach ($this->schema['properties'] as $prop_name => $prop_definition) {
        if (!empty($prop_definition['meta:enum'])) {
          $meta['properties'][$prop_name] = $this->getEnumOptions($prop_name);
        }
      }
    }
    return [
      'path' => $this->path,
      'machineName' => $this->machineName,
@@ -224,42 +229,7 @@ public function normalize(): array {
      'name' => $this->name,
      'group' => $this->group,
      'variants' => $this->variants,
      'meta' => $meta,
    ];
  }

  /**
   * Get translated options labels from enumeration.
   *
   * @param string $propertyName
   *   The enum property name.
   *
   * @return array<string, \Drupal\Core\StringTranslation\TranslatableMarkup>
   *   An array with enum options as keys and the (non-rendered)
   *   translated labels as values.
   */
  public function getEnumOptions(string $propertyName): array {
    $options = [];
    if (isset($this->schema['properties'][$propertyName])) {
      $prop_definition = $this->schema['properties'][$propertyName];
      if (!empty($prop_definition['enum'])) {
        $translation_context = $prop_definition['x-translation-context'] ?? '';
        // We convert ['a', 'b'], into ['a' => t('a'), 'b' => t('b')].
        $options = array_combine(
          $prop_definition['enum'],
          // @phpcs:ignore Drupal.Semantics.FunctionT.NotLiteralString
          array_map(fn($value) => $this->t($value, [], ['context' => $translation_context]), $prop_definition['enum']),
        );
        if (!empty($prop_definition['meta:enum'])) {
          foreach ($prop_definition['meta:enum'] as $enum_value => $enum_label) {
            $options[$enum_value] =
              // @phpcs:ignore Drupal.Semantics.FunctionT.NotLiteralString
              $this->t($enum_label, [], ['context' => $translation_context]);
          }
        }
      }
    }
    return $options;
  }

}
+0 −1
Original line number Diff line number Diff line
@@ -7,7 +7,6 @@
<div {{ attributes }}>
  <div class="component--my-banner--header">
    <h3>{{ heading }}</h3>
    <p>CTA target selected value: {{ componentMetadata.meta.properties.ctaTarget[ctaTarget] }}</p>
    {% include 'sdc_test:my-cta' with { text: ctaText, href: ctaHref, target: ctaTarget } only %}
  </div>
  <div class="component--my-banner--body">
+1 −1
Original line number Diff line number Diff line
@@ -6,5 +6,5 @@
{% endif %}

<a {{ attributes }} href="{{ href }}">
  {{ text }} ({{ componentMetadata.meta.properties.target[target] }})
  {{ text }}
</a>
+0 −189
Original line number Diff line number Diff line
<?php

declare(strict_types=1);

// cspell:ignore Abre er bânnêh en una nueba bentana la mîmma

namespace Drupal\KernelTests\Components;

use Drupal\language\Entity\ConfigurableLanguage;
use Drupal\locale\StringInterface;
use Drupal\locale\StringStorageInterface;
use Symfony\Component\DomCrawler\Crawler;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\HttpKernel\HttpKernelInterface;
use Symfony\Component\HttpKernel\TerminableInterface;

/**
 * Tests the component can be translated.
 *
 * @group sdc
 */
class ComponentTranslationTest extends ComponentKernelTestBase {

  /**
   * {@inheritdoc}
   */
  protected static $modules = [
    'system',
    'sdc_test',
    'locale',
    'language',
  ];

  /**
   * {@inheritdoc}
   */
  protected static $themes = ['sdc_theme_test'];

  /**
   * The locale storage.
   *
   * @var \Drupal\locale\StringStorageInterface
   */
  protected StringStorageInterface $storage;

  /**
   * {@inheritdoc}
   */
  protected function setUp(): void {
    parent::setUp();
    // Add a default locale storage for all these tests.
    $this->storage = $this->container->get('locale.storage');
    ConfigurableLanguage::createFromLangcode('epa')->save();
    $this->container->get('string_translation')->setDefaultLangcode('epa');
    $this->installSchema('locale', [
      'locales_location',
      'locales_source',
      'locales_target',
    ]);
  }

  /**
   * Test that components render enum props correctly with their translations.
   */
  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());
  }

  /**
   * Creates random source string object.
   *
   * @param array $values
   *   The values array.
   *
   * @return \Drupal\locale\StringInterface
   *   A locale string.
   */
  protected function buildSourceString(array $values = []): StringInterface {
    return $this->storage->createString($values += [
      'source' => $this->randomMachineName(100),
      'context' => $this->randomMachineName(20),
    ]);
  }

  /**
   * Creates single translation for source string.
   *
   * @param \Drupal\locale\StringInterface $source
   *   The source string.
   * @param string $langcode
   *   The language code.
   * @param array $values
   *   The values array.
   *
   * @return \Drupal\locale\StringInterface
   *   The translated string object.
   */
  protected function createTranslation(StringInterface $source, $langcode, array $values = []): StringInterface {
    return $this->storage->createTranslation($values + [
      'lid' => $source->lid,
      'language' => $langcode,
      'translation' => $this->randomMachineName(100),
    ])->save();
  }

  /**
   * Passes a request to the HTTP kernel and returns a response.
   *
   * @param \Symfony\Component\HttpFoundation\Request $request
   *   The request.
   *
   * @return \Symfony\Component\HttpFoundation\Response
   *   The response.
   */
  protected function request(Request $request): Response {
    // @todo We should replace this when https://drupal.org/i/3390193 lands.
    // Reset the request stack.
    // \Drupal\KernelTests\KernelTestBase::bootKernel() pushes a bogus request
    // to boot the kernel, but it is also needed for any URL generation in tests
    // to work. We also need to reset the request stack every time we make a
    // request.
    $request_stack = $this->container->get('request_stack');
    while ($request_stack->getCurrentRequest() !== NULL) {
      $request_stack->pop();
    }

    $http_kernel = $this->container->get('http_kernel');
    self::assertInstanceOf(HttpKernelInterface::class, $http_kernel);
    $response = $http_kernel->handle($request, HttpKernelInterface::MAIN_REQUEST, FALSE);
    $content = $response->getContent();
    self::assertNotFalse($content);
    $this->setRawContent($content);

    self::assertInstanceOf(TerminableInterface::class, $http_kernel);
    $http_kernel->terminate($request, $response);

    return $response;
  }

}
+115 −78
Original line number Diff line number Diff line
@@ -4,7 +4,7 @@

namespace Drupal\Tests\Core\Theme\Component;

use Drupal\Core\DependencyInjection\ContainerBuilder;
use Drupal\Core\StringTranslation\TranslatableMarkup;
use Drupal\Core\Theme\Component\ComponentMetadata;
use Drupal\Core\Render\Component\Exception\InvalidComponentException;
use Drupal\Tests\UnitTestCaseTest;
@@ -22,11 +22,7 @@ class ComponentMetadataTest extends UnitTestCaseTest {
   * Tests that the correct data is returned for each property.
   */
  #[DataProvider('dataProviderMetadata')]
  public function testMetadata(array $metadata_info, array $expectations, bool $missing_schema, ?\Throwable $expectedException = NULL): void {
    if ($expectedException !== NULL) {
      $this->expectException($expectedException::class);
      $this->expectExceptionMessage($expectedException->getMessage());
    }
  public function testMetadata(array $metadata_info, array $expectations): void {
    $metadata = new ComponentMetadata($metadata_info, 'foo/', FALSE);
    $this->assertSame($expectations['path'], $metadata->path);
    $this->assertSame($expectations['status'], $metadata->status);
@@ -38,23 +34,17 @@ public function testMetadata(array $metadata_info, array $expectations, bool $mi
   * Tests the correct checks when enforcing schemas or not.
   */
  #[DataProvider('dataProviderMetadata')]
  public function testMetadataEnforceSchema(array $metadata_info, array $expectations, bool $missing_schema, ?\Throwable $expected_exception = NULL): void {
  public function testMetadataEnforceSchema(array $metadata_info, array $expectations, bool $missing_schema): void {
    if ($missing_schema) {
      $this->expectException(InvalidComponentException::class);
      $this->expectExceptionMessage('The component "' . $metadata_info['id'] . '" does not provide schema information. Schema definitions are mandatory for components declared in modules. For components declared in themes, schema definitions are only mandatory if the "enforce_prop_schemas" key is set to "true" in the theme info file.');
      new ComponentMetadata($metadata_info, 'foo/', TRUE);
    }
    else {
      if ($expected_exception !== NULL) {
        $this->expectException($expected_exception::class);
        $this->expectExceptionMessage($expected_exception->getMessage());
      }
      new ComponentMetadata($metadata_info, 'foo/', TRUE);
      if ($expected_exception === NULL) {
      $this->expectNotToPerformAssertions();
    }
  }
  }

  /**
   * Data provider for the test testMetadataEnforceSchema.
@@ -140,6 +130,11 @@ public static function dataProviderMetadata(): array {
                  'like',
                  'external',
                ],
                'meta:enum' => [
                  'power' => new TranslatableMarkup('power', [], ['context' => '']),
                  'like' => new TranslatableMarkup('like', [], ['context' => '']),
                  'external' => new TranslatableMarkup('external', [], ['context' => '']),
                ],
              ],
            ],
          ],
@@ -211,16 +206,71 @@ public static function dataProviderMetadata(): array {
                  'external',
                ],
                'meta:enum' => [
                  'power' => 'Power',
                  'fav' => 'Favorite',
                  'external' => 'External',
                  'power' => new TranslatableMarkup('Power', [], ['context' => '']),
                  'like' => new TranslatableMarkup('like', [], ['context' => '']),
                  'external' => new TranslatableMarkup('External', [], ['context' => '']),
                ],
              ],
            ],
          ],
        ],
        FALSE,
      ],
      'complete example with schema, but no meta:enum, prop value not as string' => [
        [
          '$schema' => 'https://git.drupalcode.org/project/drupal/-/raw/HEAD/core/assets/schemas/v1/metadata.schema.json',
          'id' => 'core:my-button',
          'machineName' => 'my-button',
          'path' => 'foo/my-other/path',
          'name' => 'Button',
          'description' => 'JavaScript enhanced button that tracks the number of times a user clicked it.',
          'libraryOverrides' => ['dependencies' => ['core/drupal']],
          'group' => 'my-group',
          'props' => [
            'type' => 'object',
            'required' => ['text'],
            'properties' => [
              'col' => [
                'type' => 'string',
                'title' => 'Column',
                'enum' => [
                  1,
                  2,
                  3,
                ],
              ],
            ],
          ],
        ],
        [
          'path' => 'my-other/path',
          'status' => 'stable',
          'thumbnail' => '',
          'group' => 'my-group',
          'additionalProperties' => FALSE,
          'props' => [
            'type' => 'object',
            'required' => ['text'],
            'additionalProperties' => FALSE,
            'properties' => [
              'col' => [
                'type' => ['string', 'object'],
                'title' => 'Column',
                'enum' => [
                  1,
                  2,
                  3,
                ],
                'meta:enum' => [
                  1 => new TranslatableMarkup('1', [], ['context' => '']),
                  2 => new TranslatableMarkup('2', [], ['context' => '']),
                  3 => new TranslatableMarkup('3', [], ['context' => '']),
                ],
              ],
            ],
          ],
        ],
        FALSE,
        new InvalidComponentException('The values for the iconType prop enum in component core:my-button must be defined in meta:enum.'),
      ],
      'complete example with schema (including meta:enum)' => [
        [
@@ -287,9 +337,9 @@ public static function dataProviderMetadata(): array {
                  'external',
                ],
                'meta:enum' => [
                  'power' => 'Power',
                  'like' => 'Like',
                  'external' => 'External',
                  'power' => new TranslatableMarkup('Power', [], ['context' => '']),
                  'like' => new TranslatableMarkup('Like', [], ['context' => '']),
                  'external' => new TranslatableMarkup('External', [], ['context' => '']),
                ],
              ],
            ],
@@ -363,9 +413,9 @@ public static function dataProviderMetadata(): array {
                  'external',
                ],
                'meta:enum' => [
                  'power' => 'Power',
                  'like' => 'Like',
                  'external' => 'External',
                  'power' => new TranslatableMarkup('Power', [], ['context' => 'Icon Type']),
                  'like' => new TranslatableMarkup('Like', [], ['context' => 'Icon Type']),
                  'external' => new TranslatableMarkup('External', [], ['context' => 'Icon Type']),
                ],
                'x-translation-context' => 'Icon Type',
              ],
@@ -374,93 +424,80 @@ public static function dataProviderMetadata(): array {
        ],
        FALSE,
      ],
    ];
  }

  public static function dataProviderEnumOptionsMetadata(): array {
    $common_schema = [
      'complete example with schema (including meta:enum and x-translation-context and an empty value)' => [
        [
          '$schema' => 'https://git.drupalcode.org/project/drupal/-/raw/HEAD/core/assets/schemas/v1/metadata.schema.json',
          'id' => 'core:my-button',
          'machineName' => 'my-button',
          'path' => 'foo/my-other/path',
          'name' => 'Button',
    ];
    return [
      'no meta:enum' => [$common_schema +
        [
          'description' => 'JavaScript enhanced button that tracks the number of times a user clicked it.',
          'libraryOverrides' => ['dependencies' => ['core/drupal']],
          'group' => 'my-group',
          'props' => [
            'type' => 'object',
            'required' => ['text'],
            'properties' => [
              'iconType' => [
              'text' => [
                'type' => 'string',
                'title' => 'Title',
                'description' => 'The title for the button',
                'minLength' => 2,
                'examples' => ['Press', 'Submit now'],
              ],
              'target' => [
                'type' => 'string',
                'title' => 'Icon Type',
                'enum' => [
                  'power',
                  'like',
                  'external',
                ],
                  '',
                  '_blank',
                ],
                'meta:enum' => [
                  '' => 'Opens in same window',
                  '_blank' => 'Opens in new window',
                ],
                'x-translation-context' => 'Link target',
              ],
            ],
        'iconType',
        [
          'power' => 'power',
          'like' => 'like',
          'external' => 'external',
          ],
        '',
        ],
      'meta:enum, with x-translation-context' => [$common_schema +
        [
          'path' => 'my-other/path',
          'status' => 'stable',
          'thumbnail' => '',
          'group' => 'my-group',
          'additionalProperties' => FALSE,
          'props' => [
            'type' => 'object',
            'required' => ['text'],
            'additionalProperties' => FALSE,
            'properties' => [
              'text' => [
                'type' => ['string', 'object'],
                'title' => 'Title',
                'description' => 'The title for the button',
                'minLength' => 2,
                'examples' => ['Press', 'Submit now'],
              ],
              'target' => [
                'type' => 'string',
                'type' => ['string', 'object'],
                'title' => 'Icon Type',
                'enum' => [
                  '',
                  '_blank',
                ],
                'meta:enum' => [
                  '' => 'Opens in same window',
                  '_blank' => 'Opens in new window',
                  '' => new TranslatableMarkup('Opens in same window', [], ['context' => 'Link target']),
                  '_blank' => new TranslatableMarkup('Opens in new window', [], ['context' => 'Link target']),
                ],
                'x-translation-context' => 'Link target',
              ],
            ],
          ],
        ],
        'target',
        [
          '' => 'Opens in same window',
          '_blank' => 'Opens in new window',
        ],
        'Link target',
        FALSE,
      ],
    ];
  }

  /**
   * @covers ::getEnumOptions
   */
  #[DataProvider('dataProviderEnumOptionsMetadata')]
  public function testGetEnumOptions(array $metadata_info, string $prop_name, array $expected_values, string $expected_context): void {
    $translation = $this->getStringTranslationStub();
    $container = new ContainerBuilder();
    $container->set('string_translation', $translation);
    \Drupal::setContainer($container);

    $component_metadata = new ComponentMetadata($metadata_info, 'foo/', TRUE);
    $options = $component_metadata->getEnumOptions($prop_name);
    $rendered_options = array_map(fn($value) => (string) $value, $options);
    $this->assertSame($expected_values, $rendered_options);
    foreach ($options as $translatable) {
      $this->assertSame($expected_context, $translatable->getOption('context'));
    }
  }

}
Loading