? .svn ? simpletest ? tests/.svn Index: drupal_test_case.php =================================================================== RCS file: /cvs/drupal/contributions/modules/simpletest/drupal_test_case.php,v retrieving revision 1.28.2.3 diff -u -p -r1.28.2.3 drupal_test_case.php --- drupal_test_case.php 17 Sep 2007 16:27:54 -0000 1.28.2.3 +++ drupal_test_case.php 20 Jan 2008 23:22:43 -0000 @@ -13,7 +13,7 @@ class DrupalTestCase extends WebTestCase var $_cleanupVariables = array(); var $_cleanupUsers = array(); var $_cleanupRoles = array(); - + var $_cleanupTerms = array(); function DrupalTestCase($label = NULL) { if (! $label) { @@ -24,13 +24,13 @@ class DrupalTestCase extends WebTestCase } $this->WebTestCase($label); } - + /** - * @abstract Checks to see if we need to send + * @abstract Checks to see if we need to send * a http-auth header to authenticate * when browsing a site. * - * @param status Boolean pass true if you want to know if we are using + * @param status Boolean pass true if you want to know if we are using * HTTP-AUTH * @return void */ @@ -47,47 +47,49 @@ class DrupalTestCase extends WebTestCase /** * @abstract Brokder for the get function - * addes the authetnication headers if + * addes the authetnication headers if * neccessary * @author Earnest Berry III * * @param url string Url to retch * @return void */ - function drupalGet($url) { + function drupalGet($path, $query = NULL, $fragment = NULL) { + $url = url($path, $query, $fragment, TRUE); $html = $this->_browser->get($url); - + if( $this->drupalCheckAuth(true) ) { $html .= $this->drupalCheckAuth(); } - + $this->_content = $this->_browser->getContent(); - + return $html; } /** * @abstract Brokder for the post function - * addes the authetnication headers if + * addes the authetnication headers if * neccessary * @author Earnest Berry III * * @param url string Url to retch * @return void */ - function drupalRawPost($action, $edit = array()) { - $html = $this->_browser->post($action, $edit); - + function drupalRawPost($path, $edit = array(), $query = NULL, $fragment = NULL) { + $url = url($path, $query, $fragment, TRUE); + $html = $this->_browser->post($url, $edit); + if( $this->drupalCheckAuth(true) ) { $html .= $this->drupalCheckAuth(); } - + $this->_content = $this->_browser->getContent(); - + return $html; } - + /** * Do a post request on a drupal page. @@ -100,8 +102,8 @@ class DrupalTestCase extends WebTestCase * @param string $submit name of the submit button, untranslated * @param boolean $reporting assertations or not */ - function drupalPostRequest($path, $edit = array(), $submit, $edit_multi = array()) { - $url = url($path, NULL, NULL, TRUE); + function drupalPostRequest($path, $edit = array(), $submit, $edit_multi = array(), $query = NULL, $fragment = NULL) { + $url = url($path, $query, $fragment, TRUE); $ret = $this->drupalGet($url); $this->assertTrue($ret, " [browser] GET $url"); @@ -119,7 +121,7 @@ class DrupalTestCase extends WebTestCase $this->assertTrue($ret, " [browser] Setting multi-field $field_name=\"(" . implode(',', $field_values) . ")\""); } } - + $ret = $this->_browser->clickSubmit(t($submit)) || $this->_browser->clickSubmitByName($submit) || $this->_browser->clickImageByName($submit); // $ret = $this->_browser->clickSubmitByName('op'); $this->assertTrue($ret, ' [browser] POST by click on ' . t($submit)); @@ -170,8 +172,7 @@ class DrupalTestCase extends WebTestCase * @param integer $number number of characters * @return ransom string */ - function randomName($number = 4, $prefix = 'simpletest_') { - $chars = 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ_'; + function randomName($number = 4, $prefix = 'simpletest_', $chars = 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ_') { for ($x = 0; $x < $number; $x++) { $prefix .= $chars{mt_rand(0, strlen($chars)-1)}; if ($x == 0) { @@ -182,12 +183,27 @@ class DrupalTestCase extends WebTestCase } /** + * Generate a random email address. + * + * @param $number + * Number of characters in first part of email address. + * @param $prefix + * Prefix for first part of email address. + * + * @return + * A random email address @exmample.com. + */ + function randomEmail($number = 4, $prefix = 'simpletest_') { + return $this->randomName($number, $prefix, 'abcdefghijklmnopqrstuvwxyz') .'@example.com'; + } + + /** * Enables a drupal module * @param string $name name of the module * @return boolean success */ function drupalModuleEnable($name) { - if (module_exists($name)) { + if (module_exists($name)) { $this->pass(" [module] $name already enabled"); return TRUE; } @@ -206,7 +222,7 @@ class DrupalTestCase extends WebTestCase } $this->pass(" [module] $name enabled"); return TRUE; - } + } else { $this->fail(" [module] $name could not be enbled, probably file not exists"); return FALSE; @@ -323,6 +339,45 @@ class DrupalTestCase extends WebTestCase } /** + * Creates a user assigned to a role with the given name. + * + * @param $role_name + * The name of the desired role. + * + * @return + * FALSE if it fails or a fully loaded user object with added pass_raw property. + */ + function drupalCreateUser($role_name) { + // Find role ID. + $rid = db_result(db_query("SELECT rid FROM {role} WHERE name = '%s'", $role_name)); + if ($rid === FALSE) { + return FALSE; + } + + // Create user. + $ua = array( + 'name' => $this->randomName(), + 'mail' => $ua['name'] .'@example.com', + 'roles' => array($rid => $rid), + 'pass' => user_password(), + 'status' => 1, + ); + $user = user_save('', $ua); + + $this->assertTrue(!empty($user->uid), " [user] name: $ua[name] pass: $ua[pass] created"); + if (empty($user->uid)) { + return FALSE; + } + + /* Add to cleanup list */ + $this->_cleanupUsers[] = $user->uid; + + /* Add the raw password */ + $user->pass_raw = $ua['pass']; + return $user; + } + + /** * Logs in a user with the internal browser * * @param object user object with pass_raw property! @@ -336,7 +391,7 @@ class DrupalTestCase extends WebTestCase if ($user === NULL) { $user = $this->drupalCreateUserRolePerm(); } - + $edit = array('name' => $user->name, 'pass' => $user->pass_raw); $this->drupalPostRequest('user', $edit, $submit); @@ -348,6 +403,14 @@ class DrupalTestCase extends WebTestCase } /** + * Logs a user out with the internal browser. + */ + function drupalLogoutUser() { + $this->drupalGet(url('logout', NULL, NULL, TRUE) ); + $this->assertResponse(200); + } + + /** * tearDown implementation, setting back switched modules etc */ function tearDown() { @@ -383,6 +446,13 @@ class DrupalTestCase extends WebTestCase } user_delete(array(), $uid); } + + // Cleanup taxonomy terms created during testing. + while (sizeof($this->_cleanupTerms) > 0) { + $tid = array_pop($this->_cleanupTerms); + taxonomy_del_term($tid); + } + parent::tearDown(); } @@ -400,7 +470,7 @@ class DrupalTestCase extends WebTestCase array_pop($reporter->test_info_stack); } - + /** * Will trigger a pass if the raw text is found on the loaded page * Fail otherwise. @@ -416,7 +486,7 @@ class DrupalTestCase extends WebTestCase $message); } - + /** * Will trigger a pass if the raw text is NOT found on the loaded page * Fail otherwise. Index: drupal_unit_tests.php =================================================================== RCS file: /cvs/drupal/contributions/modules/simpletest/drupal_unit_tests.php,v retrieving revision 1.5.2.2 diff -u -p -r1.5.2.2 drupal_unit_tests.php --- drupal_unit_tests.php 13 Aug 2007 18:35:54 -0000 1.5.2.2 +++ drupal_unit_tests.php 20 Jan 2008 23:22:43 -0000 @@ -8,7 +8,7 @@ class DrupalTestSuite extends TestSuite function DrupalTestSuite($label) { $this->TestSuite($label); } - + /** * @return array of instanciated tests that this GroupTests holds */ @@ -19,8 +19,8 @@ class DrupalTestSuite extends TestSuite $this->_test_cases[$i] = &new $class(); } } - return $this->_test_cases; - } + return $this->_test_cases; + } } class DrupalUnitTests extends DrupalTestSuite { @@ -32,11 +32,11 @@ class DrupalUnitTests extends DrupalTest function DrupalUnitTests($class_list = NULL) { static $classes; $this->DrupalTestSuite('Drupal Unit Tests'); - - /* Tricky part to avoid double inclusion */ - if (!$classes) { + + // Tricky part to avoid double inclusion. + if (!isset($classes)) { $files = module_invoke_all('simpletest'); - + $existing_classes = get_declared_classes(); foreach ($files as $file) { include_once($file); @@ -44,42 +44,34 @@ class DrupalUnitTests extends DrupalTest $classes = array_diff(get_declared_classes(), $existing_classes); } if (!is_null($class_list)) { - $classes = $class_list; - } + $classes = $class_list; + } if (count($classes) == 0) { $this->addTestCase(new BadGroupTest($test_file, 'No new test cases')); return; } + $groups = array(); foreach ($classes as $class) { - $this->_addClassToGroups($groups, $class); + // Make sure this class implements get_info(), hence a test. + $test = &new $class(); + if (method_exists($test, 'get_info')) { + $info = $test->get_info(); + $groups[$info['group']][] = $test; + } } + + // Now iterate through groups and add test cases to SimpleTest. foreach ($groups as $group_name => $group) { $group_test = &new DrupalTestSuite($group_name); foreach ($group as $key => $v) { - $group_test->addTestCase($group[$key]); + $group_test->addTestCase($group[$key]); } $this->addTestCase($group_test); } } /** - * Adds a class to a groups array specified by the get_info of the group - * @param array $groups Group of categorizesd tests - * @param string $class Name of a class - */ - function _addClassToGroups(&$groups, $class) { - $test = &new $class(); - if (method_exists($test, 'get_info')) { - $info = $test->get_info(); - $groups[$info['group']][] = $test; - } - else { - $groups[$class][] = $test; - } - } - - /** * Invokes run() on all of the held test cases, instantiating * them if necessary. * The Druapl version uses paintHeader instead of paintGroupStart @@ -92,10 +84,10 @@ class DrupalUnitTests extends DrupalTest cache_clear_all(); @set_time_limit(0); ignore_user_abort(true); - + // Disable known problematic modules $this->drupalModuleDisable('devel'); - + parent::run($reporter); // Restores modules @@ -105,7 +97,7 @@ class DrupalUnitTests extends DrupalTest $this->_cleanupModules = array(); } - + /** * Enables a drupal module * @param string $name name of the module Index: simpletest.module =================================================================== RCS file: /cvs/drupal/contributions/modules/simpletest/simpletest.module,v retrieving revision 1.22.2.4 diff -u -p -r1.22.2.4 simpletest.module --- simpletest.module 16 Aug 2007 20:23:26 -0000 1.22.2.4 +++ simpletest.module 20 Jan 2008 23:22:43 -0000 @@ -23,29 +23,69 @@ function simpletest_help($section) { * Implementation of hook_menu(). */ function simpletest_menu($may_cache) { + $items = array(); if ($may_cache) { - $items = array(); - $items[] = array('path' => 'admin/build/simpletest', - 'title' => 'Simpletest unit testing', - 'callback' => 'simpletest_entrypoint', - 'description' => t('Run tests against Drupal core and your active modules. These tests help assure that your site code is working as designed.'), - 'access' => user_access('administer unit tests')); - $items[] = array('path' => 'admin/settings/simpletest', - 'title' => t('Simpletest settings'), + $items[] = array( + 'path' => 'admin/build/simpletest', + 'title' => 'SimpleTest unit testing', + 'callback' => 'simpletest_entrypoint', + 'description' => t('Run tests against Drupal core and your active modules. These tests help assure that your site code is working as designed.'), + 'access' => user_access('administer unit tests'), + ); + $items[] = array( + 'path' => 'admin/settings/simpletest', + 'title' => t('SimpleTest settings'), 'description' => t('Configure unit testing framework.'), 'callback' => 'drupal_get_form', - 'callback arguments' => 'simpletest_settings', - 'access' => user_access('administer unit tests') + 'callback arguments' => array('simpletest_settings'), + 'access' => user_access('administer unit tests'), + ); + + $items[] = array( + 'path' => 'admin/simpletest', + 'title' => $test_info['name'], + 'callback' => 'simpletest_entrypoint', + 'description' => $test_info['desc'], + 'access' => user_access('administer unit tests'), + ); + + $items[] = array( + 'path' => 'admin/simpletest', + 'title' => t('Unit testing'), + 'description' => t('Run SimpleTest unit tests that are defined for your site.'), + 'position' => 'left', + 'callback' => 'system_admin_menu_block_page', + 'access' => user_access('administer unit tests'), ); - return $items; + + // Get all tests so we can build menu items to run them. + $all_tests =& simpletest_get_total_test(); + $test_instances = $all_tests->getTestInstances(); + foreach ($test_instances as $test_instance) { + $tests = $test_instance->getTestInstances(); + foreach ($tests as $test) { + $test_info = $test->get_info(); + $test_name = get_class($test); + $items[] = array( + 'path' => 'admin/simpletest/'. $test_name, + 'title' => $test_info['name'], + 'callback' => 'simpletest_page_run_test', + 'callback arguments' => array($test_name), + 'description' => $test_info['desc'], + 'access' => user_access('administer unit tests'), + ); + } + } } + + return $items; } /** * Implementation of hook_perm(). */ function simpletest_perm() { - return array('administer unit tests'); + return array('administer unit tests'); } /** @@ -55,42 +95,38 @@ function simpletest_perm() { function simpletest_load() { static $loaded; if ($loaded) { - return true; + return true; } - global $user; - if ($user->uid != 1) { - drupal_set_message(t('We strongly suggest running the simpletests with uid=1!')); - } - + $loaded = true; if (!defined('SIMPLE_TEST')) { - define('SIMPLE_TEST', drupal_get_path('module', 'simpletest'). '/simpletest'); - } + define('SIMPLE_TEST', drupal_get_path('module', 'simpletest'). '/simpletest'); + } if (!is_dir(SIMPLE_TEST)){ - $output = t('Sorry but the simpletest cannot be found in the current installation. Please notice that simpletest.module needs Simpletest framework. ' + $output = t('Sorry but the SimpleTest cannot be found in the current installation. Please notice that simpletest.module needs SimpleTest framework. ' . 'Please download it from !simpletest_link and place it into the same directory as simpletest.module: %simpletest_directory', - array('!simpletest_link' => l('Simpletest on SourceForge', 'https://sourceforge.net/project/showfiles.php?group_id=76550'), + array('!simpletest_link' => l('SimpleTest on SourceForge', 'https://sourceforge.net/project/showfiles.php?group_id=76550'), '%simpletest_directory' => SIMPLE_TEST)); drupal_set_message($output, 'error'); return false; } - + /* We currently use only the web tester that DrupalTestCase is built upon */ require_once(SIMPLE_TEST . '/web_tester.php'); require_once(SIMPLE_TEST . '/reporter.php'); require_once('drupal_reporter.php'); - + if (version_compare(SimpleTest::getVersion() , '1.0.1beta2') < 0) { - $output = t('Due to a lot of refactoring done on simpletest library side. Simpletest module is not compatible with simpeltest versions lower thab 1.0.1beta2. ' + $output = t('Due to a lot of refactoring done on SimpleTest library side. SimpleTest module is not compatible with simpeltest versions lower thab 1.0.1beta2. ' . 'Please download the latest version from !simpletest_link and place it into the same directory as simpletest.module: %simpletest_directory', - array('!simpletest_link' => l('Simpletest on SourceForge', 'https://sourceforge.net/project/showfiles.php?group_id=76550'), + array('!simpletest_link' => l('SimpleTest on SourceForge', 'https://sourceforge.net/project/showfiles.php?group_id=76550'), '%simpletest_directory' => SIMPLE_TEST)); drupal_set_message($output, 'error'); return false; } - - + + $path = drupal_get_path('module', 'simpletest'). '/'; require_once($path . 'drupal_test_case.php'); require_once($path . 'drupal_unit_tests.php'); @@ -107,11 +143,11 @@ function simpletest_entrypoint() { * @todo find a better way for this return, * currently needed to show error, true leads to page not found */ - return ' '; - } + return ' '; + } drupal_add_js(drupal_get_path('module', 'simpletest') . '/simpletest.js', 'module'); $output = drupal_get_form('simpletest_overview_form'); - + print theme('page', simpletest_running_output() . $output); } @@ -126,49 +162,49 @@ function simpletest_running_output($outp /** * FAPI form submit for simpletest_overview_form * - * @param $form_id + * @param $form_id * @param $form_values */ function simpletest_overview_form_submit($form_id, $form_values) { - + switch ($form_values['running_options']){ - + case 'all_tests': $output = simpletest_run_tests(); - break; - + break; + case 'selected_tests': $test_list = array(); foreach ($form_values as $item => $value){ - if ($value == 1 && strpos($item, 'selectall') === FALSE) { + if ($value == 1 && !preg_match('/(selectall|form_token)/',$item)) { $tests_list[] = $item; } } if (count($tests_list) > 0 ) { - $output = simpletest_run_tests($tests_list); - } - else { - // no test has been selected - drupal_set_message(t('No test has been selected.'), 'error'); - } - break; - + $output = simpletest_run_tests($tests_list); + } + else { + // no test has been selected + drupal_set_message(t('No test has been selected.'), 'error'); + } + break; + default: drupal_set_message(t('No test has been selected.'), 'error'); } - + simpletest_running_output($output); return FALSE; } /** - * Create simpletest_overview_form + * Create simpletest_overview_form */ function simpletest_overview_form() { $output = array(); - + $total_test = &simpletest_get_total_test(); - + $test_instances = $total_test->getTestInstances(); foreach ($test_instances as $group_test) { @@ -180,22 +216,22 @@ function simpletest_overview_form() { '#collapsible' => TRUE, '#collapsed' => TRUE, '#title' => 'Tests', - '#attributes' => array('class' => $group_class), + '#attributes' => array('class' => $group_class), ); foreach($tests as $test) { $test_info = $test->get_info(); $desc = $test_info['desc']; $group['tests'][get_class($test)] = array('#type' => 'checkbox', '#title' => $test_info['name'], '#default_value' => 0, '#description' => $desc); - + } $output[] = array_merge($group, array('#type' => 'fieldset', '#collapsible' => FALSE, '#collapsed' => FALSE, '#title' => $group_test->getLabel(), '#attributes' => array('class' => 'select_all'))); } - - $submit['running_options'] = array('#type' => 'radios', '#default_value' => 'selected_tests', + + $submit['running_options'] = array('#type' => 'radios', '#default_value' => 'selected_tests', '#options' => array('all_tests' => t('Run all tests (WARNING, this may take a long time)'), 'selected_tests' => t('Run selected tests'))); $submit['op'] = array('#type' => 'submit', '#value' => t('Begin')); $output[] = array_merge($submit, @@ -203,6 +239,21 @@ function simpletest_overview_form() { return $output; } +/** + * Menu callback; run a specific test and report the result. + */ +function simpletest_page_run_test($test_name) { + global $base_url; + + // If we have a different base_url to use for SimpleTest, + // redirect before running tests. + $simpletest_base_url = variable_get('simpletest_base_url', $base_url); + if ($base_url != $simpletest_base_url) { + drupal_goto($simpletest_base_url .'/'. $_GET['q']); + } + + return simpletest_run_tests(array($test_name)); +} /** * Actually runs tests @@ -231,8 +282,8 @@ function simpletest_run_tests($testlist * Implementation of hook_simpletest(). */ function simpletest_simpletest() { - $dir = drupal_get_path('module', 'simpletest'). '/tests'; - $tests = file_scan_directory($dir, '\.test$'); + $dir = drupal_get_path('module', 'simpletest'). '/tests'; + $tests = file_scan_directory($dir, '\.test$'); return array_keys($tests); } @@ -246,7 +297,7 @@ function &simpletest_get_total_test($cla static $total_test; if (!$total_test) { if (!simpletest_load()) { - return FALSE; + return FALSE; } $total_test = &new DrupalUnitTests(); } @@ -280,7 +331,7 @@ function simpletest_settings() { '#type' => 'password', '#default_value' => variable_get('simpletest_httpauth_pass', '') ); - + return system_settings_form($form); }