You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Moodle插件PHPUnit测试:enrol_wallet消息提供者无法向用户发消息求助

问题:Moodle插件PHPUnit测试中消息提供者无法启用

我为自己的Moodle插件编写了PHPUnit测试,但始终遇到错误。预期每完成一笔交易就向用户发送消息,该功能在生产网站中运行正常,但在PHPUnit测试中失效。

错误信息

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

调用栈

  • /lib/messagelib.php第225行:调用debugging()
  • /enrol/wallet/classes/notifications.php第80行:调用message_send()
  • /enrol/wallet/classes/transactions.php第144行:调用enrol_wallet\notifications::transaction_notify()
  • /enrol/wallet/tests/transactions_test.php第64行:调用enrol_wallet\transactions::debit()
  • 后续PHPUnit相关调用栈略

排查情况

使用以下代码查询时,均显示该消息提供者存在且处于活跃状态:

message_get_providers_from_db('enrol_wallet');

或

message_get_providers_from_file('enrol_wallet');

但使用message_get_providers_for_user($user->id);查询时,该提供者未出现在返回的数组中。

请问如何在测试代码中启用这个消息提供者?


解决方法

  • 直接为用户启用消息提供者
    在测试代码中,调用message_provider_enable_user()函数,手动为测试用户启用指定的消息提供者。示例代码:

    // 假设$user是你的测试用户对象
    message_provider_enable_user($user->id, 'enrol_wallet', 'wallet_transaction');
    

    调用后,该提供者会被添加到用户的可用列表中,message_get_providers_for_user()即可查询到。

  • 设置用户消息偏好
    在测试的setUp()方法中,针对该消息提供者为用户设置启用偏好:

    // 单独设置该提供者的启用状态
    $preference = new stdClass();
    $preference->userid = $user->id;
    $preference->component = 'enrol_wallet';
    $preference->eventname = 'wallet_transaction';
    $preference->processor = 'email'; // 替换为你需要的消息处理器,比如popup
    $preference->preference = 'enabled';
    $preference->value = 1;
    
    message_set_user_preference($preference);
    
  • 确保消息处理器处于启用状态
    测试环境中可能默认关闭了部分消息处理器,可手动启用:

    message_processor_enable('email'); // 根据需求替换处理器名称
    

内容的提问来源于stack exchange,提问作者Mohammad Farouk

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.20 10:20:12