I write a test using Drupal\simpletest\WebTestBase for a custom module, which implements hook_cron(). The test saves a configuration object, which should be used by the cron, and then runs the cron:

function testCron()
    $config = \Drupal::service('config.factory')->getEditable('mymodule.settings');
      ->set('some_setting', 'some value')
      ->set('another_setting', 'another value');

The settings are saved and can be used during the test. But when I want to use them in the cron (in mymodule.module):

 * Implements hook_cron().
function mymodule_cron()
    $config = \Drupal::config('mymodule.settings');

then instead of the saved values, the default settings from config/import/mymodule.settings.yml are used.

How can I force the cron to use the settings saved in the test?

  • You already posted a bunch of related questions, which all essentially result from something going very very wrong. What you're describing should simply not happen, but I'm not sure if it can be answered, would more likely need to be debugged on your system.
    – Berdir
    Apr 13 '17 at 21:05
  • 2
    Actually, I take that back. Your example is missing the save() call?
    – Berdir
    Apr 13 '17 at 21:05
  • You are right. I started a new job and write tests for a bunch of Drupal custom modules. I have wrote a lot of tests in Yii2/Codeception, but testing in Drupal is quite different... I have no save() call in the test. I will add it to the test as soon as I'm in my office (on Tuesday). Thanks. Apr 14 '17 at 4:17
  • The save() call is the correct solution. I just added '->save()' after '->set('another_setting', 'another value')'. Would you prefer to write the answer, or should I write it? Thanks. Apr 18 '17 at 7:20

As commented, you were missing the save call in the test.

That means that the change is only in the runtime object returned by getEditable() and since get() and getEditable() do not share the static cache, you didn't see the change.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.