Verified Commit b7ee3dcf authored by Lauri Timmanee's avatar Lauri Timmanee
Browse files

Issue #3377030 by Wim Leers, borisson_, smustgrave: Add validation constraint...

Issue #3377030 by Wim Leers, borisson_, smustgrave: Add validation constraint to `type: label`: disallow multiple lines
parent 4cb10842
Loading
Loading
Loading
Loading
+9 −0
Changes for core/config/schema/core.data_types.schema.yml: 9 added lines, 0 removed lines.
Original line number Diff line number Diff line
@@ -62,6 +62,15 @@ label:
  type: string
  label: 'Label'
  translatable: true
  constraints:
    Regex:
      # Forbid any kind of line ending:
      # - Windows: `\r\n`
      # - old macOS: `\r`
      # - *nix: `\n`
      pattern: '/(\r\n|\r|\n)/'
      match: false
      message: 'Labels are not allowed to span multiple lines.'

# String containing plural variants, separated by EXT.
plural_label:
+27 −0
Changes for core/modules/block/tests/src/Kernel/BlockValidationTest.php: 27 added lines, 0 removed lines.
Original line number Diff line number Diff line
@@ -3,6 +3,7 @@
namespace Drupal\Tests\block\Kernel;

use Drupal\block\Entity\Block;
use Drupal\Core\Config\Entity\ConfigEntityInterface;
use Drupal\KernelTests\Core\Config\ConfigEntityValidationTestBase;

/**
@@ -27,6 +28,9 @@ protected function setUp(): void {
      'id' => 'test_block',
      'theme' => 'stark',
      'plugin' => 'system_powered_by_block',
      'settings' => [
        'label' => 'Powered by Drupal 🚀',
      ],
    ]);
    $this->entity->save();
  }
@@ -62,4 +66,27 @@ public function providerInvalidMachineNameCharacters(): array {
    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."]);
  }

}
+1 −1
Changes for core/modules/contact/config/schema/contact.schema.yml: 1 added line, 1 removed line.
Original line number Diff line number Diff line
@@ -28,7 +28,7 @@ contact.form.*:
      type: integer
      label: 'Weight'
    message:
      type: label
      type: text
      label: 'Message displayed to user on submission'
    redirect:
      type: path
+11 −0
Changes for core/modules/contact/tests/src/Kernel/ContactFormValidationTest.php: 11 added lines, 0 removed lines.
Original line number Diff line number Diff line
@@ -2,6 +2,7 @@

namespace Drupal\Tests\contact\Kernel;

use Drupal\contact\ContactFormInterface;
use Drupal\contact\Entity\ContactForm;
use Drupal\KernelTests\Core\Config\ConfigEntityValidationTestBase;

@@ -30,4 +31,14 @@ protected function setUp(): void {
    $this->entity->save();
  }

  /**
   * Tests validation of message.
   */
  public function testMessageValidation(): void {
    assert($this->entity instanceof ContactFormInterface);
    // Messages should be able to span multiple lines.
    $this->entity->setMessage("Multi\nLine");
    $this->assertValidationErrors([]);
  }

}
+9 −0
Changes for core/modules/editor/tests/src/Kernel/EditorValidationTest.php: 9 added lines, 0 removed lines.
Original line number Diff line number Diff line
@@ -70,4 +70,13 @@ public function testInvalidPluginId(): void {
    $this->assertValidationErrors(['editor' => "The 'non_existent' plugin does not exist."]);
  }

  /**
   * {@inheritdoc}
   */
  public function testLabelValidation(): void {
    // @todo Remove this override in https://www.drupal.org/i/3231354. The label of Editor entities is dynamically computed: it's retrieved from the associated FilterFormat entity. That issue will change this.
    // @see \Drupal\editor\Entity\Editor::label()
    $this->markTestSkipped();
  }

}
Loading