1

I would like to pull certain configuration items from settings.php from a DrupalWebTestCase in order to test my module (in this case, a SFTP module). I want to do it this way so that I don't hardcode my SFTP credentials.

For example, in my DrupalWebTestCase, I have:

  public function test_sftp()
  {
    $this->assertTrue(variable_get('mymodule_test_sftp'));
    // then I would try to connect, etc....
  }

In settings.php:

$conf['mymodule_test_sftp'] = array(
 'host' => 'example.com',
 // etc...
);

When I run the unit testing suite, this unit test fails, the value returned by variable_get is NULL.

Edit:

Running the code below, returns the correct database settings, but I get "Undefined index: mymodule_test_sftp":

  public function test_sftp()
  {
    global $databases, $conf;
    $this->assertTrue($databases);
    $this->assertTrue($conf['mymodule_test_sftp']);
  }
9
  • Have you used variables_set('mymodule_test_sftp', $variable); first? Commented Jan 27, 2016 at 13:25
  • No, but I do not want to hard code my credentials. That's why I want to use settings.php
    – Weboide
    Commented Jan 27, 2016 at 13:26
  • in your simpletest check the db values manually to ensure they are there Commented Jan 27, 2016 at 13:31
  • See my edit, it does pull the right databases information. I also tried with global $conf, but this still gives an error.
    – Weboide
    Commented Jan 27, 2016 at 13:43
  • 2
    Simpletest is running on separate Drupal instance witch don't have setting.php edited by you. variable_get() gets you values from system.variable but in your simpletest from simpletestxxxx.system.variable I suposse Commented Jan 27, 2016 at 13:57

2 Answers 2

2

Just faced a similar issue. What I have found out is that in your DrupalWebTestCase child class you still have access to all variables defined in settings.php before calling parent::setUp(). Which means you can do something like this:

class MymoduleWebTestCase extends DrupalWebTestCase {

  public function setUp() {
    $mymodule_test_sftp = variable_get('mymodule_test_sftp');

    parent::setUp();

    variable_set('mymodule_test_sftp', $mymodule_test_sftp);
  }

}
0

In the current Drupal 7, I found that the above is only true the first time the test case setUp() method is called. On subsequent calls, the values from settings.php will either be gone or replaced by what was in the database, which is not always the same. A local development instance of a site, for example, might have a definition for "site_name" that is different from the "site_name" value in the database copied from the live site.

You can catch that first value and use it thereafter:

class MymoduleWebTestCase extends DrupalWebTestCase {

  public function setUp() {
    static $mymodule_test_sftp = NULL;
    if ($mymodule_test_sftp === NULL) {
        // if it's not defined, don't keep checking
        $mymodule_test_sftp = variable_get('mymodule_test_sftp', FALSE);
    }

    parent::setUp();

    if ($mymodule_test_sftp) {
        variable_set('mymodule_test_sftp', $mymodule_test_sftp);
    }
  }
 
}

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.