5

I have written a drupal module that uses the CiviCRM api. I want to build tests for my module. Drupal 7 includes the simpletest module in core, so I'm using that. I have created the following test:

class ExampleCiviCRMTestCase extends DrupalWebTestCase {

public static function getInfo(){
    return array(
        'name' => 'Example CiviCRM Test',
        'description' => 'Check that CiviCRM API can be used in tests',
        'group' => "CiviCRM",
    );
}

public function setUp() {
    parent::setUp(array('civicrm'));
}

public function testCivicrmWorks(){
    civicrm_initialize();
    try {
        $result = civicrm_api3('Contact', 'get', array(
            'sequential' => 1,
        ));
    } catch (CiviCRM_API3_Exception $e) {
        $error = $e->getMessage();
        $this->fail($error);
    }
    $this->assertFalse(empty($result['count']));
}
}

This test fails. The error page states that the civicrm module was successfully enabled, but then this error occured: Access to undeclared static property: Database::$ignoreTargets. The error occured in database.inc on line 1473. Does anyone have any experience using simpletest with civicrm?

1
  • I'm interested in this, too. I've done testing with CiviCRM using Selenium (drives an actual web browser), and also using phpunit - a bit of a hack (and not unit testing) but you can bootstrap Drupal and civi and then use normal php unit stuff. I realise this is not an answer to your Q, but that's why it's in a comment :-) Commented Apr 18, 2016 at 14:43

1 Answer 1

2

I'm not an expert of testing but i'm pretty sure that SimpleTest creates a new instance of Drupal with a new database each time the testing process is ran.

CiviCRM is not a standard Drupal module and cannot just be enabled. In your SetUp function, You need to install CiviCRM to create the corresponding tables in the database. It could be a rather complex task but it's possible to do so. There is a drush script to do it so it might be a good inspiration.

You might want to check drush_civicrm_install in https://github.com/civicrm/civicrm-drupal/blob/7.x-master/drush/civicrm.drush.inc

Otherwise, you might find easier to use CiviCRM testing way (phpUnit) : https://wiki.civicrm.org/confluence/display/CRMDOC/Testing

And an example : https://github.com/civicrm/civicrm-core/blob/master/tests/phpunit/api/v3/ActivityTest.php

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.