t('SpamSpan basic tests'), 'description' => t('Test SpamSpan email replacement functions.'), 'group' => t('SpamSpan'), ); } /** * Implementation of setUp(). */ function setUp() { parent::setUp('spamspan'); // set up required defaults variable_set('spamspan_userclass', 'u'); variable_set('spamspan_domainclass', 'd'); variable_set('spamspan_anchorclass', 't'); variable_set('spamspan_at', ' [at] '); variable_set('spamspan_use_graphic', 0); } /** * A utility function to check that emails within text are converted */ function _checkEmail($prefix = '', $suffix = '', $original = "", $shouldbe = '', $message = '') { $this->assertEqual($prefix . $shouldbe . $suffix, spamspan($prefix . $original . $suffix, ""), $message ); } /** * Test that strings without emails are passed through unchanged */ function testSpamSpanNoEmail() { $noemails = array( 'no email here', 'oneword', '', 'notan@email', 'notan@email either', 'some text and notan.email@something here', ); while ($text = each($noemails)) { $this->assertEqual(spamspan($text, ""), $text, "Checking for false positives"); } } /** * Check that emails in odd places are properly converted * * Each sub-test is run several times, using a different type of email * address each time. */ function testSpamSpanEmail() { // a list of address, together with what they should look like $emails = array('user@example.com' => 'user [at] example [dot] com', 'user@example.co.uk' => 'user [at] example [dot] co [dot] uk', 'user@example.museum' => 'user [at] example [dot] museum', 'user.user@example.com' => 'user [dot] user [at] example [dot] com', 'user\'user@example.com' => 'user\'user [at] example [dot] com', 'user-user@example.com' => 'user-user [at] example [dot] com', 'user_user@example.com' => 'user_user [at] example [dot] com', 'user+user@example.com' => 'user+user [at] example [dot] com', 'user!#$%*+-/=?^_`{|}~user@example.com' => #strange characters: #906474 'user!#$%*+-/=?^_`{|}~user [at] example [dot] com', ); foreach ($emails as $original => $shouldbe) { $this->_checkEmail("", "" , $original, $shouldbe, "Test for bare email"); } // Test for email with text at the start foreach ($emails as $original => $shouldbe) { $this->_checkEmail("some text here ", "", $original, $shouldbe, "Test for email with text at the start"); } // Test for email with text at the end foreach ($emails as $original => $shouldbe) { $this->_checkEmail("", " some text here", $original, $shouldbe, "Test for email with text at the end"); } // Test for email with text at the start and end foreach ($emails as $original => $shouldbe) { $this->_checkEmail("some text here ", " some text here", $original, $shouldbe, "Test for email with text at the start and end"); } // Test for email with tags at the start and end foreach ($emails as $original => $shouldbe) { $this->_checkEmail("
", "
", $original, $shouldbe, "Test for email with tags at the start and end"); } // Test for email with trailing commas foreach ($emails as $original => $shouldbe) { $this->_checkEmail("some text here. ", ", Next sentence", $original, $shouldbe, "Test for email with trailing commas"); } // Test for email with trailing full stop foreach ($emails as $original => $shouldbe) { $this->_checkEmail("some text here. ", ". Next sentence", $original, $shouldbe, "Test for email with trailing full stop"); } // Test for email with preceding tag foreach ($emails as $original => $shouldbe) { $this->_checkEmail("", "
", $original, $shouldbe, "Test for email with preceding tag"); } // Test for email with preceding tag, and no closing tag foreach ($emails as $original => $shouldbe) { $this->_checkEmail("