class ComponentMetadataTest

Same name and namespace in other branches
  1. 11.x core/tests/Drupal/Tests/Core/Theme/Component/ComponentMetadataTest.php \Drupal\Tests\Core\Theme\Component\ComponentMetadataTest
  2. 10 core/tests/Drupal/Tests/Core/Theme/Component/ComponentMetadataTest.php \Drupal\Tests\Core\Theme\Component\ComponentMetadataTest

Unit tests for the component metadata class.

Attributes

#[CoversClass(ComponentMetadata::class)] #[Group('sdc')]

Hierarchy

Expanded class hierarchy of ComponentMetadataTest

File

core/tests/Drupal/Tests/Core/Theme/Component/ComponentMetadataTest.php, line 18

Namespace

Drupal\Tests\Core\Theme\Component
View source
class ComponentMetadataTest extends UnitTestCase {
  
  /**
   * Tests that the correct data is returned for each property.
   */
  public function testMetadata(array $metadata_info, array $expectations, bool $missing_schema) : void {
    $metadata = new ComponentMetadata($metadata_info, 'foo/', FALSE);
    $this->assertSame($expectations['path'], $metadata->path);
    $this->assertSame($expectations['status'], $metadata->status);
    $this->assertSame($expectations['thumbnail'], $metadata->getThumbnailPath());
    $this->assertEquals($expectations['props'], $metadata->schema);
    $this->assertEquals($expectations['noUi'] ?? TRUE, $metadata->noUi);
  }
  
  /**
   * Tests the correct checks when enforcing schemas or not.
   */
  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 {
      new ComponentMetadata($metadata_info, 'foo/', TRUE);
      $this->expectNotToPerformAssertions();
    }
  }
  
  /**
   * Data provider for the test testMetadataEnforceSchema.
   *
   * @return array[]
   *   The batches of data.
   */
  public static function dataProviderMetadata() : array {
    return [
      'minimal example without schemas' => [
        [
          'path' => 'foo/bar/component-name',
          'id' => 'core:component-name',
          'name' => 'Component Name',
          'libraryOverrides' => [
            'dependencies' => [
              'core/drupal',
            ],
          ],
          'group' => 'my-group',
          'description' => 'My description',
          'noUi' => TRUE,
        ],
        [
          'path' => 'bar/component-name',
          'status' => 'stable',
          'thumbnail' => '',
          'props' => NULL,
          'noUi' => TRUE,
        ],
        TRUE,
      ],
      'complete example with schema, but no meta:enum' => [
        [
          '$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',
          'noUi' => FALSE,
          'props' => [
            'type' => 'object',
            'required' => [
              'text',
            ],
            'properties' => [
              'text' => [
                'type' => 'string',
                'title' => 'Title',
                'description' => 'The title for the button',
                'minLength' => 2,
                'examples' => [
                  'Press',
                  'Submit now',
                ],
              ],
              'iconType' => [
                'type' => 'string',
                'title' => 'Icon Type',
                'enum' => [
                  'power',
                  'like',
                  'external',
                ],
              ],
            ],
          ],
        ],
        [
          'path' => 'my-other/path',
          'status' => 'stable',
          'thumbnail' => '',
          'group' => 'my-group',
          'additionalProperties' => FALSE,
          'noUi' => 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',
                ],
              ],
              'iconType' => [
                'type' => [
                  'string',
                  'object',
                ],
                'title' => 'Icon Type',
                'enum' => [
                  'power',
                  'like',
                  'external',
                ],
                'meta:enum' => [
                  'power' => new TranslatableMarkup('power', [], [
                    'context' => '',
                  ]),
                  'like' => new TranslatableMarkup('like', [], [
                    'context' => '',
                  ]),
                  'external' => new TranslatableMarkup('external', [], [
                    'context' => '',
                  ]),
                ],
              ],
            ],
          ],
        ],
        FALSE,
      ],
      'complete example with schema, but no matching meta:enum' => [
        [
          '$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' => [
              'text' => [
                'type' => 'string',
                'title' => 'Title',
                'description' => 'The title for the button',
                'minLength' => 2,
                'examples' => [
                  'Press',
                  'Submit now',
                ],
              ],
              'iconType' => [
                'type' => 'string',
                'title' => 'Icon Type',
                'enum' => [
                  'power',
                  'like',
                  'external',
                ],
                'meta:enum' => [
                  'power' => 'Power',
                  'fav' => 'Favorite',
                  'external' => 'External',
                ],
              ],
            ],
          ],
        ],
        [
          'path' => 'my-other/path',
          'status' => 'stable',
          'thumbnail' => '',
          'group' => 'my-group',
          'additionalProperties' => FALSE,
          'noUi' => 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',
                ],
              ],
              'iconType' => [
                'type' => [
                  'string',
                  'object',
                ],
                'title' => 'Icon Type',
                'enum' => [
                  'power',
                  'like',
                  'external',
                ],
                'meta:enum' => [
                  '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,
          'noUi' => 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,
      ],
      'complete example with schema (including meta:enum)' => [
        [
          '$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' => [
              'text' => [
                'type' => 'string',
                'title' => 'Title',
                'description' => 'The title for the button',
                'minLength' => 2,
                'examples' => [
                  'Press',
                  'Submit now',
                ],
              ],
              'iconType' => [
                'type' => 'string',
                'title' => 'Icon Type',
                'enum' => [
                  'power',
                  'like',
                  'external',
                ],
                'meta:enum' => [
                  'power' => 'Power',
                  'like' => 'Like',
                  'external' => 'External',
                ],
              ],
            ],
          ],
        ],
        [
          'path' => 'my-other/path',
          'status' => 'stable',
          'thumbnail' => '',
          'group' => 'my-group',
          'additionalProperties' => FALSE,
          'noUi' => 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',
                ],
              ],
              'iconType' => [
                'type' => [
                  'string',
                  'object',
                ],
                'title' => 'Icon Type',
                'enum' => [
                  'power',
                  'like',
                  'external',
                ],
                'meta:enum' => [
                  'power' => new TranslatableMarkup('Power', [], [
                    'context' => '',
                  ]),
                  'like' => new TranslatableMarkup('Like', [], [
                    'context' => '',
                  ]),
                  'external' => new TranslatableMarkup('External', [], [
                    'context' => '',
                  ]),
                ],
              ],
            ],
          ],
        ],
        FALSE,
      ],
      'complete example with schema (including meta:enum and x-translation-context)' => [
        [
          '$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' => [
              'text' => [
                'type' => 'string',
                'title' => 'Title',
                'description' => 'The title for the button',
                'minLength' => 2,
                'examples' => [
                  'Press',
                  'Submit now',
                ],
              ],
              'iconType' => [
                'type' => 'string',
                'title' => 'Icon Type',
                'enum' => [
                  'power',
                  'like',
                  'external',
                ],
                'meta:enum' => [
                  'power' => 'Power',
                  'like' => 'Like',
                  'external' => 'External',
                ],
                'x-translation-context' => 'Icon Type',
              ],
            ],
          ],
        ],
        [
          'path' => 'my-other/path',
          'status' => 'stable',
          'thumbnail' => '',
          'group' => 'my-group',
          'additionalProperties' => FALSE,
          'noUi' => 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',
                ],
              ],
              'iconType' => [
                'type' => [
                  'string',
                  'object',
                ],
                'title' => 'Icon Type',
                'enum' => [
                  'power',
                  'like',
                  'external',
                ],
                'meta:enum' => [
                  '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',
              ],
            ],
          ],
        ],
        FALSE,
      ],
      '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',
          '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' => [
              'text' => [
                'type' => 'string',
                'title' => 'Title',
                'description' => 'The title for the button',
                'minLength' => 2,
                'examples' => [
                  'Press',
                  'Submit now',
                ],
              ],
              'target' => [
                'type' => 'string',
                'title' => 'Icon Type',
                'enum' => [
                  '',
                  '_blank',
                ],
                'meta:enum' => [
                  '' => 'Opens in same window',
                  '_blank' => 'Opens in new window',
                ],
                'x-translation-context' => 'Link target',
              ],
            ],
          ],
        ],
        [
          'path' => 'my-other/path',
          'status' => 'stable',
          'thumbnail' => '',
          'group' => 'my-group',
          'additionalProperties' => FALSE,
          'noUi' => 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',
                  'object',
                ],
                'title' => 'Icon Type',
                'enum' => [
                  '',
                  '_blank',
                ],
                'meta:enum' => [
                  '' => new TranslatableMarkup('Opens in same window', [], [
                    'context' => 'Link target',
                  ]),
                  '_blank' => new TranslatableMarkup('Opens in new window', [], [
                    'context' => 'Link target',
                  ]),
                ],
                'x-translation-context' => 'Link target',
              ],
            ],
          ],
        ],
        FALSE,
      ],
    ];
  }

}

