Problem/Motivation
We want to get rid of simpletest.module in Drupal 9. We want to prepare the latest Drupal 8 development branch as much as possible before opening the Drupal 9 development branch (see #2608062: [META] Requirements for opening the Drupal 9 branch). Therefore we need to convert all web tests to BrowserTestBase or UnitTestBase or KernelTestBase and deprecate WebTestBase.
Proposed resolution
1) Convert a small part of Drupal core web tests to Browser tests to get them battle tested. See all the conversion child issues that have been opened here before 2016-Sept-01.
2) Implement a backward compatibility layer as AssertLegacyTrait used by BrowserTestBase. Ideally a test conversion should only be the change of the namespace and used base class. Everything else should be covered by backwards compatible methods as far as possible/reasonable.
3) Prepare one big bang patch of test conversions in #2770921: Feb 21st: Convert chunk of WTB to BTB by just moving classes, changing use statements adding traits. Commit it during code freeze of the next Drupal minor release (likely March 2017 for 8.3).
4) In the meantime open child issues to convert web tests for each core module. While doing so we should also check if the test cases actually require a browser or can be converted to unit/kernel tests. They will be postponed until #2770921: Feb 21st: Convert chunk of WTB to BTB by just moving classes, changing use statements adding traits is committed.
Instructions for converting a Simpletest web test
- Read https://www.drupal.org/phpunit and all subpages, especially https://www.drupal.org/node/2783189 .
- Do not bulk move all the test files but move and work on one test at a time.
- Move the test file to a tests/src/Functional folder in the module.
- Rename the namespace, make the class extend BrowserTestBase and fix the use statement.
- If there is a test method which does not perform any web request (for example no ->drupalGet() or ->drupalPostForm() calls) then that method should be extracted to a unit test or kernel test. $this->setRawContent() generally means the test should be converted to a Kernel test
- $this->drupalPostAjaxForm(); means the test should be converted to a Javascript test. There most likely already is an issue for it, see #2809161: Convert Javascript/AJAX testing to use JavascriptTestBase
- Otherwise try to change as little as possible in the converted web test. Do not get tempted to "improve" or "tidy up" the tests - the conversion should be easy to review with only the minimum necessary changes. Further cleanup can be done in a follow-up issue.
- Run the single test file with phpunit (replace the path with the file you converted):
cd core ../vendor/bin/phpunit modules/help/tests/src/Functional/HelpTest.php --stop-on-failure
- Regularly upload a patch to see if the tests pass on the testbot as well.
- If the conversion starts to involve complex changes, please reach out to people or search the other conversion issues. Occasionally it can be helpful to skip tricky tests, in which case update the issue summry stating which tests are not converted yet.
- Create follow-up issues to deal with blockers so that progress can continue with converting the majority of tests in a module. Update the "Conversions blocked by another" section on this page.
- Any TestBase or Trait which is in the old /src/Tests/ location should remain there and be deprecated. Copy the TestBase or Trait file into the new location and alter the tests to use the new version. The deprecated class must not "use" the newly-moved class, but remain exactly as it is. Create a change record for the deprecation notice - one CR can cover all deprecations within a module.
Hints and Tips
- Simpletest allowed typecasting of elements. BrowserTestBase does not. Instead of (string) $element use $element->getText() or $element->getHtml();
- Views tests need the new constructor format with
$import_test_views = TRUE
as the first parameter in the setUp() method. - Attributes are fetched with
$item->getAttribute('value');
now, previously it was$item->value
or$item->attributes()->value.
- Not all legacy assertion methods are implemented yet, for example you may get
Fatal error: undefined function assertNoLink()
. Check #2750941: Additional BC assertions from WebTestBase to BrowserTestBase to borrow from there.
Remaining tasks
Resolve all the child issues. Convert web tests in child issues per module/component in Drupal core.
Issue creation
Please add the tag 'phpunit initiative' to every issue and add the issue to the correct Component (if it's a test for the comment module, add it to comment.module).
Remaining simpletests
Check http://simpletest-countdown.org/simpletestcount/simpletests
- Drupal\file\Tests\FileFieldWidgetTest #2809503: Convert AJAX part of \Drupal\file\Tests\FileFieldWidgetTest to JavascriptTestBase
- Drupal\quickedit\Tests\QuickEditAutocompleteTermTest #2870458: Convert web tests to browser tests for quickedit module
- Drupal\quickedit\Tests\QuickEditLoadingTest #2870458: Convert web tests to browser tests for quickedit module
- Drupal\responsive_image\Tests\ResponsiveImageFieldUiTest #2809513: [PP-1] Convert AJAX part of \Drupal\responsive_image\Tests\ResponsiveImageFieldUiTest to JavascriptTestBase and the rest to BrowserTestBase
- Drupal\simpletest\Tests\BrokenSetUpTest #2981870: Duplicate BrokenSetUpTest for BrowserTestBase
- Drupal\simpletest\Tests\BrowserTest #2932909: Convert web tests to browser tests for Simpletest module
- Drupal\simpletest\Tests\InstallationProfileModuleTestsTest #2932909: Convert web tests to browser tests for Simpletest module
- Drupal\simpletest\Tests\KernelTestBaseTest #2932909: Convert web tests to browser tests for Simpletest module
- Drupal\simpletest\Tests\MissingCheckedRequirementsTest #2932909: Convert web tests to browser tests for Simpletest module
- Drupal\simpletest\Tests\SimpleTestBrowserTest #2932909: Convert web tests to browser tests for Simpletest module
- Drupal\simpletest\Tests\SimpleTestInstallBatchTest #2932909: Convert web tests to browser tests for Simpletest module
- Drupal\simpletest\Tests\SimpleTestTest #2932909: Convert web tests to browser tests for Simpletest module
- Drupal\simpletest\Tests\TimeZoneTest #2932909: Convert web tests to browser tests for Simpletest module
- Drupal\simpletest\Tests\UiPhpUnitOutputTest #2932909: Convert web tests to browser tests for Simpletest module
- Drupal\simpletest\Tests\WebTestBaseInstallTest #2932909: Convert web tests to browser tests for Simpletest module
- Drupal\system\Tests\Session\SessionHttpsTest #3011079: Convert \Drupal\system\Tests\Session\SessionHttpsTest to PHPUnit
Progress report
Want to know how we are doing? Check: http://simpletest-countdown.org
API changes
Simpletest and WebTestBase will be deprecated.