From f06b5ab1611f29c82d176888b17a9c6d456bde1c Mon Sep 17 00:00:00 2001
From: Paul Klimov <klimov.paul@gmail.com>
Date: Fri, 20 Dec 2013 14:04:26 +0200
Subject: [PATCH] Widget "yii\authclient\widgets\Choice" fixed.

---
 extensions/yii/authclient/ClientInterface.php        |  5 +++++
 extensions/yii/authclient/ClientTrait.php            | 35 ++++++++++++++++++++++++++++++++++-
 extensions/yii/authclient/clients/GoogleOpenId.php   | 14 ++++++++++++++
 extensions/yii/authclient/clients/YandexOpenId.php   | 14 ++++++++++++++
 extensions/yii/authclient/widgets/Choice.php         | 77 +++++++++++++++++++++++++++++++++++++++--------------------------------------
 tests/unit/extensions/authclient/ClientTraitTest.php | 37 +++++++++++++++++++++++++------------
 6 files changed, 131 insertions(+), 51 deletions(-)

diff --git a/extensions/yii/authclient/ClientInterface.php b/extensions/yii/authclient/ClientInterface.php
index 2b08e1e..bd76e3b 100644
--- a/extensions/yii/authclient/ClientInterface.php
+++ b/extensions/yii/authclient/ClientInterface.php
@@ -49,4 +49,9 @@ interface ClientInterface
 	 * @return array list of user attributes
 	 */
 	public function getUserAttributes();
+
+	/**
+	 * @return array view options in format: optionName => optionValue
+	 */
+	public function getViewOptions();
 }
\ No newline at end of file
diff --git a/extensions/yii/authclient/ClientTrait.php b/extensions/yii/authclient/ClientTrait.php
index cdd3150..6ce1574 100644
--- a/extensions/yii/authclient/ClientTrait.php
+++ b/extensions/yii/authclient/ClientTrait.php
@@ -39,6 +39,10 @@ trait ClientTrait
 	 * @var array authenticated user attributes.
 	 */
 	private $_userAttributes;
+	/**
+	 * @var array view options in format: optionName => optionValue
+	 */
+	private $_viewOptions;
 
 	/**
 	 * @param string $id service id.
@@ -111,12 +115,31 @@ trait ClientTrait
 	/**
 	 * @param array $userAttributes list of user attributes
 	 */
-	public function setUserAttributes(array $userAttributes)
+	public function setUserAttributes($userAttributes)
 	{
 		$this->_userAttributes = $userAttributes;
 	}
 
 	/**
+	 * @param array $viewOptions view options in format: optionName => optionValue
+	 */
+	public function setViewOptions($viewOptions)
+	{
+		$this->_viewOptions = $viewOptions;
+	}
+
+	/**
+	 * @return array view options in format: optionName => optionValue
+	 */
+	public function getViewOptions()
+	{
+		if ($this->_viewOptions === null) {
+			$this->_viewOptions = $this->defaultViewOptions();
+		}
+		return $this->_viewOptions;
+	}
+
+	/**
 	 * Generates service name.
 	 * @return string service name.
 	 */
@@ -142,4 +165,14 @@ trait ClientTrait
 	{
 		throw new NotSupportedException('Method "' . get_class($this) . '::' . __FUNCTION__ . '" not implemented.');
 	}
+
+	/**
+	 * Returns the default [[viewOptions]] value.
+	 * Particular client may override this method in order to provide specific default view options.
+	 * @return array list of default [[viewOptions]]
+	 */
+	protected function defaultViewOptions()
+	{
+		return [];
+	}
 }
\ No newline at end of file
diff --git a/extensions/yii/authclient/clients/GoogleOpenId.php b/extensions/yii/authclient/clients/GoogleOpenId.php
index 420f827..a0d3e0f 100644
--- a/extensions/yii/authclient/clients/GoogleOpenId.php
+++ b/extensions/yii/authclient/clients/GoogleOpenId.php
@@ -17,6 +17,9 @@ use yii\authclient\OpenId;
  */
 class GoogleOpenId extends OpenId
 {
+	/**
+	 * @inheritdoc
+	 */
 	public function init()
 	{
 		parent::init();
@@ -28,4 +31,15 @@ class GoogleOpenId extends OpenId
 			'pref/language',
 		];
 	}
+
+	/**
+	 * @inheritdoc
+	 */
+	protected function defaultViewOptions()
+	{
+		return [
+			'popupWidth' => 880,
+			'popupHeight' => 520,
+		];
+	}
 }
\ No newline at end of file
diff --git a/extensions/yii/authclient/clients/YandexOpenId.php b/extensions/yii/authclient/clients/YandexOpenId.php
index f2a856e..5ffc347 100644
--- a/extensions/yii/authclient/clients/YandexOpenId.php
+++ b/extensions/yii/authclient/clients/YandexOpenId.php
@@ -17,6 +17,9 @@ use yii\authclient\OpenId;
  */
 class YandexOpenId extends OpenId
 {
+	/**
+	 * @inheritdoc
+	 */
 	public function init()
 	{
 		parent::init();
@@ -26,4 +29,15 @@ class YandexOpenId extends OpenId
 			'contact/email',
 		];
 	}
