Subversion Repositories SmartDukaan

Rev

Details | Last modification | View Log | RSS feed

Rev Author Line No. Line
16591 anikendra 1
<?php
2
/**
3
 * PaginatorHelperTest file
4
 *
5
 * CakePHP(tm) Tests <http://book.cakephp.org/2.0/en/development/testing.html>
6
 * Copyright (c) Cake Software Foundation, Inc. (http://cakefoundation.org)
7
 *
8
 * Licensed under The MIT License
9
 * For full copyright and license information, please see the LICENSE.txt
10
 * Redistributions of files must retain the above copyright notice
11
 *
12
 * @copyright     Copyright (c) Cake Software Foundation, Inc. (http://cakefoundation.org)
13
 * @link          http://book.cakephp.org/2.0/en/development/testing.html CakePHP(tm) Tests
14
 * @package       Cake.Test.Case.View.Helper
15
 * @since         CakePHP(tm) v 1.2.0.4206
16
 * @license       http://www.opensource.org/licenses/mit-license.php MIT License
17
 */
18
 
19
App::uses('View', 'View');
20
App::uses('HtmlHelper', 'View/Helper');
21
App::uses('JsHelper', 'View/Helper');
22
App::uses('PaginatorHelper', 'View/Helper');
23
App::uses('FormHelper', 'View/Helper');
24
 
25
if (!defined('FULL_BASE_URL')) {
26
	define('FULL_BASE_URL', 'http://cakephp.org');
27
}
28
 
29
/**
30
 * PaginatorHelperTest class
31
 *
32
 * @package       Cake.Test.Case.View.Helper
33
 */
