RequirementsPageTrait.php 3.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980
  1. <?php
  2. namespace Drupal\Tests;
  3. /**
  4. * Provides helper methods for the requirements page.
  5. */
  6. trait RequirementsPageTrait {
  7. /**
  8. * Handles the update requirements page.
  9. */
  10. protected function updateRequirementsProblem() {
  11. // Assert a warning is shown on older test environments.
  12. $links = $this->getSession()->getPage()->findAll('named', ['link', 'try again']);
  13. if ($links && version_compare(phpversion(), DRUPAL_MINIMUM_SUPPORTED_PHP) < 0) {
  14. $this->assertNoText('Errors found');
  15. $this->assertWarningSummaries(['PHP']);
  16. $this->clickLink('try again');
  17. $this->checkForMetaRefresh();
  18. }
  19. }
  20. /**
  21. * Continues installation when the expected warnings are found.
  22. *
  23. * This function is no longer called by any core test, but it is retained for
  24. * use by contrib/custom tests. It is not deprecated, because it remains the
  25. * recommended function to call for its purpose.
  26. *
  27. * @param string[] $expected_warnings
  28. * A list of warning summaries to expect on the requirements screen (e.g.
  29. * 'PHP', 'PHP OPcode caching', etc.). If only the expected warnings
  30. * are found, the test will click the "continue anyway" link to go to the
  31. * next screen of the installer. If an expected warning is not found, or if
  32. * a warning not in the list is present, a fail is raised.
  33. */
  34. protected function continueOnExpectedWarnings($expected_warnings = []) {
  35. $this->assertNoText('Errors found');
  36. $this->assertWarningSummaries($expected_warnings);
  37. $this->clickLink('continue anyway');
  38. $this->checkForMetaRefresh();
  39. }
  40. /**
  41. * Assert the given warning summaries are present on the page.
  42. *
  43. * If an expected warning is not found, or if a warning not in the list is
  44. * present, a fail is raised.
  45. *
  46. * @param string[] $warning_summaries
  47. * A list of warning summaries to expect on the requirements screen (e.g.
  48. * 'PHP', 'PHP OPcode caching', etc.).
  49. */
  50. protected function assertWarningSummaries(array $warning_summaries) {
  51. // Allow only details elements that are directly after the warning header
  52. // or each other. There is no guaranteed wrapper we can rely on across
  53. // distributions. When there are multiple warnings, the selectors will be:
  54. // - h3#warning+details summary
  55. // - h3#warning+details+details summary
  56. // - etc.
  57. // We add one more selector than expected warnings to confirm that there
  58. // isn't any other warning before clicking the link.
  59. // @todo Make this more reliable in
  60. // https://www.drupal.org/project/drupal/issues/2927345.
  61. $selectors = [];
  62. for ($i = 0; $i <= count($warning_summaries); $i++) {
  63. $selectors[] = 'h3#warning' . implode('', array_fill(0, $i + 1, '+details')) . ' summary';
  64. }
  65. $warning_elements = $this->cssSelect(implode(', ', $selectors));
  66. // Confirm that there are only the expected warnings.
  67. $warnings = [];
  68. foreach ($warning_elements as $warning) {
  69. $warnings[] = trim($warning->getText());
  70. }
  71. $this->assertEquals($warning_summaries, $warnings);
  72. }
  73. }