0

I am trying to establish a test suite for my project using the core testing module. But I can't get past such a trivial thing as setting a database variable.

A lot of tutorials on the web tell you to use $this->drupalVariableSet - e.g.:

public function setUp() {
  parent::setUp(array('my_module'));
  $this->drupalVariableSet('my_module_var', 42);
}

This throws "Call to undefined method Mymoduletest::drupalVariableSet()" - so it looks like it has been depreciated at some point.

Using variable_set, e.g.:

public function setUp() {
  parent::setUp(array('my_module'));
  variable_set('my_module_var', 42);
}

does not produce an error, but the setting has no effect (i.e. the variable has its default value when it is used in one of the my test cases.

Any help will be appreciated.

1 Answer 1

2

drupalVariableSet() has been removed as per Remove drupalVariableSet.

variable_set() is the function you should use. In fact, DrupalWebTestCase::setUp() uses it for some persistent variables used from Drupal core, right after the call to drupal_install_system().

  // Perform the actual Drupal installation.
  include_once DRUPAL_ROOT . '/includes/install.inc';
  drupal_install_system();

  $this->preloadRegistry();

  // Set path variables.
  variable_set('file_public_path', $this->public_files_directory);
  variable_set('file_private_path', $this->private_files_directory);
  variable_set('file_temporary_path', $this->temp_files_directory);

  // Set the 'simpletest_parent_profile' variable to add the parent profile's
  // search path to the child site's search paths.
  // @see drupal_system_listing()
  // @todo This may need to be primed like 'install_profile' above.
  variable_set('simpletest_parent_profile', $this->originalProfile);

As long as the following conditions are met, using variable_set() should work as expected.

  • The test class derives from DrupalWebTestCase and not from DrupalTestCase
  • variable_set() is called after parent::setUp()
  • The name of the persistent variable used in variable_set() doesn't contain typos
2
  • Thanks for answering. It was a typo. I was at my wit's end and just needed to know what would work if one did it correctly. IMHO those tutorials on the net using drupalVariableSet should be deleted or updated - as they are now very misleading and make it hard to locate those that show how to do it correctly. Feb 28, 2016 at 3:18
  • Yes, they confuse people, and make harder to find the right way, especially when the documentation is missing or hard to find.
    – apaderno
    Feb 28, 2016 at 7:58

Your Answer

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

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