Verified Commit 54879508 authored by godotislate's avatar godotislate
Browse files

fix: #3559874 The UUID data type should validate that its value is an actual UUID

By: phenaproxima
By: wim leers
By: lendude
By: longwave
By: angel_devoeted
By: dcam
By: idebr
By: godotislate
parent 3ae3915e
Loading
Loading
Loading
Loading
Loading
+10 −1
Changes for core/lib/Drupal/Core/Field/Plugin/Field/FieldType/UuidItem.php: 10 added lines, 1 removed line.
Original line number Diff line number Diff line
@@ -17,7 +17,16 @@
  label: new TranslatableMarkup("UUID"),
  description: new TranslatableMarkup("An entity field containing a UUID."),
  default_formatter: "string",
  no_ui: TRUE
  no_ui: TRUE,
  constraints: [
    "ComplexData" => [
      'properties' => [
        "value" => [
          "Uuid" => [],
        ],
      ],
    ],
  ],
)]
class UuidItem extends StringItem {

+12 −0
Changes for core/modules/field/tests/src/Kernel/KernelString/UuidItemTest.php: 12 added lines, 0 removed lines.
Original line number Diff line number Diff line
@@ -31,4 +31,16 @@ public function testSampleValue(): void {
    $this->assertTrue(Uuid::isValid($uuid_field->value));
  }

  /**
   * Tests that UUID item values must be valid UUIDs.
   */
  public function testInvalidUuid(): void {
    $entity = EntityTest::create([
      'uuid' => 'not a valid uuid',
    ]);
    $violation = $entity->validate()->get(0);
    $this->assertSame('This is not a valid UUID.', (string) $violation->getMessage());
    $this->assertSame('uuid.0.value', $violation->getPropertyPath());
  }

}
+1 −1
Changes for core/modules/rest/tests/src/Functional/EntityResource/EntityResourceTestBase.php: 1 added line, 1 removed line.
Original line number Diff line number Diff line
@@ -847,7 +847,7 @@ protected function doTestPost(): void {
    // @todo Fix this in https://www.drupal.org/node/2149851.
    if ($this->entity->getEntityType()->hasKey('uuid')) {
      $response = $this->request('POST', $url, $request_options);
      $this->assertResourceErrorResponse(422, "Unprocessable Entity: validation failed.\nuuid.0.value: UUID: may not be longer than 128 characters.\n", $response);
      $this->assertResourceErrorResponse(422, "Unprocessable Entity: validation failed.\nuuid.0.value: This is not a valid UUID.\nuuid.0.value: UUID: may not be longer than 128 characters.\n", $response);
    }

    $request_options[RequestOptions::BODY] = $parseable_invalid_request_body_3;
+2 −2
Changes for core/tests/Drupal/KernelTests/Core/DefaultContent/ImporterTest.php: 2 added lines, 2 removed lines.
Original line number Diff line number Diff line
@@ -79,7 +79,7 @@ public function onPreSave(ContentEntityInterface $entity): void {
  public function onPreEntityImport(PreEntityImportEvent $event): void {
    self::assertSame('entity_test', $event->metadata['entity_type']);

    if ($event->metadata['uuid'] === '01234567-89ab-cdef-0123-456789abcdef') {
    if ($event->metadata['uuid'] === '2ab4da9f-7132-4578-99ac-3ee46ce8ce09') {
      $event->data['default']['name'] = [
        ['value' => 'Changed name'],
      ];
@@ -99,7 +99,7 @@ public function testChangeDataOnImport(): void {
    self::assertTrue($this->wasSyncing);

    $entity = $this->container->get(EntityRepositoryInterface::class)
      ->loadEntityByUuid('entity_test', '01234567-89ab-cdef-0123-456789abcdef');
      ->loadEntityByUuid('entity_test', '2ab4da9f-7132-4578-99ac-3ee46ce8ce09');
    self::assertSame('Changed name', $entity?->label());
  }

+3 −2
Changes for core/tests/Drupal/KernelTests/Core/Entity/EntityValidationTest.php: 3 added lines, 2 removed lines.
Original line number Diff line number Diff line
@@ -163,8 +163,9 @@ protected function checkValidation($entity_type): void {
    $test_entity = clone $entity;
    $test_entity->uuid->value = $this->randomString(129);
    $violations = $test_entity->validate();
    $this->assertEquals(1, $violations->count(), 'Validation failed.');
    $this->assertEquals('UUID: may not be longer than 128 characters.', $violations[0]->getMessage());
    $this->assertEquals(2, $violations->count(), 'Validation failed.');
    $this->assertEquals('This is not a valid UUID.', $violations[0]->getMessage());
    $this->assertEquals('UUID: may not be longer than 128 characters.', $violations[1]->getMessage());

    $test_entity = clone $entity;
    $langcode_key = $this->entityTypeManager->getDefinition($entity_type)->getKey('langcode');
Loading