PHP asserts not running in CI/during tests

Created on 16 January 2025, 5 days ago

Overview

It appears as though CI doesn't catch assert errors
See πŸ› HEAD broken with asserts enabled Active

Proposed resolution

Work out how to enable asserts during PHPUnit tests

User interface changes

πŸ“Œ Task
Status

Active

Version

0.0

Component

Data model

Created by

πŸ‡¦πŸ‡ΊAustralia larowlan πŸ‡¦πŸ‡ΊπŸ.au GMT+10

Live updates comments and jobs are added and updated live.
Sign in to follow issues

Merge Requests

Comments & Activities

  • Issue created by @larowlan
  • πŸ‡§πŸ‡ͺBelgium wim leers Ghent πŸ‡§πŸ‡ͺπŸ‡ͺπŸ‡Ί

    Found this via πŸ› HEAD broken with asserts enabled Active .

    Quoting myself from there:

    😱

    This suggests that assertions are disabled on CI somehow?!? We ran into this months ago IIRC.

    That would be a crucial thing to restore on CI 😬

    β€” #3500109-6: HEAD broken with asserts enabled β†’

    Both \XbConfigEntityHttpApiTest::testPattern() and \PropSourceEndpointTest() cause \Drupal\experience_builder\ClientSideRepresentation::renderPreviewIfAny() to be executed.

    I'm shocked that this somehow regressed. πŸ€ͺ

    β€” #3500109-7: HEAD broken with asserts enabled β†’

    Note: this would also mean that core's \Drupal\jsonapi\EventSubscriber\ResourceResponseValidator::onResponse() will never actually verify JSON:API response shape compliance, and the same thing would be true for XB's OpenAPI request + response body OpenAPI compliance validation (see \Drupal\experience_builder\EventSubscriber\ApiMessageValidatorBase::isProd())! 😱😱😱

    β€” #3500109-8: HEAD broken with asserts enabled β†’

  • πŸ‡§πŸ‡ͺBelgium wim leers Ghent πŸ‡§πŸ‡ͺπŸ‡ͺπŸ‡Ί

    On my local development environment, the same thing is happening! 😱 Because even this does not fail:

    diff --git a/index.php b/index.php
    index 750dc282dc2..f7d7ccccd60 100644
    --- a/index.php
    +++ b/index.php
    @@ -11,6 +11,8 @@
     use Drupal\Core\DrupalKernel;
     use Symfony\Component\HttpFoundation\Request;
     
    +assert(TRUE === FALSE);
    +
     $autoloader = require_once 'autoload.php';
     
     $kernel = new DrupalKernel('prod', $autoloader);
    

    So let's investigate:

    $ php -i | grep assert
    zend.assertions => 1 => 1
    assert.active => On => On
    assert.bail => Off => Off
    assert.callback => no value => no value
    assert.exception => On => On
    assert.warning => On => On
    

    (same exact values when I look at <?php phpinfo(); FWIW, so same settings for CLI and non-CLI)

    yet:

    ini_get('zend.assertions') === '1'
    ini_get('assert.exception') === '1'
    ini_get('assert.active') === '0'
    

    😬

    This is apparently caused by #3186524: Fix htaccess files for PHP 8 β†’ . I probably had an override for that in my settings.local.php previously and lost it at some point.

    Yep:

    diff --git a/.htaccess b/.htaccess
    index 1ac01a117b2..1dc9fce4c81 100644
    --- a/.htaccess
    +++ b/.htaccess
    @@ -30,7 +30,7 @@ AddType image/webp .webp
     # Drupal\Core\DrupalKernel::bootEnvironment() for settings that can be
     # changed at runtime.
     <IfModule mod_php.c>
    -  php_value assert.active                   0
    +  #php_value assert.active                   0
     </IfModule>
     
     # Requires mod_expires to be enabled.
    

    causes things to fail as expected.

  • πŸ‡§πŸ‡ͺBelgium wim leers Ghent πŸ‡§πŸ‡ͺπŸ‡ͺπŸ‡Ί

    Aha, I had been relying on the original example.settings.local.php for a long time, but πŸ“Œ Fix deprecated assert_options() function usage for PHP 8.3 Needs review removed the enabling of assertions at the end of 2023.

    Restoring those 2 lines:

    assert_options(ASSERT_ACTIVE, TRUE);
    assert_options(ASSERT_EXCEPTION, TRUE);
    

    … makes things work as expected again.

    Recently, during a reinstall of Drupal core, I had upgraded my settings.local.php to match the latest upstream one and hence lost the above. I wrongly assumed it'd continue to do the same things 😬 At least there's sanity again β€” I thought I was failing massively for never having noticed assertions were actually turned off for the entirety of the last >6 months of XB development! πŸ‘»πŸ€ͺ

    Now let's fix this.

  • πŸ‡§πŸ‡ͺBelgium wim leers Ghent πŸ‡§πŸ‡ͺπŸ‡ͺπŸ‡Ί

    Looks like πŸ“Œ Fix deprecated assert_options() function usage for PHP 8.3 Needs review not only broke having assertions enabled for Drupal developers on their local dev environments (in combination with #3186524: Fix htaccess files for PHP 8 β†’ 2 years prior), but … it also removed

            // Web tests are to be conducted with runtime assertions active.
            assert_options(ASSERT_ACTIVE, TRUE);
            // Force assertion failures to be thrown as exceptions.
            assert_options(ASSERT_EXCEPTION, TRUE);
    

    … 😱

    That'd explain why tests are not failing on GitLab CI either!

    Apparently there is a CR for this for Drupal 10.3: https://www.drupal.org/node/3391611 β†’ , but that states that only adding

    ini_set('zend.assertions', 1);
    

    is sufficient. But it is not! 😬 At least not on PHP 8.3.15 (the latest).

    It is not sufficient because

    # Most of the following PHP settings cannot be changed at runtime. See
    # sites/default/default.settings.php and
    # Drupal\Core\DrupalKernel::bootEnvironment() for settings that can be
    # changed at runtime.
    <IfModule mod_php.c>
      php_value assert.active                   0
    </IfModule>
    

    overrides it from its default of 1. πŸ€ͺ

    So this is a critical core bug, because Drupal core's .htaccess prevents the very intent of πŸ“Œ Fix deprecated assert_options() function usage for PHP 8.3 Needs review as documented in https://www.drupal.org/node/3391611 β†’ . 😭

  • πŸ‡§πŸ‡ͺBelgium wim leers Ghent πŸ‡§πŸ‡ͺπŸ‡ͺπŸ‡Ί

    Apparently there is a core issue for this: πŸ“Œ Remove mentions of assert.active from .htaccess Needs work .

    Bumped it to critical and explained why: #3398033-31: Fix deprecated assert.active directive β†’ .

  • First commit to issue fork.
  • Merge request !559Try to enable assertions in PHPUnit. β†’ (Merged) created by longwave
  • πŸ‡¬πŸ‡§United Kingdom longwave UK

    The MR is no good, enabling them in PHPUnit isn't the problem, it's in the site under test that we need to do it.

  • πŸ‡§πŸ‡ͺBelgium wim leers Ghent πŸ‡§πŸ‡ͺπŸ‡ͺπŸ‡Ί
  • πŸ‡§πŸ‡ͺBelgium wim leers Ghent πŸ‡§πŸ‡ͺπŸ‡ͺπŸ‡Ί

    Let's merge this to make CI tests fail explicitly, so that the title of πŸ“Œ Improve or remove ComponentSourceInterface::getClientSideInfo() Active becomes a reality.

  • Pipeline finished with Skipped
    4 days ago
    #397640
  • πŸ‡§πŸ‡ͺBelgium wim leers Ghent πŸ‡§πŸ‡ͺπŸ‡ͺπŸ‡Ί
Production build 0.71.5 2024