Problem/Motivation

MigrationConfigurationTrait is not covered by unit tests.

While writing the unit test here, it was also discovered that it catches a PDOException in a situation where it should be catching a DatabaseWrapperException.

To elaborate, there are two try/catch blocks in MigrationConfigurationTrait::getLegacyDrupalVersion. The second block, which was updated somewhat recently appears to catch the correct exception type. According to documentation (and poring over the code), Connection::query can throw DatabaseWrapperException, not a \PDOException.

Therefore, the first try/catch block should be updated to match the second one.

Proposed resolution

It is proposed that we should correct the exception handling as part of writing the unit test.

Remaining tasks

Review

User interface changes

none

API changes

none

Data model changes

none

Release notes snippet

none

Support from Acquia helps fund testing for Drupal Acquia logo

Comments

abhishek-anand created an issue. See original summary.

abhishek-anand’s picture

Issue tags: +Needs tests, +Migrate critical
xjm’s picture

I think so long as #2647470: Write tests goes in, this can be added to core during the beta for 8.1.x or in a patch release of 8.1.x even.

xjm’s picture

Project: Migrate Upgrade » Drupal core
Version: 8.x-1.x-dev » 8.1.x-dev
Component: Code » migration system

Version: 8.1.x-dev » 8.2.x-dev

Drupal 8.1.0-beta1 was released on March 2, 2016, which means new developments and disruptive changes should now be targeted against the 8.2.x-dev branch. For more information see the Drupal 8 minor version schedule and the Allowed changes during the Drupal 8 release cycle.

ultimike’s picture

Issue tags: -Migrate critical

This issue was discussed at the DrupalCon New Orleans migrate critical issue triage meeting. xjm, alexpott, mikeryan, and the rest of the migrate contributors agreed that this is a "nice to have", but shouldn't be considered a critical issue.

-mike

hanoii’s picture

Version: 8.2.x-dev » 8.3.x-dev

Drupal 8.2.0-beta1 was released on August 3, 2016, which means new developments and disruptive changes should now be targeted against the 8.3.x-dev branch. For more information see the Drupal 8 minor version schedule and the Allowed changes during the Drupal 8 release cycle.

Version: 8.3.x-dev » 8.4.x-dev

Drupal 8.3.0-alpha1 will be released the week of January 30, 2017, which means new developments and disruptive changes should now be targeted against the 8.4.x-dev branch. For more information see the Drupal 8 minor version schedule and the Allowed changes during the Drupal 8 release cycle.

Version: 8.4.x-dev » 8.5.x-dev

Drupal 8.4.0-alpha1 will be released the week of July 31, 2017, which means new developments and disruptive changes should now be targeted against the 8.5.x-dev branch. For more information see the Drupal 8 minor version schedule and the Allowed changes during the Drupal 8 release cycle.

Version: 8.5.x-dev » 8.6.x-dev

Drupal 8.5.0-alpha1 will be released the week of January 17, 2018, which means new developments and disruptive changes should now be targeted against the 8.6.x-dev branch. For more information see the Drupal 8 minor version schedule and the Allowed changes during the Drupal 8 release cycle.

Version: 8.6.x-dev » 8.7.x-dev

Drupal 8.6.0-alpha1 will be released the week of July 16, 2018, which means new developments and disruptive changes should now be targeted against the 8.7.x-dev branch. For more information see the Drupal 8 minor version schedule and the Allowed changes during the Drupal 8 release cycle.

quietone’s picture

@abhishek-anand, still planning to work on this?

robpowell’s picture

Title: Write UnitTest for MigrationCreationTrait » Write UnitTest for MigrationConfigurationTrait

per [#268258], MigrationCreationTrait is now MigrationConfigurationTrait.

I was wondering how would one go about testing a class with protected methods? The only example I have to go off of it StringTranslationTrait. This trait is tested via passing a mock created in UnitTestCase::getStringTranslationStub to StringTranslationTrait::setStringTranslation. Is this the correct pattern to follow?

quietone’s picture

Assigned: abhishek-anand » Unassigned

abhishek-anand hasn't responded and it has been 3 weeks, so unassigning.

robpowell’s picture

Assigned: Unassigned » robpowell
Issue tags: +ContributionWeekend2019

Version: 8.7.x-dev » 8.8.x-dev

Drupal 8.7.0-alpha1 will be released the week of March 11, 2019, which means new developments and disruptive changes should now be targeted against the 8.8.x-dev branch. For more information see the Drupal 8 minor version schedule and the Allowed changes during the Drupal 8 release cycle.

robpowell’s picture

FileSize
6.02 KB

This is a first stab at setting unit tests, I used the doc here to guide most decisions https://www.drupal.org/docs/8/phpunit/unit-testing-more-complicated-drup.... Any var_dump() commands are still in code because it can be helpful when troubleshooting. I currently run this test by calling phpunit on the new test file:

vendor/bin/phpunit --configuration core core/modules/migrate_drupal/tests/src/Unit/MigrationConfigurationTraitTest.php

This is very much a work in progress and couldn't have gotten as far without the assistance of @phenaproxima

robpowell’s picture

Assigned: robpowell » Unassigned
Issue tags: -ContributionWeekend2019

Version: 8.8.x-dev » 8.9.x-dev

Drupal 8.8.0-alpha1 will be released the week of October 14th, 2019, which means new developments and disruptive changes should now be targeted against the 8.9.x-dev branch. (Any changes to 8.9.x will also be committed to 9.0.x in preparation for Drupal 9’s release, but some changes like significant feature additions will be deferred to 9.1.x.). For more information see the Drupal 8 and 9 minor version schedule and the Allowed changes during the Drupal 8 and 9 release cycles.

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

Drupal 8.9.0-beta1 was released on March 20, 2020. 8.9.x is the final, long-term support (LTS) minor release of Drupal 8, which means new developments and disruptive changes should now be targeted against the 9.1.x-dev branch. For more information see the Drupal 8 and 9 minor version schedule and the Allowed changes during the Drupal 8 and 9 release cycles.

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.

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.

danflanagan8’s picture

Status: Active » Needs review
Issue tags: -Needs tests
FileSize
3.32 KB

I've been going through some stale migrate issues and this caught my eye. (I've been working on getting better at Unit tests.) This patch is a new approach compared to #18, so there's no interdiff.

