I think adding some automated tests might help to solve some issues. I am not aiming for full test coverage, but on the short term these might be useful:

  • Kernel HighContrastConfigOverrideTest to ensure that the high contrast logo is returned from the configuration.
  • (Can be based on https://git.drupalcode.org/project/whitelabel/blob/8.x-2.x/tests/src/Ker...)

  • Functional HighContrastCacheTest to ensure that cache contexts are applied correctly and the right CSS file is served. Also ensure that changing te settings updates the cached result.
  • FunctionalJavascript HighContrastBlockTest to ensure that all switching methods work.

This can also be used to trigger deprecation notices and to prevent WSODs during installation..

Comments

Neograph734 created an issue. See original summary.

neograph734’s picture

Assigned: Unassigned » neograph734

Having a look at this next week.

neograph734’s picture

Status: Active » Needs review
StatusFileSize
new5.95 KB

I need some more time for the block test, but I think this should already be a nice start. (I think caching is included in the stylesheet test)

neograph734’s picture

StatusFileSize
new10.55 KB

Argh, a typo in the namespace; high_contast...
Retrying

Status: Needs review » Needs work

The last submitted patch, 4: high_contrast_tests_4.patch, failed testing. View results
- codesniffer_fixes.patch Interdiff of automated coding standards fixes only.

hkirsman’s picture

StatusFileSize
new13.07 KB
new4.43 KB

Automated testing is quite new to me but I'm starting to love it. It's a struggle though to set it up in the first place. I actually wrote 2 functional tests yesterday and thought I'd use the same environment and voila Error: Class 'org\bovigo\vfs\vfsStream' not found :D
composer require mikey179/vfsstream --dev fixes that but there's something else going on. I think you're running them somehow differently because I'm getting the same errors as is the results of #5.

Basically what I run is this:
./vendor/bin/phpunit web/modules/contrib/high_contrast/tests/src/FunctionalJavascript/HighContrastStyleSheetTest.php
or

./vendor/bin/phpunit web/modules/contrib/high_contrast/

You should have 7.5 if you're running on Drupal 8.8:

./vendor/bin/phpunit --version
PHPUnit 7.5.20 by Sebastian Bergmann and contributors.

But to the tests

1.
Drupal\Tests\high_contrast\FunctionalJavascript\HighContrastStyleSheetTest::testStyleSheetGenerated
Error: Call to undefined function file_save_data()

This one was strange. You can basically disable everything in the test and you still got the error. Seems though what is triggering it adding 'high_contrast' to $modules :S

In the mean time I had to also install selenium

lando composer require behat/mink-selenium2-driver --dev

Ok, so file_save_data() is used in high_contrast_install() and that breaks it. So file is not test dependency, but module dependency.

dependencies:
  - drupal:file

What's odd for me is that I can't remove the file dependendy from test. Maybe it's KernelTestBase peculiarity

I also added - drupal:block as dependency as I know the module adds block.

2.
I see this patch will conflict with https://www.drupal.org/project/high_contrast/issues/3114528 There I did changes like:

-  function enable_high_contrast() {
+  public function enableHighContrast() {

Could we merge that and then do updates here?

3.
Drupal\Tests\high_contrast\Kernel\HighContrastConfigOverrideTest::testConfigurationOverrides
Drupal\Component\Plugin\Exception\PluginNotFoundException: The "user" entity type does not exist.

In this case it seems that $this->installEntitySchema() needs user. I searched in core and there are about 5 tests where it's used and all have user as dependency. The error is gone.

4.
New errors now, yay! :D

Drupal\Tests\high_contrast\FunctionalJavascript\HighContrastStyleSheetTest::testStyleSheetGenerated
Symfony\Component\DependencyInjection\Exception\ServiceCircularReferenceException: Circular reference detected for service "theme_handler", path: "theme_handler -> config.factory -> high_contrast.overrider -> theme.manager -> theme.negotiator -> access_check.theme".

Ok, so I searched for "{ name: config.factory.override }" in core and none of them had any arguments, which was suspicious. Then I came across https://www.drupal.org/docs/8/api/configuration-api/configuration-overri... . One sentence there cought my eye:

It is worth reiterating that if you need to read the original configuration values in an override, for example, to compare or merge, you would need to load from the \Drupal::configFactory and not \Drupal::config to avoid a nested loop into your override: -
$original = \Drupal::configFactory()->getEditable('system.site')->getOriginal('name', FALSE);

But as the error is about theme, I removed '@theme.manager' from arguments and instead used \Drupal::theme(). No more errors

But now I'm at point where it says

OK, but incomplete, skipped, or risky tests!
Tests: 2, Assertions: 2, Skipped: 2.

neograph734’s picture

1.

Ok, so file_save_data() is used in high_contrast_install() and that breaks it. So file is not test dependency, but module dependency.

Yes I figured that out too. It should have been a dependency ever since that install hook :(.

What's odd for me is that I can't remove the file dependendy from test. Maybe it's KernelTestBase peculiarity

Functional tests install modules with all dependencies resolved; Kernel tests do not enable any of the depending modules, so they have to be enabled manually.

I also added - drupal:block as dependency as I know the module adds block.

The module adds a block, but I'd say it is a soft dependency. Theoretically the module can function without block being enabled (though there is currently no option to enable the high contrast mode), toggling high contrast could also be done with Core's action module for example. IMO it does not need to be in the .info.yml file.

2.

Could we merge that and then do updates here?

#3114528: Use Short syntax of array. is committed.

3.

In this case it seems that $this->installEntitySchema() needs user. I searched in core and there are about 5 tests where it's used and all have user as dependency. The error is gone.

Thanks for figuring that out. It didn't really make sense to me as there are no users created during the tests.

4.
I initially didn't understand this one, because the ConfigOverrideTest for whitelabel does not have an issue. https://git.drupalcode.org/project/whitelabel/blob/8.x-2.x/tests/src/Ker...

But then I realised that testStyleSheetGenerated is totally out of place here. The stylesheet is only saved in the submit callback of HighContrastConfigurationForm. So in ConfigOverrideTest we should remove testStyleSheetGenerated. It should be moved into a new functional test in which we could also assert that the values entered in the form are properly saved to the database.

UPDATE This was no longer about the ConfigOverrideTest, but still we cannot assert the stylesheet generation without form submission, so the point above remains valid.

UPDATE2 with #3115713: Move stylesheet generation to an event subscriber the test can remain as it is as it would remove the need to save the form.

hkirsman’s picture

StatusFileSize
new12.65 KB
new2.01 KB

1.2
Tx about info on kernel tests!

1.3
About the block dependency. I think it should be there until the module can be used theoretically without block :) Main way how to enable/disable Drupal 7 High Contrast is to use tab key. Maybe now it also has block in D7, but that was the original idea. It's used here for example https://www.valitsus.ee/en - press tab 3 times and you'll see 'Toggle high contrast' link, then press enter. So if that feature would be in Drupal 8, then I would treat block as soft dependency. I propose new task for this feature. What do you think?

2. Cool, tx! I'll re-add the patch against latest changes.

4. Still have to wrap around my head on this change. Haven't had time but will get back to this tomorrow.

Adding patch against latest changes in repo. Interdiff is a bit confusing. Maybe I was too tired but should be ok. There is not trace of enable_high_contrast left in the new patch.

-  public function enable_high_contrast() {
+  function enable_high_contrast() {
neograph734’s picture

neograph734’s picture

Status: Needs work » Needs review
StatusFileSize
new2.75 KB

Lets keep it small. I believe this test one test did pass. For the rest we need to decide on a strategy first.

Status: Needs review » Needs work

The last submitted patch, 10: high_contrast-add-tests-3114980-10.patch, failed testing. View results

neograph734’s picture

Status: Needs work » Needs review
StatusFileSize
new7.96 KB

Reverting the theme manager injection for HighContrastConfigOverride to prevent circular injections.

Status: Needs review » Needs work

The last submitted patch, 12: high_contrast-add-tests-3114980-12.patch, failed testing. View results

neograph734’s picture

Status: Needs work » Needs review
StatusFileSize
new8.54 KB
new468 bytes

Forgot te remove it from the services file.

Status: Needs review » Needs work

The last submitted patch, 14: high_contrast-add-tests-3114980-14.patch, failed testing. View results

neograph734’s picture

Status: Needs work » Needs review
StatusFileSize
new5.34 KB

How did that other test get back..
Last attempt.

neograph734’s picture

Status: Needs review » Postponed

Finally. Committed. Now lets continue after #2853420: Check if works properly with Boost or Varnish.

  • Neograph734 committed 8aede6d on 8.x-1.x
    Issue #3114980 by Neograph734, hkirsman: Add some automated tests (first...
hkirsman’s picture

Nice, tx!