浏览代码

Merge pull request #126 from Adnan0703/master

testcases
Mark S. 11 年之前
父节点
当前提交
365c7248e4
共有 2 个文件被更改,包括 56 次插入1 次删除
  1. 2 1
      Controller/Component/CommonComponent.php
  2. 54 0
      Test/Case/Controller/Component/CommonComponentTest.php

+ 2 - 1
Controller/Component/CommonComponent.php

@@ -6,6 +6,7 @@ if (!defined('CLASS_USER')) {
 App::uses('Component', 'Controller');
 App::uses('Sanitize', 'Utility');
 App::uses('Utility', 'Tools.Utility');
+App::uses('Hash', 'Utility');
 
 /**
  * A component included in every app to take care of common stuff.
@@ -896,7 +897,7 @@ class CommonComponent extends Component {
 	 */
 	public function extractEmailInfo($email, $type = null) {
 		//$checkpos = strrpos($email, '@');
-		$nameParts = explode('@', $email);
+		$nameParts = Hash::filter(explode('@', $email));
 		if (count($nameParts) !== 2) {
 			return false;
 		}

+ 54 - 0
Test/Case/Controller/Component/CommonComponentTest.php

@@ -255,7 +255,61 @@ class CommonComponentTest extends CakeTestCase {
 		$this->assertSame($array, $this->Controller->request->data);
 		$this->assertSame($array, $this->Controller->request->query);
 	}
+	
+	/**
+	* CommonComponentTest::testExtractEmailInfo()
+	*
+	*/
+	public function testExtractEmailInfoWithValidEmail() {
+		$email = 'xyz@e.abc.de';
+		$result = $this->Controller->Common->extractEmailInfo($email, 'username');
+		$expected = 'xyz';
+		$this->assertEquals($expected, $result);
+		
+		$result = $this->Controller->Common->extractEmailInfo($email, 'hostname');
+		$expected = 'e.abc.de';
+		$this->assertEquals($expected, $result);
+		
+		$result = $this->Controller->Common->extractEmailInfo($email, 'tld');
+		$expected = 'de';
+		$this->assertEquals($expected, $result);
+		
+		$result = $this->Controller->Common->extractEmailInfo($email, 'domain');
+		$expected = 'abc';
+		$this->assertEquals($expected, $result);
+		
+		$result = $this->Controller->Common->extractEmailInfo($email, 'subdomain');
+		$expected = 'e';
+		$this->assertEquals($expected, $result);
+	}
+	
+	/**
+	* CommonComponentTest::testExtractEmailInfo()
+	*
+	*/
+	public function testExtractEmailInfoWithInvalidEmail() {
+		$email = 'abc.de';
+		$result = $this->Controller->Common->extractEmailInfo($email, 'username');
+		$this->assertFalse($result);
+		
+		$email = 'xyz@';
+		$result = $this->Controller->Common->extractEmailInfo($email, 'username');
+		$this->assertFalse($result); 
+	}
 
+	/**
+	* CommonComponentTest::testContainsAtSign()
+	*
+	*/
+	public function testContainsAtSign() {
+		$email = 'xyz@e.abc.de';
+		$result = $this->Controller->Common->containsAtSign($email);
+		$this->assertTrue($result);
+		
+		$email = 'e.abc.de';
+		$result = $this->Controller->Common->containsAtSign($email);
+		$this->assertFalse($result);
+	}
 }
 
 /*** additional helper classes ***/