Browse Source

Merge pull request #247 from dereuromark/bugfix/gravatar

Fix Avatar escaping
Mark Sch 6 years ago
parent
commit
6d49c0b3fe
2 changed files with 24 additions and 17 deletions
  1. 11 2
      src/View/Helper/GravatarHelper.php
  2. 13 15
      tests/TestCase/View/Helper/GravatarHelperTest.php

+ 11 - 2
src/View/Helper/GravatarHelper.php

@@ -81,8 +81,13 @@ class GravatarHelper extends Helper {
 	 * @return string Gravatar image string
 	 */
 	public function image($email, array $options = []) {
-		$imageUrl = $this->url($email, $options);
+		$imageOptions = $options += [
+			'escape' => false,
+		];
+		$imageUrl = $this->url($email, $imageOptions);
+
 		unset($options['default'], $options['size'], $options['rating'], $options['ext']);
+
 		return $this->Html->image($imageUrl, $options);
 	}
 
@@ -96,6 +101,10 @@ class GravatarHelper extends Helper {
 	 */
 	public function url($email, array $options = []) {
 		$options = $this->_cleanOptions($options + $this->_config);
+		$options += [
+			'escape' => true,
+		];
+
 		$ext = $options['ext'];
 		$secure = $options['secure'];
 		unset($options['ext'], $options['secure']);
@@ -179,7 +188,7 @@ class GravatarHelper extends Helper {
 				$value = $options[$key];
 				$optionArray[] = $key . '=' . mb_strtolower($value);
 			}
-			return '?' . implode('&', $optionArray);
+			return '?' . implode(!empty($options['escape']) ? '&' : '&', $optionArray);
 		}
 		return '';
 	}

+ 13 - 15
tests/TestCase/View/Helper/GravatarHelperTest.php

@@ -52,42 +52,40 @@ class GravatarHelperTest extends TestCase {
 	/**
 	 * @return void
 	 */
-	public function testImages() {
+	public function testImage() {
 		$is = $this->Gravatar->image($this->garageEmail);
-		//$this->debug($is);
+
 		$this->assertTrue(!empty($is));
 
 		$is = $this->Gravatar->image($this->testEmail);
-		//$this->debug($is);
-		$this->assertTrue(!empty($is));
+		$this->assertTextContains('.gravatar.com/avatar/', $is);
 
 		$is = $this->Gravatar->image($this->testEmail, ['size' => '200']);
-		//$this->debug($is);
-		$this->assertTrue(!empty($is));
+		$this->assertTextContains('?size=200"', $is);
 
 		$is = $this->Gravatar->image($this->testEmail, ['size' => '20']);
-		//$this->debug($is);
-		$this->assertTrue(!empty($is));
+		$this->assertTextContains('?size=20"', $is);
 
 		$is = $this->Gravatar->image($this->testEmail, ['rating' => 'X']); # note the capit. x
-		//$this->debug($is);
-		$this->assertTrue(!empty($is));
+		$this->assertTextContains('?rating=x"', $is);
 
 		$is = $this->Gravatar->image($this->testEmail, ['ext' => true]);
-		//$this->debug($is);
-		$this->assertTrue(!empty($is));
+		$this->assertTextContains('.jpg"', $is);
 
 		$is = $this->Gravatar->image($this->testEmail, ['default' => 'none']);
-		//$this->debug($is);
 		$this->assertTrue(!empty($is));
 
 		$is = $this->Gravatar->image($this->garageEmail, ['default' => 'none']);
-		//$this->debug($is);
 		$this->assertTrue(!empty($is));
 
 		$is = $this->Gravatar->image($this->garageEmail, ['default' => 'http://2.gravatar.com/avatar/8379aabc84ecee06f48d8ca48e09eef4?d=identicon']);
-		//$this->debug($is);
 		$this->assertTrue(!empty($is));
+
+		$is = $this->Gravatar->image($this->testEmail, ['size' => '20']);
+		$this->assertTextContains('?size=20"', $is);
+
+		$is = $this->Gravatar->image($this->testEmail, ['rating' => 'X', 'size' => 20, 'default' => 'none']);
+		$this->assertTextContains('?rating=x&size=20&default=none"', $is);
 	}
 
 	/**