class BlockValidationTest

Same name and namespace in other branches
  1. 11.x core/modules/block/tests/src/Kernel/BlockValidationTest.php \Drupal\Tests\block\Kernel\BlockValidationTest

Tests validation of block entities.

@group block @group #slow

Hierarchy

Expanded class hierarchy of BlockValidationTest

File

core/modules/block/tests/src/Kernel/BlockValidationTest.php, line 17

Namespace

Drupal\Tests\block\Kernel
View source
class BlockValidationTest extends ConfigEntityValidationTestBase {
  
  /**
   * {@inheritdoc}
   */
  protected static $modules = [
    'block',
  ];
  
  /**
   * {@inheritdoc}
   */
  protected function setUp() : void {
    parent::setUp();
    $this->entity = Block::create([
      'id' => 'test_block',
      'theme' => 'stark',
      'plugin' => 'system_powered_by_block',
      'settings' => [
        'label' => 'Powered by Drupal 🚀',
      ],
    ]);
    $this->entity
      ->save();
  }
  
  /**
   * Tests validating a block with an unknown plugin ID.
   */
  public function testInvalidPluginId() : void {
    $this->entity
      ->set('plugin', 'block_content:d7c9d8ba-663f-41b4-8756-86bc55c44653');
    // Block config entities with invalid block plugin IDs automatically fall
    // back to the `broken` block plugin.
    // @see https://www.drupal.org/node/2249303
    // @see \Drupal\Core\Block\BlockManager::getFallbackPluginId()
    // @see \Drupal\Core\Block\Plugin\Block\Broken
    $this->assertValidationErrors([]);
    $this->entity
      ->set('plugin', 'non_existent');
    // @todo Expect error for this in https://www.drupal.org/project/drupal/issues/3377709
    $this->assertValidationErrors([]);
  }
  
  /**
   * Block names are atypical in that they allow periods in the machine name.
   */
  public static function providerInvalidMachineNameCharacters() : array {
    $cases = parent::providerInvalidMachineNameCharacters();
    // Remove the existing test case that verifies a machine name containing
    // periods is invalid.
    self::assertSame([
      'period.separated',
      FALSE,
    ], $cases['INVALID: period separated']);
    unset($cases['INVALID: period separated']);
    // And instead add a test case that verifies it is allowed for blocks.
    $cases['VALID: period separated'] = [
      'period.separated',
      TRUE,
    ];
    return $cases;
  }
  
  /**
   * {@inheritdoc}
   */
  protected static function setLabel(ConfigEntityInterface $block, string $label) : void {
    static::assertInstanceOf(Block::class, $block);
    $settings = $block->get('settings');
    static::assertNotEmpty($settings['label']);
    $settings['label'] = $label;
    $block->set('settings', $settings);
  }
  
  /**
   * {@inheritdoc}
   */
  public function testLabelValidation() : void {
    static::setLabel($this->entity, "Multi\nLine");
    // TRICKY: because the Block config entity type does not specify a `label`
    // key, it is impossible for the generic ::testLabelValidation()
    // implementation in the base class to know at which property to expect a
    // validation error. Hence it is hardcoded in this case.
    $this->assertValidationErrors([
      'settings.label' => "Labels are not allowed to span multiple lines or contain control characters.",
    ]);
  }

}

Members

Title Sort descending Modifiers Object type Summary Overriden Title Overrides
BlockValidationTest::$modules protected static property Modules to install.
BlockValidationTest::providerInvalidMachineNameCharacters public static function Block names are atypical in that they allow periods in the machine name. Overrides ConfigEntityValidationTestBase::providerInvalidMachineNameCharacters
BlockValidationTest::setLabel protected static function Sets the label of the given config entity. Overrides ConfigEntityValidationTestBase::setLabel
BlockValidationTest::setUp protected function Overrides ConfigEntityValidationTestBase::setUp
BlockValidationTest::testInvalidPluginId public function Tests validating a block with an unknown plugin ID.
BlockValidationTest::testLabelValidation public function Tests validation of config entity's label. Overrides ConfigEntityValidationTestBase::testLabelValidation
ConfigEntityValidationTestBase::$entity protected property The config entity being tested.
ConfigEntityValidationTestBase::$hasLabel protected property Whether a config entity of this type has a label. 4
ConfigEntityValidationTestBase::$propertiesWithOptionalValues protected static property The config entity properties whose values are optional (set to NULL). 4
ConfigEntityValidationTestBase::$propertiesWithRequiredKeys protected static property The config entity mapping properties with >=1 required keys. 1
ConfigEntityValidationTestBase::assertValidationErrors protected function Asserts a set of validation errors is raised when the entity is validated.
ConfigEntityValidationTestBase::getMachineNameConstraints protected function Returns the validation constraints applied to the entity's ID.
ConfigEntityValidationTestBase::getPropertiesWithOptionalValues protected function Determines the config entity properties with optional values.
ConfigEntityValidationTestBase::getRequiredPropertyKeys protected function Determines the config entity mapping properties with required keys.
ConfigEntityValidationTestBase::isFullyValidatable protected function Whether the tested config entity type is fully validatable.
ConfigEntityValidationTestBase::providerConfigDependenciesValidation public static function Data provider for ::testConfigDependenciesValidation().
ConfigEntityValidationTestBase::testConfigDependenciesValidation public function Tests validation of config dependencies.
ConfigEntityValidationTestBase::testEntityIsValid public function Ensures that the entity created in ::setUp() has no validation errors.
ConfigEntityValidationTestBase::testImmutableProperties public function Tests that immutable properties cannot be changed. 11
ConfigEntityValidationTestBase::testInvalidMachineNameCharacters public function Tests that the entity's ID is tested for invalid characters.
ConfigEntityValidationTestBase::testLangcode public function Tests that the config entity's langcode is validated.
ConfigEntityValidationTestBase::testMachineNameLength public function Tests that the entity ID's length is validated if it is a machine name.
ConfigEntityValidationTestBase::testRequiredPropertyKeysMissing public function A property that is required must have a value (i.e. not NULL). 2
ConfigEntityValidationTestBase::testRequiredPropertyValuesMissing public function A property that is required must have a value (i.e. not NULL). 2

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