party.test in Party 7
Same filename and directory in other branches
Tests for the Party module.
File
tests/party.testView source
<?php
/**
* @file
* Tests for the Party module.
*/
/**
* Base class for all Party tests.
*/
class PartyBaseTestCase extends DrupalWebTestCase {
/**
* Extend the error handler to cope with E_USER_DEPRECATED if available.
*
* @see http://drupal.org/node/2101235
*/
public function errorHandler($severity, $message, $file = NULL, $line = NULL) {
if ($severity & error_reporting()) {
$error_map = array(
E_STRICT => 'Run-time notice',
E_WARNING => 'Warning',
E_NOTICE => 'Notice',
E_CORE_ERROR => 'Core error',
E_CORE_WARNING => 'Core warning',
E_USER_ERROR => 'User error',
E_USER_WARNING => 'User warning',
E_USER_NOTICE => 'User notice',
E_RECOVERABLE_ERROR => 'Recoverable error',
);
if (defined('E_DEPRECATED')) {
$error_map[E_DEPRECATED] = 'Run-time notice';
$error_map[E_USER_DEPRECATED] = 'Run-time notice';
}
$backtrace = debug_backtrace();
if (defined('E_USER_DEPRECATED') && $severity == E_USER_DEPRECATED) {
array_shift($backtrace);
}
$this
->error($message, $error_map[$severity], _drupal_get_last_caller($backtrace));
}
return TRUE;
}
/**
* Create a test profile2 type and return the profile2 type machine name.
*/
public function createTestProfile2() {
$label = 'Party Test Profile2';
$machine_name = 'party_test_profile2';
$profile_type = new ProfileType(array(
'type' => $machine_name,
'label' => $label,
'weight' => 0,
));
profile2_type_save($profile_type);
// Clear Profile2 cache.
profile2_load_multiple(FALSE, array(), TRUE);
// Clear the Data Set cache.
$this
->assertNotNull(party_get_data_set_info(NULL, TRUE), "Data Set has been created for profile2 type {$label}");
// Add a field to Party Test type.
$field = array(
'field_name' => 'party_test_field',
'type' => 'text',
'cardinality' => 1,
'translatable' => FALSE,
);
field_create_field($field);
$instance = array(
'entity_type' => 'profile2',
'field_name' => 'party_test_field',
'bundle' => $machine_name,
'label' => 'Party Test Field',
'description' => 'Party Test Field - Text type.',
'widget' => array(
'type' => 'text_textfield',
'weight' => 0,
),
);
field_create_instance($instance);
// Clear all caches
drupal_flush_all_caches();
// @todo: Check permission have been created for the new data set.
$this
->checkPermissions(array(
'attach party profile2_' . $machine_name,
), TRUE);
return $machine_name;
}
}
/**
* Test Core Party functionality
*/
class PartyTestCase extends PartyBaseTestCase {
protected $privileged_user;
public static function getInfo() {
return array(
'name' => 'Party',
'description' => 'Party CRUD test.',
'group' => 'Party',
);
}
public function setUp() {
parent::setUp('party');
// Enable any modules required for the test
$this->privileged_user = $this
->drupalCreateUser(array(
'administer crm settings',
'administer parties',
'create parties',
'view parties',
));
$this
->drupalLogin($this->privileged_user);
}
/**
* Test CRUD on the Party Entities
*/
public function testPartyCRUD() {
// Test Party Creation.
$this
->drupalGet('admin/community');
$this
->clickLink('Add party');
// Assert that the add party page exists and is accessible.
$this
->assertRaw(t('Add party'), 'Add Party page found');
$this
->drupalPost(null, array(), t('Save'));
// Test Reading Parties.
$this
->drupalGet('admin/community');
// Assert that the created party appears on the Party List page.
$this
->assertRaw('Party 1', 'Test Party found');
// Assert that there is a party with pid 1 in the database.
$this
->assertNotNull(db_query("SELECT pid FROM {party} WHERE pid = 1"), 'Party with pid = 1 found in db');
// Test Updating Parties.
// @TODO Need to add field to main profile and make sure it works with Party?
// @TODO Also change the Party label?
// Test Party Deletion.
$this
->drupalGet('admin/community');
$this
->clickLink('delete');
$this
->drupalPost(null, array(), t('Confirm'));
$this
->drupalGet('admin/community');
// Assert that Party no longer appears on the Party List page (admin/community).
$this
->assertNoRaw('Party 1', 'Test Party not found (delete worked)');
///////////////////////////////////////////////////////////////////////////
// Party CRUD Programmatically
///////////////////////////////////////////////////////////////////////////
// Test Party Creation.
$party = party_create();
$party
->save();
// Assert the Party Object was Created and Saved.
$this
->assertNotNull($party);
// Assert that a pid has been set.
$this
->assertNotNull($party->pid);
// Test Reading Parties.
$pid = $party->pid;
unset($party);
$party = party_load($pid);
// Assert that party_load has returned an object (and not FALSE).
$this
->assertTrue(is_object($party));
// Asset that correct party has been loaded.
$this
->assertEqual($pid, $party->pid);
// Test Updating Parties
// @TODO Need to do some sort of update.
// Test Party Deletion
party_delete($party);
// Assert that the Party can no longer be loaded.
$this
->assertFalse(party_load($pid));
// Make sure there is no party with the deleted party's pid in database.
$result = db_query("SELECT pid FROM {party} WHERE pid = :pid", array(
':pid' => $pid,
))
->fetchField();
// Assert that no Party was found with this pid.
$this
->assertFalse($result, 'Party with pid = ' . $pid . ' not found in db (delete worked)');
}
}
Classes
Name | Description |
---|---|
PartyBaseTestCase | Base class for all Party tests. |
PartyTestCase | Test Core Party functionality |