Problem/Motivation

Tests rely on actual produced output in /tests/expectations. This outup is based upon nodes and taxonomy terms, both of which are subject to changes in Drupal core. This also means it hard to test between multiple core version at the same time.

Proposed resolution

We should fix that.

Remaining tasks

Changes to tests:

  1. Maybe create our own Entity types in test modules to test against?
    1. Nodes replaced.
    2. Taxonomy Terms/Vocabs?
  2. Implement varying types of tests, in particular adding Kernal tests to verify parts of the schema before testing with requests.
  3. Other ideas?
CommentFileSizeAuthor
#17 2930810-interdiff-16-17.diff2.78 KBrichgerdes
#17 2930810-17.patch315.41 KBrichgerdes
#16 2930810-16.patch315.56 KBtedbow
#16 interdiff-15-16.txt2.84 KBtedbow
#15 interdiff-14-15.txt5.18 KBtedbow
#15 2930810-15.patch314.2 KBtedbow
#14 2930810-14-make-tests-more-resilient.patch311.74 KBtedbow
#14 interdiff-10-14.txt255 bytestedbow
#10 2930810-10-make-tests-more-resilient.patch311.51 KBrichgerdes
#9 2930810-9-make-tests-more-resilient.patch311.51 KBrichgerdes
#7 2930810-7-make-tests-more-resilient.patch311.92 KBrichgerdes
#6 2930810-6-make-tests-more-resilient.patch244.08 KBrichgerdes
#5 2930810-5-make-tests-more-resilient.patch10.43 KBrichgerdes
#4 2930810-4-make-tests-more-resilient.patch10.41 KBrichgerdes
Support from Acquia helps fund testing for Drupal Acquia logo

Comments

tedbow created an issue. See original summary.

tedbow’s picture

So after #2936116: Test fail against current core verison. tests pass against 8.4.x, 8.4.x

They are very close to passing in 8.5.x but in 8.5.x there is extra element under the security array for GET requests.

I haven't had a chances to look at that yet but pretty sure having our own Entity types would not solve this.

I know @richgerdes mention solving this issue in #2936910: Project Status Inquiry so I will be interested in his ideas for changing how the testing works.

The current methods was quick fix to have any testing at all.

richgerdes’s picture

Some initial thoughts here, first looking at the code base I think the best approach would be to address testing in a way that relies as little on core as possible. Right now the tests use real json output, which can result in problems. If we better test individual components then we can probably avoid many of the high level validation requirements. For example using Unit and Kernel tests to target the key functions and validate that the json logic is being built correctly, would make tests more flexible.

There are overall improvements to the module like leveraging plugins for setting up routing/endpoints. Then tests could be built to validate the key components of the resulting schema, and individual tests for the jsonapi or rest components that may be unique. Taking that approach has some larger requirements and refactoring that would need to be completed prior to solving tests, and should be handled after the initial test refactor.

I think adding and testing our own entity type is a safe approach as it lets us focus on the resulting structure more then relying on a core feature to show all functions of the module. However, yes there are still cases where core may differ between versions, such as the security section you mentioned, and we need to account for those. I think having the differentiated types of tests can get us around many of those rigid requirements.

Still trying to flush out a few thoughts about this. I am sure you have more experience with tests in general, so if you have any suggestions, would be good to hear them.

richgerdes’s picture

Took a first pass over the tests. The functionality I targeted first with the host and base path definitions. The use of string replacements was causing problems running on my https dev box. Ultimately php has the parse_url function for doing this, so its silly for us to attempt this in a way that imposes limitations. I converted both the host and base path detection logic to use it. There are also cases in the Generators where we may wish to use the same logic, but we should come back to that after the tests.

With the improved test cases, it seemed silly to store {host} in the expectation files since we will just be replacing it. The same applies to {base_path}. The updated test would not use string replacement here, but prepend the base path. When exporting the expectations, the basePath gets stripped from the generated url.

The tests still need work. Uploading the patch so far to test against DrupalCI.

richgerdes’s picture

Attempt at fixing sub directory.

richgerdes’s picture

Taking another pass at the tests. I took an initial stab at supplying our own entity type. The patch includes a new module with a basic entity type with bundles. I replaced nodes in the tests with the new entity type. We still have to think about Taxonomy Terms and Vocabularies. They probably can be removed, since i have added the new entity bundle definition to the api as well.

The updated patch includes updated expectation files as well. Lets see if it passes ci.

@tedbow, any thoughts on where to go from here with the tests?

richgerdes’s picture

Taking another stab at cleaning this up. I did some major refactoring between this and the previous patch, to account of the changes in drupal 8.4 to 8.5.

Here is a quick breakdown:

  • Improved validation of path and host to ensure they match expected.
  • Added verification of existence of swagger, schemes, info, and security sections.
  • Verified that Paths have valid tags, schemes, and security methods.
  • Reduced expectations files to eliminate all the above "dynamic" type data, and only track the more static path's and definitions.

