Problem/Motivation

We do not have test coverage of the Simpletest UI - we should test sessions and logging in a Webtestbase, and running KernalTestBase and PHPUnit tests so we don't break it (whilst we have a UI).

Proposed resolution

Add test coverage and fix PHPUnit tests.

This patch:

  • extends SimpleTestTest::stubTest() to also test user logging in thereby testing the browser and session system when testing through the UI
  • tests running KernalTestBase through the UI (this was broken for months due to sessions bleeding from the parent enviroment)
  • fixes and tests running PHPUnit through the UI
  • makes SimpleTestTest a bit simpler to understand by moving tests that are not run in the test in test to another test.

Remaining tasks

Review patch.

User interface changes

None

API changes

None

Support from Acquia helps fund testing for Drupal Acquia logo

Comments

alexpott’s picture

FileSize
1.29 KB

So the error in the web test is "file_get_contents(/Volumes/devdisk/dev/sites/drupal8alt.dev/sites/simpletest/254593/.htkey): failed to open stream: No such file or directory" which seems like we fail to properly set up a webtest once we're inside a webtest.

The patch attached shows just how messed up things currently are.

alexpott’s picture

FileSize
2.33 KB
2.7 KB

The patch attached fixes running webtestbase tests from the UI in a webtestbase - aka testception :)

We need #2239969: Session of (UI) test runner leaks into web tests in order to fix KernelTestBase tests - but that patch broke WebTestBase testing in the UI so I suggest we commit this one before that.

The last submitted patch, 1: 2272879.1.patch, failed testing.

The last submitted patch, d8.simpletesttest-will-fail.patch, failed testing.

alexpott’s picture

Issue summary: View changes
sun’s picture

Issue summary: View changes

1. Web tests are covered by SimpleTestTest already.

2. If those changes to drupal_generate_test_ua() are actually relevant in any way, then that (whichever?) edge-case MUST trigger a fatal error (by throwing an uncaught exception, whatever).

3. In short, the proposed patch looks very dubious to me. The drupal_generate_test_ua()/drupal_valid_test_ua() logic was and is invoked in a very controlled way. There MAY be bugs in the (dated) UI test runner, but not in this code.

The code explicitly prevents any further code execution in case conditions are not met. In case that logic was muddied through follow-up patches that I'm not aware of, then we have a much larger (security) problem.

Status: Needs review » Needs work

The last submitted patch, 2: 2272879.2.patch, failed testing.

alexpott’s picture

Status: Needs work » Needs review
FileSize
3.64 KB
14.23 KB
15.83 KB

Well the SimpletestTest::stubTest never tests making a call to anything that makes a request and this is exactly what was broken in #2239969: Session of (UI) test runner leaks into web tests so our test coverage is not enough.

In fact the SimpleTestTest is a mess - the test methods testInternalBrowser() and testUserAgentValidation() should not be in there since they impact the output of stubTest() that is run from testWebTestRunner() when we're in test inception. Having these additional do nothing test methods results in the child test running three test methods two of which do nothing accept print additional Enabled modules: non_existent_module messages.