Members

Title Sort descending Deprecated Modifiers Object type Summary Overrides
ComponentMetadataTest::dataProviderMetadata public static function Data provider for the test testMetadataEnforceSchema.
ComponentMetadataTest::testMetadata public function Tests that the correct data is returned for each property.
ComponentMetadataTest::testMetadataEnforceSchema public function Tests the correct checks when enforcing schemas or not.
DrupalTestCaseTrait::checkErrorHandlerOnTearDown public function Checks the test error handler after test execution.
ExpectDeprecationTrait::expectDeprecation Deprecated public function Adds an expected deprecation.
ExpectDeprecationTrait::regularExpressionForFormatDescription private function
RandomGeneratorTrait::getRandomGenerator protected function Gets the random generator for the utility methods.
RandomGeneratorTrait::randomMachineName protected function Generates a unique random string containing letters and numbers.
RandomGeneratorTrait::randomObject public function Generates a random PHP object.
RandomGeneratorTrait::randomString public function Generates a pseudo-random string of ASCII characters of codes 32 to 126.
UnitTestCase::$root protected property The app root.
UnitTestCase::getClassResolverStub protected function Returns a stub class resolver.
UnitTestCase::getConfigFactoryStub public function Returns a stub config factory that behaves according to the passed array.
UnitTestCase::getContainerWithCacheTagsInvalidator protected function Sets up a container with a cache tags invalidator.
UnitTestCase::getStringTranslationStub public function Returns a stub translation manager that just returns the passed string.
UnitTestCase::setDebugDumpHandler public static function Registers the dumper CLI handler when the DebugDump extension is enabled.
UnitTestCase::setUp protected function 366
UnitTestCase::setupMockIterator protected function Set up a traversable class mock to return specific items when iterated.

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