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("
", ". no tag here", $original, $shouldbe, "Test for email with preceding tag, and no closing tag"); } // Test for brackets foreach ($emails as $original => $shouldbe) { $this->_checkEmail("(", ")", $original, $shouldbe, "Test for brackets"); } // Test for angle brackets; foreach ($emails as $original => $shouldbe) { $this->_checkEmail("<", ">", $original, $shouldbe, "Test for angle brackets"); } // Test for newlines foreach ($emails as $original => $shouldbe) { $this->_checkEmail("\n", "\n", $original, $shouldbe, "Test for newlines"); } // Test for spaces foreach ($emails as $original => $shouldbe) { $this->_checkEmail(" ", " ", $original, $shouldbe, "Test for spaces"); } } } class SpamSpanMailtoTestCase extends DrupalWebTestCase { /** * Implementation of getInfo(). */ public static function getInfo() { return array( 'name' => t('SpamSpan mailto: tests'), 'description' => t('Test SpamSpan mailto: 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); } /** * Test for proper conversion of mailto: URLs */ function testSpamSpanMailto() { // plain $this->assertEqual(spamspan(""), 'email [at] example [dot] com', "Test plain mailto:"); // with anchor text $this->assertEqual(spamspan("some text"), 'email [at] example [dot] com (some text)', "Test mailto: with anchor tag"); // with an extra attribute $this->assertEqual(spamspan(""), 'email [at] example [dot] com', "test mailto: with extra attribute"); // with nested tags $this->assertEqual(spamspan("some text"), 'email [at] example [dot] com (some text)', "Test mailto: with nested tags"); // with an email address as the tag contents $this->assertEqual(spamspan("email@example.com"), 'email [at] example [dot] com', "Test mailto: with email as tag contents"); // with a URL parameter string $this->assertEqual(spamspan("some text"), 'email [at] example [dot] com (subject: subject) (some text)', "Test mailto: with a URL parameter string"); // with a URL parameter string - multiple headers $this->assertEqual(spamspan('some text'), 'email [at] example [dot] com (subject: test%20subject, body: some%20text) (some text)', "Test mailto: with a URL parameter string - multiple headers"); } }