0

I made a PHPUnit test for my moodle plugin But always get this error. It suppose that for every transaction a message sent to the user In live website, this function works fine but not in the PHPUnit test?

How to enable this message provider in the test code?

Debugging: Attempt to send msg from a provider enrol_wallet/wallet_transaction that is inactive or not allowed for the user id=148000

  • line 225 of /lib/messagelib.php: call to debugging()
  • line 80 of /enrol/wallet/classes/notifications.php: call to message_send()
  • line 144 of /enrol/wallet/classes/transactions.php: call to enrol_wallet\notifications::transaction_notify()
  • line 64 of /enrol/wallet/tests/transactions_test.php: call to enrol_wallet\transactions::debit()
  • line 1548 of /vendor/phpunit/phpunit/src/Framework/TestCase.php: call to enrol_wallet\transactions_test->test_credit_debit()
  • line 1154 of /vendor/phpunit/phpunit/src/Framework/TestCase.php: call to PHPUnit\Framework\TestCase->runTest()
  • line 80 of /lib/phpunit/classes/advanced_testcase.php: call to PHPUnit\Framework\TestCase->runBare()
  • line 728 of /vendor/phpunit/phpunit/src/Framework/TestResult.php: call to advanced_testcase->runBare()
  • line 904 of /vendor/phpunit/phpunit/src/Framework/TestCase.php: call to PHPUnit\Framework\TestResult->run()
  • line 675 of /vendor/phpunit/phpunit/src/Framework/TestSuite.php: call to PHPUnit\Framework\TestCase->run()
  • line 675 of /vendor/phpunit/phpunit/src/Framework/TestSuite.php: call to PHPUnit\Framework\TestSuite->run()
  • line 675 of /vendor/phpunit/phpunit/src/Framework/TestSuite.php: call to PHPUnit\Framework\TestSuite->run()
  • line 661 of /vendor/phpunit/phpunit/src/TextUI/TestRunner.php: call to PHPUnit\Framework\TestSuite->run()
  • line 144 of /vendor/phpunit/phpunit/src/TextUI/Command.php: call to PHPUnit\TextUI\TestRunner->run()
  • line 97 of /vendor/phpunit/phpunit/src/TextUI/Command.php: call to PHPUnit\TextUI\Command->run()
  • line 98 of /vendor/phpunit/phpunit/phpunit: call to PHPUnit\TextUI\Command::main()
  • line 122 of /vendor/bin/phpunit: call to include()

When I used:

message_get_providers_from_db('enrol_wallet');

or

message_get_providers_from_file('enrol_wallet');

both indicated that the provider existed and active

but when I use:

message_get_providers_for_user($user->id);

The provider not exist in the array.

1 Answer 1

0

I found that the problem is the enrol plugin itself isn't enabled

So the message provider with the plugin will not work in PHPUnit testunless the plugin enabled first.

so I enabled it first before taking action using the code and add it to the test at the beginning:

global $CFG;
if (!enrol_is_enabled('wallet')) {
    $class = \core_plugin_manager::resolve_plugininfo_class('enrol');
    // This method isn't exist in 3.11.
    if (method_exists($class, 'enable_plugin')) {
        $class::enable_plugin('wallet', true);
    } else {
        $plugins = [];
        if (!empty($CFG->enrol_plugins_enabled)) {
            $plugins = array_flip(explode(',', $CFG->enrol_plugins_enabled));
        }
        if (!array_key_exists('wallet', $plugins)) {
            $plugins['wallet'] = 'wallet';
            $new = implode(',', array_flip($plugins));
            add_to_config_log('enrol_plugins_enabled', false, true, 'wallet');
            set_config('enrol_plugins_enabled', $new);
            // Reset caches.
            \core_plugin_manager::reset_caches();
            // Resets all enrol caches.
            $syscontext = \context_system::instance();
            $syscontext->mark_dirty();
        }
    }
}
Sign up to request clarification or add additional context in comments.

2 Comments

As it’s currently written, your answer is unclear. Please edit to add additional details that will help others understand how this addresses the question asked. You can find more information on how to write good answers in the help center.
I edited it, I hope it is clearer now

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.