I have written my first simpletest which tests a custom module I have written (a CiviCRM customisation).

The test is finishing successfully but I see the message

"The test run did not successfully finish."

If I try to put in a second test function into the class, or to run another class afterwards it hangs & never progresses. Any ideas what I am missing - the class below passes if I remove the second function but will never get to it (based on the rows in the 'simpletest' table) if I leave it in.

Drupal 6

NB - I do have xdebug installed but I set max nesting = 500 which I saw on the instructions

class egwcMemberEventsInternalsTestCase extends DrupalWebTestCase {
  /**
   * getInfo() returns properties that are displayed in the test selection form.
   */
  public static function getInfo() {
    return array(
      'name' => ' Memberevents -  internal functions',
      'description' => 'Test util functions.',
      'group' => ' Memberevents',
    );
  }
    /**
   * setUp() performs any pre-requisite tasks that need to happen. Creates Event, Contact & Membership
   */
  public function setUp() {
    $this->timeLimit = 600;
    // Enable any modules required for the test.
    parent::setUp( 'civicrm_egwcmemberevents');
    if (!civicrm_initialize()){
      $this->assertText('0',$result['is_error'],"CiviCRM not initialized");
    }
    require_once 'api/api.php';
    $year = date('Y');//not being used yet but a reminder to make dates generated
    variable_set('civicrm_egwcmemberevents_membership',2);
    variable_set('civicrm_egwcmemberevents_eventid',154);
    variable_set('civicrm_egwcmemberevents_eventsdate',153);
    variable_set('civicrm_egwcmemberevents_eventtypes', array(8 => 8));

    
    $result = civicrm_api('Event','Create',array('version' => 3, 'title' => 'test_event', 'event_type_id' => 8, 'start_date' => '2011-01-08','end_date' => '2012-01-04', 'is_active' => 1));
    $this->assertEqual('0',$result['is_error'],"Event Created : ".$result['id'] );
    $this->ids['event_id'] = $result['id'];
    
    $this->authenticatedUser = $this->drupalCreateUser(array('register for events','access all custom data'));  

    $result = civicrm_api('Contact','Create',array('version' => 3, 'email' => $this->authenticatedUser->mail, 'contact_type' => 'Individual', 'display_name' => 'test individual'));
    $this->assertEqual('0',$result['is_error'],empty($result['error_message'])?0:$result['error_message']);
    $this->ids['contact_id'] = $result['id'];
 
    $result = civicrm_api('Membership','Create',array('version' => 3, 'start_date' => '2011-01-04','end_date' => '2012-01-04', 'contact_id' => $this->ids['contact_id'], 'membership_type_id' => variable_get('civicrm_egwcmemberevents_membership','')));
    $this->assertEqual('0',$result['is_error'],empty($result['error_message'])?0:$result['error_message'] . " check variable is set " . variable_get('civicrm_egwcmemberevents_membership',''));
    $this->ids['membership_id'] = $result['id'];
    /*
     * Use user 3 for permissions to test function (authenticated user in real DB) see :
     * http://drupal.org/simpletest-tutorial-drupal7
     * Users in test and the core environment

Tests run in a separate (sandbox) environment. Logging a user in with $this->drupalLogin($account) happens in the test environment. Test methods, like drupalGet and drupalPost also act in that sandbox. If you want to call core API functions (eg: user_access) it's highly recommended to assign the test user to the core environment:
<?php
$account = $this->drupalCreateUser(array('access content'));
$this->drupalLogin($account);
global $user;
$user = user_load($account->uid);
$this->assertFalse(user_access('access content'));
?>
     */
    
    $this->drupalLogin($this->authenticatedUser);
    global $user;
    $user = user_load(3);

    $result = civicrm_api('uf_match', 'get', array('check_permissions' => 1, 'version' => 3, 'uf_id' =>  $this->authenticatedUser->uid, 'sequential' => 1));
    $this->assertEqual('1',$result['is_error'],"Check permissions affect api call");
  
  }
  /**
   * tearDown() removes set-up entities 
   */
  public function tearDown() {
    $result =  civicrm_api('Event','Delete',array('version' => 3,'check_permissions' => 0, 'id' => $this->ids['event_id']  ));
    $this->assertEqual('0',$result['is_error'],"Event Deleted " . empty($result['error_message'])?$this->ids['event_id']:$result['error_message']);
    $result = civicrm_api('Contact','Delete',array('version' => 3,'check_permissions' => 0, 'id' => $this->ids['contact_id'] ) );
    $this->assertEqual('0',$result['is_error'],"Contact Deleted " . empty($result['error_message'])?$this->ids['contact_id']:$result['error_message']);
 
  }
    public function testSimpleTestEventsFuncs() { 
      $this->assertTrue(array_key_exists(8, _civicrm_egwcmemberevents_get_valid_event_types()));
    }
    
    public function testSimpleTestEventsFuncsTwo() { 
      $this->assertEqual(1,1);
    }
}

Comments

dave reid’s picture

First, do not run tests or assertions inside setUp or tearDown(). Make a testSomething() for that.

Second, your tearDown() function forgets to call parent::tearDown().

dave reid’s picture

Status: Active » Fixed
eileenmcnaughton’s picture

Hi,

Sorry, I didn't get notified of your reply - someone did say to me something about drupal.org changing the way it did it's notifications.

The missing tear down was exactly what I was missing.

The main reason I put the assertions in my set up & tear down was to help me get that part working. I see your point though.

Status: Fixed » Closed (fixed)

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