+
+	/**
+	 * @inheritdoc
+	 */
+	protected function defaultViewOptions()
+	{
+		return [
+			'popupWidth' => 900,
+			'popupHeight' => 550,
+		];
+	}
 }
\ No newline at end of file
diff --git a/extensions/yii/authclient/widgets/Choice.php b/extensions/yii/authclient/widgets/Choice.php
index 94b32ff..501aa01 100644
--- a/extensions/yii/authclient/widgets/Choice.php
+++ b/extensions/yii/authclient/widgets/Choice.php
@@ -10,12 +10,12 @@ namespace yii\authclient\widgets;
 use yii\base\Widget;
 use Yii;
 use yii\helpers\Html;
-use yii\authclient\provider\ProviderInterface;
+use yii\authclient\ClientInterface;
 
 /**
  * Class Choice
  *
- * @property ProviderInterface[] $providers auth providers list.
+ * @property ClientInterface[] $providers auth providers list.
  * @property array $baseAuthUrl configuration for the external services base authentication URL.
  *
  * @author Paul Klimov <klimov.paul@gmail.com>
@@ -24,28 +24,28 @@ use yii\authclient\provider\ProviderInterface;
 class Choice extends Widget
 {
 	/**
-	 * @var ProviderInterface[] auth providers list.
+	 * @var ClientInterface[] auth providers list.
 	 */
-	private $_providers;
+	private $_clients;
 	/**
-	 * @var string name of the auth provider collection application component.
+	 * @var string name of the auth client collection application component.
 	 * This component will be used to fetch {@link services} value if it is not set.
 	 */
-	public $providerCollection;
+	public $clientCollection = 'auth';
 	/**
-	 * @var array configuration for the external services base authentication URL.
+	 * @var array configuration for the external clients base authentication URL.
 	 */
 	private $_baseAuthUrl;
 	/**
-	 * @var string name of the GET param , which should be used to passed auth provider id to URL
+	 * @var string name of the GET param , which should be used to passed auth client id to URL
 	 * defined by {@link baseAuthUrl}.
 	 */
-	public $providerIdGetParamName = 'provider';
+	public $clientIdGetParamName = 'client_id';
 	/**
 	 * @var array the HTML attributes that should be rendered in the div HTML tag representing the container element.
 	 */
 	public $mainContainerHtmlOptions = [
-		'class' => 'services'
+		'class' => 'auth-clients'
 	];
 	/**
 	 * @var boolean indicates if popup window should be used instead of direct links.
@@ -58,22 +58,22 @@ class Choice extends Widget
 	public $autoRender = true;
 
 	/**
-	 * @param ProviderInterface[] $providers auth providers
+	 * @param ClientInterface[] $clients auth providers
 	 */
-	public function setProviders(array $providers)
+	public function setClients(array $clients)
 	{
-		$this->_providers = $providers;
+		$this->_clients = $clients;
 	}
 
 	/**
-	 * @return ProviderInterface[] auth providers
+	 * @return ClientInterface[] auth providers
 	 */