34
class PaginatorHelperTest extends CakeTestCase {
35
 
36
/**
37
 * setUp method
38
 *
39
 * @return void
40
 */
41
	public function setUp() {
42
		parent::setUp();
43
		Configure::write('Config.language', 'eng');
44
		$controller = null;
45
		$this->View = new View($controller);
46
		$this->Paginator = new PaginatorHelper($this->View);
47
		$this->Paginator->Js = $this->getMock('PaginatorHelper', array(), array($this->View));
48
		$this->Paginator->request = new CakeRequest(null, false);
49
		$this->Paginator->request->addParams(array(
50
			'paging' => array(
51
				'Article' => array(
52
					'page' => 2,
53
					'current' => 9,
54
					'count' => 62,
55
					'prevPage' => false,
56
					'nextPage' => true,
57
					'pageCount' => 7,
58
					'order' => null,
59
					'limit' => 20,
60
					'options' => array(
61
						'page' => 1,
62
						'conditions' => array()
63
					),
64
					'paramType' => 'named'
65
				)
66
			)
67
		));
68
		$this->Paginator->Html = new HtmlHelper($this->View);
69
 
70
		Configure::write('Routing.prefixes', array());
71
		Router::reload();
72
	}
73
 
74
/**
75
 * tearDown method
76
 *
77
 * @return void
78
 */
79
	public function tearDown() {
80
		parent::tearDown();
81
		unset($this->View, $this->Paginator);
82
	}
83
 
84
/**
85
 * testHasPrevious method
86
 *
87
 * @return void
88
 */
89
	public function testHasPrevious() {
90
		$this->assertFalse($this->Paginator->hasPrev());
91
		$this->Paginator->request->params['paging']['Article']['prevPage'] = true;
92
		$this->assertTrue($this->Paginator->hasPrev());
93
		$this->Paginator->request->params['paging']['Article']['prevPage'] = false;
94
	}
95
 
96
/**
97
 * testHasNext method
98
 *
99
 * @return void
100
 */
101
	public function testHasNext() {
102
		$this->assertTrue($this->Paginator->hasNext());
103
		$this->Paginator->request->params['paging']['Article']['nextPage'] = false;
104
		$this->assertFalse($this->Paginator->hasNext());
105
		$this->Paginator->request->params['paging']['Article']['nextPage'] = true;
106
	}
107
 
108
/**
109
 * testDisabledLink method
110
 *
111
 * @return void
112
 */
113
	public function testDisabledLink() {
114
		$this->Paginator->request->params['paging']['Article']['nextPage'] = false;
115
		$this->Paginator->request->params['paging']['Article']['page'] = 1;
116
		$result = $this->Paginator->next('Next', array(), true);
117
		$expected = '<span class="next">Next</span>';
118
		$this->assertEquals($expected, $result);
119
 
120
		$this->Paginator->request->params['paging']['Article']['prevPage'] = false;
121
		$result = $this->Paginator->prev('prev', array('update' => 'theList', 'indicator' => 'loading', 'url' => array('controller' => 'posts')), null, array('class' => 'disabled', 'tag' => 'span'));
122
		$expected = array(
123
			'span' => array('class' => 'disabled'), 'prev', '/span'
124
		);
125
		$this->assertTags($result, $expected);
126
	}
127
 
128
/**
129
 * testSortLinks method
130
 *
131
 * @return void
132
 */
133
	public function testSortLinks() {
134
		Router::reload();
135
		Router::parse('/');
136
		Router::setRequestInfo(array(
137
			array('plugin' => null, 'controller' => 'accounts', 'action' => 'index', 'pass' => array(), 'url' => array('url' => 'accounts/')),
138
			array('base' => '/officespace', 'here' => '/officespace/accounts/', 'webroot' => '/officespace/')
139
		));
140
		$this->Paginator->options(array('url' => array('param')));
141
		$this->Paginator->request['paging'] = array(
142
			'Article' => array(
143
				'current' => 9,
144
				'count' => 62,
145
				'prevPage' => false,
146
				'nextPage' => true,
147
				'pageCount' => 7,
148
				'options' => array(
149
					'page' => 1,
150
					'order' => array('date' => 'asc'),
151
					'conditions' => array()
152
				),
153
				'paramType' => 'named'
154
			)
155
		);
156
 
157
		$result = $this->Paginator->sort('title');
158
		$expected = array(
159
			'a' => array('href' => '/officespace/accounts/index/param/sort:title/direction:asc'),
160
			'Title',
161
			'/a'
162
		);
163
		$this->assertTags($result, $expected);
164
 
165
		$result = $this->Paginator->sort('date');
166
		$expected = array(
167
			'a' => array('href' => '/officespace/accounts/index/param/sort:date/direction:desc', 'class' => 'asc'),
168
			'Date',
169
			'/a'
170
		);
171
		$this->assertTags($result, $expected);
172
 
173
		$result = $this->Paginator->sort('title', 'TestTitle');
174
		$expected = array(
175
			'a' => array('href' => '/officespace/accounts/index/param/sort:title/direction:asc'),
176
			'TestTitle',
177
			'/a'
178
		);
179
		$this->assertTags($result, $expected);
180
 
181
		$result = $this->Paginator->sort('title', array('asc' => 'ascending', 'desc' => 'descending'));
182
		$expected = array(
183
			'a' => array('href' => '/officespace/accounts/index/param/sort:title/direction:asc'),
184
			'ascending',
185
			'/a'
186
		);
187
		$this->assertTags($result, $expected);
188
 
189
		$this->Paginator->request->params['paging']['Article']['options']['sort'] = 'title';
190
		$result = $this->Paginator->sort('title', array('asc' => 'ascending', 'desc' => 'descending'));
191
		$expected = array(
192
			'a' => array('href' => '/officespace/accounts/index/param/sort:title/direction:desc', 'class' => 'asc'),
193
			'descending',
194
			'/a'
195
		);
196
		$this->assertTags($result, $expected);
197
 
198
		$this->Paginator->request->params['paging']['Article']['options']['order'] = array('Article.title' => 'desc');
199
		$this->Paginator->request->params['paging']['Article']['options']['sort'] = null;
200
		$result = $this->Paginator->sort('title');
201
		$this->assertRegExp('/\/accounts\/index\/param\/sort:title\/direction:asc" class="desc">Title<\/a>$/', $result);
202
 
203
		$this->Paginator->request->params['paging']['Article']['options']['order'] = array('Article.title' => 'asc');
204
		$this->Paginator->request->params['paging']['Article']['options']['sort'] = null;
205
		$result = $this->Paginator->sort('title');
206
		$this->assertRegExp('/\/accounts\/index\/param\/sort:title\/direction:desc" class="asc">Title<\/a>$/', $result);
207
 
208
		$this->Paginator->request->params['paging']['Article']['options']['order'] = array('Article.title' => 'desc');
209
		$this->Paginator->request->params['paging']['Article']['options']['sort'] = null;
210
		$result = $this->Paginator->sort('title', 'Title', array('direction' => 'desc'));
211
		$this->assertRegExp('/\/accounts\/index\/param\/sort:title\/direction:asc" class="desc">Title<\/a>$/', $result);
212
 
213
		$this->Paginator->request->params['paging']['Article']['options']['order'] = array('Article.title' => 'desc');
214
		$this->Paginator->request->params['paging']['Article']['options']['sort'] = null;
215
		$result = $this->Paginator->sort('title', 'Title', array('direction' => 'asc'));
216
		$this->assertRegExp('/\/accounts\/index\/param\/sort:title\/direction:asc" class="desc">Title<\/a>$/', $result);
217
 
218
		$this->Paginator->request->params['paging']['Article']['options']['order'] = array('Article.title' => 'asc');
219
		$this->Paginator->request->params['paging']['Article']['options']['sort'] = null;
220
		$result = $this->Paginator->sort('title', 'Title', array('direction' => 'asc'));
221
		$this->assertRegExp('/\/accounts\/index\/param\/sort:title\/direction:desc" class="asc">Title<\/a>$/', $result);
222
 
223
		$this->Paginator->request->params['paging']['Article']['options']['order'] = array('Article.title' => 'asc');
224
		$this->Paginator->request->params['paging']['Article']['options']['sort'] = null;
225
		$result = $this->Paginator->sort('title', 'Title', array('direction' => 'desc'));
226
		$this->assertRegExp('/\/accounts\/index\/param\/sort:title\/direction:desc" class="asc">Title<\/a>$/', $result);
227
 
228
		$this->Paginator->request->params['paging']['Article']['options']['order'] = array('Article.title' => 'asc');
229
		$this->Paginator->request->params['paging']['Article']['options']['sort'] = null;
230
		$result = $this->Paginator->sort('title', 'Title', array('direction' => 'desc', 'class' => 'foo'));
231
		$this->assertRegExp('/\/accounts\/index\/param\/sort:title\/direction:desc" class="foo asc">Title<\/a>$/', $result);
232
	}
233
 
234
/**
235
 * testSortLinksWithLockOption method
236
 *
237
 * @return void
238
 */
239
	public function testSortLinksWithLockOption() {
240
		Router::reload();
241
		Router::parse('/');
242
		Router::setRequestInfo(array(
243
			array('plugin' => null, 'controller' => 'accounts', 'action' => 'index', 'pass' => array(), 'url' => array('url' => 'accounts/')),
244
			array('base' => '/officespace', 'here' => '/officespace/accounts/', 'webroot' => '/officespace/')
245
		));
246
		$this->Paginator->options(array('url' => array('param')));
247
		$this->Paginator->request['paging'] = array(
248
			'Article' => array(
249
				'current' => 9,
250
				'count' => 62,
251
				'prevPage' => false,
252
				'nextPage' => true,
253
				'pageCount' => 7,
254
				'options' => array(
255
					'page' => 1,
256
					'order' => array('date' => 'asc'),
257
					'conditions' => array()
258
				),
259
				'paramType' => 'named'
260
			)
261
		);
262
 
263
		$result = $this->Paginator->sort('distance', null, array('lock' => true));
264
		$expected = array(
265
			'a' => array('href' => '/officespace/accounts/index/param/sort:distance/direction:asc'),
266
			'Distance',
267
			'/a'
268
		);
269
		$this->assertTags($result, $expected);
270
 
271
		$this->Paginator->request->params['paging']['Article']['options']['sort'] = 'distance';
272
		$result = $this->Paginator->sort('distance', null, array('lock' => true));
273
		$expected = array(
274
			'a' => array('href' => '/officespace/accounts/index/param/sort:distance/direction:asc', 'class' => 'asc locked'),
275
			'Distance',
276
			'/a'
277
		);
278
		$this->assertTags($result, $expected);
279
	}
280
 
281
/**
282
 * test that sort() works with virtual field order options.
283
 *
284
 * @return void
285
 */
286
	public function testSortLinkWithVirtualField() {
287
		Router::setRequestInfo(array(
288
			array('plugin' => null, 'controller' => 'accounts', 'action' => 'index', 'pass' => array(), 'form' => array(), 'url' => array('url' => 'accounts/')),
289
			array('base' => '', 'here' => '/accounts/', 'webroot' => '/')
290
		));
291
		$this->Paginator->request->params['paging']['Article']['options']['order'] = array('full_name' => 'asc');
292
 
293
		$result = $this->Paginator->sort('Article.full_name');
294
		$expected = array(
295
			'a' => array('href' => '/accounts/index/sort:Article.full_name/direction:desc', 'class' => 'asc'),
296
			'Article Full Name',
297
			'/a'
298
		);
299
		$this->assertTags($result, $expected);
300
 
301
		$result = $this->Paginator->sort('full_name');
302
		$expected = array(
303
			'a' => array('href' => '/accounts/index/sort:full_name/direction:desc', 'class' => 'asc'),
304
			'Full Name',
305
			'/a'
306
		);
307
		$this->assertTags($result, $expected);
308
 
309
		$this->Paginator->request->params['paging']['Article']['options']['order'] = array('full_name' => 'desc');
310
		$result = $this->Paginator->sort('Article.full_name');
311
		$expected = array(
312
			'a' => array('href' => '/accounts/index/sort:Article.full_name/direction:asc', 'class' => 'desc'),
313
			'Article Full Name',
314
			'/a'
315
		);
316
		$this->assertTags($result, $expected);
317
 
318
		$result = $this->Paginator->sort('full_name');
319
		$expected = array(
320
			'a' => array('href' => '/accounts/index/sort:full_name/direction:asc', 'class' => 'desc'),
321
			'Full Name',
322
			'/a'
323
		);
324
		$this->assertTags($result, $expected);
325
	}
326
 
327
/**
328
 * testSortLinksUsingDirectionOption method
329
 *
330
 * @return void
331
 */
332
	public function testSortLinksUsingDirectionOption() {
333
		Router::reload();
334
		Router::parse('/');
335
		Router::setRequestInfo(array(
336
			array('plugin' => null, 'controller' => 'accounts', 'action' => 'index', 'pass' => array(),
337
				'url' => array('url' => 'accounts/', 'mod_rewrite' => 'true')),
338
			array('base' => '/', 'here' => '/accounts/', 'webroot' => '/')
339
		));
340
		$this->Paginator->options(array('url' => array('param')));
341
 
342
		$result = $this->Paginator->sort('title', 'TestTitle', array('direction' => 'desc'));
343
		$expected = array(
344
			'a' => array('href' => '/accounts/index/param/sort:title/direction:desc'),
345
			'TestTitle',
346
			'/a'
347
		);
348
		$this->assertTags($result, $expected);
349
 
350
		$result = $this->Paginator->sort('title', array('asc' => 'ascending', 'desc' => 'descending'), array('direction' => 'desc'));
351
		$expected = array(
352
			'a' => array('href' => '/accounts/index/param/sort:title/direction:desc'),
353
			'descending',
354
			'/a'
355
		);
356
		$this->assertTags($result, $expected);
357
	}
358
 
359
/**
360
 * testSortLinksUsingDotNotation method
361
 *
362
 * @return void
363
 */
364
	public function testSortLinksUsingDotNotation() {
365
		Router::reload();
366
		Router::parse('/');
367
		Router::setRequestInfo(array(
368
			array('plugin' => null, 'controller' => 'accounts', 'action' => 'index', 'pass' => array(), 'form' => array(), 'url' => array('url' => 'accounts/', 'mod_rewrite' => 'true'), 'bare' => 0),
369
			array('base' => '/officespace', 'here' => '/officespace/accounts/', 'webroot' => '/officespace/')
370
		));
371
 
372
		$this->Paginator->request->params['paging']['Article']['options']['order'] = array('Article.title' => 'desc');
373
		$result = $this->Paginator->sort('Article.title');
374
		$expected = array(
375
			'a' => array('href' => '/officespace/accounts/index/sort:Article.title/direction:asc', 'class' => 'desc'),
376
			'Article Title',
377
			'/a'
378
		);
379
		$this->assertTags($result, $expected);
380
 
381
		$this->Paginator->request->params['paging']['Article']['options']['order'] = array('Article.title' => 'desc');
382
		$result = $this->Paginator->sort('Article.title', 'Title');
383
		$expected = array(
384
			'a' => array('href' => '/officespace/accounts/index/sort:Article.title/direction:asc', 'class' => 'desc'),
385
			'Title',
386
			'/a'
387
		);
388
		$this->assertTags($result, $expected);
389
 
390
		$this->Paginator->request->params['paging']['Article']['options']['order'] = array('Article.title' => 'asc');
391
		$result = $this->Paginator->sort('Article.title', 'Title');
392
		$expected = array(
393
			'a' => array('href' => '/officespace/accounts/index/sort:Article.title/direction:desc', 'class' => 'asc'),
394
			'Title',
395
			'/a'
396
		);
397
		$this->assertTags($result, $expected);
398
 
399
		$this->Paginator->request->params['paging']['Article']['options']['order'] = array('Account.title' => 'asc');
400
		$result = $this->Paginator->sort('title');
401
		$expected = array(
402
			'a' => array('href' => '/officespace/accounts/index/sort:title/direction:asc'),
403
			'Title',
404
			'/a'
405
		);
406
		$this->assertTags($result, $expected);
407
	}
408
 
409
/**
410
 * testSortKey method
411
 *
412
 * @return void
413
 */
414
	public function testSortKey() {
415
		$result = $this->Paginator->sortKey(null, array(
416
			'order' => array('Article.title' => 'desc'
417
		)));
418
		$this->assertEquals('Article.title', $result);
419
 
420
		$result = $this->Paginator->sortKey('Article', array('order' => 'Article.title'));
421
		$this->assertEquals('Article.title', $result);
422
 
423
		$result = $this->Paginator->sortKey('Article', array('sort' => 'Article.title'));
424
		$this->assertEquals('Article.title', $result);
425
 
426
		$result = $this->Paginator->sortKey('Article', array('sort' => 'Article'));
427
		$this->assertEquals('Article', $result);
428
	}
429
 
430
/**
431
 * Test that sortKey falls back to the default sorting options set
432
 * in the $params which are the default pagination options.
433
 *
434
 * @return void
435
 */
436
	public function testSortKeyFallbackToParams() {
437
		$this->Paginator->request->params['paging']['Article']['order'] = 'Article.body';
438
		$result = $this->Paginator->sortKey();
439
		$this->assertEquals('Article.body', $result);
440
 
441
		$result = $this->Paginator->sortKey('Article');
442
		$this->assertEquals('Article.body', $result);
443
 
444
		$this->Paginator->request->params['paging']['Article']['order'] = array(
445
			'Article.body' => 'DESC'
446
		);
447
		$result = $this->Paginator->sortKey();
448
		$this->assertEquals('Article.body', $result);
449
 
450
		$result = $this->Paginator->sortKey('Article');
451
		$this->assertEquals('Article.body', $result);
452
	}
453
 
454
/**
455
 * testSortDir method
456
 *
457
 * @return void
458
 */
459
	public function testSortDir() {
460
		$result = $this->Paginator->sortDir();
461
		$expected = 'asc';
462
 
463
		$this->assertEquals($expected, $result);
464
 
465
		$this->Paginator->request->params['paging']['Article']['options']['order'] = array('Article.title' => 'desc');
466
		$result = $this->Paginator->sortDir();
467
		$expected = 'desc';
468
 
469
		$this->assertEquals($expected, $result);
470
 
471
		unset($this->Paginator->request->params['paging']['Article']['options']);
472
		$this->Paginator->request->params['paging']['Article']['options']['order'] = array('Article.title' => 'asc');
473
		$result = $this->Paginator->sortDir();
474
		$expected = 'asc';
475
 
476
		$this->assertEquals($expected, $result);
477
 
478
		unset($this->Paginator->request->params['paging']['Article']['options']);
479
		$this->Paginator->request->params['paging']['Article']['options']['order'] = array('title' => 'desc');
480
		$result = $this->Paginator->sortDir();
481
		$expected = 'desc';
482
 
483
		$this->assertEquals($expected, $result);
484
 
485
		unset($this->Paginator->request->params['paging']['Article']['options']);
486
		$this->Paginator->request->params['paging']['Article']['options']['order'] = array('title' => 'asc');
487
		$result = $this->Paginator->sortDir();
488
		$expected = 'asc';
489
 
490
		$this->assertEquals($expected, $result);
491
 
492
		unset($this->Paginator->request->params['paging']['Article']['options']);
493
		$this->Paginator->request->params['paging']['Article']['options']['direction'] = 'asc';
494
		$result = $this->Paginator->sortDir();
495
		$expected = 'asc';
496
 
497
		$this->assertEquals($expected, $result);
498
 
499
		unset($this->Paginator->request->params['paging']['Article']['options']);
500
		$this->Paginator->request->params['paging']['Article']['options']['direction'] = 'desc';
501
		$result = $this->Paginator->sortDir();
502
		$expected = 'desc';
503
 
504
		$this->assertEquals($expected, $result);
505
 
506
		unset($this->Paginator->request->params['paging']['Article']['options']);
507
		$result = $this->Paginator->sortDir('Article', array('direction' => 'asc'));
508
		$expected = 'asc';
509
 
510
		$this->assertEquals($expected, $result);
511
 
512
		$result = $this->Paginator->sortDir('Article', array('direction' => 'desc'));
513
		$expected = 'desc';
514
 
515
		$this->assertEquals($expected, $result);
516
 
517
		$result = $this->Paginator->sortDir('Article', array('direction' => 'asc'));
518
		$expected = 'asc';
519
 
520
		$this->assertEquals($expected, $result);
521
	}
522
 
523
/**
524
 * Test that sortDir falls back to the default sorting options set
525
 * in the $params which are the default pagination options.
526
 *
527
 * @return void
528
 */
529
	public function testSortDirFallbackToParams() {
530
		$this->Paginator->request->params['paging']['Article']['order'] = array(
531
			'Article.body' => 'ASC'
532
		);
533
		$result = $this->Paginator->sortDir();
534
		$this->assertEquals('asc', $result);
535
 
536
		$result = $this->Paginator->sortDir('Article');
537
		$this->assertEquals('asc', $result);
538
 
539
		$this->Paginator->request->params['paging']['Article']['order'] = array(
540
			'Article.body' => 'DESC'
541
		);
542
		$result = $this->Paginator->sortDir();
543
		$this->assertEquals('desc', $result);
544
 
545
		$result = $this->Paginator->sortDir('Article');
546
		$this->assertEquals('desc', $result);
547
	}
548
 
549
/**
550
 * testSortAdminLinks method
551
 *
552
 * @return void
553
 */
554
	public function testSortAdminLinks() {
555
		Configure::write('Routing.prefixes', array('admin'));
556
 
557
		Router::reload();
558
		Router::setRequestInfo(array(
559
			array('pass' => array(), 'named' => array(), 'controller' => 'users', 'plugin' => null, 'action' => 'admin_index', 'prefix' => 'admin', 'admin' => true, 'url' => array('ext' => 'html', 'url' => 'admin/users')),
560
			array('base' => '', 'here' => '/admin/users', 'webroot' => '/')
561
		));
562
		Router::parse('/admin/users');
563
		$this->Paginator->request->params['paging']['Article']['page'] = 1;
564
		$result = $this->Paginator->next('Next');
565
		$expected = array(
566
			'span' => array('class' => 'next'),
567
			'a' => array('href' => '/admin/users/index/page:2', 'rel' => 'next'),
568
			'Next',
569
			'/a',
570
			'/span'
571
		);
572
		$this->assertTags($result, $expected);
573
 
574
		Router::reload();
575
		Router::setRequestInfo(array(
576
			array('plugin' => null, 'controller' => 'test', 'action' => 'admin_index', 'pass' => array(), 'prefix' => 'admin', 'admin' => true, 'url' => array('url' => 'admin/test')),
577
			array('base' => '', 'here' => '/admin/test', 'webroot' => '/')
578
		));
579
		Router::parse('/');
580
		$this->Paginator->options(array('url' => array('param')));
581
		$result = $this->Paginator->sort('title');
582
		$expected = array(
583
			'a' => array('href' => '/admin/test/index/param/sort:title/direction:asc'),
584
			'Title',
585
			'/a'
586
		);
587
		$this->assertTags($result, $expected);
588
 
589
		$this->Paginator->options(array('url' => array('param')));
590
		$result = $this->Paginator->sort('Article.title', 'Title');
591
		$expected = array(
592
			'a' => array('href' => '/admin/test/index/param/sort:Article.title/direction:asc'),
593
			'Title',
594
			'/a'
595
		);
596
		$this->assertTags($result, $expected);
597
	}
598
 
599
/**
600
 * testUrlGeneration method
601
 *
602
 * @return void
603
 */
604
	public function testUrlGeneration() {
605
		$result = $this->Paginator->sort('controller');
606
		$expected = array(
607
			'a' => array('href' => '/index/sort:controller/direction:asc'),
608
			'Controller',
609
			'/a'
610
		);
611
		$this->assertTags($result, $expected);
612
 
613
		$result = $this->Paginator->url();
614
		$this->assertEquals('/', $result);
615
 
616
		$this->Paginator->request->params['paging']['Article']['options']['page'] = 2;
617
		$result = $this->Paginator->url();
618
		$this->assertEquals('/index/page:2', $result);
619
 
620
		$options = array('order' => array('Article' => 'desc'));
621
		$result = $this->Paginator->url($options);
622
		$this->assertEquals('/index/page:2/sort:Article/direction:desc', $result);
623
 
624
		$this->Paginator->request->params['paging']['Article']['options']['page'] = 3;
625
		$options = array('order' => array('Article.name' => 'desc'));
626
		$result = $this->Paginator->url($options);
627
		$this->assertEquals('/index/page:3/sort:Article.name/direction:desc', $result);
628
	}
629
 
630
/**
631
 * test URL generation with prefix routes
632
 *
633
 * @return void
634
 */
635
	public function testUrlGenerationWithPrefixes() {
636
		Configure::write('Routing.prefixes', array('members'));
637
		Router::reload();
638
 
639
		Router::parse('/');
640
 
641
		Router::setRequestInfo(array(
642
			array('controller' => 'posts', 'action' => 'index', 'form' => array(), 'url' => array(), 'plugin' => null),
643
			array('base' => '', 'here' => 'posts/index', 'webroot' => '/')
644
		));
645
 
646
		$this->Paginator->request->params['paging']['Article']['options']['page'] = 2;
647
		$this->Paginator->request->params['paging']['Article']['page'] = 2;
648
		$this->Paginator->request->params['paging']['Article']['prevPage'] = true;
649
		$options = array('members' => true);
650
 
651
		$result = $this->Paginator->url($options);
652
		$expected = '/members/posts/index/page:2';
653
		$this->assertEquals($expected, $result);
654
 
655
		$result = $this->Paginator->sort('name', null, array('url' => $options));
656
		$expected = array(
657
			'a' => array('href' => '/members/posts/index/page:2/sort:name/direction:asc'),
658
			'Name',
659
			'/a'
660
		);
661
		$this->assertTags($result, $expected);
662
 
663
		$result = $this->Paginator->next('next', array('url' => $options));
664
		$expected = array(
665
			'span' => array('class' => 'next'),
666
			'a' => array('href' => '/members/posts/index/page:3', 'rel' => 'next'),
667
			'next',
668
			'/a',
669
			'/span'
670
		);
671
		$this->assertTags($result, $expected);
672
 
673
		$result = $this->Paginator->prev('prev', array('url' => $options));
674
		$expected = array(
675
			'span' => array('class' => 'prev'),
676
			'a' => array('href' => '/members/posts', 'rel' => 'prev'),
677
			'prev',
678
			'/a',
679
			'/span'
680
		);
681
		$this->assertTags($result, $expected);
682
 
683
		$options = array('members' => true, 'controller' => 'posts', 'order' => array('name' => 'desc'));
684
		$result = $this->Paginator->url($options);
685
		$expected = '/members/posts/index/page:2/sort:name/direction:desc';
686
		$this->assertEquals($expected, $result);
687
 
688
		$options = array('controller' => 'posts', 'order' => array('Article.name' => 'desc'));
689
		$result = $this->Paginator->url($options);
690
		$expected = '/posts/index/page:2/sort:Article.name/direction:desc';
691
		$this->assertEquals($expected, $result);
692
	}
693
 
694
/**
695
 * testOptions method
696
 *
697
 * @return void
698
 */
699
	public function testOptions() {
700
		$this->Paginator->options('myDiv');
701
		$this->assertEquals('myDiv', $this->Paginator->options['update']);
702
 
703
		$this->Paginator->options = array();
704
		$this->Paginator->request->params = array();
705
 
706
		$options = array('paging' => array('Article' => array(
707
			'order' => 'desc',
708
			'sort' => 'title'
709
		)));
710
		$this->Paginator->options($options);
711
 
712
		$expected = array('Article' => array(
713
			'order' => 'desc',
714
			'sort' => 'title'
715
		));
716
		$this->assertEquals($expected, $this->Paginator->request->params['paging']);
717
 
718
		$this->Paginator->options = array();
719
		$this->Paginator->request->params = array();
720
 
721
		$options = array('Article' => array(
722
			'order' => 'desc',
723
			'sort' => 'title'
724
		));
725
		$this->Paginator->options($options);
726
		$this->assertEquals($expected, $this->Paginator->request->params['paging']);
727
 
728
		$options = array('paging' => array('Article' => array(
729
			'order' => 'desc',
730
			'sort' => 'Article.title'
731
		)));
732
		$this->Paginator->options($options);
733
 
734
		$expected = array('Article' => array(
735
			'order' => 'desc',
736
			'sort' => 'Article.title'
737
		));
738
		$this->assertEquals($expected, $this->Paginator->request->params['paging']);
739
	}
740
 
741
/**
742
 * testPassedArgsMergingWithUrlOptions method
743
 *
744
 * @return void
745
 */
746
	public function testPassedArgsMergingWithUrlOptions() {
747
		Router::reload();
748
		Router::parse('/');
749
		Router::setRequestInfo(array(
750
			array('plugin' => null, 'controller' => 'articles', 'action' => 'index', 'pass' => array('2'), 'named' => array('foo' => 'bar'), 'url' => array('url' => 'articles/index/2/foo:bar')),
751
			array('base' => '/', 'here' => '/articles/', 'webroot' => '/')
752
		));
753
		$this->Paginator->request->params['paging'] = array(
754
			'Article' => array(
755
				'page' => 1, 'current' => 3, 'count' => 13,
756
				'prevPage' => false, 'nextPage' => true, 'pageCount' => 8,
757
				'options' => array(
758
					'page' => 1,
759
					'order' => array(),
760
					'conditions' => array()
761
				),
762
				'paramType' => 'named'
763
			)
764
		);
765
 
766
		$this->Paginator->request->params['pass'] = array(2);
767
		$this->Paginator->request->params['named'] = array('foo' => 'bar');
768
		$this->Paginator->request->query = array('x' => 'y');
769
		$this->Paginator->beforeRender('posts/index');
770
 
771
		$result = $this->Paginator->sort('title');
772
		$expected = array(
773
			'a' => array('href' => '/articles/index/2/foo:bar/sort:title/direction:asc?x=y'),
774
			'Title',
775
			'/a'
776
		);
777
		$this->assertTags($result, $expected);
778
 
779
		$result = $this->Paginator->numbers();
780
		$expected = array(
781
			array('span' => array('class' => 'current')), '1', '/span',
782
			' | ',
783
			array('span' => array()), array('a' => array('href' => '/articles/index/2/page:2/foo:bar?x=y')), '2', '/a', '/span',
784
			' | ',
785
			array('span' => array()), array('a' => array('href' => '/articles/index/2/page:3/foo:bar?x=y')), '3', '/a', '/span',
786
			' | ',
787
			array('span' => array()), array('a' => array('href' => '/articles/index/2/page:4/foo:bar?x=y')), '4', '/a', '/span',
788
			' | ',
789
			array('span' => array()), array('a' => array('href' => '/articles/index/2/page:5/foo:bar?x=y')), '5', '/a', '/span',
790
			' | ',
791
			array('span' => array()), array('a' => array('href' => '/articles/index/2/page:6/foo:bar?x=y')), '6', '/a', '/span',
792
			' | ',
793
			array('span' => array()), array('a' => array('href' => '/articles/index/2/page:7/foo:bar?x=y')), '7', '/a', '/span',
794
		);
795
		$this->assertTags($result, $expected);
796
 
797
		$result = $this->Paginator->next('Next');
798
		$expected = array(
799
			'span' => array('class' => 'next'),
800
			'a' => array('href' => '/articles/index/2/page:2/foo:bar?x=y', 'rel' => 'next'),
801
			'Next',
802
			'/a',
803
			'/span'
804
		);
805
		$this->assertTags($result, $expected);
806
	}
807
 
808
/**
809
 * testPassedArgsMergingWithUrlOptionsParamTypeQuerystring method
810
 *
811
 * @return void
812
 */
813
	public function testPassedArgsMergingWithUrlOptionsParamTypeQuerystring() {
814
		Router::reload();
815
		Router::parse('/');
816
		Router::setRequestInfo(array(
817
			array('plugin' => null, 'controller' => 'articles', 'action' => 'index', 'pass' => array('2'), 'named' => array('foo' => 'bar'), 'url' => array('url' => 'articles/index/2/foo:bar')),
818
			array('base' => '/', 'here' => '/articles/', 'webroot' => '/')
819
		));
820
		$this->Paginator->request->params['paging'] = array(
821
			'Article' => array(
822
				'page' => 1, 'current' => 3, 'count' => 13,
823
				'prevPage' => false, 'nextPage' => true, 'pageCount' => 8,
824
				'options' => array(
825
					'page' => 1,
826
					'order' => array(),
827
					'conditions' => array()
828
				),
829
				'paramType' => 'querystring'
830
			)
831
		);
832
 
833
		$this->Paginator->request->params['pass'] = array(2);
834
		$this->Paginator->request->params['named'] = array('foo' => 'bar');
835
		$this->Paginator->request->query = array('x' => 'y');
836
		$this->Paginator->beforeRender('posts/index');
837
 
838
		$result = $this->Paginator->sort('title');
839
		$expected = array(
840
			'a' => array('href' => '/articles/index/2/foo:bar?x=y&amp;sort=title&amp;direction=asc'),
841
			'Title',
842
			'/a'
843
		);
844
		$this->assertTags($result, $expected);
845
 
846
		$result = $this->Paginator->numbers();
847
		$expected = array(
848
			array('span' => array('class' => 'current')), '1', '/span',
849
			' | ',
850
			array('span' => array()), array('a' => array('href' => '/articles/index/2/foo:bar?x=y&amp;page=2')), '2', '/a', '/span',
851
			' | ',
852
			array('span' => array()), array('a' => array('href' => '/articles/index/2/foo:bar?x=y&amp;page=3')), '3', '/a', '/span',
853
			' | ',
854
			array('span' => array()), array('a' => array('href' => '/articles/index/2/foo:bar?x=y&amp;page=4')), '4', '/a', '/span',
855
			' | ',
856
			array('span' => array()), array('a' => array('href' => '/articles/index/2/foo:bar?x=y&amp;page=5')), '5', '/a', '/span',
857
			' | ',
858
			array('span' => array()), array('a' => array('href' => '/articles/index/2/foo:bar?x=y&amp;page=6')), '6', '/a', '/span',
859
			' | ',
860
			array('span' => array()), array('a' => array('href' => '/articles/index/2/foo:bar?x=y&amp;page=7')), '7', '/a', '/span',
861
		);
862
		$this->assertTags($result, $expected);
863
 
864
		$result = $this->Paginator->next('Next');
865
		$expected = array(
866
			'span' => array('class' => 'next'),
867
			'a' => array('href' => '/articles/index/2/foo:bar?x=y&amp;page=2', 'rel' => 'next'),
868
			'Next',
869
			'/a',
870
			'/span'
871
		);
872
		$this->assertTags($result, $expected);
873
	}
874
 
875
/**
876
 * testPagingLinks method
877
 *
878
 * @return void
879
 */
880
	public function testPagingLinks() {
881
		$this->Paginator->request->params['paging'] = array(
882
			'Client' => array(
883
				'page' => 1,
884
				'current' => 3,
885
				'count' => 13,
886
				'prevPage' => false,
887
				'nextPage' => true,
888
				'pageCount' => 5,
889
				'options' => array(
890
					'page' => 1,
891
				),
892
				'paramType' => 'named'
893
			)
894
		);
895
		$result = $this->Paginator->prev('<< Previous', null, null, array('class' => 'disabled'));
896
		$expected = array(
897
			'span' => array('class' => 'disabled'),
898
			'&lt;&lt; Previous',
899
			'/span'
900
		);
901
		$this->assertTags($result, $expected);
902
 
903
		$result = $this->Paginator->prev('<< Previous', null, null, array('class' => 'disabled', 'tag' => 'div'));
904
		$expected = array(
905
			'div' => array('class' => 'disabled'),
906
			'&lt;&lt; Previous',
907
			'/div'
908
		);
909
		$this->assertTags($result, $expected);
910
 
911
		$this->Paginator->request->params['paging']['Client']['page'] = 2;
912
		$this->Paginator->request->params['paging']['Client']['prevPage'] = true;
913
		$result = $this->Paginator->prev('<< Previous', null, null, array('class' => 'disabled'));
914
		$expected = array(
915
			'span' => array('class' => 'prev'),
916
			'a' => array('href' => '/', 'rel' => 'prev'),
917
			'&lt;&lt; Previous',
918
			'/a',
919
			'/span'
920
		);
921
		$this->assertTags($result, $expected);
922
 
923
		$result = $this->Paginator->prev('<< Previous', array('tag' => false), null, array('class' => 'disabled'));
924
		$expected = array(
925
			'a' => array('href' => '/', 'rel' => 'prev', 'class' => 'prev'),
926
			'&lt;&lt; Previous',
927
			'/a'
928
		);
929
		$this->assertTags($result, $expected);
930
 
931
		$result = $this->Paginator->prev(
932
			'<< Previous',
933
			array(),
934
			null,
935
			array('disabledTag' => 'span', 'class' => 'disabled')
936
		);
937
		$expected = array(
938
			'span' => array('class' => 'prev'),
939
			'a' => array('href' => '/', 'rel' => 'prev'),
940
			'&lt;&lt; Previous',
941
			'/a',
942
			'/span'
943
		);
944
		$this->assertTags($result, $expected);
945
 
946
		$result = $this->Paginator->next('Next');
947
		$expected = array(
948
			'span' => array('class' => 'next'),
949
			'a' => array('href' => '/index/page:3', 'rel' => 'next'),
950
			'Next',
951
			'/a',
952
			'/span'
953
		);
954
		$this->assertTags($result, $expected);
955
 
956
		$result = $this->Paginator->next('Next', array('tag' => 'li'));
957
		$expected = array(
958
			'li' => array('class' => 'next'),
959
			'a' => array('href' => '/index/page:3', 'rel' => 'next'),
960
			'Next',
961
			'/a',
962
			'/li'
963
		);
964
		$this->assertTags($result, $expected);
965
 
966
		$result = $this->Paginator->next('Next', array('tag' => false));
967
		$expected = array(
968
			'a' => array('href' => '/index/page:3', 'rel' => 'next', 'class' => 'next'),
969
			'Next',
970
			'/a'
971
		);
972
		$this->assertTags($result, $expected);
973
 
974
		$result = $this->Paginator->prev('<< Previous', array('escape' => true));
975
		$expected = array(
976
			'span' => array('class' => 'prev'),
977
			'a' => array('href' => '/', 'rel' => 'prev'),
978
			'&lt;&lt; Previous',
979
			'/a',
980
			'/span'
981
		);
982
		$this->assertTags($result, $expected);
983
 
984
		$result = $this->Paginator->prev('<< Previous', array('escape' => false));
985
		$expected = array(
986
			'span' => array('class' => 'prev'),
987
			'a' => array('href' => '/', 'rel' => 'prev'),
988
			'preg:/<< Previous/',
989
			'/a',
990
			'/span'
991
		);
992
		$this->assertTags($result, $expected);
993
 
994
		$this->Paginator->request->params['paging'] = array(
995
			'Client' => array(
996
				'page' => 1,
997
				'current' => 1,
998
				'count' => 13,
999
				'prevPage' => false,
1000
				'nextPage' => true,
1001
				'pageCount' => 5,
1002
				'options' => array(
1003
					'page' => 1,
1004
				),
1005
				'paramType' => 'named'
1006
			)
1007
		);
1008
 
1009
		$result = $this->Paginator->prev('<i class="fa fa-angle-left"></i>', array('escape' => false), null, array('class' => 'prev disabled'));
1010
		$expected = array(
1011
			'span' => array('class' => 'prev disabled'),
1012
			'i' => array('class' => 'fa fa-angle-left'),
1013
			'/i',
1014
			'/span'
1015
		);
1016
		$this->assertTags($result, $expected);
1017
 
1018
		$result = $this->Paginator->prev('<i class="fa fa-angle-left"></i>', array('escape' => false), null, array('escape' => true));
1019
		$expected = array(
1020
			'span' => array('class' => 'prev'),
1021
			'&lt;i class=&quot;fa fa-angle-left&quot;&gt;&lt;/i&gt;',
1022
			'/span'
1023
		);
1024
		$this->assertTags($result, $expected);
1025
 
1026
		$result = $this->Paginator->prev('<< Previous', null, '<strong>Disabled</strong>');
1027
		$expected = array(
1028
			'span' => array('class' => 'prev'),
1029
			'&lt;strong&gt;Disabled&lt;/strong&gt;',
1030
			'/span'
1031
		);
1032
		$this->assertTags($result, $expected);
1033
 
1034
		$result = $this->Paginator->prev('<< Previous', null, '<strong>Disabled</strong>', array('escape' => true));
1035
		$expected = array(
1036
			'span' => array('class' => 'prev'),
1037
			'&lt;strong&gt;Disabled&lt;/strong&gt;',
1038
			'/span'
1039
		);
1040
		$this->assertTags($result, $expected);
1041
 
1042
		$result = $this->Paginator->prev('<< Previous', null, '<strong>Disabled</strong>', array('escape' => false));
1043
		$expected = array(
1044
			'span' => array('class' => 'prev'),
1045
			'<strong', 'Disabled', '/strong',
1046
			'/span'
1047
		);
1048
		$this->assertTags($result, $expected);
1049
 
1050
		$result = $this->Paginator->prev('<< Previous', array('tag' => false), '<strong>Disabled</strong>');
1051
		$expected = array(
1052
			'span' => array('class' => 'prev'),
1053
			'&lt;strong&gt;Disabled&lt;/strong&gt;',
1054
			'/span'
1055
		);
1056
		$this->assertTags($result, $expected);
1057
 
1058
		$result = $this->Paginator->prev(
1059
			'<< Previous',
1060
			array('tag' => 'li'),
1061
			null,
1062
			array('tag' => 'li', 'disabledTag' => 'span', 'class' => 'disabled')
1063
		);
1064
		$expected = array(
1065
			'li' => array('class' => 'disabled'),
1066
			'span' => array(),
1067
			'&lt;&lt; Previous',
1068
			'/span',
1069
			'/li'
1070
		);
1071
		$this->assertTags($result, $expected);
1072
		$result = $this->Paginator->prev(
1073
			'<< Previous',
1074
			array(),
1075
			null,
1076
			array('tag' => false, 'disabledTag' => 'span', 'class' => 'disabled')
1077
		);
1078
		$expected = array(
1079
			'span' => array('class' => 'disabled'),
1080
			'&lt;&lt; Previous',
1081
			'/span',
1082
		);
1083
		$this->assertTags($result, $expected);
1084
 
1085
		$this->Paginator->request->params['paging'] = array(
1086
			'Client' => array(
1087
				'page' => 1,
1088
				'current' => 3,
1089
				'count' => 13,
1090
				'prevPage' => false,
1091
				'nextPage' => true,
1092
				'pageCount' => 5,
1093
				'options' => array(
1094
					'page' => 1,
1095
					'limit' => 3,
1096
					'order' => array('Client.name' => 'DESC'),
1097
				),
1098
				'paramType' => 'named'
1099
			)
1100
		);
1101
 
1102
		$this->Paginator->request->params['paging']['Client']['page'] = 2;
1103
		$this->Paginator->request->params['paging']['Client']['prevPage'] = true;
1104
		$result = $this->Paginator->prev('<< Previous', null, null, array('class' => 'disabled'));
1105
		$expected = array(
1106
			'span' => array('class' => 'prev'),
1107
			'a' => array(
1108
				'href' => '/index/limit:3/sort:Client.name/direction:DESC',
1109
				'rel' => 'prev'
1110
			),
1111
			'&lt;&lt; Previous',
1112
			'/a',
1113
			'/span'
1114
		);
1115
		$this->assertTags($result, $expected);
1116
 
1117
		$result = $this->Paginator->next('Next');
1118
		$expected = array(
1119
			'span' => array('class' => 'next'),
1120
			'a' => array(
1121
				'href' => '/index/page:3/limit:3/sort:Client.name/direction:DESC',
1122
				'rel' => 'next'
1123
			),
1124
			'Next',
1125
			'/a',
1126
			'/span'
1127
		);
1128
		$this->assertTags($result, $expected);
1129
 
1130
		$this->Paginator->request->params['paging'] = array(
1131
			'Client' => array(
1132
				'page' => 2,
1133
				'current' => 1,
1134
				'count' => 13,
1135
				'prevPage' => true,
1136
				'nextPage' => false,
1137
				'pageCount' => 2,
1138
				'options' => array(
1139
					'page' => 2,
1140
					'limit' => 10,
1141
					'order' => array(),
1142
					'conditions' => array()
1143
				),
1144
				'paramType' => 'named'
1145
			)
1146
		);
1147
		$result = $this->Paginator->prev('Prev');
1148
		$expected = array(
1149
			'span' => array('class' => 'prev'),
1150
			'a' => array('href' => '/index/limit:10', 'rel' => 'prev'),
1151
			'Prev',
1152
			'/a',
1153
			'/span'
1154
		);
1155
		$this->assertTags($result, $expected);
1156
 
1157
		$result = $this->Paginator->next('Next', array(), null, array('tag' => false));
1158
		$expected = array(
1159
			'span' => array('class' => 'next'),
1160
			'Next',
1161
			'/span'
1162
		);
1163
		$this->assertTags($result, $expected);
1164
 
1165
		$this->Paginator->request->params['paging'] = array(
1166
			'Client' => array(
1167
				'page' => 2, 'current' => 1, 'count' => 13, 'prevPage' => true,
1168
				'nextPage' => false, 'pageCount' => 2,
1169
				'defaults' => array(),
1170
				'options' => array(
1171
					'page' => 2, 'limit' => 10, 'order' => array(), 'conditions' => array()
1172
				),
1173
				'paramType' => 'named'
1174
			)
1175
		);
1176
		$this->Paginator->options(array('url' => array(12, 'page' => 3)));
1177
		$result = $this->Paginator->prev('Prev', array('url' => array('foo' => 'bar')));
1178
		$expected = array(
1179
			'span' => array('class' => 'prev'),
1180
			'a' => array('href' => '/index/12/limit:10/foo:bar', 'rel' => 'prev'),
1181
			'Prev',
1182
			'/a',
1183
			'/span'
1184
		);
1185
		$this->assertTags($result, $expected);
1186
	}
1187
 
1188
/**
1189
 * test that __pagingLink methods use $options when $disabledOptions is an empty value.
1190
 * allowing you to use shortcut syntax
1191
 *
1192
 * @return void
1193
 */
1194
	public function testPagingLinksOptionsReplaceEmptyDisabledOptions() {
1195
		$this->Paginator->request->params['paging'] = array(
1196
			'Client' => array(
1197
				'page' => 1,
1198
				'current' => 3,
1199
				'count' => 13,
1200
				'prevPage' => false,
1201
				'nextPage' => true,
1202
				'pageCount' => 5,
1203
				'options' => array(
1204
					'page' => 1,
1205
				),
1206
				'paramType' => 'named'
1207
			)
1208
		);
1209
		$result = $this->Paginator->prev('<< Previous', array('escape' => false));
1210
		$expected = array(
1211
			'span' => array('class' => 'prev'),
1212
			'preg:/<< Previous/',
1213
			'/span'
1214
		);
1215
		$this->assertTags($result, $expected);
1216
 
1217
		$result = $this->Paginator->next('Next >>', array('escape' => false));
1218
		$expected = array(
1219
			'span' => array('class' => 'next'),
1220
			'a' => array('href' => '/index/page:2', 'rel' => 'next'),
1221
			'preg:/Next >>/',
1222
			'/a',
1223
			'/span'
1224
		);
1225
		$this->assertTags($result, $expected);
1226
	}
1227
 
1228
/**
1229
 * testPagingLinksNotDefaultModel
1230
 *
1231
 * Test the creation of paging links when the non default model is used.
1232
 *
1233
 * @return void
1234
 */
1235
	public function testPagingLinksNotDefaultModel() {
1236
		// Multiple Model Paginate
1237
		$this->Paginator->request->params['paging'] = array(
1238
			'Client' => array(
1239
				'page' => 1,
1240
				'current' => 3,
1241
				'count' => 13,
1242
				'prevPage' => false,
1243
				'nextPage' => true,
1244
				'pageCount' => 5,
1245
				'options' => array(
1246
					'page' => 1,
1247
				),
1248
				'paramType' => 'named'
1249
			),
1250
			'Server' => array(
1251
				'page' => 1,
1252
				'current' => 1,
1253
				'count' => 5,
1254
				'prevPage' => false,
1255
				'nextPage' => false,
1256
				'pageCount' => 5,
1257
				'options' => array(
1258
					'page' => 1,
1259
				),
1260
				'paramType' => 'named'
1261
			)
1262
		);
1263
		$result = $this->Paginator->sort('title', 'Title', array('model' => 'Client'));
1264
		$expected = array(
1265
			'a' => array('href' => '/index/sort:title/direction:asc'),
1266
			'Title',
1267
			'/a'
1268
		);
1269
		$this->assertTags($result, $expected);
1270
 
1271
		$result = $this->Paginator->next('Next', array('model' => 'Client'));
1272
		$expected = array(
1273
			'span' => array('class' => 'next'),
1274
			'a' => array('href' => '/index/page:2', 'rel' => 'next'),
1275
			'Next',
1276
			'/a',
1277
			'/span'
1278
		);
1279
		$this->assertTags($result, $expected);
1280
 
1281
		$result = $this->Paginator->next('Next', array('model' => 'Server'), 'No Next', array('model' => 'Server'));
1282
		$expected = array(
1283
			'span' => array('class' => 'next'), 'No Next', '/span'
1284
		);
1285
		$this->assertTags($result, $expected);
1286
	}
1287
 
1288
/**
1289
 * Test creating paging links for missing models.
1290
 *
1291
 * @return void
1292
 */
1293
	public function testPagingLinksMissingModel() {
1294
		$result = $this->Paginator->sort('title', 'Title', array('model' => 'Missing'));
1295
		$expected = array(
1296
			'a' => array('href' => '/index/sort:title/direction:asc'),
1297
			'Title',
1298
			'/a'
1299
		);
1300
		$this->assertTags($result, $expected);
1301
 
1302
		$result = $this->Paginator->next('Next', array('model' => 'Missing'));
1303
		$expected = array(
1304
			'span' => array('class' => 'next'),
1305
			'a' => array('href' => '/index/page:2', 'rel' => 'next'),
1306
			'Next',
1307
			'/a',
1308
			'/span'
1309
		);
1310
		$this->assertTags($result, $expected);
1311
 
1312
		$result = $this->Paginator->prev('Prev', array('model' => 'Missing'));
1313
		$expected = array(
1314
			'span' => array('class' => 'prev'),
1315
			'Prev',
1316
			'/span'
1317
		);
1318
		$this->assertTags($result, $expected);
1319
	}
1320
 
1321
/**
1322
 * testGenericLinks method
1323
 *
1324
 * @return void
1325
 */
1326
	public function testGenericLinks() {
1327
		$result = $this->Paginator->link('Sort by title on page 5', array('sort' => 'title', 'page' => 5, 'direction' => 'desc'));
1328
		$expected = array(
1329
			'a' => array('href' => '/index/page:5/sort:title/direction:desc'),
1330
			'Sort by title on page 5',
1331
			'/a'
1332
		);
1333
		$this->assertTags($result, $expected);
1334
 
1335
		$this->Paginator->request->params['paging']['Article']['options']['page'] = 2;
1336
		$result = $this->Paginator->link('Sort by title', array('sort' => 'title', 'direction' => 'desc'));
1337
		$expected = array(
1338
			'a' => array('href' => '/index/page:2/sort:title/direction:desc'),
1339
			'Sort by title',
1340
			'/a'
1341
		);
1342
		$this->assertTags($result, $expected);
1343
 
1344
		$this->Paginator->request->params['paging']['Article']['options']['page'] = 4;
1345
		$result = $this->Paginator->link('Sort by title on page 4', array('sort' => 'Article.title', 'direction' => 'desc'));
1346
		$expected = array(
1347
			'a' => array('href' => '/index/page:4/sort:Article.title/direction:desc'),
1348
			'Sort by title on page 4',
1349
			'/a'
1350
		);
1351
		$this->assertTags($result, $expected);
1352
	}
1353
 
1354
/**
1355
 * Tests generation of generic links with preset options
1356
 *
1357
 * @return void
1358
 */
1359
	public function testGenericLinksWithPresetOptions() {
1360
		$result = $this->Paginator->link('Foo!', array('page' => 1));
1361
		$this->assertTags($result, array('a' => array('href' => '/'), 'Foo!', '/a'));
1362
 
1363
		$this->Paginator->options(array('sort' => 'title', 'direction' => 'desc'));
1364
		$result = $this->Paginator->link('Foo!', array('page' => 1));
1365
		$this->assertTags($result, array(
1366
			'a' => array(
1367
				'href' => '/',
1368
				'sort' => 'title',
1369
				'direction' => 'desc'
1370
			),
1371
			'Foo!',
1372
			'/a'
1373
		));
1374
 
1375
		$this->Paginator->options(array('sort' => null, 'direction' => null));
1376
		$result = $this->Paginator->link('Foo!', array('page' => 1));
1377
		$this->assertTags($result, array('a' => array('href' => '/'), 'Foo!', '/a'));
1378
 
1379
		$this->Paginator->options(array('url' => array(
1380
			'sort' => 'title',
1381
			'direction' => 'desc'
1382
		)));
1383
		$result = $this->Paginator->link('Foo!', array('page' => 1));
1384
		$this->assertTags($result, array(
1385
			'a' => array('href' => '/index/sort:title/direction:desc'),
1386
			'Foo!',
1387
			'/a'
1388
		));
1389
	}
1390
 
1391
/**
1392
 * testNumbers method
1393
 *
1394
 * @return void
1395
 */
1396
	public function testNumbers() {
1397
		$this->Paginator->request->params['paging'] = array(
1398
			'Client' => array(
1399
				'page' => 8,
1400
				'current' => 3,
1401
				'count' => 30,
1402
				'prevPage' => false,
1403
				'nextPage' => 2,
1404
				'pageCount' => 15,
1405
				'options' => array(
1406
					'page' => 1,
1407
				),
1408
				'paramType' => 'named'
1409
			)
1410
		);
1411
		$result = $this->Paginator->numbers();
1412
		$expected = array(
1413
			array('span' => array()), array('a' => array('href' => '/index/page:4')), '4', '/a', '/span',
1414
			' | ',
1415
			array('span' => array()), array('a' => array('href' => '/index/page:5')), '5', '/a', '/span',
1416
			' | ',
1417
			array('span' => array()), array('a' => array('href' => '/index/page:6')), '6', '/a', '/span',
1418
			' | ',
1419
			array('span' => array()), array('a' => array('href' => '/index/page:7')), '7', '/a', '/span',
1420
			' | ',
1421
			array('span' => array('class' => 'current')), '8', '/span',
1422
			' | ',
1423
			array('span' => array()), array('a' => array('href' => '/index/page:9')), '9', '/a', '/span',
1424
			' | ',
1425
			array('span' => array()), array('a' => array('href' => '/index/page:10')), '10', '/a', '/span',
1426
			' | ',
1427
			array('span' => array()), array('a' => array('href' => '/index/page:11')), '11', '/a', '/span',
1428
			' | ',
1429
			array('span' => array()), array('a' => array('href' => '/index/page:12')), '12', '/a', '/span',
1430
		);
1431
		$this->assertTags($result, $expected);
1432
 
1433
		$result = $this->Paginator->numbers(array('tag' => 'li'));
1434
		$expected = array(
1435
			array('li' => array()), array('a' => array('href' => '/index/page:4')), '4', '/a', '/li',
1436
			' | ',
1437
			array('li' => array()), array('a' => array('href' => '/index/page:5')), '5', '/a', '/li',
1438
			' | ',
1439
			array('li' => array()), array('a' => array('href' => '/index/page:6')), '6', '/a', '/li',
1440
			' | ',
1441
			array('li' => array()), array('a' => array('href' => '/index/page:7')), '7', '/a', '/li',
1442
			' | ',
1443
			array('li' => array('class' => 'current')), '8', '/li',
1444
			' | ',
1445
			array('li' => array()), array('a' => array('href' => '/index/page:9')), '9', '/a', '/li',
1446
			' | ',
1447
			array('li' => array()), array('a' => array('href' => '/index/page:10')), '10', '/a', '/li',
1448
			' | ',
1449
			array('li' => array()), array('a' => array('href' => '/index/page:11')), '11', '/a', '/li',
1450
			' | ',
1451
			array('li' => array()), array('a' => array('href' => '/index/page:12')), '12', '/a', '/li',
1452
		);
1453
		$this->assertTags($result, $expected);
1454
 
1455
		$result = $this->Paginator->numbers(array('tag' => 'li', 'separator' => false));
1456
		$expected = array(
1457
			array('li' => array()), array('a' => array('href' => '/index/page:4')), '4', '/a', '/li',
1458
			array('li' => array()), array('a' => array('href' => '/index/page:5')), '5', '/a', '/li',
1459
			array('li' => array()), array('a' => array('href' => '/index/page:6')), '6', '/a', '/li',
1460
			array('li' => array()), array('a' => array('href' => '/index/page:7')), '7', '/a', '/li',
1461
			array('li' => array('class' => 'current')), '8', '/li',
1462
			array('li' => array()), array('a' => array('href' => '/index/page:9')), '9', '/a', '/li',
1463
			array('li' => array()), array('a' => array('href' => '/index/page:10')), '10', '/a', '/li',
1464
			array('li' => array()), array('a' => array('href' => '/index/page:11')), '11', '/a', '/li',
1465
			array('li' => array()), array('a' => array('href' => '/index/page:12')), '12', '/a', '/li',
1466
		);
1467
		$this->assertTags($result, $expected);
1468
 
1469
		$result = $this->Paginator->numbers(true);
1470
		$expected = array(
1471
			array('span' => array()), array('a' => array('href' => '/', 'rel' => 'first')), 'first', '/a', '/span',
1472
			' | ',
1473
			array('span' => array()), array('a' => array('href' => '/index/page:4')), '4', '/a', '/span',
1474
			' | ',
1475
			array('span' => array()), array('a' => array('href' => '/index/page:5')), '5', '/a', '/span',
1476
			' | ',
1477
			array('span' => array()), array('a' => array('href' => '/index/page:6')), '6', '/a', '/span',
1478
			' | ',
1479
			array('span' => array()), array('a' => array('href' => '/index/page:7')), '7', '/a', '/span',
1480
			' | ',
1481
			array('span' => array('class' => 'current')), '8', '/span',
1482
			' | ',
1483
			array('span' => array()), array('a' => array('href' => '/index/page:9')), '9', '/a', '/span',
1484
			' | ',
1485
			array('span' => array()), array('a' => array('href' => '/index/page:10')), '10', '/a', '/span',
1486
			' | ',
1487
			array('span' => array()), array('a' => array('href' => '/index/page:11')), '11', '/a', '/span',
1488
			' | ',
1489
			array('span' => array()), array('a' => array('href' => '/index/page:12')), '12', '/a', '/span',
1490
			' | ',
1491
			array('span' => array()), array('a' => array('href' => '/index/page:15', 'rel' => 'last')), 'last', '/a', '/span',
1492
		);
1493
		$this->assertTags($result, $expected);
1494
 
1495
		$this->Paginator->request->params['paging'] = array(
1496
			'Client' => array(
1497
				'page' => 1,
1498
				'current' => 3,
1499
				'count' => 30,
1500
				'prevPage' => false,
1501
				'nextPage' => 2,
1502
				'pageCount' => 15,
1503
				'options' => array(
1504
					'page' => 1,
1505
				),
1506
				'paramType' => 'named'
1507
			)
1508
		);
1509
		$result = $this->Paginator->numbers();
1510
		$expected = array(
1511
			array('span' => array('class' => 'current')), '1', '/span',
1512
			' | ',
1513
			array('span' => array()), array('a' => array('href' => '/index/page:2')), '2', '/a', '/span',
1514
			' | ',
1515
			array('span' => array()), array('a' => array('href' => '/index/page:3')), '3', '/a', '/span',
1516
			' | ',
1517
			array('span' => array()), array('a' => array('href' => '/index/page:4')), '4', '/a', '/span',
1518
			' | ',
1519
			array('span' => array()), array('a' => array('href' => '/index/page:5')), '5', '/a', '/span',
1520
			' | ',
1521
			array('span' => array()), array('a' => array('href' => '/index/page:6')), '6', '/a', '/span',
1522
			' | ',
1523
			array('span' => array()), array('a' => array('href' => '/index/page:7')), '7', '/a', '/span',
1524
			' | ',
1525
			array('span' => array()), array('a' => array('href' => '/index/page:8')), '8', '/a', '/span',
1526
			' | ',
1527
			array('span' => array()), array('a' => array('href' => '/index/page:9')), '9', '/a', '/span',
1528
		);
1529
		$this->assertTags($result, $expected);
1530
 
1531
		$this->Paginator->request->params['paging'] = array(
1532
			'Client' => array(
1533
				'page' => 14,
1534
				'current' => 3,
1535
				'count' => 30,
1536
				'prevPage' => false,
1537
				'nextPage' => 2,
1538
				'pageCount' => 15,
1539
				'options' => array(
1540
					'page' => 1,
1541
				),
1542
				'paramType' => 'named'
1543
			)
1544
		);
1545
		$result = $this->Paginator->numbers();
1546
		$expected = array(
1547
			array('span' => array()), array('a' => array('href' => '/index/page:7')), '7', '/a', '/span',
1548
			' | ',
1549
			array('span' => array()), array('a' => array('href' => '/index/page:8')), '8', '/a', '/span',
1550
			' | ',
1551
			array('span' => array()), array('a' => array('href' => '/index/page:9')), '9', '/a', '/span',
1552
			' | ',
1553
			array('span' => array()), array('a' => array('href' => '/index/page:10')), '10', '/a', '/span',
1554
			' | ',
1555
			array('span' => array()), array('a' => array('href' => '/index/page:11')), '11', '/a', '/span',
1556
			' | ',
1557
			array('span' => array()), array('a' => array('href' => '/index/page:12')), '12', '/a', '/span',
1558
			' | ',
1559
			array('span' => array()), array('a' => array('href' => '/index/page:13')), '13', '/a', '/span',
1560
			' | ',
1561
			array('span' => array('class' => 'current')), '14', '/span',
1562
			' | ',
1563
			array('span' => array()), array('a' => array('href' => '/index/page:15')), '15', '/a', '/span',
1564
		);
1565
		$this->assertTags($result, $expected);
1566
 
1567
		$this->Paginator->request->params['paging'] = array(
1568
			'Client' => array(
1569
				'page' => 2,
1570
				'current' => 3,
1571
				'count' => 27,
1572
				'prevPage' => false,
1573
				'nextPage' => 2,
1574
				'pageCount' => 9,
1575
				'options' => array(
1576
					'page' => 1,
1577
				),
1578
				'paramType' => 'named'
1579
			)
1580
		);
1581
 
1582
		$result = $this->Paginator->numbers(array('first' => 1, 'class' => 'page-link'));
1583
		$expected = array(
1584
			array('span' => array('class' => 'page-link')), array('a' => array('href' => '/')), '1', '/a', '/span',
1585
			' | ',
1586
			array('span' => array('class' => 'current page-link')), '2', '/span',
1587
			' | ',
1588
			array('span' => array('class' => 'page-link')), array('a' => array('href' => '/index/page:3')), '3', '/a', '/span',
1589
			' | ',
1590
			array('span' => array('class' => 'page-link')), array('a' => array('href' => '/index/page:4')), '4', '/a', '/span',
1591
			' | ',
1592
			array('span' => array('class' => 'page-link')), array('a' => array('href' => '/index/page:5')), '5', '/a', '/span',
1593
			' | ',
1594
			array('span' => array('class' => 'page-link')), array('a' => array('href' => '/index/page:6')), '6', '/a', '/span',
1595
			' | ',
1596
			array('span' => array('class' => 'page-link')), array('a' => array('href' => '/index/page:7')), '7', '/a', '/span',
1597
			' | ',
1598
			array('span' => array('class' => 'page-link')), array('a' => array('href' => '/index/page:8')), '8', '/a', '/span',
1599
			' | ',
1600
			array('span' => array('class' => 'page-link')), array('a' => array('href' => '/index/page:9')), '9', '/a', '/span',
1601
		);
1602
		$this->assertTags($result, $expected);
1603
 
1604
		$result = $this->Paginator->numbers(array('first' => 1, 'currentClass' => 'active'));
1605
		$expected = array(
1606
			array('span' => array()), array('a' => array('href' => '/')), '1', '/a', '/span',
1607
			' | ',
1608
			array('span' => array('class' => 'active')), '2', '/span',
1609
			' | ',
1610
			array('span' => array()), array('a' => array('href' => '/index/page:3')), '3', '/a', '/span',
1611
			' | ',
1612
			array('span' => array()), array('a' => array('href' => '/index/page:4')), '4', '/a', '/span',
1613
			' | ',
1614
			array('span' => array()), array('a' => array('href' => '/index/page:5')), '5', '/a', '/span',
1615
			' | ',
1616
			array('span' => array()), array('a' => array('href' => '/index/page:6')), '6', '/a', '/span',
1617
			' | ',
1618
			array('span' => array()), array('a' => array('href' => '/index/page:7')), '7', '/a', '/span',
1619
			' | ',
1620
			array('span' => array()), array('a' => array('href' => '/index/page:8')), '8', '/a', '/span',
1621
			' | ',
1622
			array('span' => array()), array('a' => array('href' => '/index/page:9')), '9', '/a', '/span',
1623
		);
1624
		$this->assertTags($result, $expected);
1625
 
1626
		$result = $this->Paginator->numbers(array('first' => 1, 'tag' => 'li', 'currentClass' => 'active', 'currentTag' => 'a'));
1627
		$expected = array(
1628
			array('li' => array()), array('a' => array('href' => '/')), '1', '/a', '/li',
1629
			' | ',
1630
			array('li' => array('class' => 'active')), array('a' => array()), '2', '/a', '/li',
1631
			' | ',
1632
			array('li' => array()), array('a' => array('href' => '/index/page:3')), '3', '/a', '/li',
1633
			' | ',
1634
			array('li' => array()), array('a' => array('href' => '/index/page:4')), '4', '/a', '/li',
1635
			' | ',
1636
			array('li' => array()), array('a' => array('href' => '/index/page:5')), '5', '/a', '/li',
1637
			' | ',
1638
			array('li' => array()), array('a' => array('href' => '/index/page:6')), '6', '/a', '/li',
1639
			' | ',
1640
			array('li' => array()), array('a' => array('href' => '/index/page:7')), '7', '/a', '/li',
1641
			' | ',
1642
			array('li' => array()), array('a' => array('href' => '/index/page:8')), '8', '/a', '/li',
1643
			' | ',
1644
			array('li' => array()), array('a' => array('href' => '/index/page:9')), '9', '/a', '/li',
1645
		);
1646
		$this->assertTags($result, $expected);
1647
 
1648
		$result = $this->Paginator->numbers(array('first' => 1, 'class' => 'page-link', 'currentClass' => 'active'));
1649
		$expected = array(
1650
			array('span' => array('class' => 'page-link')), array('a' => array('href' => '/')), '1', '/a', '/span',
1651
			' | ',
1652
			array('span' => array('class' => 'active page-link')), '2', '/span',
1653
			' | ',
1654
			array('span' => array('class' => 'page-link')), array('a' => array('href' => '/index/page:3')), '3', '/a', '/span',
1655
			' | ',
1656
			array('span' => array('class' => 'page-link')), array('a' => array('href' => '/index/page:4')), '4', '/a', '/span',
1657
			' | ',
1658
			array('span' => array('class' => 'page-link')), array('a' => array('href' => '/index/page:5')), '5', '/a', '/span',
1659
			' | ',
1660
			array('span' => array('class' => 'page-link')), array('a' => array('href' => '/index/page:6')), '6', '/a', '/span',
1661
			' | ',
1662
			array('span' => array('class' => 'page-link')), array('a' => array('href' => '/index/page:7')), '7', '/a', '/span',
1663
			' | ',
1664
			array('span' => array('class' => 'page-link')), array('a' => array('href' => '/index/page:8')), '8', '/a', '/span',
1665
			' | ',
1666
			array('span' => array('class' => 'page-link')), array('a' => array('href' => '/index/page:9')), '9', '/a', '/span',
1667
		);
1668
		$this->assertTags($result, $expected);
1669
 
1670
		$result = $this->Paginator->numbers(array('last' => 1));
1671
		$expected = array(
1672
			array('span' => array()), array('a' => array('href' => '/')), '1', '/a', '/span',
1673
			' | ',
1674
			array('span' => array('class' => 'current')), '2', '/span',
1675
			' | ',
1676
			array('span' => array()), array('a' => array('href' => '/index/page:3')), '3', '/a', '/span',
1677
			' | ',
1678
			array('span' => array()), array('a' => array('href' => '/index/page:4')), '4', '/a', '/span',
1679
			' | ',
1680
			array('span' => array()), array('a' => array('href' => '/index/page:5')), '5', '/a', '/span',
1681
			' | ',
1682
			array('span' => array()), array('a' => array('href' => '/index/page:6')), '6', '/a', '/span',
1683
			' | ',
1684
			array('span' => array()), array('a' => array('href' => '/index/page:7')), '7', '/a', '/span',
1685
			' | ',
1686
			array('span' => array()), array('a' => array('href' => '/index/page:8')), '8', '/a', '/span',
1687
			' | ',
1688
			array('span' => array()), array('a' => array('href' => '/index/page:9')), '9', '/a', '/span',
1689
		);
1690
		$this->assertTags($result, $expected);
1691
 
1692
		$this->Paginator->request->params['paging'] = array(
1693
			'Client' => array(
1694
				'page' => 15,
1695
				'current' => 3,
1696
				'count' => 30,
1697
				'prevPage' => false,
1698
				'nextPage' => 2,
1699
				'pageCount' => 15,
1700
				'options' => array(
1701
					'page' => 1,
1702
				),
1703
				'paramType' => 'named'
1704
			)
1705
		);
1706
 
1707
		$result = $this->Paginator->numbers(array('first' => 1));
1708
		$expected = array(
1709
			array('span' => array()), array('a' => array('href' => '/')), '1', '/a', '/span',
1710
			'...',
1711
			array('span' => array()), array('a' => array('href' => '/index/page:7')), '7', '/a', '/span',
1712
			' | ',
1713
			array('span' => array()), array('a' => array('href' => '/index/page:8')), '8', '/a', '/span',
1714
			' | ',
1715
			array('span' => array()), array('a' => array('href' => '/index/page:9')), '9', '/a', '/span',
1716
			' | ',
1717
			array('span' => array()), array('a' => array('href' => '/index/page:10')), '10', '/a', '/span',
1718
			' | ',
1719
			array('span' => array()), array('a' => array('href' => '/index/page:11')), '11', '/a', '/span',
1720
			' | ',
1721
			array('span' => array()), array('a' => array('href' => '/index/page:12')), '12', '/a', '/span',
1722
			' | ',
1723
			array('span' => array()), array('a' => array('href' => '/index/page:13')), '13', '/a', '/span',
1724
			' | ',
1725
			array('span' => array()), array('a' => array('href' => '/index/page:14')), '14', '/a', '/span',
1726
			' | ',
1727
			array('span' => array('class' => 'current')), '15', '/span',
1728
 
1729
		);
1730
		$this->assertTags($result, $expected);
1731
 
1732
		$this->Paginator->request->params['paging'] = array(
1733
			'Client' => array(
1734
				'page' => 10,
1735
				'current' => 3,
1736
				'count' => 30,
1737
				'prevPage' => false,
1738
				'nextPage' => 2,
1739
				'pageCount' => 15,
1740
				'options' => array(
1741
					'page' => 1,
1742
				),
1743
				'paramType' => 'named'
1744
			)
1745
		);
1746
 
1747
		$result = $this->Paginator->numbers(array('first' => 1, 'last' => 1));
1748
		$expected = array(
1749
			array('span' => array()), array('a' => array('href' => '/')), '1', '/a', '/span',
1750
			'...',
1751
			array('span' => array()), array('a' => array('href' => '/index/page:6')), '6', '/a', '/span',
1752
			' | ',
1753
			array('span' => array()), array('a' => array('href' => '/index/page:7')), '7', '/a', '/span',
1754
			' | ',
1755
			array('span' => array()), array('a' => array('href' => '/index/page:8')), '8', '/a', '/span',
1756
			' | ',
1757
			array('span' => array()), array('a' => array('href' => '/index/page:9')), '9', '/a', '/span',
1758
			' | ',
1759
			array('span' => array('class' => 'current')), '10', '/span',
1760
			' | ',
1761
			array('span' => array()), array('a' => array('href' => '/index/page:11')), '11', '/a', '/span',
1762
			' | ',
1763
			array('span' => array()), array('a' => array('href' => '/index/page:12')), '12', '/a', '/span',
1764
			' | ',
1765
			array('span' => array()), array('a' => array('href' => '/index/page:13')), '13', '/a', '/span',
1766
			' | ',
1767
			array('span' => array()), array('a' => array('href' => '/index/page:14')), '14', '/a', '/span',
1768
			' | ',
1769
			array('span' => array()), array('a' => array('href' => '/index/page:15')), '15', '/a', '/span',
1770
		);
1771
		$this->assertTags($result, $expected);
1772
 
1773
		$this->Paginator->request->params['paging'] = array(
1774
			'Client' => array(
1775
				'page' => 6,
1776
				'current' => 15,
1777
				'count' => 623,
1778
				'prevPage' => 1,
1779
				'nextPage' => 1,
1780
				'pageCount' => 42,
1781
				'options' => array(
1782
					'page' => 6,
1783
				),
1784
				'paramType' => 'named'
1785
			)
1786
		);
1787
 
1788
		$result = $this->Paginator->numbers(array('first' => 1, 'last' => 1));
1789
		$expected = array(
1790
			array('span' => array()), array('a' => array('href' => '/')), '1', '/a', '/span',
1791
			' | ',
1792
			array('span' => array()), array('a' => array('href' => '/index/page:2')), '2', '/a', '/span',
1793
			' | ',
1794
			array('span' => array()), array('a' => array('href' => '/index/page:3')), '3', '/a', '/span',
1795
			' | ',
1796
			array('span' => array()), array('a' => array('href' => '/index/page:4')), '4', '/a', '/span',
1797
			' | ',
1798
			array('span' => array()), array('a' => array('href' => '/index/page:5')), '5', '/a', '/span',
1799
			' | ',
1800
			array('span' => array('class' => 'current')), '6', '/span',
1801
			' | ',
1802
			array('span' => array()), array('a' => array('href' => '/index/page:7')), '7', '/a', '/span',
1803
			' | ',
1804
			array('span' => array()), array('a' => array('href' => '/index/page:8')), '8', '/a', '/span',
1805
			' | ',
1806
			array('span' => array()), array('a' => array('href' => '/index/page:9')), '9', '/a', '/span',
1807
			' | ',
1808
			array('span' => array()), array('a' => array('href' => '/index/page:10')), '10', '/a', '/span',
1809
			'...',
1810
			array('span' => array()), array('a' => array('href' => '/index/page:42')), '42', '/a', '/span',
1811
		);
1812
		$this->assertTags($result, $expected);
1813
 
1814
		$this->Paginator->request->params['paging'] = array(
1815
			'Client' => array(
1816
				'page' => 37,
1817
				'current' => 15,
1818
				'count' => 623,
1819
				'prevPage' => 1,
1820
				'nextPage' => 1,
1821
				'pageCount' => 42,
1822
				'options' => array(
1823
					'page' => 37,
1824
				),
1825
				'paramType' => 'named'
1826
			)
1827
		);
1828
 
1829
		$result = $this->Paginator->numbers(array('first' => 1, 'last' => 1));
1830
		$expected = array(
1831
			array('span' => array()), array('a' => array('href' => '/')), '1', '/a', '/span',
1832
			'...',
1833
			array('span' => array()), array('a' => array('href' => '/index/page:33')), '33', '/a', '/span',
1834
			' | ',
1835
			array('span' => array()), array('a' => array('href' => '/index/page:34')), '34', '/a', '/span',
1836
			' | ',
1837
			array('span' => array()), array('a' => array('href' => '/index/page:35')), '35', '/a', '/span',
1838
			' | ',
1839
			array('span' => array()), array('a' => array('href' => '/index/page:36')), '36', '/a', '/span',
1840
			' | ',
1841
			array('span' => array('class' => 'current')), '37', '/span',
1842
			' | ',
1843
			array('span' => array()), array('a' => array('href' => '/index/page:38')), '38', '/a', '/span',
1844
			' | ',
1845
			array('span' => array()), array('a' => array('href' => '/index/page:39')), '39', '/a', '/span',
1846
			' | ',
1847
			array('span' => array()), array('a' => array('href' => '/index/page:40')), '40', '/a', '/span',
1848
			' | ',
1849
			array('span' => array()), array('a' => array('href' => '/index/page:41')), '41', '/a', '/span',
1850
			' | ',
1851
			array('span' => array()), array('a' => array('href' => '/index/page:42')), '42', '/a', '/span',
1852
		);
1853
		$this->assertTags($result, $expected);
1854
 
1855
		$this->Paginator->request->params['paging'] = array(
1856
			'Client' => array(
1857
				'page' => 1,
1858
				'current' => 10,
1859
				'count' => 30,
1860
				'prevPage' => false,
1861
				'nextPage' => 2,
1862
				'pageCount' => 3,
1863
				'options' => array(
1864
					'page' => 1,
1865
				),
1866
				'paramType' => 'named'
1867
			)
1868
		);
1869
		$options = array('modulus' => 10);
1870
		$result = $this->Paginator->numbers($options);
1871
		$expected = array(
1872
			array('span' => array('class' => 'current')), '1', '/span',
1873
			' | ',
1874
			array('span' => array()), array('a' => array('href' => '/index/page:2')), '2', '/a', '/span',
1875
			' | ',
1876
			array('span' => array()), array('a' => array('href' => '/index/page:3')), '3', '/a', '/span',
1877
		);
1878
		$this->assertTags($result, $expected);
1879
 
1880
		$result = $this->Paginator->numbers(array('modulus' => 3, 'currentTag' => 'span', 'tag' => 'li'));
1881
		$expected = array(
1882
			array('li' => array('class' => 'current')), array('span' => array()), '1', '/span', '/li',
1883
			' | ',
1884
			array('li' => array()), array('a' => array('href' => '/index/page:2')), '2', '/a', '/li',
1885
			' | ',
1886
			array('li' => array()), array('a' => array('href' => '/index/page:3')), '3', '/a', '/li',
1887
		);
1888
		$this->assertTags($result, $expected);
1889
 
1890
		$this->Paginator->request->params['paging'] = array(
1891
			'Client' => array(
1892
				'page' => 2,
1893
				'current' => 10,
1894
				'count' => 31,
1895
				'prevPage' => true,
1896
				'nextPage' => true,
1897
				'pageCount' => 4,
1898
				'options' => array(
1899
					'page' => 1,
1900
					'order' => array('Client.name' => 'DESC'),
1901
				),
1902
				'paramType' => 'named'
1903
			)
1904
		);
1905
		$result = $this->Paginator->numbers(array('class' => 'page-link'));
1906
		$expected = array(
1907
			array('span' => array('class' => 'page-link')), array('a' => array('href' => '/index/sort:Client.name/direction:DESC')), '1', '/a', '/span',
1908
			' | ',
1909
			array('span' => array('class' => 'current page-link')), '2', '/span',
1910
			' | ',
1911
			array('span' => array('class' => 'page-link')), array('a' => array('href' => '/index/page:3/sort:Client.name/direction:DESC')), '3', '/a', '/span',
1912
			' | ',
1913
			array('span' => array('class' => 'page-link')), array('a' => array('href' => '/index/page:4/sort:Client.name/direction:DESC')), '4', '/a', '/span',
1914
		);
1915
		$this->assertTags($result, $expected);
1916
 
1917
		$this->Paginator->request->params['paging'] = array(
1918
			'Client' => array(
1919
				'page' => 4895,
1920
				'current' => 10,
1921
				'count' => 48962,
1922
				'prevPage' => 1,
1923
				'nextPage' => 1,
1924
				'pageCount' => 4897,
1925
				'options' => array(
1926
					'page' => 4894,
1927
				),
1928
				'paramType' => 'named'
1929
			)
1930
		);
1931
 
1932
		$result = $this->Paginator->numbers(array('first' => 2, 'modulus' => 2, 'last' => 2));
1933
		$expected = array(
1934
			array('span' => array()), array('a' => array('href' => '/')), '1', '/a', '/span',
1935
			' | ',
1936
			array('span' => array()), array('a' => array('href' => '/index/page:2')), '2', '/a', '/span',
1937
			'...',
1938
			array('span' => array()), array('a' => array('href' => '/index/page:4894')), '4894', '/a', '/span',
1939
			' | ',
1940
			array('span' => array('class' => 'current')), '4895', '/span',
1941
			' | ',
1942
			array('span' => array()), array('a' => array('href' => '/index/page:4896')), '4896', '/a', '/span',
1943
			' | ',
1944
			array('span' => array()), array('a' => array('href' => '/index/page:4897')), '4897', '/a', '/span',
1945
		);
1946
		$this->assertTags($result, $expected);
1947
 
1948
		$this->Paginator->request->params['paging']['Client']['page'] = 3;
1949
 
1950
		$result = $this->Paginator->numbers(array('first' => 2, 'modulus' => 2, 'last' => 2));
1951
		$expected = array(
1952
			array('span' => array()), array('a' => array('href' => '/')), '1', '/a', '/span',
1953
			' | ',
1954
			array('span' => array()), array('a' => array('href' => '/index/page:2')), '2', '/a', '/span',
1955
			' | ',
1956
			array('span' => array('class' => 'current')), '3', '/span',
1957
			' | ',
1958
			array('span' => array()), array('a' => array('href' => '/index/page:4')), '4', '/a', '/span',
1959
			'...',
1960
			array('span' => array()), array('a' => array('href' => '/index/page:4896')), '4896', '/a', '/span',
1961
			' | ',
1962
			array('span' => array()), array('a' => array('href' => '/index/page:4897')), '4897', '/a', '/span',
1963
		);
1964
		$this->assertTags($result, $expected);
1965
 
1966
		$result = $this->Paginator->numbers(array('first' => 2, 'modulus' => 2, 'last' => 2, 'separator' => ' - '));
1967
		$expected = array(
1968
			array('span' => array()), array('a' => array('href' => '/')), '1', '/a', '/span',
1969
			' - ',
1970
			array('span' => array()), array('a' => array('href' => '/index/page:2')), '2', '/a', '/span',
1971
			' - ',
1972
			array('span' => array('class' => 'current')), '3', '/span',
1973
			' - ',
1974
			array('span' => array()), array('a' => array('href' => '/index/page:4')), '4', '/a', '/span',
1975
			'...',
1976
			array('span' => array()), array('a' => array('href' => '/index/page:4896')), '4896', '/a', '/span',
1977
			' - ',
1978
			array('span' => array()), array('a' => array('href' => '/index/page:4897')), '4897', '/a', '/span',
1979
		);
1980
		$this->assertTags($result, $expected);
1981
 
1982
		$result = $this->Paginator->numbers(array('first' => 5, 'modulus' => 5, 'last' => 5, 'separator' => ' - '));
1983
		$expected = array(
1984
			array('span' => array()), array('a' => array('href' => '/')), '1', '/a', '/span',
1985
			' - ',
1986
			array('span' => array()), array('a' => array('href' => '/index/page:2')), '2', '/a', '/span',
1987
			' - ',
1988
			array('span' => array('class' => 'current')), '3', '/span',
1989
			' - ',
1990
			array('span' => array()), array('a' => array('href' => '/index/page:4')), '4', '/a', '/span',
1991
			' - ',
1992
			array('span' => array()), array('a' => array('href' => '/index/page:5')), '5', '/a', '/span',
1993
			' - ',
1994
			array('span' => array()), array('a' => array('href' => '/index/page:6')), '6', '/a', '/span',
1995
			'...',
1996
			array('span' => array()), array('a' => array('href' => '/index/page:4893')), '4893', '/a', '/span',
1997
			' - ',
1998
			array('span' => array()), array('a' => array('href' => '/index/page:4894')), '4894', '/a', '/span',
1999
			' - ',
2000
			array('span' => array()), array('a' => array('href' => '/index/page:4895')), '4895', '/a', '/span',
2001
			' - ',
2002
			array('span' => array()), array('a' => array('href' => '/index/page:4896')), '4896', '/a', '/span',
2003
			' - ',
2004
			array('span' => array()), array('a' => array('href' => '/index/page:4897')), '4897', '/a', '/span',
2005
		);
2006
		$this->assertTags($result, $expected);
2007
 
2008
		$this->Paginator->request->params['paging']['Client']['page'] = 4893;
2009
		$result = $this->Paginator->numbers(array('first' => 5, 'modulus' => 4, 'last' => 5, 'separator' => ' - '));
2010
		$expected = array(
2011
			array('span' => array()), array('a' => array('href' => '/')), '1', '/a', '/span',
2012
			' - ',
2013
			array('span' => array()), array('a' => array('href' => '/index/page:2')), '2', '/a', '/span',
2014
			' - ',
2015
			array('span' => array()), array('a' => array('href' => '/index/page:3')), '3', '/a', '/span',
2016
			' - ',
2017
			array('span' => array()), array('a' => array('href' => '/index/page:4')), '4', '/a', '/span',
2018
			' - ',
2019
			array('span' => array()), array('a' => array('href' => '/index/page:5')), '5', '/a', '/span',
2020
			'...',
2021
			array('span' => array()), array('a' => array('href' => '/index/page:4891')), '4891', '/a', '/span',
2022
			' - ',
2023
			array('span' => array()), array('a' => array('href' => '/index/page:4892')), '4892', '/a', '/span',
2024
			' - ',
2025
			array('span' => array('class' => 'current')), '4893', '/span',
2026
			' - ',
2027
			array('span' => array()), array('a' => array('href' => '/index/page:4894')), '4894', '/a', '/span',
2028
			' - ',
2029
			array('span' => array()), array('a' => array('href' => '/index/page:4895')), '4895', '/a', '/span',
2030
			' - ',
2031
			array('span' => array()), array('a' => array('href' => '/index/page:4896')), '4896', '/a', '/span',
2032
			' - ',
2033
			array('span' => array()), array('a' => array('href' => '/index/page:4897')), '4897', '/a', '/span',
2034
		);
2035
		$this->assertTags($result, $expected);
2036
 
2037
		$this->Paginator->request->params['paging']['Client']['page'] = 58;
2038
		$result = $this->Paginator->numbers(array('first' => 5, 'modulus' => 4, 'last' => 5, 'separator' => ' - '));
2039
		$expected = array(
2040
			array('span' => array()), array('a' => array('href' => '/')), '1', '/a', '/span',
2041
			' - ',
2042
			array('span' => array()), array('a' => array('href' => '/index/page:2')), '2', '/a', '/span',
2043
			' - ',
2044
			array('span' => array()), array('a' => array('href' => '/index/page:3')), '3', '/a', '/span',
2045
			' - ',
2046
			array('span' => array()), array('a' => array('href' => '/index/page:4')), '4', '/a', '/span',
2047
			' - ',
2048
			array('span' => array()), array('a' => array('href' => '/index/page:5')), '5', '/a', '/span',
2049
			'...',
2050
			array('span' => array()), array('a' => array('href' => '/index/page:56')), '56', '/a', '/span',
2051
			' - ',
2052
			array('span' => array()), array('a' => array('href' => '/index/page:57')), '57', '/a', '/span',
2053
			' - ',
2054
			array('span' => array('class' => 'current')), '58', '/span',
2055
			' - ',
2056
			array('span' => array()), array('a' => array('href' => '/index/page:59')), '59', '/a', '/span',
2057
			' - ',
2058
			array('span' => array()), array('a' => array('href' => '/index/page:60')), '60', '/a', '/span',
2059
			'...',
2060
			array('span' => array()), array('a' => array('href' => '/index/page:4893')), '4893', '/a', '/span',
2061
			' - ',
2062
			array('span' => array()), array('a' => array('href' => '/index/page:4894')), '4894', '/a', '/span',
2063
			' - ',
2064
			array('span' => array()), array('a' => array('href' => '/index/page:4895')), '4895', '/a', '/span',
2065
			' - ',
2066
			array('span' => array()), array('a' => array('href' => '/index/page:4896')), '4896', '/a', '/span',
2067
			' - ',
2068
			array('span' => array()), array('a' => array('href' => '/index/page:4897')), '4897', '/a', '/span',
2069
		);
2070
		$this->assertTags($result, $expected);
2071
 
2072
		$this->Paginator->request->params['paging']['Client']['page'] = 5;
2073
		$result = $this->Paginator->numbers(array('first' => 5, 'modulus' => 4, 'last' => 5, 'separator' => ' - '));
2074
		$expected = array(
2075
			array('span' => array()), array('a' => array('href' => '/')), '1', '/a', '/span',
2076
			' - ',
2077
			array('span' => array()), array('a' => array('href' => '/index/page:2')), '2', '/a', '/span',
2078
			' - ',
2079
			array('span' => array()), array('a' => array('href' => '/index/page:3')), '3', '/a', '/span',
2080
			' - ',
2081
			array('span' => array()), array('a' => array('href' => '/index/page:4')), '4', '/a', '/span',
2082
			' - ',
2083
			array('span' => array('class' => 'current')), '5', '/span',
2084
			' - ',
2085
			array('span' => array()), array('a' => array('href' => '/index/page:6')), '6', '/a', '/span',
2086
			' - ',
2087
			array('span' => array()), array('a' => array('href' => '/index/page:7')), '7', '/a', '/span',
2088
			'...',
2089
			array('span' => array()), array('a' => array('href' => '/index/page:4893')), '4893', '/a', '/span',
2090
			' - ',
2091
			array('span' => array()), array('a' => array('href' => '/index/page:4894')), '4894', '/a', '/span',
2092
			' - ',
2093
			array('span' => array()), array('a' => array('href' => '/index/page:4895')), '4895', '/a', '/span',
2094
			' - ',
2095
			array('span' => array()), array('a' => array('href' => '/index/page:4896')), '4896', '/a', '/span',
2096
			' - ',
2097
			array('span' => array()), array('a' => array('href' => '/index/page:4897')), '4897', '/a', '/span',
2098
		);
2099
		$this->assertTags($result, $expected);
2100
 
2101
		$this->Paginator->request->params['paging']['Client']['page'] = 3;
2102
		$result = $this->Paginator->numbers(array('first' => 2, 'modulus' => 2, 'last' => 2, 'separator' => ' - ', 'ellipsis' => ' ~~~ '));
2103
		$expected = array(
2104
			array('span' => array()), array('a' => array('href' => '/')), '1', '/a', '/span',
2105
			' - ',
2106
			array('span' => array()), array('a' => array('href' => '/index/page:2')), '2', '/a', '/span',
2107
			' - ',
2108
			array('span' => array('class' => 'current')), '3', '/span',
2109
			' - ',
2110
			array('span' => array()), array('a' => array('href' => '/index/page:4')), '4', '/a', '/span',
2111
			' ~~~ ',
2112
			array('span' => array()), array('a' => array('href' => '/index/page:4896')), '4896', '/a', '/span',
2113
			' - ',
2114
			array('span' => array()), array('a' => array('href' => '/index/page:4897')), '4897', '/a', '/span',
2115
		);
2116
		$this->assertTags($result, $expected);
2117
 
2118
		$this->Paginator->request->params['paging']['Client']['page'] = 3;
2119
		$result = $this->Paginator->numbers(array('first' => 2, 'modulus' => 2, 'last' => 2, 'separator' => ' - ', 'ellipsis' => '<span class="ellipsis">...</span>'));
2120
		$expected = array(
2121
			array('span' => array()), array('a' => array('href' => '/')), '1', '/a', '/span',
2122
			' - ',
2123
			array('span' => array()), array('a' => array('href' => '/index/page:2')), '2', '/a', '/span',
2124
			' - ',
2125
			array('span' => array('class' => 'current')), '3', '/span',
2126
			' - ',
2127
			array('span' => array()), array('a' => array('href' => '/index/page:4')), '4', '/a', '/span',
2128
			array('span' => array('class' => 'ellipsis')), '...', '/span',
2129
			array('span' => array()), array('a' => array('href' => '/index/page:4896')), '4896', '/a', '/span',
2130
			' - ',
2131
			array('span' => array()), array('a' => array('href' => '/index/page:4897')), '4897', '/a', '/span',
2132
		);
2133
		$this->assertTags($result, $expected);
2134
 
2135
		$this->Paginator->request->params['paging'] = array(
2136
			'Client' => array(
2137
				'page' => 2,
2138
				'current' => 2,
2139
				'count' => 30,
2140
				'prevPage' => false,
2141
				'nextPage' => 3,
2142
				'pageCount' => 3,
2143
				'options' => array(
2144
					'page' => 1,
2145
				),
2146
				'paramType' => 'named'
2147
			)
2148
		);
2149
 
2150
		$request = new CakeRequest();
2151
		$request->addParams(array(
2152
			'controller' => 'clients', 'action' => 'index', 'plugin' => null, 'page' => 2
2153
		));
2154
		$request->base = '';
2155
		$request->here = '/clients/index/page:2';
2156
		$request->webroot = '/';
2157
 
2158
		Router::setRequestInfo($request);
2159
 
2160
		$result = $this->Paginator->numbers();
2161
		$expected = array(
2162
			array('span' => array()), array('a' => array('href' => '/clients')), '1', '/a', '/span',
2163
			' | ',
2164
			array('span' => array('class' => 'current')), '2', '/span',
2165
			' | ',
2166
			array('span' => array()), array('a' => array('href' => '/clients/index/page:3')), '3', '/a', '/span',
2167
 
2168
		);
2169
		$this->assertTags($result, $expected);
2170
 
2171
		$this->Paginator->request->params['paging'] = array(
2172
			'Client' => array(
2173
				'page' => 2,
2174
				'current' => 2,
2175
				'count' => 30,
2176
				'prevPage' => false,
2177
				'nextPage' => 3,
2178
				'pageCount' => 3,
2179
				'options' => array(
2180
					'page' => 1,
2181
				),
2182
				'paramType' => 'querystring'
2183
			)
2184
		);
2185
 
2186
		$request = new CakeRequest();
2187
		$request->addParams(array(
2188
			'controller' => 'clients', 'action' => 'index', 'plugin' => null
2189
		));
2190
		$request->base = '';
2191
		$request->here = '/clients?page=2';
2192
		$request->webroot = '/';
2193
 
2194
		Router::setRequestInfo($request);
2195
 
2196
		$result = $this->Paginator->numbers();
2197
		$expected = array(
2198
			array('span' => array()), array('a' => array('href' => '/clients')), '1', '/a', '/span',
2199
			' | ',
2200
			array('span' => array('class' => 'current')), '2', '/span',
2201
			' | ',
2202
			array('span' => array()), array('a' => array('href' => '/clients?page=3')), '3', '/a', '/span',
2203
 
2204
		);
2205
		$this->assertTags($result, $expected);
2206
	}
2207
 
2208
/**
2209
 * test first() and last() with tag options
2210
 *
2211
 * @return void
2212
 */
2213
	public function testFirstAndLastTag() {
2214
		$result = $this->Paginator->first('<<', array('tag' => 'li', 'class' => 'first'));
2215
		$expected = array(
2216
			'li' => array('class' => 'first'),
2217
			'a' => array('href' => '/', 'rel' => 'first'),
2218
			'&lt;&lt;',
2219
			'/a',
2220
			'/li'
2221
		);
2222
		$this->assertTags($result, $expected);
2223
 
2224
		$result = $this->Paginator->last(2, array('tag' => 'li', 'class' => 'last'));
2225
		$expected = array(
2226
			'...',
2227
			'li' => array('class' => 'last'),
2228
			array('a' => array('href' => '/index/page:6')), '6', '/a',
2229
			'/li',
2230
			' | ',
2231
			array('li' => array('class' => 'last')),
2232
			array('a' => array('href' => '/index/page:7')), '7', '/a',
2233
			'/li',
2234
		);
2235
		$this->assertTags($result, $expected);
2236
	}
2237
 
2238
/**
2239
 * test that on the last page you don't get a link ot the last page.
2240
 *
2241
 * @return void
2242
 */
2243
	public function testLastNoOutput() {
2244
		$this->Paginator->request->params['paging']['Article']['page'] = 15;
2245
		$this->Paginator->request->params['paging']['Article']['pageCount'] = 15;
2246
 
2247
		$result = $this->Paginator->last();
2248
		$expected = '';
2249
		$this->assertEquals($expected, $result);
2250
	}
2251
 
2252
/**
2253
 * test first() on the first page.
2254
 *
2255
 * @return void
2256
 */
2257
	public function testFirstEmpty() {
2258
		$this->Paginator->request->params['paging']['Article']['page'] = 1;
2259
 
2260
		$result = $this->Paginator->first();
2261
		$expected = '';
2262
		$this->assertEquals($expected, $result);
2263
	}
2264
 
2265
/**
2266
 * test first() and options()
2267
 *
2268
 * @return void
2269
 */
2270
	public function testFirstFullBaseUrl() {
2271
		$this->Paginator->request->params['paging']['Article']['options']['order'] = array('Article.title' => 'DESC');
2272
 
2273
		$this->Paginator->options(array('url' => array('full_base' => true)));
2274
 
2275
		$result = $this->Paginator->first();
2276
		$expected = array(
2277
			'<span',
2278
			array('a' => array(
2279
				'href' => FULL_BASE_URL . '/index/sort:Article.title/direction:DESC', 'rel' => 'first'
2280
			)),
2281
			'&lt;&lt; first',
2282
			'/a',
2283
			'/span',
2284
		);
2285
		$this->assertTags($result, $expected);
2286
	}
2287
 
2288
/**
2289
 * test first() on the fence-post
2290
 *
2291
 * @return void
2292
 */
2293
	public function testFirstBoundaries() {
2294
		$result = $this->Paginator->first();
2295
		$expected = array(
2296
			'<span',
2297
			'a' => array('href' => '/', 'rel' => 'first'),
2298
			'&lt;&lt; first',
2299
			'/a',
2300
			'/span'
2301
		);
2302
		$this->assertTags($result, $expected);
2303
 
2304
		$result = $this->Paginator->first(2);
2305
		$expected = array(
2306
			'<span',
2307
			array('a' => array('href' => '/')), '1', '/a',
2308
			'/span',
2309
			' | ',
2310
			'<span',
2311
			array('a' => array('href' => '/index/page:2')), '2', '/a',
2312
			'/span'
2313
		);
2314
		$this->assertTags($result, $expected);
2315
 
2316
		$this->Paginator->request->params['paging']['Article']['page'] = 2;
2317
		$result = $this->Paginator->first(3);
2318
		$this->assertEquals('', $result, 'When inside the first links range, no links should be made');
2319
	}
2320
 
2321
/**
2322
 * test params() method
2323
 *
2324
 * @return void
2325
 */
2326
	public function testParams() {
2327
		$result = $this->Paginator->params();
2328
		$this->assertArrayHasKey('page', $result);
2329
		$this->assertArrayHasKey('pageCount', $result);
2330
	}
2331
 
2332
/**
2333
 * test param() method
2334
 *
2335
 * @return void
2336
 */
2337
	public function testParam() {
2338
		$result = $this->Paginator->param('count');
2339
		$this->assertSame(62, $result);
2340
 
2341
		$result = $this->Paginator->param('imaginary');
2342
		$this->assertNull($result);
2343
	}
2344
 
2345
/**
2346
 * test last() method
2347
 *
2348
 * @return void
2349
 */
2350
	public function testLast() {
2351
		$result = $this->Paginator->last();
2352
		$expected = array(
2353
			'<span',
2354
			'a' => array('href' => '/index/page:7', 'rel' => 'last'),
2355
			'last &gt;&gt;',
2356
			'/a',
2357
			'/span'
2358
		);
2359
		$this->assertTags($result, $expected);
2360
 
2361
		$result = $this->Paginator->last(1);
2362
		$expected = array(
2363
			'...',
2364
			'<span',
2365
			'a' => array('href' => '/index/page:7'),
2366
			'7',
2367
			'/a',
2368
			'/span'
2369
		);
2370
		$this->assertTags($result, $expected);
2371
 
2372
		$this->Paginator->request->params['paging']['Article']['page'] = 6;
2373
 
2374
		$result = $this->Paginator->last(2);
2375
		$expected = array(
2376
			'...',
2377
			'<span',
2378
			array('a' => array('href' => '/index/page:6')), '6', '/a',
2379
			'/span',
2380
			' | ',
2381
			'<span',
2382
			array('a' => array('href' => '/index/page:7')), '7', '/a',
2383
			'/span',
2384
		);
2385
		$this->assertTags($result, $expected);
2386
 
2387
		$result = $this->Paginator->last(3);
2388
		$this->assertEquals('', $result, 'When inside the last links range, no links should be made');
2389
	}
2390
 
2391
/**
2392
 * undocumented function
2393
 *
2394
 * @return void
2395
 */
2396
	public function testLastOptions() {
2397
		$this->Paginator->request->params['paging'] = array(
2398
			'Client' => array(
2399
				'page' => 4,
2400
				'current' => 3,
2401
				'count' => 30,
2402
				'prevPage' => false,
2403
				'nextPage' => 2,
2404
				'pageCount' => 15,
2405
				'options' => array(
2406
					'page' => 1,
2407
					'order' => array('Client.name' => 'DESC'),
2408
				),
2409
				'paramType' => 'named'
2410
			)
2411
		);
2412
 
2413
		$result = $this->Paginator->last();
2414
		$expected = array(
2415
			'<span',
2416
			array('a' => array(
2417
				'href' => '/index/page:15/sort:Client.name/direction:DESC',
2418
				'rel' => 'last'
2419
			)),
2420
				'last &gt;&gt;', '/a',
2421
			'/span',
2422
		);
2423
		$this->assertTags($result, $expected);
2424
 
2425
		$result = $this->Paginator->last(1);
2426
		$expected = array(
2427
			'...',
2428
			'<span',
2429
			array('a' => array('href' => '/index/page:15/sort:Client.name/direction:DESC')), '15', '/a',
2430
			'/span',
2431
		);
2432
		$this->assertTags($result, $expected);
2433
 
2434
		$result = $this->Paginator->last(2);
2435
		$expected = array(
2436
			'...',
2437
			'<span',
2438
			array('a' => array('href' => '/index/page:14/sort:Client.name/direction:DESC')), '14', '/a',
2439
			'/span',
2440
			' | ',
2441
			'<span',
2442
			array('a' => array('href' => '/index/page:15/sort:Client.name/direction:DESC')), '15', '/a',
2443
			'/span',
2444
		);
2445
		$this->assertTags($result, $expected);
2446
 
2447
		$result = $this->Paginator->last(2, array('ellipsis' => '<span class="ellipsis">...</span>'));
2448
		$expected = array(
2449
			array('span' => array('class' => 'ellipsis')), '...', '/span',
2450
			'<span',
2451
			array('a' => array('href' => '/index/page:14/sort:Client.name/direction:DESC')), '14', '/a',
2452
			'/span',
2453
			' | ',
2454
			'<span',
2455
			array('a' => array('href' => '/index/page:15/sort:Client.name/direction:DESC')), '15', '/a',
2456
			'/span',
2457
		);
2458
		$this->assertTags($result, $expected);
2459
	}
2460
 
2461
/**
2462
 * testCounter method
2463
 *
2464
 * @return void
2465
 */
2466
	public function testCounter() {
2467
		$this->Paginator->request->params['paging'] = array(
2468
			'Client' => array(
2469
				'page' => 1,
2470
				'current' => 3,
2471
				'count' => 13,
2472
				'prevPage' => false,
2473
				'nextPage' => true,
2474
				'pageCount' => 5,
2475
				'limit' => 3,
2476
				'options' => array(
2477
					'page' => 1,
2478
					'order' => array('Client.name' => 'DESC'),
2479
				),
2480
				'paramType' => 'named'
2481
			)
2482
		);
2483
		$input = 'Page %page% of %pages%, showing %current% records out of %count% total, ';
2484
		$input .= 'starting on record %start%, ending on %end%';
2485
		$result = $this->Paginator->counter($input);
2486
		$expected = 'Page 1 of 5, showing 3 records out of 13 total, starting on record 1, ';
2487
		$expected .= 'ending on 3';
2488
		$this->assertEquals($expected, $result);
2489
 
2490
		$input = 'Page {:page} of {:pages}, showing {:current} records out of {:count} total, ';
2491
		$input .= 'starting on record {:start}, ending on {:end}';
2492
		$result = $this->Paginator->counter($input);
2493
		$this->assertEquals($expected, $result);
2494
 
2495
		$input = 'Page %page% of %pages%';
2496
		$result = $this->Paginator->counter($input);
2497
		$expected = 'Page 1 of 5';
2498
		$this->assertEquals($expected, $result);
2499
 
2500
		$result = $this->Paginator->counter(array('format' => $input));
2501
		$expected = 'Page 1 of 5';
2502
		$this->assertEquals($expected, $result);
2503
 
2504
		$result = $this->Paginator->counter(array('format' => 'pages'));
2505
		$expected = '1 of 5';
2506
		$this->assertEquals($expected, $result);
2507
 
2508
		$result = $this->Paginator->counter(array('format' => 'range'));
2509
		$expected = '1 - 3 of 13';
2510
		$this->assertEquals($expected, $result);
2511
 
2512
		$result = $this->Paginator->counter('Showing %page% of %pages% %model%');
2513
		$this->assertEquals('Showing 1 of 5 clients', $result);
2514
	}
2515
 
2516
/**
2517
 * testHasPage method
2518
 *
2519
 * @return void
2520
 */
2521
	public function testHasPage() {
2522
		$result = $this->Paginator->hasPage('Article', 15);
2523
		$this->assertFalse($result);
2524
 
2525
		$result = $this->Paginator->hasPage('UndefinedModel', 2);
2526
		$this->assertFalse($result);
2527
 
2528
		$result = $this->Paginator->hasPage('Article', 2);
2529
		$this->assertTrue($result);
2530
 
2531
		$result = $this->Paginator->hasPage(2);
2532
		$this->assertTrue($result);
2533
	}
2534
 
2535
/**
2536
 * testWithPlugin method
2537
 *
2538
 * @return void
2539
 */
2540
	public function testWithPlugin() {
2541
		Router::reload();
2542
		Router::setRequestInfo(array(
2543
			array(
2544
				'pass' => array(), 'named' => array(), 'prefix' => null, 'form' => array(),
2545
				'controller' => 'magazines', 'plugin' => 'my_plugin', 'action' => 'index',
2546
				'url' => array('ext' => 'html', 'url' => 'my_plugin/magazines')),
2547
			array('base' => '', 'here' => '/my_plugin/magazines', 'webroot' => '/')
2548
		));
2549
 
2550
		$result = $this->Paginator->link('Page 3', array('page' => 3));
2551
		$expected = array(
2552
			'a' => array('href' => '/my_plugin/magazines/index/page:3'), 'Page 3', '/a'
2553
		);
2554
		$this->assertTags($result, $expected);
2555
 
2556
		$this->Paginator->options(array('url' => array('action' => 'another_index')));
2557
		$result = $this->Paginator->link('Page 3', array('page' => 3));
2558
		$expected = array(
2559
			'a' => array('href' => '/my_plugin/magazines/another_index/page:3'), 'Page 3', '/a'
2560
		);
2561
		$this->assertTags($result, $expected);
2562
 
2563
		$this->Paginator->options(array('url' => array('controller' => 'issues')));
2564
		$result = $this->Paginator->link('Page 3', array('page' => 3));
2565
		$expected = array(
2566
			'a' => array('href' => '/my_plugin/issues/index/page:3'), 'Page 3', '/a'
2567
		);
2568
		$this->assertTags($result, $expected);
2569
 
2570
		$this->Paginator->options(array('url' => array('plugin' => null)));
2571
		$result = $this->Paginator->link('Page 3', array('page' => 3));
2572
		$expected = array(
2573
			'a' => array('href' => '/magazines/index/page:3'), 'Page 3', '/a'
2574
		);
2575
		$this->assertTags($result, $expected);
2576
 
2577
		$this->Paginator->options(array('url' => array('plugin' => null, 'controller' => 'issues')));
2578
		$result = $this->Paginator->link('Page 3', array('page' => 3));
2579
		$expected = array(
2580
			'a' => array('href' => '/issues/index/page:3'), 'Page 3', '/a'
2581
		);
2582
		$this->assertTags($result, $expected);
2583
	}
2584
 
2585
/**
2586
 * testNextLinkUsingDotNotation method
2587
 *
2588
 * @return void
2589
 */
2590
	public function testNextLinkUsingDotNotation() {
2591
		Router::reload();
2592
		Router::parse('/');
2593
		Router::setRequestInfo(array(
2594
			array('plugin' => null, 'controller' => 'accounts', 'action' => 'index', 'pass' => array(), 'url' => array('url' => 'accounts/')),
2595
			array('base' => '/officespace', 'here' => '/officespace/accounts/', 'webroot' => '/officespace/', 'passedArgs' => array())
2596
		));
2597
 
2598
		$this->Paginator->request->params['paging']['Article']['options']['order'] = array('Article.title' => 'asc');
2599
		$this->Paginator->request->params['paging']['Article']['page'] = 1;
2600
 
2601
		$test = array('url' => array(
2602
			'page' => '1',
2603
			'sort' => 'Article.title',
2604
			'direction' => 'asc',
2605
		));
2606
		$this->Paginator->options($test);
2607
 
2608
		$result = $this->Paginator->next('Next');
2609
		$expected = array(
2610
			'span' => array('class' => 'next'),
2611
			'a' => array(
2612
				'href' => '/officespace/accounts/index/page:2/sort:Article.title/direction:asc',
2613
				'rel' => 'next'
2614
			),
2615
			'Next',
2616
			'/a',
2617
			'/span',
2618
		);
2619
		$this->assertTags($result, $expected);
2620
	}
2621
 
2622
/**
2623
 * Ensure that the internal link class object is called when the update key is present
2624
 *
2625
 * @return void
2626
 */
2627
	public function testAjaxLinkGenerationNumbers() {
2628
		$this->Paginator->Js->expectCallCount('link', 2);
2629
		$this->Paginator->numbers(array(
2630
			'modulus' => '2',
2631
			'url' => array('controller' => 'projects', 'action' => 'sort'),
2632
			'update' => 'list'
2633
		));
2634
	}
2635
 
2636
/**
2637
 * test that paginatorHelper::link() uses JsHelper to make links when 'update' key is present
2638
 *
2639
 * @return void
2640
 */
2641
	public function testAjaxLinkGenerationLink() {
2642
		$this->Paginator->Js->expects($this->once())
2643
			->method('link')
2644
			->will($this->returnValue('I am a link'));
2645
 
2646
		$result = $this->Paginator->link('test', array('controller' => 'posts'), array('update' => '#content'));
2647
		$this->assertEquals('I am a link', $result);
2648
	}
2649
 
2650
/**
2651
 * test that mock classes injected into paginatorHelper are called when using link()
2652
 *
2653
 * @expectedException CakeException
2654
 * @return void
2655
 */
2656
	public function testMockAjaxProviderClassInjection() {
2657
		$mock = $this->getMock('PaginatorHelper', array(), array($this->View), 'PaginatorMockJsHelper');
2658
		$Paginator = new PaginatorHelper($this->View, array('ajax' => 'PaginatorMockJs'));
2659
		$Paginator->request->params['paging'] = array(
2660
			'Article' => array(
2661
				'current' => 9,
2662
				'count' => 62,
2663
				'prevPage' => false,
2664
				'nextPage' => true,
2665
				'pageCount' => 7,
2666
				'defaults' => array(),
2667
				'options' => array(),
2668
				'paramType' => 'named'
2669
			)
2670
		);
2671
		$Paginator->PaginatorMockJs = $mock;
2672
		$Paginator->PaginatorMockJs->expects($this->once())->method('link');
2673
		$Paginator->link('Page 2', array('page' => 2), array('update' => '#content'));
2674
 
2675
		new PaginatorHelper($this->View, array('ajax' => 'Form'));
2676
	}
2677
 
2678
/**
2679
 * test that query string URLs can be generated.
2680
 *
2681
 * @return void
2682
 */
2683
	public function testQuerystringUrlGeneration() {
2684
		$this->Paginator->request->params['paging']['Article']['paramType'] = 'querystring';
2685
		$result = $this->Paginator->url(array('page' => '1'));
2686
		$expected = '/';
2687
		$this->assertEquals($expected, $result);
2688
 
2689
		$result = $this->Paginator->url(array('page' => '1', 'limit' => 10, 'something' => 'else'));
2690
		$expected = '/index/something:else?limit=10';
2691
		$this->assertEquals($expected, $result);
2692
 
2693
		$result = $this->Paginator->url(array('page' => '4'));
2694
		$expected = '/?page=4';
2695
		$this->assertEquals($expected, $result);
2696
 
2697
		$result = $this->Paginator->url(array('page' => '4', 'limit' => 10, 'something' => 'else'));
2698
		$expected = '/index/something:else?page=4&amp;limit=10';
2699
		$this->assertEquals($expected, $result);
2700
	}
2701
 
2702
/**
2703
 * test query string paging link.
2704
 *
2705
 * @return void
2706
 */
2707
	public function testQuerystringNextAndPrev() {
2708
		$this->Paginator->request->params['paging']['Article']['paramType'] = 'querystring';
2709
		$this->Paginator->request->params['paging']['Article']['page'] = 2;
2710
		$this->Paginator->request->params['paging']['Article']['nextPage'] = true;
2711
		$this->Paginator->request->params['paging']['Article']['prevPage'] = true;
2712
 
2713
		$result = $this->Paginator->next('Next');
2714
		$expected = array(
2715
			'span' => array('class' => 'next'),
2716
			'a' => array('href' => '/?page=3', 'rel' => 'next'),
2717
			'Next',
2718
			'/a',
2719
			'/span'
2720
		);
2721
		$this->assertTags($result, $expected);
2722
 
2723
		$result = $this->Paginator->prev('Prev');
2724
		$expected = array(
2725
			'span' => array('class' => 'prev'),
2726
			'a' => array('href' => '/', 'rel' => 'prev'),
2727
			'Prev',
2728
			'/a',
2729
			'/span'
2730
		);
2731
		$this->assertTags($result, $expected);
2732
	}
2733
 
2734
/**
2735
 * test that additional keys can be flagged as query string args.
2736
 *
2737
 * @return void
2738
 */
2739
	public function testOptionsConvertKeys() {
2740
		$this->Paginator->options(array(
2741
			'convertKeys' => array('something'),
2742
			'Article' => array('paramType' => 'querystring')
2743
		));
2744
		$result = $this->Paginator->url(array('page' => '4', 'something' => 'bar'));
2745
		$expected = '/?page=4&amp;something=bar';
2746
		$this->assertEquals($expected, $result);
2747
	}
2748
 
2749
/**
2750
 * test the current() method
2751
 *
2752
 * @return void
2753
 */
2754
	public function testCurrent() {
2755
		$result = $this->Paginator->current();
2756
		$this->assertEquals($this->Paginator->request->params['paging']['Article']['page'], $result);
2757
 
2758
		$result = $this->Paginator->current('Incorrect');
2759
		$this->assertEquals(1, $result);
2760
	}
2761
 
2762
/**
2763
 * test the defaultModel() method
2764
 *
2765
 * @return void
2766
 */
2767
	public function testNoDefaultModel() {
2768
		$this->Paginator->request = new CakeRequest(null, false);
2769
		$this->assertNull($this->Paginator->defaultModel());
2770
	}
2771
 
2772
/**
2773
 * test the numbers() method when there is only one page
2774
 *
2775
 * @return void
2776
 */
2777
	public function testWithOnePage() {
2778
		$this->Paginator->request['paging'] = array(
2779
			'Article' => array(
2780
				'page' => 1,
2781
				'current' => 2,
2782
				'count' => 2,
2783
				'prevPage' => false,
2784
				'nextPage' => true,
2785
				'pageCount' => 1,
2786
				'options' => array(
2787
					'page' => 1,
2788
				),
2789
				'paramType' => 'named',
2790
			)
2791
		);
2792
		$this->assertSame('', $this->Paginator->numbers());
2793
		$this->assertSame('', $this->Paginator->first());
2794
		$this->assertSame('', $this->Paginator->last());
2795
	}
2796
 
2797
/**
2798
 * test the numbers() method when there is only one page
2799
 *
2800
 * @return void
2801
 */
2802
	public function testWithZeroPages() {
2803
		$this->Paginator->request['paging'] = array(
2804
			'Article' => array(
2805
				'page' => 0,
2806
				'current' => 0,
2807
				'count' => 0,
2808
				'prevPage' => false,
2809
				'nextPage' => false,
2810
				'pageCount' => 0,
2811
				'limit' => 10,
2812
				'options' => array(
2813
					'page' => 0,
2814
					'conditions' => array()
2815
				),
2816
				'paramType' => 'named',
2817
			)
2818
		);
2819
 
2820
		$result = $this->Paginator->counter(array('format' => 'pages'));
2821
		$expected = '0 of 1';
2822
		$this->assertEquals($expected, $result);
2823
	}
2824
 
2825
/**
2826
 * Verify that no next and prev links are created for single page results
2827
 *
2828
 * @return void
2829
 */
2830
	public function testMetaPage0() {
2831
		$this->Paginator->request['paging'] = array(
2832
			'Article' => array(
2833
				'page' => 1,
2834
				'prevPage' => false,
2835
				'nextPage' => false,
2836
				'pageCount' => 1,
2837
			)
2838
		);
2839
		$expected = '';
2840
		$result = $this->Paginator->meta();
2841
		$this->assertSame($expected, $result);
2842
	}
2843
 
2844
/**
2845
 * Verify that page 1 only has a next link
2846
 *
2847
 * @return void
2848
 */
2849
	public function testMetaPage1() {
2850
		$this->Paginator->request['paging'] = array(
2851
			'Article' => array(
2852
				'page' => 1,
2853
				'prevPage' => false,
2854
				'nextPage' => true,
2855
				'pageCount' => 2,
2856
				'options' => array(),
2857
				'paramType' => 'querystring'
2858
			)
2859
		);
2860
		$expected = '<link href="/?page=2" rel="next"/>';
2861
		$result = $this->Paginator->meta();
2862
		$this->assertSame($expected, $result);
2863
	}
2864
 
2865
/**
2866
 * Verify that the method will append to a block
2867
 *
2868
 * @return void
2869
 */
2870
	public function testMetaPage1InlineFalse() {
2871
		$this->Paginator->request['paging'] = array(
2872
			'Article' => array(
2873
				'page' => 1,
2874
				'prevPage' => false,
2875
				'nextPage' => true,
2876
				'pageCount' => 2,
2877
				'options' => array(),
2878
				'paramType' => 'querystring'
2879
			)
2880
		);
2881
		$expected = '<link href="/?page=2" rel="next"/>';
2882
		$this->Paginator->meta(array('block' => true));
2883
		$result = $this->View->fetch('meta');
2884
		$this->assertSame($expected, $result);
2885
	}
2886
 
2887
/**
2888
 * Verify that the last page only has a prev link
2889
 *
2890
 * @return void
2891
 */
2892
	public function testMetaPage1Last() {
2893
		$this->Paginator->request['paging'] = array(
2894
			'Article' => array(
2895
				'page' => 2,
2896
				'prevPage' => true,
2897
				'nextPage' => false,
2898
				'pageCount' => 2,
2899
				'options' => array(),
2900
				'paramType' => 'querystring'
2901
			)
2902
		);
2903
		$expected = '<link href="/" rel="prev"/>';
2904
		$result = $this->Paginator->meta();
2905
		$this->assertSame($expected, $result);
2906
	}
2907
 
2908
/**
2909
 * Verify that a page in the middle has both links
2910
 *
2911
 * @return void
2912
 */
2913
	public function testMetaPage10Last() {
2914
		$this->Paginator->request['paging'] = array(
2915
			'Article' => array(
2916
				'page' => 5,
2917
				'prevPage' => true,
2918
				'nextPage' => true,
2919
				'pageCount' => 10,
2920
				'options' => array(),
2921
				'paramType' => 'querystring'
2922
			)
2923
		);
2924
		$expected = '<link href="/?page=4" rel="prev"/>';
2925
		$expected .= '<link href="/?page=6" rel="next"/>';
2926
		$result = $this->Paginator->meta();
2927
		$this->assertSame($expected, $result);
2928
	}
2929
 
2930
}