Browse Source

gravatar helper

m 14 years ago
parent
commit
79b8003c83
2 changed files with 438 additions and 0 deletions
  1. 252 0
      tests/cases/helpers/gravatar.test.php
  2. 186 0
      views/helpers/gravatar.php

+ 252 - 0
tests/cases/helpers/gravatar.test.php

@@ -0,0 +1,252 @@
+<?php
+
+if (!defined('CAKEPHP_UNIT_TEST_EXECUTION')) {
+	define('CAKEPHP_UNIT_TEST_EXECUTION', 1);
+
+}
+
+define('VALID_TEST_EMAIL', 'graham@grahamweldon.com'); # for testing normal behavior
+define('GARBIGE_TEST_EMAIL', 'test@test.de'); # for testing default image behavior
+
+App::import('Helper', 'Html');
+App::import('Helper', 'Tools.Gravatar');
+
+/**
+ * Gravatar Test Case
+ *
+ * 2010-05-27 ms
+ */
+class GravatarTest extends CakeTestCase {
+
+	/**
+	 * setUp method
+	 */
+	function setUp() {
+		$this->Gravatar = new GravatarHelper();
+		$this->Gravatar->Html = new HtmlHelper();
+	}
+
+	/**
+	 * tearDown method
+	 */
+	function tearDown() {
+		unset($this->Gravatar);
+	}
+
+/** OWN ONES **/
+
+	/**
+	 * @access public
+	 * @return void
+	 * 2009-07-30 ms
+	 */
+	function testDefaultImages() {
+
+		$is = $this->Gravatar->defaultImages();
+		$expectedCount = 7;
+
+		foreach ($is as $image) {
+			echo $image.' ';
+		}
+		$this->assertTrue(is_array($is) && (count($is) === $expectedCount));
+
+	}
+
+
+	/**
+	 * @access public
+	 * @return void
+	 * 2009-07-30 ms
+	 */
+	function testImages() {
+
+		$is = $this->Gravatar->image(GARBIGE_TEST_EMAIL);
+		echo $is;
+		$this->assertTrue(!empty($is));
+
+		$is = $this->Gravatar->image(Configure::read('Config.admin_email'));
+		echo $is;
+		$this->assertTrue(!empty($is));
+
+		$is = $this->Gravatar->image(VALID_TEST_EMAIL);
+		echo $is;
+		$this->assertTrue(!empty($is));
+
+		$is = $this->Gravatar->image(VALID_TEST_EMAIL, array('size'=>'200'));
+		echo $is;
+		$this->assertTrue(!empty($is));
+
+		$is = $this->Gravatar->image(VALID_TEST_EMAIL, array('size'=>'20'));
+		echo $is;
+		$this->assertTrue(!empty($is));
+
+		$is = $this->Gravatar->image(VALID_TEST_EMAIL, array('rating'=>'X')); # note the capit. x
+		echo $is;
+		$this->assertTrue(!empty($is));
+
+		$is = $this->Gravatar->image(VALID_TEST_EMAIL, array('ext'=>true));
+		echo $is;
+		$this->assertTrue(!empty($is));
+
+		$is = $this->Gravatar->image(VALID_TEST_EMAIL, array('default'=>'none'));
+		echo $is;
+		$this->assertTrue(!empty($is));
+
+		$is = $this->Gravatar->image(GARBIGE_TEST_EMAIL, array('default'=>'none'));
+		echo $is;
+		$this->assertTrue(!empty($is));
+
+		$is = $this->Gravatar->image(GARBIGE_TEST_EMAIL, array('default'=>'http://2.gravatar.com/avatar/8379aabc84ecee06f48d8ca48e09eef4?d=identicon'));
+		echo $is;
+		$this->assertTrue(!empty($is));
+
+	}
+
+/** BASE TEST CASES **/
+
+/**
+ * testBaseUrlGeneration
+ *
+ * @return void
+ * @access public
+ */
+	public function testBaseUrlGeneration() {
+		$expected = 'http://www.gravatar.com/avatar/' . md5('example@gravatar.com');
+		$result = $this->Gravatar->url('example@gravatar.com', array('ext' => false, 'default' => 'wavatar'));
+		list($url, $params) = explode('?', $result);
+		$this->assertEqual($expected, $url);
+	}
+
+/**
+ * testExtensions
+ *
+ * @return void
+ * @access public
+ */
+	public function testExtensions() {
+		$result = $this->Gravatar->url('example@gravatar.com', array('ext' => true, 'default' => 'wavatar'));
+		$this->assertPattern('/\.jpg(?:$|\?)/', $result);
+	}
+
+/**
+ * testRating
+ *
+ * @return void
+ * @access public
+ */
+	public function testRating() {
+		$result = $this->Gravatar->url('example@gravatar.com', array('ext' => true, 'default' => 'wavatar'));
+		$this->assertPattern('/\.jpg(?:$|\?)/', $result);
+	}
+
+/**
+ * testAlternateDefaultIcon
+ *
+ * @return void
+ * @access public
+ */
+	public function testAlternateDefaultIcon() {
+		$result = $this->Gravatar->url('example@gravatar.com', array('ext' => false, 'default' => 'wavatar'));
+		list($url, $params) = explode('?', $result);
+		$this->assertPattern('/default=wavatar/', $params);
+	}
+
+/**
+ * testAlternateDefaultIconCorrection
+ *
+ * @return void
+ * @access public
+ */
+	public function testAlternateDefaultIconCorrection() {
+		$result = $this->Gravatar->url('example@gravatar.com', array('ext' => false, 'default' => '12345'));
+		$this->assertPattern('/[^\?]+/', $result);
+	}
+
+/**
+ * testSize
+ *
+ * @return void
+ * @access public
+ */
+	public function testSize() {
+		$result = $this->Gravatar->url('example@gravatar.com', array('size' => '120'));
+		list($url, $params) = explode('?', $result);
+		$this->assertPattern('/size=120/', $params);
+	}
+
+/**
+ * testImageTag
+ *
+ * @return void
+ * @access public
+ */
+	public function testImageTag() {
+		$expected = '<img src="http://www.gravatar.com/avatar/' . md5('example@gravatar.com') . '" alt="" />';
+		$result = $this->Gravatar->image('example@gravatar.com', array('ext' => false));
+		$this->assertEqual($expected, $result);
+
+		$expected = '<img src="http://www.gravatar.com/avatar/' . md5('example@gravatar.com') . '" alt="Gravatar" />';
+		$result = $this->Gravatar->image('example@gravatar.com', array('ext' => false, 'alt' => 'Gravatar'));
+		$this->assertEqual($expected, $result);
+	}
+
+/**
+ * testDefaulting
+ *
+ * @return void
+ * @access public
+ */
+	public function testDefaulting() {
+		$result = $this->Gravatar->url('example@gravatar.com', array('default' => 'wavatar', 'size' => 'default'));
+		list($url, $params) = explode('?', $result);
+		$this->assertEqual($params, 'default=wavatar');
+	}
+
+/**
+ * testNonSecureUrl
+ *
+ * @return void
+ * @access public
+ */
+	public function testNonSecureUrl() {
+		$_SERVER['HTTPS'] = false;
+
+		$expected = 'http://www.gravatar.com/avatar/' . md5('example@gravatar.com');
+		$result = $this->Gravatar->url('example@gravatar.com', array('ext' => false));
+		$this->assertEqual($expected, $result);
+
+		$expected = 'http://www.gravatar.com/avatar/' . md5('example@gravatar.com');
+		$result = $this->Gravatar->url('example@gravatar.com', array('ext' => false, 'secure' => false));
+		$this->assertEqual($expected, $result);
+
+		$_SERVER['HTTPS'] = true;
+		$expected = 'http://www.gravatar.com/avatar/' . md5('example@gravatar.com');
+		$result = $this->Gravatar->url('example@gravatar.com', array('ext' => false, 'secure' => false));
+		$this->assertEqual($expected, $result);
+	}
+
+/**
+ * testSecureUrl
+ *
+ * @return void
+ * @access public
+ */
+	public function testSecureUrl() {
+		$expected = 'https://secure.gravatar.com/avatar/' . md5('example@gravatar.com');
+		$result = $this->Gravatar->url('example@gravatar.com', array('ext' => false, 'secure' => true));
+		$this->assertEqual($expected, $result);
+
+		$_SERVER['HTTPS'] = true;
+
+		$expected = 'https://secure.gravatar.com/avatar/' . md5('example@gravatar.com');
+		$result = $this->Gravatar->url('example@gravatar.com', array('ext' => false));
+		$this->assertEqual($expected, $result);
+
+		$expected = 'https://secure.gravatar.com/avatar/' . md5('example@gravatar.com');
+		$result = $this->Gravatar->url('example@gravatar.com', array('ext' => false, 'secure' => true));
+		$this->assertEqual($expected, $result);
+	}
+
+
+}
+