-	public function getProviders()
+	public function getClients()
 	{
-		if ($this->_providers === null) {
-			$this->_providers = $this->defaultProviders();
+		if ($this->_clients === null) {
+			$this->_clients = $this->defaultClients();
 		}
-		return $this->_providers;
+		return $this->_clients;
 	}
 
 	/**
@@ -96,14 +96,14 @@ class Choice extends Widget
 	}
 
 	/**
-	 * Returns default auth providers list.
-	 * @return ProviderInterface[] auth providers list.
+	 * Returns default auth clients list.
+	 * @return ClientInterface[] auth clients list.
 	 */
-	protected function defaultProviders()
+	protected function defaultClients()
 	{
-		/** @var $collection \yii\authclient\provider\Collection */
-		$collection = Yii::$app->getComponent($this->providerCollection);
-		return $collection->getProviders();
+		/** @var $collection \yii\authclient\Collection */
+		$collection = Yii::$app->getComponent($this->clientCollection);
+		return $collection->getClients();
 	}
 
 	/**
@@ -116,47 +116,48 @@ class Choice extends Widget
 			Yii::$app->controller->getRoute()
 		];
 		$params = $_GET;
-		unset($params[$this->providerIdGetParamName]);
+		unset($params[$this->clientIdGetParamName]);
 		$baseAuthUrl = array_merge($baseAuthUrl, $params);
 		return $baseAuthUrl;
 	}
 
 	/**
 	 * Outputs external service auth link.
-	 * @param ProviderInterface $service external auth service instance.
+	 * @param ClientInterface $client external auth client instance.
 	 * @param string $text link text, if not set - default value will be generated.
 	 * @param array $htmlOptions link HTML options.
 	 */
-	public function providerLink($service, $text = null, array $htmlOptions = [])
+	public function providerLink($client, $text = null, array $htmlOptions = [])
 	{
 		if ($text === null) {
-			$text = Html::tag('span', ['class' => 'auth-icon ' . $service->getName()], '');
-			$text .= Html::tag('span', ['class' => 'auth-title'], $service->getTitle());
+			$text = Html::tag('span', '', ['class' => 'auth-icon ' . $client->getName()]);
+			$text .= Html::tag('span', $client->getTitle(), ['class' => 'auth-title']);
 		}
 		if (!array_key_exists('class', $htmlOptions)) {
-			$htmlOptions['class'] = 'auth-link ' . $service->getName();
+			$htmlOptions['class'] = 'auth-link ' . $client->getName();
 		}
 		if ($this->popupMode) {
-			if (isset($service->popupWidth)) {
-				$htmlOptions['data-popup-width'] = $service->popupWidth;
+			$viewOptions = $client->getViewOptions();
+			if (isset($viewOptions['popupWidth'])) {
+				$htmlOptions['data-popup-width'] = $viewOptions['popupWidth'];
 			}
-			if (isset($service->popupHeight)) {
-				$htmlOptions['data-popup-height'] = $service->popupHeight;
+			if (isset($viewOptions['popupHeight'])) {
+				$htmlOptions['data-popup-height'] = $viewOptions['popupHeight'];
 			}
 		}
-		echo Html::a($text, $this->createProviderUrl($service), $htmlOptions);
+		echo Html::a($text, $this->createProviderUrl($client), $htmlOptions);
 	}
 
 	/**
 	 * Composes external service auth URL.
-	 * @param ProviderInterface $provider external auth service instance.
+	 * @param ClientInterface $provider external auth service instance.
 	 * @return string auth URL.
 	 */
 	public function createProviderUrl($provider)
 	{
 		$this->autoRender = false;
 		$url = $this->getBaseAuthUrl();
-		$url[$this->providerIdGetParamName] = $provider->getId();
+		$url[$this->clientIdGetParamName] = $provider->getId();
 		return Html::url($url);
 	}
 
@@ -166,7 +167,7 @@ class Choice extends Widget
 	protected function renderMainContent()
 	{
 		echo Html::beginTag('ul', ['class' => 'auth-services clear']);
-		foreach ($this->getProviders() as $externalService) {
+		foreach ($this->getClients() as $externalService) {
 			echo Html::beginTag('li', ['class' => 'auth-service']);
 			$this->providerLink($externalService);
 			echo Html::endTag('li');
diff --git a/tests/unit/extensions/authclient/ClientTraitTest.php b/tests/unit/extensions/authclient/ClientTraitTest.php
index 404533f..5f51a08 100644
--- a/tests/unit/extensions/authclient/ClientTraitTest.php
+++ b/tests/unit/extensions/authclient/ClientTraitTest.php
@@ -26,33 +26,46 @@ class ClientTraitTest extends TestCase
 
 	public function testSetGet()
 	{
-		$provider = new Client();
+		$client = new Client();
 
 		$id = 'test_id';
-		$provider->setId($id);
-		$this->assertEquals($id, $provider->getId(), 'Unable to setup id!');
+		$client->setId($id);
+		$this->assertEquals($id, $client->getId(), 'Unable to setup id!');
 
 		$name = 'test_name';
-		$provider->setName($name);
-		$this->assertEquals($name, $provider->getName(), 'Unable to setup name!');
+		$client->setName($name);
+		$this->assertEquals($name, $client->getName(), 'Unable to setup name!');
 
 		$title = 'test_title';
-		$provider->setTitle($title);
-		$this->assertEquals($title, $provider->getTitle(), 'Unable to setup title!');
+		$client->setTitle($title);
+		$this->assertEquals($title, $client->getTitle(), 'Unable to setup title!');
+
+		$userAttributes = [
+			'attribute1' => 'value1',
+			'attribute2' => 'value2',
+		];
+		$client->setUserAttributes($userAttributes);
+		$this->assertEquals($userAttributes, $client->getUserAttributes(), 'Unable to setup user attributes!');
+
+		$viewOptions = [
+			'option1' => 'value1',
+			'option2' => 'value2',
+		];
+		$client->setViewOptions($viewOptions);
+		$this->assertEquals($viewOptions, $client->getViewOptions(), 'Unable to setup view options!');
 	}
 
-	public function testGetDescriptiveData()
+	public function testGetDefaults()
 	{
 		$provider = new Client();
 
-		$this->assertNotEmpty($provider->getName(), 'Unable to get name!');
-		$this->assertNotEmpty($provider->getTitle(), 'Unable to get title!');
+		$this->assertNotEmpty($provider->getName(), 'Unable to get default name!');
+		$this->assertNotEmpty($provider->getTitle(), 'Unable to get default title!');
+		$this->assertNotNull($provider->getViewOptions(), 'Unable to get default view options!');
 	}
 }
 
 class Client extends Object implements ClientInterface
 {
 	use ClientTrait;
-
-	public function authenticate() {}
 }
\ No newline at end of file
--
libgit2 0.27.1