Contributed modules that provide Rules actions and conditions (plugins), and want to have Unit tests for them, need to subclassRulesIntegrationTestBase.

The method RulesIntegrationTestBase::enableModule() "Fakes the enabling of a module and adds its namespace for plugin loading. Default behaviour works fine for core modules."

Remember that modules are never enabled when Unit testing, but this enableModule() method is important because properly configuring the namespace is the only way for contributed modules to expose their plugins for testing. As the method's description says, this "works fine for core modules". The reason for that qualification is because configuring the namespace requires a directory path, and the method hardwires in '/core/modules' (relative to Drupal root) as the place to look for modules - that's where core modules live, but not so with contributed modules.

While a contributed module CAN pass in a directory path where it can be found as an argument to enableModule(), this path is system-specific. And in particular, a path that works for local testing on a local server will NOT work for DrupalCI testing in the testbot. So RulesIntegrationTestBase::enableModule() doesn't really work for contributed modules.

The solution is to implement a more general path discovery in RulesIntegrationTestBase.

Rather than just hardwiring '/core/modules' or allowing contributed modules to specify their own path, the attached patch copies some code out of the core Drupal ExtensionDiscovery class. This allows Rules Unit tests to discover modules anywhere in the file system, using only the module name, in exactly the same way that core Drupal finds modules on a real site. This makes enableModule() work for both core AND contributed modules. (And TEST modules too!)

This patch thus simplifies writing Unit tests for Rules contributed modules, and allows those tests to run in all environments. This patch also adds a test which tries to discover the plugins from the rules_test test module.

Also attached is a test-only patch to demonstrate the problem and to prove the solution. This patch will FAIL without the modified version of RulesIntegrationTestBase::enableModule().

Comments

TR created an issue. See original summary.

tr’s picture

Issue summary: View changes

The last submitted patch, contributed-plugin-discovery-test-only.patch, failed testing. View results

  • TR committed 0e7327b on 8.x-3.x
    Issue #3033431 by TR: Allow RulesIntegrationTestBase to discover...
tr’s picture

Status: Needs review » Fixed

Test results were as expected. I fixed the 1 reported coding standards message and committed the patch.

Status: Fixed » Closed (fixed)

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