Add assertUniqueText() and assertNoUniqueText(). From: Damien Tournoud --- simpletest/drupal_web_test_case.php | 72 +++++++++++++++++++++++++++++++++++ simpletest/tests/form.test | 29 ++++++++++++++ simpletest/tests/form_test.module | 29 ++++++++++++++ 3 files changed, 130 insertions(+), 0 deletions(-) diff --git modules/simpletest/drupal_web_test_case.php modules/simpletest/drupal_web_test_case.php index df6e148..9c5dd1a 100644 --- modules/simpletest/drupal_web_test_case.php +++ modules/simpletest/drupal_web_test_case.php @@ -1705,6 +1705,78 @@ class DrupalWebTestCase { } /** + * Pass if the text is found ONLY ONCE on the text version of the page. + * + * The text version is the equivalent of what a user would see when viewing + * through a web browser. In other words the HTML has been filtered out of + * the contents. + * + * @param $text + * Plain text to look for. + * @param $message + * Message to display. + * @param $group + * The group this message belongs to, defaults to 'Other'. + * @return + * TRUE on pass, FALSE on fail. + */ + protected function assertUniqueText($text, $message = '', $group = 'Other') { + return $this->assertUniqueTextHelper($text, $message, $group, TRUE); + } + + /** + * Pass if the text is found MORE THAN ONCE on the text version of the page. + * + * The text version is the equivalent of what a user would see when viewing + * through a web browser. In other words the HTML has been filtered out of + * the contents. + * + * @param $text + * Plain text to look for. + * @param $message + * Message to display. + * @param $group + * The group this message belongs to, defaults to 'Other'. + * @return + * TRUE on pass, FALSE on fail. + */ + protected function assertNoUniqueText($text, $message = '', $group = 'Other') { + return $this->assertUniqueTextHelper($text, $message, $group, FALSE); + } + + /** + * Helper for assertUniqueText and assertNoUniqueText. + * + * It is not recommended to call this function directly. + * + * @param $text + * Plain text to look for. + * @param $message + * Message to display. + * @param $group + * The group this message belongs to. + * @param $be_unique + * TRUE if this text should be found only once, FALSE if it should be found more than once. + * @return + * TRUE on pass, FALSE on fail. + */ + protected function assertUniqueTextHelper($text, $message, $group, $be_unique) { + if ($this->plainTextContent === FALSE) { + $this->plainTextContent = filter_xss($this->content, array()); + } + if (!$message) { + $message = '"' . $text . '"'. ($be_unique ? ' found only once' : ' found more than once'); + } + $first_occurance = strpos($this->plainTextContent, $text); + if ($first_occurance === FALSE) { + return $this->assert(FALSE, $message, $group); + } + $offset = $first_occurance + strlen($text); + $second_occurance = strpos($this->plainTextContent, $text, $offset); + return $this->assert($be_unique == ($second_occurance === FALSE), $message, $group); + } + + /** * Will trigger a pass if the Perl regex pattern is found in the raw content. * * @param $pattern diff --git modules/simpletest/tests/form.test modules/simpletest/tests/form.test index 2eef641..9544fd7 100644 --- modules/simpletest/tests/form.test +++ modules/simpletest/tests/form.test @@ -315,3 +315,32 @@ class FormsElementsTableSelectFunctionalTest extends DrupalWebTestCase { } +/** + * Test the form_clean_id() for expected behavior. + */ +class FormsFormCleanIdFunctionalTest extends DrupalWebTestCase { + + function getInfo() { + return array( + 'name' => t('form_clean_id() test'), + 'description' => t('Test the function form_clean_id() for expected behavior'), + 'group' => t('Form API'), + ); + } + + function setUp() { + parent::setUp('form_test'); + } + + /** + * Test the uniqueness of the form_clean_id() function. + */ + function testFormCleanId() { + $this->drupalGet('form_test/form_clean_id'); + $this->assertNoUniqueText('form-test-form-clean-id-presence'); + $this->assertUniqueText('form-test-form-clean-id-presence-1'); + $this->assertUniqueText('form-test-form-clean-id-presence-2'); + $this->assertNoUniqueText('Test Textfield'); + } + +} diff --git modules/simpletest/tests/form_test.module modules/simpletest/tests/form_test.module index a7c31f0..c913150 100644 --- modules/simpletest/tests/form_test.module +++ modules/simpletest/tests/form_test.module @@ -44,10 +44,39 @@ function form_test_menu() { 'type' => MENU_CALLBACK, ); + $items['form_test/form_clean_id'] = array( + 'title' => 'form_clean_id test', + 'page callback' => 'form_test_form_clean_id_page', + 'access arguments' => array('access content'), + 'type' => MENU_CALLBACK, + ); + return $items; } /** + * Generate a page with three form, to test the clean_id generation. + */ +function form_test_form_clean_id_page() { + $output = drupal_get_form('form_test_test_form'); + $output .= drupal_get_form('form_test_test_form'); + $output .= drupal_get_form('form_test_test_form'); + return $output; +} + +/** + * A simple form to test clean_id generation. + */ +function form_test_test_form(&$form_state) { + $form['input'] = array( + '#type' => 'item', + '#title' => 'Test Textfield', + '#markup' => form_clean_id('form_test_form_clean_id_presence'), + ); + return $form; +} + +/** * Create a header and options array. Helper function for callbacks. */ function _form_test_tableselect_get_data() {