The attached patch contains the refactored test logic, which hopefully will pass for both 8.4 and 8.5.

Status: Needs review » Needs work

The last submitted patch, 7: 2930810-7-make-tests-more-resilient.patch, failed testing. View results
- codesniffer_fixes.patch Interdiff of automated coding standards fixes only.

richgerdes’s picture

richgerdes’s picture

tedbow’s picture

  1. +++ b/tests/src/Functional/RequestTest.php
    @@ -63,7 +86,7 @@ class RequestTest extends BrowserTestBase {
           // Add access-protected field.
           FieldStorageConfig::create([
    

    This not part of the patch but I think this was copy/paste error. This field isn't not access protected in anyway I don't think.

    It has been awhile since I wrote this. @richgerdes does this make sense to you?

  2. +++ b/tests/src/Functional/RequestTest.php
    @@ -110,29 +134,31 @@ class RequestTest extends BrowserTestBase {
    +    $option_sets = [
    +      'openapi_test_entity' => [
    +        'entity_type_id' => 'openapi_test_entity',
    +      ],
    +      'openapi_test_entity_type' => [
    +        'entity_type_id' => 'openapi_test_entity_type',
    +      ],
    +      'openapi_test_entity_camelids' => [
    +        'entity_type_id' => 'openapi_test_entity',
    +        'bundle_name' => 'camelids',
    +      ],
    +      'taxonomy_term' => [
    +        'entity_type_id' => 'taxonomy_term',
    +      ],
    +      'taxonomy_term_camelids' => [
    +        'entity_type_id' => 'taxonomy_term',
    +        'bundle_name' => 'camelids',
    +      ],
    +      'user' => [
    +        'entity_type_id' => 'user',
    +      ],
    +    ];
    

    I know this would take the test run much slower but I wonder if using a `@dataProvider` function here would make the test simpler. I am ok either way

  3. +++ b/tests/src/Functional/RequestTest.php
    @@ -160,34 +186,117 @@ class RequestTest extends BrowserTestBase {
    +        if (isset($method_schema['schemes'])) {
    ...
    +        if (isset($method_schema['security'])) {
    

    Instead of checking for isset here could we check

    $api_module !=
     'jsonapi'

    ?

    Otherwise if these aren' returned for REST the test won't fail.

tedbow’s picture

@richgerdes are you seeing test failures locally for core 8.5.x? Did you say the 8.5.x errors for deprecations?

I am running 8.5.x and 8.6.x and both are passing.

Re #11.2 if we moved to use @dataProvider function then we would least which data option caused the error.

richgerdes’s picture

For #2930810-11: Make tests more resilient to changes in Drupal core.1, we are creating our own field here, once on the custom entity type, once on taxomony_term. The comment doesn't seem relevant and should probably be changed to simply "Added test fields". Seems like the current comment is most likely the result of copy/paste.

For #11.2, I am not familiar with how `@dataProvider` works. I do think tracking the list of types and bundles here is a little clunky in terms of the test though. I wouldn't be against changing them to be sourced from elsewhere.

For #11.3, I think the requirement to address jsonapi differently then core rest stems from a larger problem which should be solved by providing those keys as part of the generator's jsonapi schema. Even if those options are not supported, we still should probably be providing the lack of data there in order to be consistent. In the mean time, I think that its safe to wrap these in the #api_module !== 'jsonapi' for now, with a plan to revise it later.

tedbow’s picture

@richgerdes as we chatted about the test failures in #10 with 8.5 is because the composer.json file is pointing to jsonapi 1.0.

This was an error in JSON API that is was fixed here #2926291: Fatal error on 8.5.x: RelationshipItemNormalizer implements RefinableCacheableDependencyInterface, but should not

Updating just that to prove that the same tests will pass.

tedbow’s picture

Adding dataprovider to simplify the tests

General info on dataProviders: https://phpunit.de/manual/current/en/writing-tests-for-phpunit.html#writ...

tedbow’s picture

Here is just some coding standards fix and an update to comment in #11.1

richgerdes’s picture

Status: Needs review » Needs work

The last submitted patch, 17: 2930810-interdiff-16-17.diff, failed testing. View results

tedbow’s picture

Status: Needs work » Needs review

just interdiff failed to apply

richgerdes’s picture

Status: Needs review » Reviewed & tested by the community

Previous patch passed tests. My interdiff caused it to fail.

talked with @tedbow, this is a good improvement to the module tests and functionality. Marking RTBC.

  • richgerdes authored 9a9eeb1 on 8.x-1.x
    Issue #2930810 by richgerdes, tedbow: Make tests more resilient to...
tedbow’s picture

Status: Reviewed & tested by the community » Fixed

Committed 🎉! @richgerdes thanks for your work on this!

Status: Fixed » Closed (fixed)

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