Qiang Xue
12 years ago
17 changed files with 2292 additions and 2408 deletions
File diff suppressed because it is too large
Load Diff
@ -1,110 +1,109 @@
|
||||
<?php |
||||
/** |
||||
* @link http://www.yiiframework.com/ |
||||
* @copyright Copyright (c) 2008 Yii Software LLC |
||||
* @license http://www.yiiframework.com/license/ |
||||
*/ |
||||
|
||||
namespace yii\web; |
||||
|
||||
use Yii; |
||||
use yii\base\ActionFilter; |
||||
use yii\base\Action; |
||||
use yii\base\View; |
||||
use yii\caching\Dependency; |
||||
|
||||
/** |
||||
* @author Qiang Xue <qiang.xue@gmail.com> |
||||
* @since 2.0 |
||||
*/ |
||||
class PageCache extends ActionFilter |
||||
{ |
||||
/** |
||||
* @var boolean whether the content being cached should be differentiated according to the route. |
||||
* A route consists of the requested controller ID and action ID. Defaults to true. |
||||
*/ |
||||
public $varyByRoute = true; |
||||
/** |
||||
* @var View the view object that is used to create the fragment cache widget to implement page caching. |
||||
* If not set, the view registered with the application will be used. |
||||
*/ |
||||
public $view; |
||||
|
||||
/** |
||||
* @var string the ID of the cache application component. Defaults to 'cache' (the primary cache application component.) |
||||
*/ |
||||
public $cacheID = 'cache'; |
||||
/** |
||||
* @var integer number of seconds that the data can remain valid in cache. |
||||
* Use 0 to indicate that the cached data will never expire. |
||||
*/ |
||||
public $duration = 60; |
||||
/** |
||||
* @var array|Dependency the dependency that the cached content depends on. |
||||
* This can be either a [[Dependency]] object or a configuration array for creating the dependency object. |
||||
* For example, |
||||
* |
||||
* ~~~ |
||||
* array( |
||||
* 'class' => 'yii\caching\DbDependency', |
||||
* 'sql' => 'SELECT MAX(lastModified) FROM Post', |
||||
* ) |
||||
* ~~~ |
||||
* |
||||
* would make the output cache depends on the last modified time of all posts. |
||||
* If any post has its modification time changed, the cached content would be invalidated. |
||||
*/ |
||||
public $dependency; |
||||
/** |
||||
* @var array list of factors that would cause the variation of the content being cached. |
||||
* Each factor is a string representing a variation (e.g. the language, a GET parameter). |
||||
* The following variation setting will cause the content to be cached in different versions |
||||
* according to the current application language: |
||||
* |
||||
* ~~~ |
||||
* array( |
||||
* Yii::$app->language, |
||||
* ) |
||||
*/ |
||||
public $variations; |
||||
/** |
||||
* @var boolean whether to enable the fragment cache. You may use this property to turn on and off |
||||
* the fragment cache according to specific setting (e.g. enable fragment cache only for GET requests). |
||||
*/ |
||||
public $enabled = true; |
||||
|
||||
|
||||
public function init() |
||||
{ |
||||
parent::init(); |
||||
if ($this->view === null) { |
||||
$this->view = Yii::$app->getView(); |
||||
} |
||||
} |
||||
|
||||
/** |
||||
* This method is invoked right before an action is to be executed (after all possible filters.) |
||||
* You may override this method to do last-minute preparation for the action. |
||||
* @param Action $action the action to be executed. |
||||
* @return boolean whether the action should continue to be executed. |
||||
*/ |
||||
public function beforeAction($action) |
||||
{ |
||||
$properties = array(); |
||||
foreach (array('cacheID', 'duration', 'dependency', 'variations', 'enabled') as $name) { |
||||
$properties[$name] = $this->$name; |
||||
} |
||||
$id = $this->varyByRoute ? $action->getUniqueId() : __CLASS__; |
||||
return $this->view->beginCache($id, $properties); |
||||
} |
||||
|
||||
/** |
||||
* This method is invoked right after an action is executed. |
||||
* You may override this method to do some postprocessing for the action. |
||||
* @param Action $action the action just executed. |
||||
*/ |
||||
public function afterAction($action) |
||||
{ |
||||
$this->view->endCache(); |
||||
} |
||||
<?php |
||||
/** |
||||
* @link http://www.yiiframework.com/ |
||||
* @copyright Copyright (c) 2008 Yii Software LLC |
||||
* @license http://www.yiiframework.com/license/ |
||||
*/ |
||||
|
||||
namespace yii\web; |
||||
|
||||
use Yii; |
||||
use yii\base\ActionFilter; |
||||
use yii\base\Action; |
||||
use yii\base\View; |
||||
use yii\caching\Dependency; |
||||
|
||||
/** |
||||
* @author Qiang Xue <qiang.xue@gmail.com> |
||||
* @since 2.0 |
||||
*/ |
||||
class PageCache extends ActionFilter |
||||
{ |
||||
/** |
||||
* @var boolean whether the content being cached should be differentiated according to the route. |
||||
* A route consists of the requested controller ID and action ID. Defaults to true. |
||||
*/ |
||||
public $varyByRoute = true; |
||||
/** |
||||
* @var View the view object that is used to create the fragment cache widget to implement page caching. |
||||
* If not set, the view registered with the application will be used. |
||||
*/ |
||||
public $view; |
||||
/** |
||||
* @var string the application component ID of the [[\yii\caching\Cache|cache]] object. |
||||
*/ |
||||
public $cache = 'cache'; |
||||
/** |
||||
* @var integer number of seconds that the data can remain valid in cache. |
||||
* Use 0 to indicate that the cached data will never expire. |
||||
*/ |
||||
public $duration = 60; |
||||
/** |
||||
* @var array|Dependency the dependency that the cached content depends on. |
||||
* This can be either a [[Dependency]] object or a configuration array for creating the dependency object. |
||||
* For example, |
||||
* |
||||
* ~~~ |
||||
* array( |
||||
* 'class' => 'yii\caching\DbDependency', |
||||
* 'sql' => 'SELECT MAX(lastModified) FROM Post', |
||||
* ) |
||||
* ~~~ |
||||
* |
||||
* would make the output cache depends on the last modified time of all posts. |
||||
* If any post has its modification time changed, the cached content would be invalidated. |
||||
*/ |
||||
public $dependency; |
||||
/** |
||||
* @var array list of factors that would cause the variation of the content being cached. |
||||
* Each factor is a string representing a variation (e.g. the language, a GET parameter). |
||||
* The following variation setting will cause the content to be cached in different versions |
||||
* according to the current application language: |
||||
* |
||||
* ~~~ |
||||
* array( |
||||
* Yii::$app->language, |
||||
* ) |
||||
*/ |
||||
public $variations; |
||||
/** |
||||
* @var boolean whether to enable the fragment cache. You may use this property to turn on and off |
||||
* the fragment cache according to specific setting (e.g. enable fragment cache only for GET requests). |
||||
*/ |
||||
public $enabled = true; |
||||
|
||||
|
||||
public function init() |
||||
{ |
||||
parent::init(); |
||||
if ($this->view === null) { |
||||
$this->view = Yii::$app->getView(); |
||||
} |
||||
} |
||||
|
||||
/** |
||||
* This method is invoked right before an action is to be executed (after all possible filters.) |
||||
* You may override this method to do last-minute preparation for the action. |
||||
* @param Action $action the action to be executed. |
||||
* @return boolean whether the action should continue to be executed. |
||||
*/ |
||||
public function beforeAction($action) |
||||
{ |
||||
$properties = array(); |
||||
foreach (array('cache', 'duration', 'dependency', 'variations', 'enabled') as $name) { |
||||
$properties[$name] = $this->$name; |
||||
} |
||||
$id = $this->varyByRoute ? $action->getUniqueId() : __CLASS__; |
||||
return $this->view->beginCache($id, $properties); |
||||
} |
||||
|
||||
/** |
||||
* This method is invoked right after an action is executed. |
||||
* You may override this method to do some postprocessing for the action. |
||||
* @param Action $action the action just executed. |
||||
*/ |
||||
public function afterAction($action) |
||||
{ |
||||
$this->view->endCache(); |
||||
} |
||||
} |
@ -1,213 +1,184 @@
|
||||
<?php |
||||
/** |
||||
* @link http://www.yiiframework.com/ |
||||
* @copyright Copyright (c) 2008 Yii Software LLC |
||||
* @license http://www.yiiframework.com/license/ |
||||
*/ |
||||
|
||||
namespace yii\widgets; |
||||
|
||||
use Yii; |
||||
use yii\base\InvalidConfigException; |
||||
use yii\base\Widget; |
||||
use yii\caching\Cache; |
||||
use yii\caching\Dependency; |
||||
|
||||
/** |
||||
* @author Qiang Xue <qiang.xue@gmail.com> |
||||
* @since 2.0 |
||||
*/ |
||||
class FragmentCache extends Widget |
||||
{ |
||||
/** |
||||
* @var string the ID of the cache application component. Defaults to 'cache' (the primary cache application component.) |
||||
*/ |
||||
public $cacheID = 'cache'; |
||||
/** |
||||
* @var integer number of seconds that the data can remain valid in cache. |
||||
* Use 0 to indicate that the cached data will never expire. |
||||
*/ |
||||
public $duration = 60; |
||||
/** |
||||
* @var array|Dependency the dependency that the cached content depends on. |
||||
* This can be either a [[Dependency]] object or a configuration array for creating the dependency object. |
||||
* For example, |
||||
* |
||||
* ~~~ |
||||
* array( |
||||
* 'class' => 'yii\caching\DbDependency', |
||||
* 'sql' => 'SELECT MAX(lastModified) FROM Post', |
||||
* ) |
||||
* ~~~ |
||||
* |
||||
* would make the output cache depends on the last modified time of all posts. |
||||
* If any post has its modification time changed, the cached content would be invalidated. |
||||
*/ |
||||
public $dependency; |
||||
/** |
||||
* @var array list of factors that would cause the variation of the content being cached. |
||||
* Each factor is a string representing a variation (e.g. the language, a GET parameter). |
||||
* The following variation setting will cause the content to be cached in different versions |
||||
* according to the current application language: |
||||
* |
||||
* ~~~ |
||||
* array( |
||||
* Yii::$app->language, |
||||
* ) |
||||
*/ |
||||
public $variations; |
||||
/** |
||||
* @var boolean whether to enable the fragment cache. You may use this property to turn on and off |
||||
* the fragment cache according to specific setting (e.g. enable fragment cache only for GET requests). |
||||
*/ |
||||
public $enabled = true; |
||||
/** |
||||
* @var \yii\base\View the view object within which this widget is used. If not set, |
||||
* the view registered with the application will be used. This is mainly used by dynamic content feature. |
||||
*/ |
||||
public $view; |
||||
/** |
||||
* @var array a list of placeholders for embedding dynamic contents. This property |
||||
* is used internally to implement the content caching feature. Do not modify it. |
||||
*/ |
||||
public $dynamicPlaceholders; |
||||
|
||||
|
||||
/** |
||||
* Marks the start of content to be cached. |
||||
* Content displayed after this method call and before {@link endCache()} |
||||
* will be captured and saved in cache. |
||||
* This method does nothing if valid content is already found in cache. |
||||
*/ |
||||
public function init() |
||||
{ |
||||
if ($this->view === null) { |
||||
$this->view = Yii::$app->getView(); |
||||
} |
||||
if ($this->getCache() !== null && $this->getCachedContent() === false) { |
||||
$this->view->cacheStack[] = $this; |
||||
ob_start(); |
||||
ob_implicit_flush(false); |
||||
} |
||||
} |
||||
|
||||
/** |
||||
* Marks the end of content to be cached. |
||||
* Content displayed before this method call and after {@link init()} |
||||
* will be captured and saved in cache. |
||||
* This method does nothing if valid content is already found in cache. |
||||
*/ |
||||
public function run() |
||||
{ |
||||
if (($content = $this->getCachedContent()) !== false) { |
||||
echo $content; |
||||
} elseif (($cache = $this->getCache()) !== null) { |
||||
$content = ob_get_clean(); |
||||
array_pop($this->view->cacheStack); |
||||
if (is_array($this->dependency)) { |
||||
$this->dependency = Yii::createObject($this->dependency); |
||||
} |
||||
$data = array($content, $this->dynamicPlaceholders); |
||||
$cache->set($this->calculateKey(), $data, $this->duration, $this->dependency); |
||||
|
||||
if ($this->view->cacheStack === array() && !empty($this->dynamicPlaceholders)) { |
||||
$content = $this->updateDynamicContent($content, $this->dynamicPlaceholders); |
||||
} |
||||
echo $content; |
||||
} |
||||
} |
||||
|
||||
/** |
||||
* @var string|boolean the cached content. False if the content is not cached. |
||||
*/ |
||||
private $_content; |
||||
|
||||
/** |
||||
* Returns the cached content if available. |
||||
* @return string|boolean the cached content. False is returned if valid content is not found in the cache. |
||||
*/ |
||||
public function getCachedContent() |
||||
{ |
||||
if ($this->_content === null) { |
||||
$this->_content = false; |
||||
if (($cache = $this->getCache()) !== null) { |
||||
$key = $this->calculateKey(); |
||||
$data = $cache->get($key); |
||||
if (is_array($data) && count($data) === 2) { |
||||
list ($content, $placeholders) = $data; |
||||
if (is_array($placeholders) && count($placeholders) > 0) { |
||||
if ($this->view->cacheStack === array()) { |
||||
// outermost cache: replace placeholder with dynamic content |
||||
$content = $this->updateDynamicContent($content, $placeholders); |
||||
} |
||||
foreach ($placeholders as $name => $statements) { |
||||
$this->view->addDynamicPlaceholder($name, $statements); |
||||
} |
||||
} |
||||
$this->_content = $content; |
||||
} |
||||
} |
||||
} |
||||
return $this->_content; |
||||
} |
||||
|
||||
protected function updateDynamicContent($content, $placeholders) |
||||
{ |
||||
foreach ($placeholders as $name => $statements) { |
||||
$placeholders[$name] = $this->view->evaluateDynamicContent($statements); |
||||
} |
||||
return strtr($content, $placeholders); |
||||
} |
||||
|
||||
/** |
||||
* Generates a unique key used for storing the content in cache. |
||||
* The key generated depends on both [[id]] and [[variations]]. |
||||
* @return string a valid cache key |
||||
*/ |
||||
protected function calculateKey() |
||||
{ |
||||
$factors = array(__CLASS__, $this->getId()); |
||||
if (is_array($this->variations)) { |
||||
foreach ($this->variations as $factor) { |
||||
$factors[] = $factor; |
||||
} |
||||
} |
||||
return $this->getCache()->buildKey($factors); |
||||
} |
||||
|
||||
/** |
||||
* @var Cache |
||||
*/ |
||||
private $_cache; |
||||
|
||||
/** |
||||
* Returns the cache instance used for storing content. |
||||
* @return Cache the cache instance. Null is returned if the cache component is not available |
||||
* or [[enabled]] is false. |
||||
* @throws InvalidConfigException if [[cacheID]] does not point to a valid application component. |
||||
*/ |
||||
public function getCache() |
||||
{ |
||||
if (!$this->enabled) { |
||||
return null; |
||||
} |
||||
if ($this->_cache === null) { |
||||
$cache = Yii::$app->getComponent($this->cacheID); |
||||
if ($cache instanceof Cache) { |
||||
$this->_cache = $cache; |
||||
} else { |
||||
throw new InvalidConfigException('FragmentCache::cacheID must refer to the ID of a cache application component.'); |
||||
} |
||||
} |
||||
return $this->_cache; |
||||
} |
||||
|
||||
/** |
||||
* Sets the cache instance used by the session component. |
||||
* @param Cache $value the cache instance |
||||
*/ |
||||
public function setCache($value) |
||||
{ |
||||
$this->_cache = $value; |
||||
} |
||||
<?php |
||||
/** |
||||
* @link http://www.yiiframework.com/ |
||||
* @copyright Copyright (c) 2008 Yii Software LLC |
||||
* @license http://www.yiiframework.com/license/ |
||||
*/ |
||||
|
||||
namespace yii\widgets; |
||||
|
||||
use Yii; |
||||
use yii\base\InvalidConfigException; |
||||
use yii\base\Widget; |
||||
use yii\caching\Cache; |
||||
use yii\caching\Dependency; |
||||
|
||||
/** |
||||
* @author Qiang Xue <qiang.xue@gmail.com> |
||||
* @since 2.0 |
||||
*/ |
||||
class FragmentCache extends Widget |
||||
{ |
||||
/** |
||||
* @var Cache|string the cache object or the application component ID of the cache object. |
||||
* After the FragmentCache object is created, if you want to change this property, |
||||
* you should only assign it with a cache object. |
||||
*/ |
||||
public $cache = 'cache'; |
||||
/** |
||||
* @var integer number of seconds that the data can remain valid in cache. |
||||
* Use 0 to indicate that the cached data will never expire. |
||||
*/ |
||||
public $duration = 60; |
||||
/** |
||||
* @var array|Dependency the dependency that the cached content depends on. |
||||
* This can be either a [[Dependency]] object or a configuration array for creating the dependency object. |
||||
* For example, |
||||
* |
||||
* ~~~ |
||||
* array( |
||||
* 'class' => 'yii\caching\DbDependency', |
||||
* 'sql' => 'SELECT MAX(lastModified) FROM Post', |
||||
* ) |
||||
* ~~~ |
||||
* |
||||
* would make the output cache depends on the last modified time of all posts. |
||||
* If any post has its modification time changed, the cached content would be invalidated. |
||||
*/ |
||||
public $dependency; |
||||
/** |
||||
* @var array list of factors that would cause the variation of the content being cached. |
||||
* Each factor is a string representing a variation (e.g. the language, a GET parameter). |
||||
* The following variation setting will cause the content to be cached in different versions |
||||
* according to the current application language: |
||||
* |
||||
* ~~~ |
||||
* array( |
||||
* Yii::$app->language, |
||||
* ) |
||||
*/ |
||||
public $variations; |
||||
/** |
||||
* @var boolean whether to enable the fragment cache. You may use this property to turn on and off |
||||
* the fragment cache according to specific setting (e.g. enable fragment cache only for GET requests). |
||||
*/ |
||||
public $enabled = true; |
||||
/** |
||||
* @var \yii\base\View the view object within which this widget is used. If not set, |
||||
* the view registered with the application will be used. This is mainly used by dynamic content feature. |
||||
*/ |
||||
public $view; |
||||
/** |
||||
* @var array a list of placeholders for embedding dynamic contents. This property |
||||
* is used internally to implement the content caching feature. Do not modify it. |
||||
*/ |
||||
public $dynamicPlaceholders; |
||||
|
||||
/** |
||||
* Initializes the FragmentCache object. |
||||
*/ |
||||
public function init() |
||||
{ |
||||
parent::init(); |
||||
|
||||
if ($this->view === null) { |
||||
$this->view = Yii::$app->getView(); |
||||
} |
||||
|
||||
if (!$this->enabled) { |
||||
$this->cache = null; |
||||
} elseif (is_string($this->cache)) { |
||||
$this->cache = Yii::$app->getComponent($this->cache); |
||||
} |
||||
|
||||
if ($this->getCachedContent() === false) { |
||||
$this->view->cacheStack[] = $this; |
||||
ob_start(); |
||||
ob_implicit_flush(false); |
||||
} |
||||
} |
||||
|
||||
/** |
||||
* Marks the end of content to be cached. |
||||
* Content displayed before this method call and after {@link init()} |
||||
* will be captured and saved in cache. |
||||
* This method does nothing if valid content is already found in cache. |
||||
*/ |
||||
public function run() |
||||
{ |
||||
if (($content = $this->getCachedContent()) !== false) { |
||||
echo $content; |
||||
} elseif ($this->cache instanceof Cache) { |
||||
$content = ob_get_clean(); |
||||
array_pop($this->view->cacheStack); |
||||
if (is_array($this->dependency)) { |
||||
$this->dependency = Yii::createObject($this->dependency); |
||||
} |
||||
$data = array($content, $this->dynamicPlaceholders); |
||||
$this->cache->set($this->calculateKey(), $data, $this->duration, $this->dependency); |
||||
|
||||
if ($this->view->cacheStack === array() && !empty($this->dynamicPlaceholders)) { |
||||
$content = $this->updateDynamicContent($content, $this->dynamicPlaceholders); |
||||
} |
||||
echo $content; |
||||
} |
||||
} |
||||
|
||||
/** |
||||
* @var string|boolean the cached content. False if the content is not cached. |
||||
*/ |
||||
private $_content; |
||||
|
||||
/** |
||||
* Returns the cached content if available. |
||||
* @return string|boolean the cached content. False is returned if valid content is not found in the cache. |
||||
*/ |
||||
public function getCachedContent() |
||||
{ |
||||
if ($this->_content === null) { |
||||
$this->_content = false; |
||||
if ($this->cache instanceof Cache) { |
||||
$key = $this->calculateKey(); |
||||
$data = $this->cache->get($key); |
||||
if (is_array($data) && count($data) === 2) { |
||||
list ($content, $placeholders) = $data; |
||||
if (is_array($placeholders) && count($placeholders) > 0) { |
||||
if ($this->view->cacheStack === array()) { |
||||
// outermost cache: replace placeholder with dynamic content |
||||
$content = $this->updateDynamicContent($content, $placeholders); |
||||
} |
||||
foreach ($placeholders as $name => $statements) { |
||||
$this->view->addDynamicPlaceholder($name, $statements); |
||||
} |
||||
} |
||||
$this->_content = $content; |
||||
} |
||||
} |
||||
} |
||||
return $this->_content; |
||||
} |
||||
|
||||
protected function updateDynamicContent($content, $placeholders) |
||||
{ |
||||
foreach ($placeholders as $name => $statements) { |
||||
$placeholders[$name] = $this->view->evaluateDynamicContent($statements); |
||||
} |
||||
return strtr($content, $placeholders); |
||||
} |
||||
|
||||
/** |
||||
* Generates a unique key used for storing the content in cache. |
||||
* The key generated depends on both [[id]] and [[variations]]. |
||||
* @return string a valid cache key |
||||
*/ |
||||
protected function calculateKey() |
||||
{ |
||||
$factors = array(__CLASS__, $this->getId()); |
||||
if (is_array($this->variations)) { |
||||
foreach ($this->variations as $factor) { |
||||
$factors[] = $factor; |
||||
} |
||||
} |
||||
return $this->cache->buildKey($factors); |
||||
} |
||||
} |
@ -1,448 +1,448 @@
|
||||
<?php |
||||
|
||||
namespace yiiunit\framework\util; |
||||
|
||||
use Yii; |
||||
use yii\helpers\Html; |
||||
use yii\web\Application; |
||||
|
||||
class HtmlTest extends \yii\test\TestCase |
||||
{ |
||||
public function setUp() |
||||
{ |
||||
new Application('test', '@yiiunit/runtime', array( |
||||
'components' => array( |
||||
'request' => array( |
||||
'class' => 'yii\web\Request', |
||||
'url' => '/test', |
||||
), |
||||
), |
||||
)); |
||||
} |
||||
|
||||
public function tearDown() |
||||
{ |
||||
Yii::$app = null; |
||||
} |
||||
|
||||
public function testEncode() |
||||
{ |
||||
$this->assertEquals("a<>&"'", Html::encode("a<>&\"'")); |
||||
} |
||||
|
||||
public function testDecode() |
||||
{ |
||||
$this->assertEquals("a<>&\"'", Html::decode("a<>&"'")); |
||||
} |
||||
|
||||
public function testTag() |
||||
{ |
||||
$this->assertEquals('<br />', Html::tag('br')); |
||||
$this->assertEquals('<span></span>', Html::tag('span')); |
||||
$this->assertEquals('<div>content</div>', Html::tag('div', 'content')); |
||||
$this->assertEquals('<input type="text" name="test" value="<>" />', Html::tag('input', '', array('type' => 'text', 'name' => 'test', 'value' => '<>'))); |
||||
|
||||
Html::$closeVoidElements = false; |
||||
|
||||
$this->assertEquals('<br>', Html::tag('br')); |
||||
$this->assertEquals('<span></span>', Html::tag('span')); |
||||
$this->assertEquals('<div>content</div>', Html::tag('div', 'content')); |
||||
$this->assertEquals('<input type="text" name="test" value="<>">', Html::tag('input', '', array('type' => 'text', 'name' => 'test', 'value' => '<>'))); |
||||
|
||||
Html::$closeVoidElements = true; |
||||
|
||||
$this->assertEquals('<span disabled="disabled"></span>', Html::tag('span', '', array('disabled' => true))); |
||||
Html::$showBooleanAttributeValues = false; |
||||
$this->assertEquals('<span disabled></span>', Html::tag('span', '', array('disabled' => true))); |
||||
Html::$showBooleanAttributeValues = true; |
||||
} |
||||
|
||||
public function testBeginTag() |
||||
{ |
||||
$this->assertEquals('<br>', Html::beginTag('br')); |
||||
$this->assertEquals('<span id="test" class="title">', Html::beginTag('span', array('id' => 'test', 'class' => 'title'))); |
||||
} |
||||
|
||||
public function testEndTag() |
||||
{ |
||||
$this->assertEquals('</br>', Html::endTag('br')); |
||||
$this->assertEquals('</span>', Html::endTag('span')); |
||||
} |
||||
|
||||
public function testCdata() |
||||
{ |
||||
$data = 'test<>'; |
||||
$this->assertEquals('<![CDATA[' . $data . ']]>', Html::cdata($data)); |
||||
} |
||||
|
||||
public function testStyle() |
||||
{ |
||||
$content = 'a <>'; |
||||
$this->assertEquals("<style type=\"text/css\">/*<![CDATA[*/\n{$content}\n/*]]>*/</style>", Html::style($content)); |
||||
$this->assertEquals("<style type=\"text/less\">/*<![CDATA[*/\n{$content}\n/*]]>*/</style>", Html::style($content, array('type' => 'text/less'))); |
||||
} |
||||
|
||||
public function testScript() |
||||
{ |
||||
$content = 'a <>'; |
||||
$this->assertEquals("<script type=\"text/javascript\">/*<![CDATA[*/\n{$content}\n/*]]>*/</script>", Html::script($content)); |
||||
$this->assertEquals("<script type=\"text/js\">/*<![CDATA[*/\n{$content}\n/*]]>*/</script>", Html::script($content, array('type' => 'text/js'))); |
||||
} |
||||
|
||||
public function testCssFile() |
||||
{ |
||||
$this->assertEquals('<link type="text/css" href="http://example.com" rel="stylesheet" />', Html::cssFile('http://example.com')); |
||||
$this->assertEquals('<link type="text/css" href="/test" rel="stylesheet" />', Html::cssFile('')); |
||||
} |
||||
|
||||
public function testJsFile() |
||||
{ |
||||
$this->assertEquals('<script type="text/javascript" src="http://example.com"></script>', Html::jsFile('http://example.com')); |
||||
$this->assertEquals('<script type="text/javascript" src="/test"></script>', Html::jsFile('')); |
||||
} |
||||
|
||||
public function testBeginForm() |
||||
{ |
||||
$this->assertEquals('<form action="/test" method="post">', Html::beginForm()); |
||||
$this->assertEquals('<form action="/example" method="get">', Html::beginForm('/example', 'get')); |
||||
$hiddens = array( |
||||
'<input type="hidden" name="id" value="1" />', |
||||
'<input type="hidden" name="title" value="<" />', |
||||
); |
||||
$this->assertEquals('<form action="/example" method="get">' . "\n" . implode("\n", $hiddens), Html::beginForm('/example?id=1&title=%3C', 'get')); |
||||
} |
||||
|
||||
public function testEndForm() |
||||
{ |
||||
$this->assertEquals('</form>', Html::endForm()); |
||||
} |
||||
|
||||
public function testA() |
||||
{ |
||||
$this->assertEquals('<a>something<></a>', Html::a('something<>')); |
||||
$this->assertEquals('<a href="/example">something</a>', Html::a('something', '/example')); |
||||
$this->assertEquals('<a href="/test">something</a>', Html::a('something', '')); |
||||
} |
||||
|
||||
public function testMailto() |
||||
{ |
||||
$this->assertEquals('<a href="mailto:test<>">test<></a>', Html::mailto('test<>')); |
||||
$this->assertEquals('<a href="mailto:test>">test<></a>', Html::mailto('test<>', 'test>')); |
||||
} |
||||
|
||||
public function testImg() |
||||
{ |
||||
$this->assertEquals('<img src="/example" alt="" />', Html::img('/example')); |
||||
$this->assertEquals('<img src="/test" alt="" />', Html::img('')); |
||||
$this->assertEquals('<img src="/example" width="10" alt="something" />', Html::img('/example', array('alt' => 'something', 'width' => 10))); |
||||
} |
||||
|
||||
public function testLabel() |
||||
{ |
||||
$this->assertEquals('<label>something<></label>', Html::label('something<>')); |
||||
$this->assertEquals('<label for="a">something<></label>', Html::label('something<>', 'a')); |
||||
$this->assertEquals('<label class="test" for="a">something<></label>', Html::label('something<>', 'a', array('class' => 'test'))); |
||||
} |
||||
|
||||
public function testButton() |
||||
{ |
||||
$this->assertEquals('<button type="button">Button</button>', Html::button()); |
||||
$this->assertEquals('<button type="button" name="test" value="value">content<></button>', Html::button('test', 'value', 'content<>')); |
||||
$this->assertEquals('<button type="submit" class="t" name="test" value="value">content<></button>', Html::button('test', 'value', 'content<>', array('type' => 'submit', 'class' => "t"))); |
||||
} |
||||
|
||||
public function testSubmitButton() |
||||
{ |
||||
$this->assertEquals('<button type="submit">Submit</button>', Html::submitButton()); |
||||
$this->assertEquals('<button type="submit" class="t" name="test" value="value">content<></button>', Html::submitButton('test', 'value', 'content<>', array('class' => 't'))); |
||||
} |
||||
|
||||
public function testResetButton() |
||||
{ |
||||
$this->assertEquals('<button type="reset">Reset</button>', Html::resetButton()); |
||||
$this->assertEquals('<button type="reset" class="t" name="test" value="value">content<></button>', Html::resetButton('test', 'value', 'content<>', array('class' => 't'))); |
||||
} |
||||
|
||||
public function testInput() |
||||
{ |
||||
$this->assertEquals('<input type="text" />', Html::input('text')); |
||||
$this->assertEquals('<input type="text" class="t" name="test" value="value" />', Html::input('text', 'test', 'value', array('class' => 't'))); |
||||
} |
||||
|
||||
public function testButtonInput() |
||||
{ |
||||
$this->assertEquals('<input type="button" name="test" value="Button" />', Html::buttonInput('test')); |
||||
$this->assertEquals('<input type="button" class="a" name="test" value="text" />', Html::buttonInput('test', 'text', array('class' => 'a'))); |
||||
} |
||||
|
||||
public function testSubmitInput() |
||||
{ |
||||
$this->assertEquals('<input type="submit" value="Submit" />', Html::submitInput()); |
||||
$this->assertEquals('<input type="submit" class="a" name="test" value="text" />', Html::submitInput('test', 'text', array('class' => 'a'))); |
||||
} |
||||
|
||||
public function testResetInput() |
||||
{ |
||||
$this->assertEquals('<input type="reset" value="Reset" />', Html::resetInput()); |
||||
$this->assertEquals('<input type="reset" class="a" name="test" value="text" />', Html::resetInput('test', 'text', array('class' => 'a'))); |
||||
} |
||||
|
||||
public function testTextInput() |
||||
{ |
||||
$this->assertEquals('<input type="text" name="test" />', Html::textInput('test')); |
||||
$this->assertEquals('<input type="text" class="t" name="test" value="value" />', Html::textInput('test', 'value', array('class' => 't'))); |
||||
} |
||||
|
||||
public function testHiddenInput() |
||||
{ |
||||
$this->assertEquals('<input type="hidden" name="test" />', Html::hiddenInput('test')); |
||||
$this->assertEquals('<input type="hidden" class="t" name="test" value="value" />', Html::hiddenInput('test', 'value', array('class' => 't'))); |
||||
} |
||||
|
||||
public function testPasswordInput() |
||||
{ |
||||
$this->assertEquals('<input type="password" name="test" />', Html::passwordInput('test')); |
||||
$this->assertEquals('<input type="password" class="t" name="test" value="value" />', Html::passwordInput('test', 'value', array('class' => 't'))); |
||||
} |
||||
|
||||
public function testFileInput() |
||||
{ |
||||
$this->assertEquals('<input type="file" name="test" />', Html::fileInput('test')); |
||||
$this->assertEquals('<input type="file" class="t" name="test" value="value" />', Html::fileInput('test', 'value', array('class' => 't'))); |
||||
} |
||||
|
||||
public function testTextarea() |
||||
{ |
||||
$this->assertEquals('<textarea name="test"></textarea>', Html::textarea('test')); |
||||
$this->assertEquals('<textarea class="t" name="test">value<></textarea>', Html::textarea('test', 'value<>', array('class' => 't'))); |
||||
} |
||||
|
||||
public function testRadio() |
||||
{ |
||||
$this->assertEquals('<input type="radio" name="test" value="1" />', Html::radio('test')); |
||||
$this->assertEquals('<input type="radio" class="a" name="test" checked="checked" />', Html::radio('test', true, null, array('class' => 'a'))); |
||||
$this->assertEquals('<input type="hidden" name="test" value="0" /><input type="radio" class="a" name="test" value="2" checked="checked" />', Html::radio('test', true, 2, array('class' => 'a' , 'uncheck' => '0'))); |
||||
} |
||||
|
||||
public function testCheckbox() |
||||
{ |
||||
$this->assertEquals('<input type="checkbox" name="test" value="1" />', Html::checkbox('test')); |
||||
$this->assertEquals('<input type="checkbox" class="a" name="test" checked="checked" />', Html::checkbox('test', true, null, array('class' => 'a'))); |
||||
$this->assertEquals('<input type="hidden" name="test" value="0" /><input type="checkbox" class="a" name="test" value="2" checked="checked" />', Html::checkbox('test', true, 2, array('class' => 'a', 'uncheck' => '0'))); |
||||
} |
||||
|
||||
public function testDropDownList() |
||||
{ |
||||
$expected = <<<EOD |
||||
<select name="test"> |
||||
|
||||
</select> |
||||
EOD; |
||||
$this->assertEquals($expected, Html::dropDownList('test')); |
||||
$expected = <<<EOD |
||||
<select name="test"> |
||||
<option value="value1">text1</option> |
||||
<option value="value2">text2</option> |
||||
</select> |
||||
EOD; |
||||
$this->assertEquals($expected, Html::dropDownList('test', null, $this->getDataItems())); |
||||
$expected = <<<EOD |
||||
<select name="test"> |
||||
<option value="value1">text1</option> |
||||
<option value="value2" selected="selected">text2</option> |
||||
</select> |
||||
EOD; |
||||
$this->assertEquals($expected, Html::dropDownList('test', 'value2', $this->getDataItems())); |
||||
} |
||||
|
||||
public function testListBox() |
||||
{ |
||||
$expected = <<<EOD |
||||
<select name="test" size="4"> |
||||
|
||||
</select> |
||||
EOD; |
||||
$this->assertEquals($expected, Html::listBox('test')); |
||||
$expected = <<<EOD |
||||
<select name="test" size="5"> |
||||
<option value="value1">text1</option> |
||||
<option value="value2">text2</option> |
||||
</select> |
||||
EOD; |
||||
$this->assertEquals($expected, Html::listBox('test', null, $this->getDataItems(), array('size' => 5))); |
||||
$expected = <<<EOD |
||||
<select name="test" size="4"> |
||||
<option value="value1<>">text1<></option> |
||||
<option value="value 2">text 2</option> |
||||
</select> |
||||
EOD; |
||||
$this->assertEquals($expected, Html::listBox('test', null, $this->getDataItems2())); |
||||
$expected = <<<EOD |
||||
<select name="test" size="4"> |
||||
<option value="value1">text1</option> |
||||
<option value="value2" selected="selected">text2</option> |
||||
</select> |
||||
EOD; |
||||
$this->assertEquals($expected, Html::listBox('test', 'value2', $this->getDataItems())); |
||||
$expected = <<<EOD |
||||
<select name="test" size="4"> |
||||
<option value="value1" selected="selected">text1</option> |
||||
<option value="value2" selected="selected">text2</option> |
||||
</select> |
||||
EOD; |
||||
$this->assertEquals($expected, Html::listBox('test', array('value1', 'value2'), $this->getDataItems())); |
||||
|
||||
$expected = <<<EOD |
||||
<select name="test[]" multiple="multiple" size="4"> |
||||
|
||||
</select> |
||||
EOD; |
||||
$this->assertEquals($expected, Html::listBox('test', null, array(), array('multiple' => true))); |
||||
$expected = <<<EOD |
||||
<input type="hidden" name="test" value="0" /><select name="test" size="4"> |
||||
|
||||
</select> |
||||
EOD; |
||||
$this->assertEquals($expected, Html::listBox('test', '', array(), array('unselect' => '0'))); |
||||
} |
||||
|
||||
public function testCheckboxList() |
||||
{ |
||||
$this->assertEquals('', Html::checkboxList('test')); |
||||
|
||||
$expected = <<<EOD |
||||
<label><input type="checkbox" name="test[]" value="value1" /> text1</label> |
||||
<label><input type="checkbox" name="test[]" value="value2" checked="checked" /> text2</label> |
||||
EOD; |
||||
$this->assertEquals($expected, Html::checkboxList('test', array('value2'), $this->getDataItems())); |
||||
|
||||
$expected = <<<EOD |
||||
<label><input type="checkbox" name="test[]" value="value1<>" /> text1<></label> |
||||
<label><input type="checkbox" name="test[]" value="value 2" /> text 2</label> |
||||
EOD; |
||||
$this->assertEquals($expected, Html::checkboxList('test', array('value2'), $this->getDataItems2())); |
||||
|
||||
$expected = <<<EOD |
||||
<input type="hidden" name="test" value="0" /><label><input type="checkbox" name="test[]" value="value1" /> text1</label><br /> |
||||
<label><input type="checkbox" name="test[]" value="value2" checked="checked" /> text2</label> |
||||
EOD; |
||||
$this->assertEquals($expected, Html::checkboxList('test', array('value2'), $this->getDataItems(), array( |
||||
'separator' => "<br />\n", |
||||
'unselect' => '0', |
||||
))); |
||||
|
||||
$expected = <<<EOD |
||||
0<label>text1 <input type="checkbox" name="test[]" value="value1" /></label> |
||||
1<label>text2 <input type="checkbox" name="test[]" value="value2" checked="checked" /></label> |
||||
EOD; |
||||
$this->assertEquals($expected, Html::checkboxList('test', array('value2'), $this->getDataItems(), array( |
||||
'item' => function ($index, $label, $name, $checked, $value) { |
||||
return $index . Html::label($label . ' ' . Html::checkbox($name, $checked, $value)); |
||||
} |
||||
))); |
||||
} |
||||
|
||||
public function testRadioList() |
||||
{ |
||||
$this->assertEquals('', Html::radioList('test')); |
||||
|
||||
$expected = <<<EOD |
||||
<label><input type="radio" name="test" value="value1" /> text1</label> |
||||
<label><input type="radio" name="test" value="value2" checked="checked" /> text2</label> |
||||
EOD; |
||||
$this->assertEquals($expected, Html::radioList('test', array('value2'), $this->getDataItems())); |
||||
|
||||
$expected = <<<EOD |
||||
<label><input type="radio" name="test" value="value1<>" /> text1<></label> |
||||
<label><input type="radio" name="test" value="value 2" /> text 2</label> |
||||
EOD; |
||||
$this->assertEquals($expected, Html::radioList('test', array('value2'), $this->getDataItems2())); |
||||
|
||||
$expected = <<<EOD |
||||
<input type="hidden" name="test" value="0" /><label><input type="radio" name="test" value="value1" /> text1</label><br /> |
||||
<label><input type="radio" name="test" value="value2" checked="checked" /> text2</label> |
||||
EOD; |
||||
$this->assertEquals($expected, Html::radioList('test', array('value2'), $this->getDataItems(), array( |
||||
'separator' => "<br />\n", |
||||
'unselect' => '0', |
||||
))); |
||||
|
||||
$expected = <<<EOD |
||||
0<label>text1 <input type="radio" name="test" value="value1" /></label> |
||||
1<label>text2 <input type="radio" name="test" value="value2" checked="checked" /></label> |
||||
EOD; |
||||
$this->assertEquals($expected, Html::radioList('test', array('value2'), $this->getDataItems(), array( |
||||
'item' => function ($index, $label, $name, $checked, $value) { |
||||
return $index . Html::label($label . ' ' . Html::radio($name, $checked, $value)); |
||||
} |
||||
))); |
||||
} |
||||
|
||||
public function testRenderOptions() |
||||
{ |
||||
$data = array( |
||||
'value1' => 'label1', |
||||
'group1' => array( |
||||
'value11' => 'label11', |
||||
'group11' => array( |
||||
'value111' => 'label111', |
||||
), |
||||
'group12' => array(), |
||||
), |
||||
'value2' => 'label2', |
||||
'group2' => array(), |
||||
); |
||||
$expected = <<<EOD |
||||
<option value="">please select<></option> |
||||
<option value="value1" selected="selected">label1</option> |
||||
<optgroup label="group1"> |
||||
<option value="value11">label11</option> |
||||
<optgroup label="group11"> |
||||
<option class="option" value="value111" selected="selected">label111</option> |
||||
</optgroup> |
||||
<optgroup class="group" label="group12"> |
||||
|
||||
</optgroup> |
||||
</optgroup> |
||||
<option value="value2">label2</option> |
||||
<optgroup label="group2"> |
||||
|
||||
</optgroup> |
||||
EOD; |
||||
$attributes = array( |
||||
'prompt' => 'please select<>', |
||||
'options' => array( |
||||
'value111' => array('class' => 'option'), |
||||
), |
||||
'groups' => array( |
||||
'group12' => array('class' => 'group'), |
||||
), |
||||
); |
||||
$this->assertEquals($expected, Html::renderSelectOptions(array('value111', 'value1'), $data, $attributes)); |
||||
} |
||||
|
||||
public function testRenderAttributes() |
||||
{ |
||||
$this->assertEquals('', Html::renderTagAttributes(array())); |
||||
$this->assertEquals(' name="test" value="1<>"', Html::renderTagAttributes(array('name' => 'test', 'empty' => null, 'value' => '1<>'))); |
||||
Html::$showBooleanAttributeValues = false; |
||||
$this->assertEquals(' checked disabled', Html::renderTagAttributes(array('checked' => 'checked', 'disabled' => true, 'hidden' => false))); |
||||
Html::$showBooleanAttributeValues = true; |
||||
} |
||||
|
||||
protected function getDataItems() |
||||
{ |
||||
return array( |
||||
'value1' => 'text1', |
||||
'value2' => 'text2', |
||||
); |
||||
} |
||||
|
||||
protected function getDataItems2() |
||||
{ |
||||
return array( |
||||
'value1<>' => 'text1<>', |
||||
'value 2' => 'text 2', |
||||
); |
||||
} |
||||
} |
||||
<?php |
||||
|
||||
namespace yiiunit\framework\util; |
||||
|
||||
use Yii; |
||||
use yii\helpers\Html; |
||||
use yii\web\Application; |
||||
|
||||
class HtmlTest extends \yii\test\TestCase |
||||
{ |
||||
public function setUp() |
||||
{ |
||||
new Application('test', '@yiiunit/runtime', array( |
||||
'components' => array( |
||||
'request' => array( |
||||
'class' => 'yii\web\Request', |
||||
'url' => '/test', |
||||
), |
||||
), |
||||
)); |
||||
} |
||||
|
||||
public function tearDown() |
||||
{ |
||||
Yii::$app = null; |
||||
} |
||||
|
||||
public function testEncode() |
||||
{ |
||||
$this->assertEquals("a<>&"'", Html::encode("a<>&\"'")); |
||||
} |
||||
|
||||
public function testDecode() |
||||
{ |
||||
$this->assertEquals("a<>&\"'", Html::decode("a<>&"'")); |
||||
} |
||||
|
||||
public function testTag() |
||||
{ |
||||
$this->assertEquals('<br />', Html::tag('br')); |
||||
$this->assertEquals('<span></span>', Html::tag('span')); |
||||
$this->assertEquals('<div>content</div>', Html::tag('div', 'content')); |
||||
$this->assertEquals('<input type="text" name="test" value="<>" />', Html::tag('input', '', array('type' => 'text', 'name' => 'test', 'value' => '<>'))); |
||||
|
||||
Html::$closeVoidElements = false; |
||||
|
||||
$this->assertEquals('<br>', Html::tag('br')); |
||||
$this->assertEquals('<span></span>', Html::tag('span')); |
||||
$this->assertEquals('<div>content</div>', Html::tag('div', 'content')); |
||||
$this->assertEquals('<input type="text" name="test" value="<>">', Html::tag('input', '', array('type' => 'text', 'name' => 'test', 'value' => '<>'))); |
||||
|
||||
Html::$closeVoidElements = true; |
||||
|
||||
$this->assertEquals('<span disabled="disabled"></span>', Html::tag('span', '', array('disabled' => true))); |
||||
Html::$showBooleanAttributeValues = false; |
||||
$this->assertEquals('<span disabled></span>', Html::tag('span', '', array('disabled' => true))); |
||||
Html::$showBooleanAttributeValues = true; |
||||
} |
||||
|
||||
public function testBeginTag() |
||||
{ |
||||
$this->assertEquals('<br>', Html::beginTag('br')); |
||||
$this->assertEquals('<span id="test" class="title">', Html::beginTag('span', array('id' => 'test', 'class' => 'title'))); |
||||
} |
||||
|
||||
public function testEndTag() |
||||
{ |
||||
$this->assertEquals('</br>', Html::endTag('br')); |
||||
$this->assertEquals('</span>', Html::endTag('span')); |
||||
} |
||||
|
||||
public function testCdata() |
||||
{ |
||||
$data = 'test<>'; |
||||
$this->assertEquals('<![CDATA[' . $data . ']]>', Html::cdata($data)); |
||||
} |
||||
|
||||
public function testStyle() |
||||
{ |
||||
$content = 'a <>'; |
||||
$this->assertEquals("<style type=\"text/css\">/*<![CDATA[*/\n{$content}\n/*]]>*/</style>", Html::style($content)); |
||||
$this->assertEquals("<style type=\"text/less\">/*<![CDATA[*/\n{$content}\n/*]]>*/</style>", Html::style($content, array('type' => 'text/less'))); |
||||
} |
||||
|
||||
public function testScript() |
||||
{ |
||||
$content = 'a <>'; |
||||
$this->assertEquals("<script type=\"text/javascript\">/*<![CDATA[*/\n{$content}\n/*]]>*/</script>", Html::script($content)); |
||||
$this->assertEquals("<script type=\"text/js\">/*<![CDATA[*/\n{$content}\n/*]]>*/</script>", Html::script($content, array('type' => 'text/js'))); |
||||
} |
||||
|
||||
public function testCssFile() |
||||
{ |
||||
$this->assertEquals('<link type="text/css" href="http://example.com" rel="stylesheet" />', Html::cssFile('http://example.com')); |
||||
$this->assertEquals('<link type="text/css" href="/test" rel="stylesheet" />', Html::cssFile('')); |
||||
} |
||||
|
||||
public function testJsFile() |
||||
{ |
||||
$this->assertEquals('<script type="text/javascript" src="http://example.com"></script>', Html::jsFile('http://example.com')); |
||||
$this->assertEquals('<script type="text/javascript" src="/test"></script>', Html::jsFile('')); |
||||
} |
||||
|
||||
public function testBeginForm() |
||||
{ |
||||
$this->assertEquals('<form action="/test" method="post">', Html::beginForm()); |
||||
$this->assertEquals('<form action="/example" method="get">', Html::beginForm('/example', 'get')); |
||||
$hiddens = array( |
||||
'<input type="hidden" name="id" value="1" />', |
||||
'<input type="hidden" name="title" value="<" />', |
||||
); |
||||
$this->assertEquals('<form action="/example" method="get">' . "\n" . implode("\n", $hiddens), Html::beginForm('/example?id=1&title=%3C', 'get')); |
||||
} |
||||
|
||||
public function testEndForm() |
||||
{ |
||||
$this->assertEquals('</form>', Html::endForm()); |
||||
} |
||||
|
||||
public function testA() |
||||
{ |
||||
$this->assertEquals('<a>something<></a>', Html::a('something<>')); |
||||
$this->assertEquals('<a href="/example">something</a>', Html::a('something', '/example')); |
||||
$this->assertEquals('<a href="/test">something</a>', Html::a('something', '')); |
||||
} |
||||
|
||||
public function testMailto() |
||||
{ |
||||
$this->assertEquals('<a href="mailto:test<>">test<></a>', Html::mailto('test<>')); |
||||
$this->assertEquals('<a href="mailto:test>">test<></a>', Html::mailto('test<>', 'test>')); |
||||
} |
||||
|
||||
public function testImg() |
||||
{ |
||||
$this->assertEquals('<img src="/example" alt="" />', Html::img('/example')); |
||||
$this->assertEquals('<img src="/test" alt="" />', Html::img('')); |
||||
$this->assertEquals('<img src="/example" width="10" alt="something" />', Html::img('/example', array('alt' => 'something', 'width' => 10))); |
||||
} |
||||
|
||||
public function testLabel() |
||||
{ |
||||
$this->assertEquals('<label>something<></label>', Html::label('something<>')); |
||||
$this->assertEquals('<label for="a">something<></label>', Html::label('something<>', 'a')); |
||||
$this->assertEquals('<label class="test" for="a">something<></label>', Html::label('something<>', 'a', array('class' => 'test'))); |
||||
} |
||||
|
||||
public function testButton() |
||||
{ |
||||
$this->assertEquals('<button type="button">Button</button>', Html::button()); |
||||
$this->assertEquals('<button type="button" name="test" value="value">content<></button>', Html::button('test', 'value', 'content<>')); |
||||
$this->assertEquals('<button type="submit" class="t" name="test" value="value">content<></button>', Html::button('test', 'value', 'content<>', array('type' => 'submit', 'class' => "t"))); |
||||
} |
||||
|
||||
public function testSubmitButton() |
||||
{ |
||||
$this->assertEquals('<button type="submit">Submit</button>', Html::submitButton()); |
||||
$this->assertEquals('<button type="submit" class="t" name="test" value="value">content<></button>', Html::submitButton('test', 'value', 'content<>', array('class' => 't'))); |
||||
} |
||||
|
||||
public function testResetButton() |
||||
{ |
||||
$this->assertEquals('<button type="reset">Reset</button>', Html::resetButton()); |
||||
$this->assertEquals('<button type="reset" class="t" name="test" value="value">content<></button>', Html::resetButton('test', 'value', 'content<>', array('class' => 't'))); |
||||
} |
||||
|
||||
public function testInput() |
||||
{ |
||||
$this->assertEquals('<input type="text" />', Html::input('text')); |
||||
$this->assertEquals('<input type="text" class="t" name="test" value="value" />', Html::input('text', 'test', 'value', array('class' => 't'))); |
||||
} |
||||
|
||||
public function testButtonInput() |
||||
{ |
||||
$this->assertEquals('<input type="button" name="test" value="Button" />', Html::buttonInput('test')); |
||||
$this->assertEquals('<input type="button" class="a" name="test" value="text" />', Html::buttonInput('test', 'text', array('class' => 'a'))); |
||||
} |
||||
|
||||
public function testSubmitInput() |
||||
{ |
||||
$this->assertEquals('<input type="submit" value="Submit" />', Html::submitInput()); |
||||
$this->assertEquals('<input type="submit" class="a" name="test" value="text" />', Html::submitInput('test', 'text', array('class' => 'a'))); |
||||
} |
||||
|
||||
public function testResetInput() |
||||
{ |
||||
$this->assertEquals('<input type="reset" value="Reset" />', Html::resetInput()); |
||||
$this->assertEquals('<input type="reset" class="a" name="test" value="text" />', Html::resetInput('test', 'text', array('class' => 'a'))); |
||||
} |
||||
|
||||
public function testTextInput() |
||||
{ |
||||
$this->assertEquals('<input type="text" name="test" />', Html::textInput('test')); |
||||
$this->assertEquals('<input type="text" class="t" name="test" value="value" />', Html::textInput('test', 'value', array('class' => 't'))); |
||||
} |
||||
|
||||
public function testHiddenInput() |
||||
{ |
||||
$this->assertEquals('<input type="hidden" name="test" />', Html::hiddenInput('test')); |
||||
$this->assertEquals('<input type="hidden" class="t" name="test" value="value" />', Html::hiddenInput('test', 'value', array('class' => 't'))); |
||||
} |
||||
|
||||
public function testPasswordInput() |
||||
{ |
||||
$this->assertEquals('<input type="password" name="test" />', Html::passwordInput('test')); |
||||
$this->assertEquals('<input type="password" class="t" name="test" value="value" />', Html::passwordInput('test', 'value', array('class' => 't'))); |
||||
} |
||||
|
||||
public function testFileInput() |
||||
{ |
||||
$this->assertEquals('<input type="file" name="test" />', Html::fileInput('test')); |
||||
$this->assertEquals('<input type="file" class="t" name="test" value="value" />', Html::fileInput('test', 'value', array('class' => 't'))); |
||||
} |
||||
|
||||
public function testTextarea() |
||||
{ |
||||
$this->assertEquals('<textarea name="test"></textarea>', Html::textarea('test')); |
||||
$this->assertEquals('<textarea class="t" name="test">value<></textarea>', Html::textarea('test', 'value<>', array('class' => 't'))); |
||||
} |
||||
|
||||
public function testRadio() |
||||
{ |
||||
$this->assertEquals('<input type="radio" name="test" value="1" />', Html::radio('test')); |
||||
$this->assertEquals('<input type="radio" class="a" name="test" checked="checked" />', Html::radio('test', true, null, array('class' => 'a'))); |
||||
$this->assertEquals('<input type="hidden" name="test" value="0" /><input type="radio" class="a" name="test" value="2" checked="checked" />', Html::radio('test', true, 2, array('class' => 'a' , 'uncheck' => '0'))); |
||||
} |
||||
|
||||
public function testCheckbox() |
||||
{ |
||||
$this->assertEquals('<input type="checkbox" name="test" value="1" />', Html::checkbox('test')); |
||||
$this->assertEquals('<input type="checkbox" class="a" name="test" checked="checked" />', Html::checkbox('test', true, null, array('class' => 'a'))); |
||||
$this->assertEquals('<input type="hidden" name="test" value="0" /><input type="checkbox" class="a" name="test" value="2" checked="checked" />', Html::checkbox('test', true, 2, array('class' => 'a', 'uncheck' => '0'))); |
||||
} |
||||
|
||||
public function testDropDownList() |
||||
{ |
||||
$expected = <<<EOD |
||||
<select name="test"> |
||||
|
||||
</select> |
||||
EOD; |
||||
$this->assertEquals($expected, Html::dropDownList('test')); |
||||
$expected = <<<EOD |
||||
<select name="test"> |
||||
<option value="value1">text1</option> |
||||
<option value="value2">text2</option> |
||||
</select> |
||||
EOD; |
||||
$this->assertEquals($expected, Html::dropDownList('test', null, $this->getDataItems())); |
||||
$expected = <<<EOD |
||||
<select name="test"> |
||||
<option value="value1">text1</option> |
||||
<option value="value2" selected="selected">text2</option> |
||||
</select> |
||||
EOD; |
||||
$this->assertEquals($expected, Html::dropDownList('test', 'value2', $this->getDataItems())); |
||||
} |
||||
|
||||
public function testListBox() |
||||
{ |
||||
$expected = <<<EOD |
||||
<select name="test" size="4"> |
||||
|
||||
</select> |
||||
EOD; |
||||
$this->assertEquals($expected, Html::listBox('test')); |
||||
$expected = <<<EOD |
||||
<select name="test" size="5"> |
||||
<option value="value1">text1</option> |
||||
<option value="value2">text2</option> |
||||
</select> |
||||
EOD; |
||||
$this->assertEquals($expected, Html::listBox('test', null, $this->getDataItems(), array('size' => 5))); |
||||
$expected = <<<EOD |
||||
<select name="test" size="4"> |
||||
<option value="value1<>">text1<></option> |
||||
<option value="value 2">text 2</option> |
||||
</select> |
||||
EOD; |
||||
$this->assertEquals($expected, Html::listBox('test', null, $this->getDataItems2())); |
||||
$expected = <<<EOD |
||||
<select name="test" size="4"> |
||||
<option value="value1">text1</option> |
||||
<option value="value2" selected="selected">text2</option> |
||||
</select> |
||||
EOD; |
||||
$this->assertEquals($expected, Html::listBox('test', 'value2', $this->getDataItems())); |
||||
$expected = <<<EOD |
||||
<select name="test" size="4"> |
||||
<option value="value1" selected="selected">text1</option> |
||||
<option value="value2" selected="selected">text2</option> |
||||
</select> |
||||
EOD; |
||||
$this->assertEquals($expected, Html::listBox('test', array('value1', 'value2'), $this->getDataItems())); |
||||
|
||||
$expected = <<<EOD |
||||
<select name="test[]" multiple="multiple" size="4"> |
||||
|
||||
</select> |
||||
EOD; |
||||
$this->assertEquals($expected, Html::listBox('test', null, array(), array('multiple' => true))); |
||||
$expected = <<<EOD |
||||
<input type="hidden" name="test" value="0" /><select name="test" size="4"> |
||||
|
||||
</select> |
||||
EOD; |
||||
$this->assertEquals($expected, Html::listBox('test', '', array(), array('unselect' => '0'))); |
||||
} |
||||
|
||||
public function testCheckboxList() |
||||
{ |
||||
$this->assertEquals('', Html::checkboxList('test')); |
||||
|
||||
$expected = <<<EOD |
||||
<label><input type="checkbox" name="test[]" value="value1" /> text1</label> |
||||
<label><input type="checkbox" name="test[]" value="value2" checked="checked" /> text2</label> |
||||
EOD; |
||||
$this->assertEquals($expected, Html::checkboxList('test', array('value2'), $this->getDataItems())); |
||||
|
||||
$expected = <<<EOD |
||||
<label><input type="checkbox" name="test[]" value="value1<>" /> text1<></label> |
||||
<label><input type="checkbox" name="test[]" value="value 2" /> text 2</label> |
||||
EOD; |
||||
$this->assertEquals($expected, Html::checkboxList('test', array('value2'), $this->getDataItems2())); |
||||
|
||||
$expected = <<<EOD |
||||
<input type="hidden" name="test" value="0" /><label><input type="checkbox" name="test[]" value="value1" /> text1</label><br /> |
||||
<label><input type="checkbox" name="test[]" value="value2" checked="checked" /> text2</label> |
||||
EOD; |
||||
$this->assertEquals($expected, Html::checkboxList('test', array('value2'), $this->getDataItems(), array( |
||||
'separator' => "<br />\n", |
||||
'unselect' => '0', |
||||
))); |
||||
|
||||
$expected = <<<EOD |
||||
0<label>text1 <input type="checkbox" name="test[]" value="value1" /></label> |
||||
1<label>text2 <input type="checkbox" name="test[]" value="value2" checked="checked" /></label> |
||||
EOD; |
||||
$this->assertEquals($expected, Html::checkboxList('test', array('value2'), $this->getDataItems(), array( |
||||
'item' => function ($index, $label, $name, $checked, $value) { |
||||
return $index . Html::label($label . ' ' . Html::checkbox($name, $checked, $value)); |
||||
} |
||||
))); |
||||
} |
||||
|
||||
public function testRadioList() |
||||
{ |
||||
$this->assertEquals('', Html::radioList('test')); |
||||
|
||||
$expected = <<<EOD |
||||
<label><input type="radio" name="test" value="value1" /> text1</label> |
||||
<label><input type="radio" name="test" value="value2" checked="checked" /> text2</label> |
||||
EOD; |
||||
$this->assertEquals($expected, Html::radioList('test', array('value2'), $this->getDataItems())); |
||||
|
||||
$expected = <<<EOD |
||||
<label><input type="radio" name="test" value="value1<>" /> text1<></label> |
||||
<label><input type="radio" name="test" value="value 2" /> text 2</label> |
||||
EOD; |
||||
$this->assertEquals($expected, Html::radioList('test', array('value2'), $this->getDataItems2())); |
||||
|
||||
$expected = <<<EOD |
||||
<input type="hidden" name="test" value="0" /><label><input type="radio" name="test" value="value1" /> text1</label><br /> |
||||
<label><input type="radio" name="test" value="value2" checked="checked" /> text2</label> |
||||
EOD; |
||||
$this->assertEquals($expected, Html::radioList('test', array('value2'), $this->getDataItems(), array( |
||||
'separator' => "<br />\n", |
||||
'unselect' => '0', |
||||
))); |
||||
|
||||
$expected = <<<EOD |
||||
0<label>text1 <input type="radio" name="test" value="value1" /></label> |
||||
1<label>text2 <input type="radio" name="test" value="value2" checked="checked" /></label> |
||||
EOD; |
||||
$this->assertEquals($expected, Html::radioList('test', array('value2'), $this->getDataItems(), array( |
||||
'item' => function ($index, $label, $name, $checked, $value) { |
||||
return $index . Html::label($label . ' ' . Html::radio($name, $checked, $value)); |
||||
} |
||||
))); |
||||
} |
||||
|
||||
public function testRenderOptions() |
||||
{ |
||||
$data = array( |
||||
'value1' => 'label1', |
||||
'group1' => array( |
||||
'value11' => 'label11', |
||||
'group11' => array( |
||||
'value111' => 'label111', |
||||
), |
||||
'group12' => array(), |
||||
), |
||||
'value2' => 'label2', |
||||
'group2' => array(), |
||||
); |
||||
$expected = <<<EOD |
||||
<option value="">please select<></option> |
||||
<option value="value1" selected="selected">label1</option> |
||||
<optgroup label="group1"> |
||||
<option value="value11">label11</option> |
||||
<optgroup label="group11"> |
||||
<option class="option" value="value111" selected="selected">label111</option> |
||||
</optgroup> |
||||
<optgroup class="group" label="group12"> |
||||
|
||||
</optgroup> |
||||
</optgroup> |
||||
<option value="value2">label2</option> |
||||
<optgroup label="group2"> |
||||
|
||||
</optgroup> |
||||
EOD; |
||||
$attributes = array( |
||||
'prompt' => 'please select<>', |
||||
'options' => array( |
||||
'value111' => array('class' => 'option'), |
||||
), |
||||
'groups' => array( |
||||
'group12' => array('class' => 'group'), |
||||
), |
||||
); |
||||
$this->assertEquals($expected, Html::renderSelectOptions(array('value111', 'value1'), $data, $attributes)); |
||||
} |
||||
|
||||
public function testRenderAttributes() |
||||
{ |
||||
$this->assertEquals('', Html::renderTagAttributes(array())); |
||||
$this->assertEquals(' name="test" value="1<>"', Html::renderTagAttributes(array('name' => 'test', 'empty' => null, 'value' => '1<>'))); |
||||
Html::$showBooleanAttributeValues = false; |
||||
$this->assertEquals(' checked disabled', Html::renderTagAttributes(array('checked' => 'checked', 'disabled' => true, 'hidden' => false))); |
||||
Html::$showBooleanAttributeValues = true; |
||||
} |
||||
|
||||
protected function getDataItems() |
||||
{ |
||||
return array( |
||||
'value1' => 'text1', |
||||
'value2' => 'text2', |
||||
); |
||||
} |
||||
|
||||
protected function getDataItems2() |
||||
{ |
||||
return array( |
||||
'value1<>' => 'text1<>', |
||||
'value 2' => 'text 2', |
||||
); |
||||
} |
||||
} |
||||
|
Loading…
Reference in new issue