3 changed files with 223 additions and 44 deletions
@ -0,0 +1,175 @@ |
|||||||
|
<?php |
||||||
|
/** |
||||||
|
* @link http://www.yiiframework.com/ |
||||||
|
* @copyright Copyright (c) 2008 Yii Software LLC |
||||||
|
* @license http://www.yiiframework.com/license/ |
||||||
|
*/ |
||||||
|
|
||||||
|
namespace yiiunit\framework\data; |
||||||
|
use yii\web\UrlManager; |
||||||
|
use yiiunit\TestCase; |
||||||
|
use yii\data\Sort; |
||||||
|
|
||||||
|
|
||||||
|
/** |
||||||
|
* @author Qiang Xue <qiang.xue@gmail.com> |
||||||
|
* @since 2.0 |
||||||
|
*/ |
||||||
|
class SortTest extends TestCase |
||||||
|
{ |
||||||
|
public function testGetOrders() |
||||||
|
{ |
||||||
|
$sort = new Sort(array( |
||||||
|
'attributes' => array( |
||||||
|
'age', |
||||||
|
'name' => array( |
||||||
|
'asc' => array('first_name' => Sort::ASC, 'last_name' => Sort::ASC), |
||||||
|
'desc' => array('first_name' => Sort::DESC, 'last_name' => Sort::DESC), |
||||||
|
), |
||||||
|
), |
||||||
|
'params' => array( |
||||||
|
'sort' => 'age.name-desc' |
||||||
|
), |
||||||
|
'enableMultiSort' => true, |
||||||
|
)); |
||||||
|
|
||||||
|
$orders = $sort->getOrders(); |
||||||
|
$this->assertEquals(3, count($orders)); |
||||||
|
$this->assertEquals(Sort::ASC, $orders['age']); |
||||||
|
$this->assertEquals(Sort::DESC, $orders['first_name']); |
||||||
|
$this->assertEquals(Sort::DESC, $orders['last_name']); |
||||||
|
|
||||||
|
$sort->enableMultiSort = false; |
||||||
|
$orders = $sort->getOrders(true); |
||||||
|
$this->assertEquals(1, count($orders)); |
||||||
|
$this->assertEquals(Sort::ASC, $orders['age']); |
||||||
|
} |
||||||
|
|
||||||
|
public function testGetAttributeOrders() |
||||||
|
{ |
||||||
|
$sort = new Sort(array( |
||||||
|
'attributes' => array( |
||||||
|
'age', |
||||||
|
'name' => array( |
||||||
|
'asc' => array('first_name' => Sort::ASC, 'last_name' => Sort::ASC), |
||||||
|
'desc' => array('first_name' => Sort::DESC, 'last_name' => Sort::DESC), |
||||||
|
), |
||||||
|
), |
||||||
|
'params' => array( |
||||||
|
'sort' => 'age.name-desc' |
||||||
|
), |
||||||
|
'enableMultiSort' => true, |
||||||
|
)); |
||||||
|
|
||||||
|
$orders = $sort->getAttributeOrders(); |
||||||
|
$this->assertEquals(2, count($orders)); |
||||||
|
$this->assertEquals(Sort::ASC, $orders['age']); |
||||||
|
$this->assertEquals(Sort::DESC, $orders['name']); |
||||||
|
|
||||||
|
$sort->enableMultiSort = false; |
||||||
|
$orders = $sort->getAttributeOrders(true); |
||||||
|
$this->assertEquals(1, count($orders)); |
||||||
|
$this->assertEquals(Sort::ASC, $orders['age']); |
||||||
|
} |
||||||
|
|
||||||
|
public function testGetAttributeOrder() |
||||||
|
{ |
||||||
|
$sort = new Sort(array( |
||||||
|
'attributes' => array( |
||||||
|
'age', |
||||||
|
'name' => array( |
||||||
|
'asc' => array('first_name' => Sort::ASC, 'last_name' => Sort::ASC), |
||||||
|
'desc' => array('first_name' => Sort::DESC, 'last_name' => Sort::DESC), |
||||||
|
), |
||||||
|
), |
||||||
|
'params' => array( |
||||||
|
'sort' => 'age.name-desc' |
||||||
|
), |
||||||
|
'enableMultiSort' => true, |
||||||
|
)); |
||||||
|
|
||||||
|
$this->assertEquals(Sort::ASC, $sort->getAttributeOrder('age')); |
||||||
|
$this->assertEquals(Sort::DESC, $sort->getAttributeOrder('name')); |
||||||
|
$this->assertNull($sort->getAttributeOrder('xyz')); |
||||||
|
} |
||||||
|
|
||||||
|
public function testGetAttribute() |
||||||
|
{ |
||||||
|
$sort = new Sort(array( |
||||||
|
'attributes' => array( |
||||||
|
'age', |
||||||
|
'name' => array( |
||||||
|
'asc' => array('first_name' => Sort::ASC, 'last_name' => Sort::ASC), |
||||||
|
'desc' => array('first_name' => Sort::DESC, 'last_name' => Sort::DESC), |
||||||
|
), |
||||||
|
), |
||||||
|
'params' => array( |
||||||
|
'sort' => 'age.name-desc' |
||||||
|
), |
||||||
|
'enableMultiSort' => true, |
||||||
|
)); |
||||||
|
|
||||||
|
$attribute = array('asc' => array('age' => Sort::ASC), 'desc' => array('age' => Sort::DESC)); |
||||||
|
$this->assertEquals($attribute, $sort->getAttribute('age')); |
||||||
|
$attribute = array( |
||||||
|
'asc' => array('first_name' => Sort::ASC, 'last_name' => Sort::ASC), |
||||||
|
'desc' => array('first_name' => Sort::DESC, 'last_name' => Sort::DESC), |
||||||
|
); |
||||||
|
$this->assertEquals($attribute, $sort->getAttribute('name')); |
||||||
|
$this->assertNull($sort->getAttribute('xyz')); |
||||||
|
} |
||||||
|
|
||||||
|
public function testCreateUrl() |
||||||
|
{ |
||||||
|
$manager = new UrlManager(array( |
||||||
|
'baseUrl' => '/index.php', |
||||||
|
'cache' => null, |
||||||
|
)); |
||||||
|
|
||||||
|
$sort = new Sort(array( |
||||||
|
'attributes' => array( |
||||||
|
'age', |
||||||
|
'name' => array( |
||||||
|
'asc' => array('first_name' => Sort::ASC, 'last_name' => Sort::ASC), |
||||||
|
'desc' => array('first_name' => Sort::DESC, 'last_name' => Sort::DESC), |
||||||
|
), |
||||||
|
), |
||||||
|
'params' => array( |
||||||
|
'sort' => 'age.name-desc' |
||||||
|
), |
||||||
|
'enableMultiSort' => true, |
||||||
|
'urlManager' => $manager, |
||||||
|
'route' => 'site/index', |
||||||
|
)); |
||||||
|
|
||||||
|
$this->assertEquals('/index.php?r=site/index&sort=age-desc.name-desc', $sort->createUrl('age')); |
||||||
|
$this->assertEquals('/index.php?r=site/index&sort=name.age', $sort->createUrl('name')); |
||||||
|
} |
||||||
|
|
||||||
|
public function testLink() |
||||||
|
{ |
||||||
|
$this->mockApplication(); |
||||||
|
$manager = new UrlManager(array( |
||||||
|
'baseUrl' => '/index.php', |
||||||
|
'cache' => null, |
||||||
|
)); |
||||||
|
|
||||||
|
$sort = new Sort(array( |
||||||
|
'attributes' => array( |
||||||
|
'age', |
||||||
|
'name' => array( |
||||||
|
'asc' => array('first_name' => Sort::ASC, 'last_name' => Sort::ASC), |
||||||
|
'desc' => array('first_name' => Sort::DESC, 'last_name' => Sort::DESC), |
||||||
|
), |
||||||
|
), |
||||||
|
'params' => array( |
||||||
|
'sort' => 'age.name-desc' |
||||||
|
), |
||||||
|
'enableMultiSort' => true, |
||||||
|
'urlManager' => $manager, |
||||||
|
'route' => 'site/index', |
||||||
|
)); |
||||||
|
|
||||||
|
$this->assertEquals('<a class="asc" href="/index.php?r=site/index&sort=age-desc.name-desc">Age</a>', $sort->link('age')); |
||||||
|
} |
||||||
|
} |
Loading…
Reference in new issue