+ 186 - 0
views/helpers/gravatar.php

@@ -0,0 +1,186 @@
+<?php
+App::import(array('Validation'));
+
+/**
+ * CakePHP Gravatar Helper
+ *
+ * A CakePHP View Helper for the display of Gravatar images (http://www.gravatar.com)
+ *
+ * @copyright Copyright 2009-2010, Graham Weldon (http://grahamweldon.com)
+ * @license http://www.opensource.org/licenses/mit-license.php The MIT License
+ * @package goodies
+ * @subpackage goodies.views.helpers
+ * 
+ * hashtype now always md5
+ * 2010-12-21 ms
+ */
+class GravatarHelper extends AppHelper {
+
+/**
+ * Gravatar avatar image base URL
+ *
+ * @var string
+ */
+	private $__url = array(
+		'http' => 'http://www.gravatar.com/avatar/',
+		'https' => 'https://secure.gravatar.com/avatar/'
+	);
+
+/**
+ * Collection of allowed ratings
+ *
+ * @var array
+ */
+	private $__allowedRatings = array('g', 'pg', 'r', 'x');
+
+/**
+ * Default Icon sets
+ *
+ * @var array
+ */
+	private $__defaultIcons = array('none', 'mm', 'identicon', 'monsterid', 'retro', 'wavatar', '404');
+
+/**
+ * Default settings
+ *
+ * @var array
+ */
+	private $__default = array(
+		'default' => null,
+		'size' => null,
+		'rating' => null,
+		'ext' => false);
+
+/**
+ * Helpers used by this helper
+ *
+ * @var array
+ */
+	public $helpers = array('Html');
+
+/**
+ * Constructor
+ *
+ */
+	public function __construct($settings = array()) {
+		if (!is_array($settings)) {
+			$settings = array();
+		}
+		$this->__default = array_merge($this->__default, array_intersect_key($settings, $this->__default));
+
+		// Default the secure option to match the current URL.
+		$this->__default['secure'] = env('HTTPS');
+		parent::__construct();
+	}
+
+/**
+ * Show gravatar for the supplied email address
+ *
+ * @param string $email Email address
+ * @param array $options Array of options, keyed from default settings
+ * @return string Gravatar image string
+ */
+	public function image($email, $options = array()) {
+		$imageUrl = $this->url($email, $options);
+		unset($options['default'], $options['size'], $options['rating'], $options['ext']);
+		return $this->Html->image($imageUrl, $options);
+	}
+
+/**
+ * Generate image URL
+ *
+ * @param string $email Email address
+ * @param string $options Array of options, keyed from default settings
+ * @return string Gravatar Image URL
+ */
+	public function url($email, $options = array()) {
+		$options = $this->__cleanOptions(array_merge($this->__default, $options));
+		$ext = $options['ext'];
+		$secure = $options['secure'];
+		unset($options['ext'], $options['secure']);
+		$protocol = $secure === true ? 'https' : 'http';
+
+		$imageUrl = $this->__url[$protocol] . md5($email);
+		if ($ext === true) {
+			// If 'ext' option is supplied and true, append an extension to the generated image URL.
+			// This helps systems that don't display images unless they have a specific image extension on the URL.
+			$imageUrl .= '.jpg';
+		}
+		$imageUrl .= $this->__buildOptions($options);
+		return $imageUrl;
+	}
+
+/**
+ * Generate an array of default images for preview purposes
+ *
+ * @param array $options Array of options, keyed from default settings
+ * @return array Default images array
+ * @access public
+ */
+	public function defaultImages($options = array()) {
+		$options = $this->__cleanOptions(array_merge($this->__default, $options));
+		$images = array();
+		foreach ($this->__defaultIcons as $defaultIcon) {
+			$options['default'] = $defaultIcon;
+			$images[$defaultIcon] = $this->image(null, $options);
+		}
+		return $images;
+	}
+
+/**
+ * Sanitize the options array
+ *
+ * @param array $options Array of options, keyed from default settings
+ * @return array Clean options array
+ */
+	private function __cleanOptions($options) {
+		if (!isset($options['size']) || empty($options['size']) || !is_numeric($options['size'])) {
+			unset($options['size']);
+		} else {
+			$options['size'] = min(max($options['size'], 1), 512);
+		}
+
+		if (!$options['rating'] || !in_array(mb_strtolower($options['rating']), $this->__allowedRatings)) {
+			unset($options['rating']);
+		}
+
+		if (!$options['default']) {
+			unset($options['default']);
+		} else {
+			if (!in_array($options['default'], $this->__defaultIcons) && !Validation::url($options['default'])) {
+				unset($options['default']);
+			}
+		}
+		return $options;
+	}
+
+/**
+ * Generate email address hash
+ *
+ * @param string $email Email address
+ * @param string $type Hash type to employ
+ * @return string Email address hash
+ */
+	private function __emailHash($email, $type) {
+		return md5(mb_strtolower($email), $type);
+	}
+
+/**
+ * Build Options URL string
+ *
+ * @param array $options Array of options, keyed from default settings
+ * @return string URL string of options
+ */
+	private function __buildOptions($options = array()) {
+		$gravatarOptions = array_intersect(array_keys($options), array_keys($this->__default));
+		if (!empty($gravatarOptions)) {
+			$optionArray = array();
+			foreach ($gravatarOptions as $key) {
+				$value = $options[$key];
+				$optionArray[] = $key . '=' . mb_strtolower($value);
+			}
+			return '?' . implode('&amp;', $optionArray);
+		}
+		return '';
+	}
+}