SortTest.php 4.18 KB
Newer Older
Qiang Xue committed
1 2 3 4 5 6 7 8
<?php
/**
 * @link http://www.yiiframework.com/
 * @copyright Copyright (c) 2008 Yii Software LLC
 * @license http://www.yiiframework.com/license/
 */

namespace yiiunit\framework\data;
Alexander Makarov committed
9

Qiang Xue committed
10 11 12 13 14 15 16
use yii\web\UrlManager;
use yiiunit\TestCase;
use yii\data\Sort;

/**
 * @author Qiang Xue <qiang.xue@gmail.com>
 * @since 2.0
17 18
 *
 * @group data
Qiang Xue committed
19 20 21 22 23
 */
class SortTest extends TestCase
{
	public function testGetOrders()
	{
Alexander Makarov committed
24 25
		$sort = new Sort([
			'attributes' => [
Qiang Xue committed
26
				'age',
Alexander Makarov committed
27 28 29 30 31 32
				'name' => [
					'asc' => ['first_name' => Sort::ASC, 'last_name' => Sort::ASC],
					'desc' => ['first_name' => Sort::DESC, 'last_name' => Sort::DESC],
				],
			],
			'params' => [
Qiang Xue committed
33
				'sort' => 'age.name-desc'
Alexander Makarov committed
34
			],
Qiang Xue committed
35
			'enableMultiSort' => true,
Alexander Makarov committed
36
		]);
Qiang Xue committed
37 38 39 40 41 42 43 44 45 46 47 48 49 50 51

		$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()
	{
Alexander Makarov committed
52 53
		$sort = new Sort([
			'attributes' => [
Qiang Xue committed
54
				'age',
Alexander Makarov committed
55 56 57 58 59 60
				'name' => [
					'asc' => ['first_name' => Sort::ASC, 'last_name' => Sort::ASC],
					'desc' => ['first_name' => Sort::DESC, 'last_name' => Sort::DESC],
				],
			],
			'params' => [
Qiang Xue committed
61
				'sort' => 'age.name-desc'
Alexander Makarov committed
62
			],
Qiang Xue committed
63
			'enableMultiSort' => true,
Alexander Makarov committed
64
		]);
Qiang Xue committed
65 66 67 68 69 70 71 72 73 74 75 76 77 78

		$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()
	{
Alexander Makarov committed
79 80
		$sort = new Sort([
			'attributes' => [
Qiang Xue committed
81
				'age',
Alexander Makarov committed
82 83 84 85 86 87
				'name' => [
					'asc' => ['first_name' => Sort::ASC, 'last_name' => Sort::ASC],
					'desc' => ['first_name' => Sort::DESC, 'last_name' => Sort::DESC],
				],
			],
			'params' => [
Qiang Xue committed
88
				'sort' => 'age.name-desc'
Alexander Makarov committed
89
			],
Qiang Xue committed
90
			'enableMultiSort' => true,
Alexander Makarov committed
91
		]);
Qiang Xue committed
92 93 94 95 96 97

		$this->assertEquals(Sort::ASC, $sort->getAttributeOrder('age'));
		$this->assertEquals(Sort::DESC, $sort->getAttributeOrder('name'));
		$this->assertNull($sort->getAttributeOrder('xyz'));
	}

Qiang Xue committed
98 99
	public function testCreateSortVar()
	{
Alexander Makarov committed
100 101
		$sort = new Sort([
			'attributes' => [
Qiang Xue committed
102
				'age',
Alexander Makarov committed
103 104 105 106 107 108
				'name' => [
					'asc' => ['first_name' => Sort::ASC, 'last_name' => Sort::ASC],
					'desc' => ['first_name' => Sort::DESC, 'last_name' => Sort::DESC],
				],
			],
			'params' => [
Qiang Xue committed
109
				'sort' => 'age.name-desc'
Alexander Makarov committed
110
			],
Qiang Xue committed
111 112
			'enableMultiSort' => true,
			'route' => 'site/index',
Alexander Makarov committed
113
		]);
Qiang Xue committed
114 115 116 117 118

		$this->assertEquals('age-desc.name-desc', $sort->createSortVar('age'));
		$this->assertEquals('name.age', $sort->createSortVar('name'));
	}

Qiang Xue committed
119 120
	public function testCreateUrl()
	{
Alexander Makarov committed
121
		$manager = new UrlManager([
Qiang Xue committed
122 123
			'baseUrl' => '/index.php',
			'cache' => null,
Alexander Makarov committed
124
		]);
Qiang Xue committed
125

Alexander Makarov committed
126 127
		$sort = new Sort([
			'attributes' => [
Qiang Xue committed
128
				'age',
Alexander Makarov committed
129 130 131 132 133 134
				'name' => [
					'asc' => ['first_name' => Sort::ASC, 'last_name' => Sort::ASC],
					'desc' => ['first_name' => Sort::DESC, 'last_name' => Sort::DESC],
				],
			],
			'params' => [
Qiang Xue committed
135
				'sort' => 'age.name-desc'
Alexander Makarov committed
136
			],
Qiang Xue committed
137 138 139
			'enableMultiSort' => true,
			'urlManager' => $manager,
			'route' => 'site/index',
Alexander Makarov committed
140
		]);
Qiang Xue committed
141 142 143 144 145 146 147 148

		$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();
Alexander Makarov committed
149
		$manager = new UrlManager([
Qiang Xue committed
150 151
			'baseUrl' => '/index.php',
			'cache' => null,
Alexander Makarov committed
152
		]);
Qiang Xue committed
153

Alexander Makarov committed
154 155
		$sort = new Sort([
			'attributes' => [
Qiang Xue committed
156
				'age',
Alexander Makarov committed
157 158 159 160 161 162
				'name' => [
					'asc' => ['first_name' => Sort::ASC, 'last_name' => Sort::ASC],
					'desc' => ['first_name' => Sort::DESC, 'last_name' => Sort::DESC],
				],
			],
			'params' => [
Qiang Xue committed
163
				'sort' => 'age.name-desc'
Alexander Makarov committed
164
			],
Qiang Xue committed
165 166 167
			'enableMultiSort' => true,
			'urlManager' => $manager,
			'route' => 'site/index',
Alexander Makarov committed
168
		]);
Qiang Xue committed
169

Qiang Xue committed
170
		$this->assertEquals('<a class="asc" href="/index.php?r=site/index&amp;sort=age-desc.name-desc" data-sort="age-desc.name-desc">Age</a>', $sort->link('age'));
Qiang Xue committed
171 172
	}
}