Problem/Motivation

Calling generateSampleValue() on a UUID field does not return a proper UUID.

Proposed resolution

Implement generateSampleValue() in UuidItem. Use \Drupal::service('uuid') to fetch the UUID generator and call generate() on it to generate the UUID.

Remaining tasks

Contributor tasks needed
Task Novice task? Contributor instructions Complete?
Create a patch Instructions
Review patch to ensure that it fixes the issue, stays within scope, is properly documented, and follows coding standards Instructions

User interface changes

None.

API changes

None.

Data model changes

None.

Comments

tstoeckler created an issue. See original summary.

tstoeckler’s picture

felribeiro’s picture

Status: Active » Needs review
StatusFileSize
new656 bytes

Status: Needs review » Needs work

The last submitted patch, 3: 2827748-3.patch, failed testing.

felribeiro’s picture

Status: Needs work » Needs review
StatusFileSize
new881 bytes
tstoeckler’s picture

Status: Needs review » Needs work
Issue tags: +Needs tests

Thank you, the patch is perfect!

We now need to add a test that this works properly. I found \Drupal\Tests\field\Kernel\String\UuidFormatterTest which you could use as inspiration for a new UuidItemTest with a testRandomValue() method.

The last submitted patch, 5: 2827748-5.patch, failed testing.

faline’s picture

Status: Needs work » Needs review
StatusFileSize
new1.73 KB

@tstoeckler I never write a test before, but I give a try.
Could you please take a look if is this ok?

tstoeckler’s picture

Status: Needs review » Needs work

The test looks great, very nice. And quite impressive if that's your first test!!! Nice work, @faline

Some notes:

  1. +++ b/core/modules/field/tests/src/Kernel/String/UuidItemTest.php
    @@ -0,0 +1,29 @@
    + * Tests the values of a UUID field.
    

    I think we should just say "Tests the UUID field." so it still makes sense when we add some more tests to this at some point.

  2. +++ b/core/modules/field/tests/src/Kernel/String/UuidItemTest.php
    @@ -0,0 +1,29 @@
    +   * Tests 'uuid' random values.
    ...
    +  public function testUuidRandomValue() {
    

    I know I suggested this method name, so this is my fault, but the method we are testing is generateSampleValue() so I think the test method should be testSampleValue() (I think we can leave the UUID part out of the method name as that is in the class name).

  3. +++ b/core/modules/field/tests/src/Kernel/String/UuidItemTest.php
    @@ -0,0 +1,29 @@
    +    $this->entityValidateAndSave($entity);
    

    I think we shouldn't really care about saving the entity. I think

    $this->assertTrue(\Drupal::service('uuid')->isValid($uuid_field->value));
    

    or something like that would be a more precise assertion for what we are testing here.

faline’s picture

StatusFileSize
new1.76 KB

@tstoeckler thank you for your help! and thank you @felribeiro for supporting =)

I've change the test as suggestions in #9.
Please, could you take a look if it is ok?

faline’s picture

Status: Needs work » Needs review
tstoeckler’s picture

Issue tags: -Needs tests

Perfect, the test looks great.

In the future it would be nice if you could generate an interdiff along with your patch, so it is visible what changed between two patches. See https://www.drupal.org/documentation/git/interdiff for more information. In this case it's not that big of a deal, because the patch is pretty small, but it's considered a best practice to always provide one.

I have one super minor quibble:

+++ b/core/modules/field/tests/src/Kernel/String/UuidItemTest.php
@@ -0,0 +1,30 @@
+     $this->assertTrue(Uuid::isValid($uuid_field->value));

This line is not indented correctly.

Also, it would be awesome if you could upload a "tests-only" patch, i.e. a patch file that only contains the added test. This will fail, but that will prove that your test actually tests what we want it to.

Thanks!

faline’s picture

Sorry for that! I forgot to indent and upload the interdiff.

Follow patch, interdiff (with previous one and with #5) and also test patch only.
tks!

The last submitted patch, 13: test_only-uuiditem_should-2827748-13.patch, failed testing.

tstoeckler’s picture

Status: Needs review » Reviewed & tested by the community

That's absolutely perfect. Thank you for your persistence!

alexpott’s picture

Status: Reviewed & tested by the community » Fixed

Committed and pushed 759c27c to 8.3.x and 8a75749 to 8.2.x. Thanks!

  • alexpott committed 759c27c on 8.3.x
    Issue #2827748 by faline, felribeiro, tstoeckler: UuidItem should...

  • alexpott committed 8a75749 on 8.2.x
    Issue #2827748 by faline, felribeiro, tstoeckler: UuidItem should...

Status: Fixed » Closed (fixed)

Automatically closed - issue fixed for 2 weeks with no activity.