As you can see from the patches attached in #0 we have a problem using the internal browser once DRUPAL_TEST_IN_CHILD_SITE is set to TRUE (constants which change values - I always disliked that in the simpletest multisite patch). The moment we do a request to the internal browser it will fail - this is because we have a new database prefix but the code in drupal_generate_test_ua() makes it utterly impossible to generate a .htkey file for the child site.

    if (DRUPAL_TEST_IN_CHILD_SITE && $parent_prefix = drupal_valid_test_ua()) {
      if ($parent_prefix != $prefix) {
        throw new \RuntimeException("Malformed User-Agent: Expected '$parent_prefix' but got '$prefix'.");
      }

It seems impossible for this exception to be thrown since drupal_valid_test_ua() is called with the child test prefix in TestBase::prepareEnvironment() - therefore the condition $parent_prefix != $prefix will never be met.

I would argue that this method of test the test runner is completely false and overly complex to understand and very liable to produce false positives - for example at the moment we're not even testing the ability to enable modules successfully when running tests through the UI since the only thing in setUp that we do (when in a childsite) is enable a module that does not exist. It would be far more realistic to just run one of the thousands of tests we have.

The only advantage of SimpleTestTest::testWebTestRunner() and SimpleTestTest::stubTest() is that it makes our assumptions clear. The 2272879.8.patch attached:

  • Moves the browser tests to SimpleTestBrowserTest to keep testception down to only 1 test
  • Adds a comment to SimpleTestTest so that people are warned not to add additional test methods
  • Generates a .htkey in stubTest and logs in a user in stubTest to prove the internal browser works from the UI and that sessions do not get in the way.

BUT: I've attached 2272879-better.8.patch that does not generate the .htkey in from stubTest but includes the same fix as in #2 because I think this is the correct fix...

The drupal_generate_test_ua()/drupal_valid_test_ua() logic was and is invoked in a very controlled way

... since "this logic" Is directly responsible for this bug.

The better patch also includes a new test that just runs other tests - this would just fail if it was included in 2272879.8.patch.

alexpott’s picture

Issue summary: View changes
alexpott’s picture

FileSize
16.25 KB

Rerolled patch and we can now test both WebTestBase tests and KernelTestBase tests. Went for the option that is not change bootstrap.inc so that should make this patch acceptable to @sun - since test coverage of UI is more important.

Status: Needs review » Needs work

The last submitted patch, 10: 2272879.10.patch, failed testing.

alexpott’s picture

Status: Needs work » Needs review
Related issues: +#2306649: Running phpunit tests via the UI results in a fatal
FileSize
3.29 KB
16.8 KB

Okay we need to add PHPUnit test coverage and there is no need to cover WebTestBase tests since this is already covered in SimpleTestTest through it's testception.

Test only patch available on #2306649: Running phpunit tests via the UI results in a fatal

m.stenta’s picture

Patch in #12 worked for me!

clemens.tolboom’s picture

  1. I fixed this (but is not in the interdiff)
    +++ b/core/modules/simpletest/src/Tests/SimpleTestTest.php
    @@ -211,23 +121,39 @@ function testWebTestRunner() {
    +    // @see  drupal_generate_test_ua();
    

    1 space to many

  2. I had a lot of trouble understanding the comments in function stubTest() {. Ie what is the 'math' for
        // These cause the eleventh to fourteenth of the fifteen passes asserted in
        // confirmStubResults().
        $this->assertTrue(file_exists(conf_path() . '/settings.testing.php'));
    
  3. Shouldn't the creation of the .htkey file be done in its own method?
  4. Why don't we run into #2246725: Make sure TestSitePath exist before creating .htkey ?
clemens.tolboom’s picture

I missed the new core/modules/simpletest/src/Tests/SimpleTestBrowserTest.php completely :(

alexpott’s picture

re #14

  1. sure
  2. That's why reviewing having the full context is good... the full context is:
        // These cause the eleventh to fourteenth of the fifteen passes asserted in
        // confirmStubResults().
        $this->assertTrue(file_exists(conf_path() . '/settings.testing.php'));
        // Check the settings.testing.php file got included.
        $this->assertTrue(function_exists('simpletest_test_stub_settings_function'));
        // Check that the test-specific service file got loaded.
        $this->assertTrue($this->container->has('site.service.yml'));
        $this->assertIdentical(get_class($this->container->get('cache.backend.database')), 'Drupal\Core\Cache\MemoryBackendFactory');
    
  3. I don't think so - it is only needed by stubTest because this is a test running in test.
  4. Because stubTest is writing the file. #2246725: Make sure TestSitePath exist before creating .htkey is not really related - I guess that might be due to using the drush test runner. I run tests after a drush install all the time. But I do it using run-tests.sh

In my mind all the changes in the interdiff attached to #14 are out of scope since the fail and pass properties were not added or changed by this patch but it improves test readability so I'm ok with it.

alexpott’s picture

Issue summary: View changes
dawehner’s picture

so finally I have proper internet connection to post it.

tests running KernalTestBase through the UI (this was broken for months due to sessions bleeding from the parent enviroment)

Ha, so annoying that I though this is a feature ;)

makes SimpleTestTest a bit simpler to understand by moving tests that are not run in the test in test to another test.

Good idea!

  1. +++ b/core/modules/simpletest/src/Tests/SimpleTestBrowserTest.php
    @@ -0,0 +1,143 @@
    +  function setUp() {
    ...
    +  function testInternalBrowser() {
    ...
    +  function testUserAgentValidation() {
    ...
    +  function testTestingThroughUI() {
    

    let's directly fix the visiblity of the methods.

  2. +++ b/core/modules/simpletest/src/Tests/SimpleTestBrowserTest.php
    @@ -0,0 +1,143 @@
    +    // Test the maximum redirection option.
    +    $this->drupalLogout();
    +    // Check that current user service updated to anonymous user.
    +    $this->assertEqual(0, $this->container->get('current_user')->id(), 'Current user service updated.');
    +    $edit = array(
    +      'name' => $user->getUsername(),
    +      'pass' => $user->pass_raw
    +    );
    +    $this->maximumRedirects = 1;
    

    It would be great to move the comment below. The first is just about checking that the logout changes the UID as well.

  3. +++ b/core/modules/simpletest/src/Tests/SimpleTestTest.php
    @@ -82,101 +83,11 @@ class: Drupal\Core\Cache\MemoryBackendFactory
    +    $this->passMessage = t('SimpleTest pass.');
    +    $this->failMessage = t('SimpleTest fail.');
    

    undefined variables.

clemens.tolboom’s picture

Fixed issues from #18

vijaycs85’s picture

+1 to get this in ASAP. This is really going to save my time...

meanwhile,

Step 1: Install drupal
Step 2: Enable debug to see what's wrong with admin/config/development/testing by http://drupal.stackexchange.com/questions/127182/how-to-enable-developer...
Step 3: Go to simpletest.module and make below change:

diff --git a/core/modules/simpletest/simpletest.module b/core/modules/simpletest/simpletest.module
index fb6ae42..24665c7 100644
--- a/core/modules/simpletest/simpletest.module
+++ b/core/modules/simpletest/simpletest.module
@@ -140,7 +140,7 @@ function simpletest_run_tests($test_list) {
     ->execute();

   if (!empty($test_list['phpunit'])) {
-    $phpunit_results = simpletest_run_phpunit_tests($test_id, $phpunit_tests);
+    $phpunit_results = simpletest_run_phpunit_tests($test_id, $test_list['phpunit']);
     simpletest_process_phpunit_results($phpunit_results);
   }

dawehner’s picture

Status: Needs review » Reviewed & tested by the community

Let's get it in!

Status: Reviewed & tested by the community » Needs work

The last submitted patch, 19: can_not_run_a-2272879-19.patch, failed testing.

alexpott’s picture

Status: Needs work » Reviewed & tested by the community
FileSize
18.75 KB
webchick’s picture

Status: Reviewed & tested by the community » Fixed

Committed and pushed to 8.x. Thanks!

  • webchick committed b5ade79 on 8.0.x
    Issue #2272879 by clemens.tolboom, alexpott: Fixed Can not run a...

Status: Fixed » Closed (fixed)

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