I was wondering how would one go about testing a class with protected methods?

There's a common argument that unit testing should only concern itself with public methods. I'm not sure I quite buy the argument in all cases, but that's the approach I ended up taking here. I just test getLegacyDrupalVersion.

One way to test protected methods is to make a ReflectionClass. Another way is to do just what @robpowell did and make a new class that makes public versions of the various protected methods. Once again, I did not take either approach in this patch.

Looking at the trait, we might be ok just with testing the one public function. Most of the other functions are very simple wrappers. The only other non-trivial (protected) functions are getSystemData, createDatabaseStateSettings, and getMigrations. But if we're doing by-the-book unit tests we don't need to deal with those here.

quietone’s picture

@danflanagan8, thanks for working on this stale issue.

I think the test look good and it tests all paths through getLegacyDrupalVersion except for the two where it throws an exception. Can you add methods to test those?

danflanagan8’s picture

Thanks for the review, @quietone! Ah, I did shy away from those cases. I've dug back in and updated the patch...and in doing so discovered something interesting. Note that this test fails. (I updated drupal.cli to only run this one test.)

The first try/catch catches a \PDOException whereas the second catches a DatabaseWrapperException. I slogged through the code and it looks to me like Connection::query will throw DatabaseWrapperException, not a \PDOException.

The git history of those two try/catch blocks is very disjointed, and seeing as there was never a unit test, it's not surprising there would be a little boo boo like this.

How to proceed?

Status: Needs review » Needs work

The last submitted patch, 27: 2675006-27.patch, failed testing. View results

danflanagan8’s picture

Setting back to NR to get feedback on how to proceed.

quietone’s picture

Yea, I don't know why the wrapper fails but 'exception' => new \PDOException(), works. I do know that when I write such tests I use the specific exception being thrown, That isn't much help though, is it.

danflanagan8’s picture

I guess there are two approaches that jump out at me:

1. Write the test such that it passes and then address the incorrect exception being caught in a separate issue.
2. Change the scope of this issue and fix it now.

quietone’s picture

I think since writing the test exposed the issue, fix it here.

danflanagan8’s picture

Title: Write UnitTest for MigrationConfigurationTrait » Write UnitTest for MigrationConfigurationTrait and fix Exception Handling
Issue summary: View changes
Status: Needs work » Needs review
FileSize
573 bytes
4.9 KB

I was hoping you would say that, @quietone!

Here's the updated patch. I'll also update the IS and title.

The interdiff does not contain anything about drupal.cli, which is back to normal. Thus the longer filename, since it's not technically the interdiff. :)

quietone’s picture

Status: Needs review » Reviewed & tested by the community

@danflanagan8, thanks for fixing that up!

I do like that the clear descriptions for the test cases in the dataProvider array. Often I see and I write tests for exceptions in separate methods but in this case it is much easier to see that all paths are tested.

I think that if we want to add tests for getSystemData, createDatabaseStateSettings, and getMigrations they can be done in a new issue that addresses the protected methods.

alexpott’s picture

Version: 9.4.x-dev » 9.3.x-dev
Category: Task » Bug report
Status: Reviewed & tested by the community » Fixed

Committed and pushed ce4ce0ac356 to 10.0.x and 1f8bcfdba90 to 9.4.x and 3246a76fffb to 9.3.x. Thanks!

This is a bug fix because of the fix to the try catch block so backporting to 9.3.x

  • alexpott committed ce4ce0a on 10.0.x
    Issue #2675006 by danflanagan8, robpowell, quietone: Write UnitTest for...

  • alexpott committed 1f8bcfd on 9.4.x
    Issue #2675006 by danflanagan8, robpowell, quietone: Write UnitTest for...

  • alexpott committed 3246a76 on 9.3.x
    Issue #2675006 by danflanagan8, robpowell, quietone: Write UnitTest for...
Spokje’s picture

This seems to be breaking the HEAD of 10.0.x-dev when testing with PHP 8.1: https://www.drupal.org/pift-ci-job/2278178

danflanagan8’s picture

TIL

lol

First time my patch has broken HEAD, so that's exciting.

danflanagan8’s picture

Status: Fixed » Needs review
FileSize
1.02 KB

Here's a patch that can be applied on top of the already committed patches that should fix this. No interdiff because this is intended to apply to the dev branches where the previous patch was already applied.

Spokje’s picture

Status: Needs review » Reviewed & tested by the community

Tests are green on both PHP 8.0 and 8.1 => RTBC

alexpott’s picture

Status: Reviewed & tested by the community » Fixed

Thanks for the fix. This is a PHP 8.1 change so is failing on all branches.

  • alexpott committed d06a99d on 10.0.x
    Issue #2675006 by danflanagan8, robpowell, quietone, alexpott: Write...

  • alexpott committed fa36f85 on 9.4.x
    Issue #2675006 by danflanagan8, robpowell, quietone, alexpott: Write...

  • alexpott committed 1c0246c on 9.3.x
    Issue #2675006 by danflanagan8, robpowell, quietone, alexpott: Write...

Status: Fixed » Closed (fixed)

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