Problem/Motivation

The ResourceTestBase has a static property of $uniqueFieldNames for tracking unique field names on an entity. This is used to prevent errors when modifying and creating entities in tests.

These fields are not taken into account when the \Drupal\Tests\jsonapi\Functional\ResourceTestBase::getEntityDuplicate method is used.

I had to extend this method to fix the problem, where the sku field is unique.

  /**
   * {@inheritdoc}
   */
  protected function getEntityDuplicate(EntityInterface $original, $key) {
    $dupe = parent::getEntityDuplicate($original, $key);
    assert($dupe instanceof ProductVariationInterface);
    $dupe->setSku('XYZ789');
    return $dupe;
  }

Steps to reproduce

See #3128322: Filtering variations over JSON:API is always access false

Proposed resolution

Respect unique fields when making a duplicate. It is done there:

  protected function getModifiedEntityForPostTesting() {
    $document = $this->getPostDocument();

    // Ensure that all the unique fields of the entity type get a new random
    // value.
    foreach (static::$uniqueFieldNames as $field_name) {
      $field_definition = $this->entity->getFieldDefinition($field_name);
      $field_type_class = $field_definition->getItemDefinition()->getClass();
      $document['data']['attributes'][$field_name] = $field_type_class::generateSampleValue($field_definition);
    }

Remaining tasks

User interface changes

API changes

Data model changes

Release notes snippet

Comments

mglaman created an issue. See original summary.

mglaman’s picture

The only instance of Drupal core using this property is in FeedTest

  /**
   * {@inheritdoc}
   */
  protected function createAnotherEntity($key) {
    /* @var \Drupal\aggregator\FeedInterface $duplicate */
    $duplicate = $this->getEntityDuplicate($this->entity, $key);
    $duplicate->set('field_rest_test', 'Duplicate feed entity');
    $duplicate->setUrl("http://example.com/$key.xml");
    $duplicate->save();
    return $duplicate;
  }
mglaman’s picture

Status: Active » Needs review
Issue tags: +Contributed project soft blocker
StatusFileSize
new1.69 KB
new650 bytes

This removes the need to manually fix unique fields in a test which extends ResourceTestBase for JSON:API integration testing.

Status: Needs review » Needs work

The last submitted patch, 3: 3163874-3-test-only.patch, failed testing. View results

mglaman’s picture

Status: Needs work » Needs review

Test only patch was the failure

Version: 9.1.x-dev » 9.2.x-dev

Drupal 9.1.0-alpha1 will be released the week of October 19, 2020, which means new developments and disruptive changes should now be targeted for the 9.2.x-dev branch. For more information see the Drupal 9 minor version schedule and the Allowed changes during the Drupal 9 release cycle.

Version: 9.2.x-dev » 9.3.x-dev

Drupal 9.2.0-alpha1 will be released the week of May 3, 2021, which means new developments and disruptive changes should now be targeted for the 9.3.x-dev branch. For more information see the Drupal core minor version schedule and the Allowed changes during the Drupal core release cycle.

bbrala’s picture

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

Awesome contribution mglaman. Looking good but really do need this targeted on 9.3.x.

imalabya’s picture

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

Re-rolled the patch at #3

mglaman’s picture

Issue tags: -Needs reroll

Thanks for the reroll imalabya!

bbrala’s picture

Status: Needs review » Reviewed & tested by the community

Well done, nice work. :)

alexpott’s picture

Status: Reviewed & tested by the community » Needs work
+++ b/core/modules/jsonapi/tests/src/Functional/ResourceTestBase.php
@@ -431,6 +431,13 @@ protected function getEntityDuplicate(EntityInterface $original, $key) {
+        $duplicate->set($field_name, $field_type_class::generateSampleValue($field_definition));

generateSampleValue() is not guaranteed to return a different value. Imo you should try a few times if you get the same value and fail if you don't get a new value.

bbrala’s picture

Excellent point. Thanks.

Version: 9.3.x-dev » 9.4.x-dev

Drupal 9.3.0-rc1 was released on November 26, 2021, which means new developments and disruptive changes should now be targeted for the 9.4.x-dev branch. For more information see the Drupal core minor version schedule and the Allowed changes during the Drupal core release cycle.

Version: 9.4.x-dev » 9.5.x-dev

Drupal 9.4.0-alpha1 was released on May 6, 2022, which means new developments and disruptive changes should now be targeted for the 9.5.x-dev branch. For more information see the Drupal core minor version schedule and the Allowed changes during the Drupal core release cycle.

Version: 9.5.x-dev » 10.1.x-dev

Drupal 9.5.0-beta2 and Drupal 10.0.0-beta2 were released on September 29, 2022, which means new developments and disruptive changes should now be targeted for the 10.1.x-dev branch. For more information see the Drupal core minor version schedule and the Allowed changes during the Drupal core release cycle.

Version: 10.1.x-dev » 11.x-dev

Drupal core is moving towards using a “main” branch. As an interim step, a new 11.x branch has been opened, as Drupal.org infrastructure cannot currently fully support a branch named main. New developments and disruptive changes should now be targeted for the 11.x branch, which currently accepts only minor-version allowed changes. For more information, see the Drupal core minor version schedule and the Allowed changes during the Drupal core release cycle.

Version: 11.x-dev » main

Drupal core is now using the main branch as the primary development branch. New developments and disruptive changes should now be targeted to the main branch.

Read more in the announcement.