Problem/Motivation

\Drupal\automatic_updates\Controller\UpdateController::onFinish
has a problem where assign a value to $message

and at end of the method we output it
$this->messenger()->addStatus($message);

but between then we have

foreach ($messages as $message) {
          $this->messenger()->addStatus($message);
        }

If we go into that loop then we would overwrite $message

We probably don't have a test case that covers this.

Proposed resolution

  1. update a test or write a new failing test to prove that current but existings

    This would likely mean arriving at \Drupal\automatic_updates\Controller\UpdateController::onFinish with a message ready to be displayed that is not 'Operating in maintenance mode.' so that we would go into the foreach loop here

    
    $messages = array_filter($messages, function (string $message) {
              return !str_starts_with($message, (string) $this->t('Operating in maintenance mode.'));
            });
            $this->messenger()->deleteByType(MessengerInterface::TYPE_STATUS);
            foreach ($messages as $message) {
              $this->messenger()->addStatus($message);
            }
  2. rename one of the vars. to prove it fixes the failing test
Command icon Show commands

Start within a Git clone of the project using the version control instructions.

Or, if you do not have SSH keys set up on git.drupalcode.org:

Comments

tedbow created an issue. See original summary.

tedbow’s picture

Issue summary: View changes
rahul_’s picture

Assigned: Unassigned » rahul_

rahul_’s picture

Status: Active » Needs review
omkar.podey’s picture

Status: Needs review » Needs work

Test case Missing otherwise looks good .

tedbow’s picture

Here is the test case we need

  1. The test should not start in Maintenance mode but should put into Maintenance module during the update(the default behavior. This would be similar to \Drupal\Tests\automatic_updates\Functional\UpdaterFormTest::testSuccessfulUpdate where $maintenance_mode_on starts as false. Basically no need to call $state->set('system.maintenance_mode', $maintenance_mode_on);

    This will ensure we get inside this IF block

    // Now that the update is done, we can put the site back online if it was
          // previously not in maintenance mode.
          if (!$request->getSession()->remove(BatchProcessor::MAINTENANCE_MODE_SESSION_KEY)) {
    
  2. We need a test case where starts off with at least 2 messages, 1 "'Operating in maintenance mode.'" and 1 other one we create.
    That was we will go into the `foreach` loop in the block with an `$messages` array with 1 element, because "Operating in maintenance mode" would have been filtered out.
    $messages = array_filter($messages, function (string $message) {
              return !str_starts_with($message, (string) $this->t('Operating in maintenance mode.'));
            });
            $this->messenger()->deleteByType(MessengerInterface::TYPE_STATUS);
            foreach ($messages as $message) {
              $this->messenger()->addStatus($message);
            }
  3. To create the extra message we need a subscriber that just adds a message in `PostApplyEvent`. We could add another test subscriber to automatic_updates_test test module but I think it would be more useful to add the ability to just set messages in \Drupal\package_manager_test_validation\EventSubscriber\TestSubscriber. so we would need a method on that class
    public static function setMessage(string $message, string $message_type, string $event)

    Then we would update \Drupal\package_manager_test_validation\EventSubscriber\TestSubscriber::handleEvent to handle the case where we have message set. See how handleEvent current interacts with the other `set*` methods on that class.

    Then we could set a message in our tests like this
    TestSubscriber1::setMessage("my message", PostApply::class); like we do now for validation results.

  4. Then if we set the message correcty "Update Complete" would not show at the end of our test.

    so the end of our test would be

    $assert_session->pageTextContains('Update complete!');
    This line should currently fail, proving the bug

rahul_’s picture

Status: Needs work » Needs review

I created a failure testcase for reproduce the message override issue.
Could you please re-review MR. please let me know if any improvement needed.

tedbow’s picture

Status: Needs review » Needs work

@rahul_ thanks you for making the 2nd MR. I only review https://git.drupalcode.org/project/automatic_updates/-/merge_requests/433

Left some comments there.

rahul_’s picture

Status: Needs work » Needs review

I updated the MR, with '3304640-test-fail' rebased into '3304640-logic-error-in'.

Needs re-review of MR.
Thanks

tedbow’s picture

Status: Needs review » Reviewed & tested by the community

@rahul_ I pushed up 2 comment changes. I checked the failures on `3304640-test-fail` branch and they are what we could expect.

This looks good now. will merge if tests still pass/fail as expected with the last commits

  • tedbow committed 3308cc4 on 8.x-2.x authored by rahul_
    Issue #3304640 by rahul_, tedbow: Logic error in \Drupal\...
tedbow’s picture

Status: Reviewed & tested by the community » Fixed

Thanks @rahul_! 🎉

Status: Fixed » Closed (fixed)

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

tedbow’s picture

Issue tags: +core-mvp