diff options
author | cash <cash@36083f99-b078-4883-b0ff-0f9b5a30f544> | 2011-02-12 22:41:25 +0000 |
---|---|---|
committer | cash <cash@36083f99-b078-4883-b0ff-0f9b5a30f544> | 2011-02-12 22:41:25 +0000 |
commit | da90ff55725a9118ce6111ab2b6371650bf78ade (patch) | |
tree | 937da734d50c8147ca8f2cc52791dabf430464c6 /mod/thewire/tests/regex.php | |
parent | cc6b7d1d223241e397e0d41354924e74606eeffc (diff) | |
download | elgg-da90ff55725a9118ce6111ab2b6371650bf78ade.tar.gz elgg-da90ff55725a9118ce6111ab2b6371650bf78ade.tar.bz2 |
a mostly completed port of JHU/APL wire plugin to 1.8
git-svn-id: http://code.elgg.org/elgg/trunk@8183 36083f99-b078-4883-b0ff-0f9b5a30f544
Diffstat (limited to 'mod/thewire/tests/regex.php')
-rw-r--r-- | mod/thewire/tests/regex.php | 271 |
1 files changed, 271 insertions, 0 deletions
diff --git a/mod/thewire/tests/regex.php b/mod/thewire/tests/regex.php new file mode 100644 index 000000000..9e187cf29 --- /dev/null +++ b/mod/thewire/tests/regex.php @@ -0,0 +1,271 @@ +<?php +/** + * Regular expression tests for the wire + */ +class TheWireRegexTest extends ElggCoreUnitTest { + + /** + * Called before each test object. + */ + public function __construct() { + $this->ia = elgg_set_ignore_access(TRUE); + parent::__construct(); + + // all __construct() code should come after here + } + + /** + * Called before each test method. + */ + public function setUp() { + + } + + /** + * Called after each test method. + */ + public function tearDown() { + // do not allow SimpleTest to interpret Elgg notices as exceptions + $this->swallowErrors(); + } + + /** + * Called after each test object. + */ + public function __destruct() { + elgg_set_ignore_access($this->ia); + // all __destruct() code should go above here + parent::__destruct(); + } + + protected function getUserWireLink($username) { + global $CONFIG; + return "<a href=\"{$CONFIG->wwwroot}pg/thewire/owner/$username\">@$username</a>"; + } + + protected function getHashtagLink($tag) { + global $CONFIG; + return "<a href=\"{$CONFIG->wwwroot}pg/thewire/tag/$tag\">#$tag</a>"; + } + + protected function getEmailLink($address) { + return "<a href=\"mailto:$address\">$address</a>"; + } + + protected function getLink($address) { + return parse_urls($address); + } + + /** + * Usernames: @user + */ + public function testReplaceUsernames() { + // beginning of text + $text = "@user test"; + $expected = $this->getUserWireLink('user') . " test"; + $result = thewire_filter($text); + $this->assertEqual($result, $expected); + + // after space + $text = "test @user test"; + $expected = "test " . $this->getUserWireLink('user') . " test"; + $result = thewire_filter($text); + $this->assertEqual($result, $expected); + + // followed by comma + $text = "test @user, test"; + $expected = "test " . $this->getUserWireLink('user') . ", test"; + $result = thewire_filter($text); + $this->assertEqual($result, $expected); + + // preceded by comma + $text = "test ,@user test"; + $expected = "test ," . $this->getUserWireLink('user') . " test"; + $result = thewire_filter($text); + $this->assertEqual($result, $expected); + + // include digit + $text = "@3user test"; + $expected = $this->getUserWireLink('3user') . " test"; + $result = thewire_filter($text); + $this->assertEqual($result, $expected); + + // include underscore + $text = "@user_name test"; + $expected = $this->getUserWireLink('user_name') . " test"; + $result = thewire_filter($text); + $this->assertEqual($result, $expected); + + // parentheses + $text = "test (@user) test"; + $expected = "test (" . $this->getUserWireLink('user') . ") test"; + $result = thewire_filter($text); + $this->assertEqual($result, $expected); + } + + /** + * Hashtags: #tag + */ + public function testReplaceHashtags() { + // tag at beginning + $text = "#tag test"; + $expected = $this->getHashtagLink('tag') . " test"; + $result = thewire_filter($text); + $this->assertEqual($result, $expected); + + // tag not at beginning + $text = "test #tag test"; + $expected = "test " . $this->getHashtagLink('tag') . " test"; + $result = thewire_filter($text); + $this->assertEqual($result, $expected); + + // followed by comma + $text = "test #tag, test"; + $expected = "test " . $this->getHashtagLink('tag') . ", test"; + $result = thewire_filter($text); + $this->assertEqual($result, $expected); + + // preceded by comma + $text = "test,#tag test"; + $expected = "test," . $this->getHashtagLink('tag') . " test"; + $result = thewire_filter($text); + $this->assertEqual($result, $expected); + + // followed by period + $text = "test #tag. test"; + $expected = "test " . $this->getHashtagLink('tag') . ". test"; + $result = thewire_filter($text); + $this->assertEqual($result, $expected); + + // parentheses + $text = "test (#tag) test"; + $expected = "test (" . $this->getHashtagLink('tag') . ") test"; + $result = thewire_filter($text); + $this->assertEqual($result, $expected); + + // include number + $text = "test #tag2000 test"; + $expected = "test " . $this->getHashtagLink('tag2000') . " test"; + $result = thewire_filter($text); + $this->assertEqual($result, $expected); + + // cannot be just a number + $text = "test #1 test"; + $expected = $text; + $result = thewire_filter($text); + $this->assertEqual($result, $expected); +} + + /** + * Email: johndoe@gmail.com + */ + public function testReplaceEmailAddress() { + // email at beginning of text + $text = "test@test.com test"; + $expected = $this->getEmailLink('test@test.com') . " test"; + $result = thewire_filter($text); + $this->assertEqual($result, $expected); + + // after space + $text = "test test@test.com test"; + $expected = "test " . $this->getEmailLink('test@test.com') . " test"; + $result = thewire_filter($text); + $this->assertEqual($result, $expected); + + // followed by comma + $text = "test test@test.com, test"; + $expected = "test " . $this->getEmailLink('test@test.com') . ", test"; + $result = thewire_filter($text); + $this->assertEqual($result, $expected); + + // preceded by comma + $text = "test,test@test.com test"; + $expected = "test," . $this->getEmailLink('test@test.com') . " test"; + $result = thewire_filter($text); + $this->assertEqual($result, $expected); + + // followed by period + $text = "test test@test.com. test"; + $expected = "test " . $this->getEmailLink('test@test.com') . ". test"; + $result = thewire_filter($text); + $this->assertEqual($result, $expected); + + // parentheses + $text = "test (test@test.com) test"; + $expected = "test (" . $this->getEmailLink('test@test.com') . ") test"; + $result = thewire_filter($text); + $this->assertEqual($result, $expected); + + // includes digits + $text = "user1@domain1.com"; + $expected = $this->getEmailLink('user1@domain1.com'); + $result = thewire_filter($text); + $this->assertEqual($result, $expected); + + // includes underscore + $text = "user_name@domain.com"; + $expected = $this->getEmailLink('user_name@domain.com'); + $result = thewire_filter($text); + $this->assertEqual($result, $expected); + + // includes period + $text = "user.name@domain.com"; + $expected = $this->getEmailLink('user.name@domain.com'); + $result = thewire_filter($text); + $this->assertEqual($result, $expected); + + // includes subdomains + $text = "user.name@domain.com.uk"; + $expected = $this->getEmailLink('user.name@domain.com.uk'); + $result = thewire_filter($text); + $this->assertEqual($result, $expected); + } + + /** + * Links: http://www.example.org/ + */ + public function testReplaceLinks() { + // beginning of text + $text = "http://www.test.org"; + $expected = $this->getLink('http://www.test.org'); + $result = thewire_filter($text); + $this->assertEqual($result, $expected); + + // not at beginning of text + $text = "test http://www.test.org"; + $expected = 'test ' . $this->getLink('http://www.test.org'); + $result = thewire_filter($text); + $this->assertEqual($result, $expected); + + // followed by comma + $text = "test http://www.test.org, test"; + $expected = 'test ' . $this->getLink('http://www.test.org') . ', test'; + $result = thewire_filter($text); + $this->assertEqual($result, $expected); + + // preceeded by comma + $text = "test,http://www.test.org test"; + $expected = 'test,' . $this->getLink('http://www.test.org') . ' test'; + $result = thewire_filter($text); + $this->assertEqual($result, $expected); + + // followed by period + $text = "test http://www.test.org. test"; + $expected = 'test ' . $this->getLink('http://www.test.org') . '. test'; + $result = thewire_filter($text); + $this->assertEqual($result, $expected); + + // surrounded by parentheses + $text = "test (http://www.test.org) test"; + $expected = 'test (' . $this->getLink('http://www.test.org') . ') test'; + $result = thewire_filter($text); + $this->assertEqual($result, $expected); + + // no http:// + $text = "test www.test.org test"; + $expected = 'test ' . $this->getLink('www.test.org') . ' test'; + $result = thewire_filter($text); + $this->assertEqual($result, $expected); + } + +} |