Problem/Motivation

Discovered in #2926508-12: Add DateTimeNormalizer+TimestampNormalizer, deprecate TimestampItemNormalizer: @DataType-level normalizers are reusable by JSON:API.

#2124677: Expose URI in file fields when serializing an object added this test, and includes this in its setUp() function:

    // Set up the serializer to return an entity property.
    $this->serializer->normalize(Argument::cetera())
      ->willReturn(['value' => 'test']);

This will be called for normalizing the target_id property on EntityReferenceItems. This is an inaccurate mock (simulation) of the real world.

Consequently, when other patches land that make changes elsewhere in the system (such as #2926508: Add DateTimeNormalizer+TimestampNormalizer, deprecate TimestampItemNormalizer: @DataType-level normalizers are reusable by JSON:API making changes to ComplexDataNormalizer::normalize()), that results in false negatives (incorrect test failures).

Proposed resolution

Fix the unit test to simulate the real world accurately.

The reason you can be absolutely certain that the normalized value for an entity reference field's target_id property should be

'test'

and not

['value' => 'test']

can be found in the many functional/integration tests that we have by now. For example, from NodeResourceTestBase:

      'type' => [
        [
          'target_id' => 'camelids',
          'target_type' => 'node_type',
          'target_uuid' => NodeType::load('camelids')->uuid(),
        ],
      ],
…
      'uid' => [
        [
          'target_id' => (int) $author->id(),
          'target_type' => 'user',
          'target_uuid' => $author->uuid(),
          'url' => base_path() . 'user/' . $author->id(),
        ],

Remaining tasks

None.

User interface changes

None.

API changes

None.

Data model changes

Support from Acquia helps fund testing for Drupal Acquia logo

Comments

Wim Leers created an issue. See original summary.

Wim Leers’s picture

Wim Leers’s picture

Assigned: Wim Leers » Unassigned
Issue summary: View changes
Related issues: +#2124677: Expose URI in file fields when serializing an object
FileSize
3.65 KB

And this fixes all the others too.

The fact that I can change only some, not all, and tests still pass, mean that #2124677: Expose URI in file fields when serializing an object introduced a weak/broken test. It's because we're mocking far too much here.

Also, the reason you can be absolutely certain that the normalized value for an entity reference field's target_id property should be

'test'

and not

['value' => 'test']

can be found in the many functional/integration tests that we have by now. For example, from NodeResourceTestBase:

      'type' => [
        [
          'target_id' => 'camelids',
          'target_type' => 'node_type',
          'target_uuid' => NodeType::load('camelids')->uuid(),
        ],
      ],
…
      'uid' => [
        [
          'target_id' => (int) $author->id(),
          'target_type' => 'user',
          'target_uuid' => $author->uuid(),
          'url' => base_path() . 'user/' . $author->id(),
        ],

… which of course makes us question the value of this unit test in the first place. But dealing with that is out of scope for this issue. This is about fixing the immediate, blocking problem.

Wim Leers’s picture

Fix-mock-only patch that should fail, plus patch that will be green.

The last submitted patch, 4: 2927566-3 fix mock only FAIL.patch, failed testing. View results

The last submitted patch, 2: 2927566-2.patch, failed testing. View results

The last submitted patch, 3: 2927566-3.patch, failed testing. View results

Wim Leers’s picture

Apparently the "fail" patch in #4 was missing one newline. That's what I get for manually editing patches.

The last submitted patch, 8: 2927566-3-mock-only-FAIL.patch, failed testing. View results

Status: Needs review » Needs work

The last submitted patch, 8: 2927566-3.patch, failed testing. View results

Wim Leers’s picture

Status: Needs work » Needs review

The patch in #8 that was supposed to pass tests failed due to DrupalCI infra problems:

apcu_store(): Unable to allocate memory for pool.

Retesting. Besides, the same exact patch was green in #4.

borisson_’s picture

Status: Needs review » Reviewed & tested by the community

This looks great as a simplification of that test.

Status: Reviewed & tested by the community » Needs work

The last submitted patch, 8: 2927566-3.patch, failed testing. View results

Wim Leers’s picture

Status: Needs work » Reviewed & tested by the community
webchick’s picture

Status: Reviewed & tested by the community » Fixed

Oopsie. :P Good catch.

Committed and pushed to 8.5.x. Thanks!

  • webchick committed 66d19ea on 8.5.x
    Issue #2927566 by Wim Leers: Unit test...
Wim Leers’s picture

Status: Fixed » Closed (fixed)

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