View = new View(); $this->Paginator = new PaginatorHelper($this->View); $this->Paginator->request = new ServerRequest([ 'url' => '/', 'params' => [ 'paging' => [ 'Article' => [ 'page' => 1, 'current' => 9, 'count' => 62, 'prevPage' => false, 'nextPage' => true, 'pageCount' => 7, 'sort' => null, 'direction' => null, 'limit' => null, ] ] ] ]); Router::reload(); Router::connect('/:controller/:action/*'); Router::connect('/:plugin/:controller/:action/*'); $this->locale = I18n::getLocale(); } /** * tearDown method * * @return void */ public function tearDown() { parent::tearDown(); unset($this->View, $this->Paginator); I18n::setLocale($this->locale); } /** * Test the templates method. * * @return void */ public function testTemplates() { $result = $this->Paginator->setTemplates([ 'test' => 'val' ]); $this->assertSame( $this->Paginator, $result, 'Setting should return the same object' ); $result = $this->Paginator->getTemplates(); $this->assertArrayHasKey('test', $result); $this->assertEquals('val', $result['test']); $this->assertEquals('val', $this->Paginator->getTemplates('test')); } /** * testHasPrevious method * * @return void */ public function testHasPrevious() { $this->assertFalse($this->Paginator->hasPrev()); $this->Paginator->request = $this->Paginator->request->withParam('paging.Article.prevPage', true); $this->assertTrue($this->Paginator->hasPrev()); } /** * testHasNext method * * @return void */ public function testHasNext() { $this->assertTrue($this->Paginator->hasNext()); $this->Paginator->request = $this->Paginator->request->withParam('paging.Article.nextPage', false); $this->assertFalse($this->Paginator->hasNext()); } /** * testSortLinks method * * @return void */ public function testSortLinks() { $request = new ServerRequest([ 'url' => '/accounts/', 'params' => [ 'plugin' => null, 'controller' => 'accounts', 'action' => 'index', 'pass' => [] ], 'base' => '', 'webroot' => '/' ]); Router::setRequestInfo($request); $this->Paginator->options(['url' => ['param']]); $this->Paginator->request = $this->Paginator->request->withParam('paging', [ 'Article' => [ 'current' => 9, 'count' => 62, 'prevPage' => false, 'nextPage' => true, 'pageCount' => 7, 'sort' => 'date', 'direction' => 'asc', 'page' => 1, ] ]); $result = $this->Paginator->sort('title'); $expected = [ 'a' => ['href' => '/accounts/index/param?sort=title&direction=asc'], 'Title', '/a' ]; $this->assertHtml($expected, $result); $result = $this->Paginator->sort('title', null, ['model' => 'Nope']); $this->assertHtml($expected, $result); $result = $this->Paginator->sort('title', null, ['model' => 'Article']); $this->assertHtml($expected, $result); $result = $this->Paginator->sort('date'); $expected = [ 'a' => ['href' => '/accounts/index/param?sort=date&direction=desc', 'class' => 'asc'], 'Date', '/a' ]; $this->assertHtml($expected, $result); $result = $this->Paginator->sort('title', 'TestTitle'); $expected = [ 'a' => ['href' => '/accounts/index/param?sort=title&direction=asc'], 'TestTitle', '/a' ]; $this->assertHtml($expected, $result); $result = $this->Paginator->sort('title', ['asc' => 'ascending', 'desc' => 'descending']); $expected = [ 'a' => ['href' => '/accounts/index/param?sort=title&direction=asc'], 'ascending', '/a' ]; $this->assertHtml($expected, $result); $this->Paginator->request = $this->Paginator->request->withParam('paging.Article.sort', 'title'); $result = $this->Paginator->sort('title', ['asc' => 'ascending', 'desc' => 'descending']); $expected = [ 'a' => ['href' => '/accounts/index/param?sort=title&direction=desc', 'class' => 'asc'], 'descending', '/a' ]; $this->assertHtml($expected, $result); $this->Paginator->request = $this->Paginator->request ->withParam('paging.Article.sort', 'Article.title') ->withParam('paging.Article.direction', 'desc'); $result = $this->Paginator->sort('title'); $expected = [ 'a' => ['href' => '/accounts/index/param?sort=title&direction=asc', 'class' => 'desc'], 'Title', '/a' ]; $this->assertHtml($expected, $result); $this->Paginator->request = $this->Paginator->request ->withParam('paging.Article.sort', 'Article.title') ->withParam('paging.Article.direction', 'asc'); $result = $this->Paginator->sort('title'); $expected = [ 'a' => ['href' => '/accounts/index/param?sort=title&direction=desc', 'class' => 'asc'], 'Title', '/a' ]; $this->assertHtml($expected, $result); $this->Paginator->request = $this->Paginator->request ->withParam('paging.Article.sort', 'Article.title') ->withParam('paging.Article.direction', 'desc'); $result = $this->Paginator->sort('title', 'Title', ['direction' => 'desc']); $expected = [ 'a' => ['href' => '/accounts/index/param?sort=title&direction=asc', 'class' => 'desc'], 'Title', '/a' ]; $this->assertHtml($expected, $result); $this->Paginator->request = $this->Paginator->request ->withParam('paging.Article.sort', 'Article.title') ->withParam('paging.Article.direction', 'desc'); $result = $this->Paginator->sort('title', 'Title', ['direction' => 'ASC']); $expected = [ 'a' => ['href' => '/accounts/index/param?sort=title&direction=asc', 'class' => 'desc'], 'Title', '/a' ]; $this->assertHtml($expected, $result); $this->Paginator->request = $this->Paginator->request ->withParam('paging.Article.sort', 'Article.title') ->withParam('paging.Article.direction', 'asc'); $result = $this->Paginator->sort('title', 'Title', ['direction' => 'asc']); $expected = [ 'a' => ['href' => '/accounts/index/param?sort=title&direction=desc', 'class' => 'asc'], 'Title', '/a' ]; $this->assertHtml($expected, $result); $this->Paginator->request = $this->Paginator->request ->withParam('paging.Article.sort', 'Article.title') ->withParam('paging.Article.direction', 'asc'); $result = $this->Paginator->sort('title', 'Title', ['direction' => 'desc']); $expected = [ 'a' => ['href' => '/accounts/index/param?sort=title&direction=desc', 'class' => 'asc'], 'Title', '/a' ]; $this->assertHtml($expected, $result); } /** * test sort() with escape option */ public function testSortEscape() { $result = $this->Paginator->sort('title', 'TestTitle >'); $expected = [ 'a' => ['href' => '/index?sort=title&direction=asc'], 'TestTitle >', '/a' ]; $this->assertHtml($expected, $result); $result = $this->Paginator->sort('title', 'TestTitle >', ['escape' => true]); $this->assertHtml($expected, $result); $result = $this->Paginator->sort('title', 'TestTitle >', ['escape' => false]); $expected = [ 'a' => ['href' => '/index?sort=title&direction=asc'], 'TestTitle >', '/a' ]; $this->assertHtml($expected, $result); } /** * test that sort() works with virtual field order options. * * @return void */ public function testSortLinkWithVirtualField() { $request = new ServerRequest([ 'url' => '/accounts/', 'params' => [ 'plugin' => null, 'controller' => 'accounts', 'action' => 'index', 'pass' => [] ], 'base' => '', 'webroot' => '/' ]); Router::setRequestInfo($request); $this->Paginator->request = $this->Paginator->request ->withParam('paging.Article.sort', 'full_name') ->withParam('paging.Article.direction', 'asc'); $result = $this->Paginator->sort('Article.full_name'); $expected = [ 'a' => ['href' => '/accounts/index?sort=Article.full_name&direction=desc', 'class' => 'asc'], 'Article Full Name', '/a' ]; $this->assertHtml($expected, $result); $result = $this->Paginator->sort('full_name'); $expected = [ 'a' => ['href' => '/accounts/index?sort=full_name&direction=desc', 'class' => 'asc'], 'Full Name', '/a' ]; $this->assertHtml($expected, $result); $this->Paginator->request = $this->Paginator->request ->withParam('paging.Article.sort', 'full_name') ->withParam('paging.Article.direction', 'desc'); $result = $this->Paginator->sort('Article.full_name'); $expected = [ 'a' => ['href' => '/accounts/index?sort=Article.full_name&direction=asc', 'class' => 'desc'], 'Article Full Name', '/a' ]; $this->assertHtml($expected, $result); $result = $this->Paginator->sort('full_name'); $expected = [ 'a' => ['href' => '/accounts/index?sort=full_name&direction=asc', 'class' => 'desc'], 'Full Name', '/a' ]; $this->assertHtml($expected, $result); } /** * testSortLinksUsingDirectionOption method * * @return void */ public function testSortLinksUsingDirectionOption() { $request = new ServerRequest([ 'url' => '/accounts/', 'params' => [ 'plugin' => null, 'controller' => 'accounts', 'action' => 'index', 'pass' => [] ], 'base' => '', 'webroot' => '/' ]); Router::setRequestInfo($request); $this->Paginator->options(['url' => ['param']]); $result = $this->Paginator->sort('title', 'TestTitle', ['direction' => 'desc']); $expected = [ 'a' => ['href' => '/accounts/index/param?sort=title&direction=desc'], 'TestTitle', '/a' ]; $this->assertHtml($expected, $result); $result = $this->Paginator->sort('title', ['asc' => 'ascending', 'desc' => 'descending'], ['direction' => 'desc']); $expected = [ 'a' => ['href' => '/accounts/index/param?sort=title&direction=desc'], 'descending', '/a' ]; $this->assertHtml($expected, $result); } /** * testSortLinksUsingDotNotation method * * @return void */ public function testSortLinksUsingDotNotation() { $request = new ServerRequest([ 'url' => '/accounts/', 'params' => [ 'plugin' => null, 'controller' => 'accounts', 'action' => 'index', 'pass' => [] ], 'base' => '', 'webroot' => '/' ]); Router::setRequestInfo($request); $this->Paginator->request = $this->Paginator->request ->withParam('paging.Article.sort', 'Article.title') ->withParam('paging.Article.direction', 'desc'); $result = $this->Paginator->sort('Article.title'); $expected = [ 'a' => ['href' => '/accounts/index?sort=Article.title&direction=asc', 'class' => 'desc'], 'Article Title', '/a' ]; $this->assertHtml($expected, $result); $this->Paginator->request = $this->Paginator->request ->withParam('paging.Article.sort', 'Article.title') ->withParam('paging.Article.direction', 'desc'); $result = $this->Paginator->sort('Article.title', 'Title'); $expected = [ 'a' => ['href' => '/accounts/index?sort=Article.title&direction=asc', 'class' => 'desc'], 'Title', '/a' ]; $this->assertHtml($expected, $result); $this->Paginator->request = $this->Paginator->request ->withParam('paging.Article.sort', 'Article.title') ->withParam('paging.Article.direction', 'asc'); $result = $this->Paginator->sort('Article.title', 'Title'); $expected = [ 'a' => ['href' => '/accounts/index?sort=Article.title&direction=desc', 'class' => 'asc'], 'Title', '/a' ]; $this->assertHtml($expected, $result); $this->Paginator->request = $this->Paginator->request ->withParam('paging.Article.sort', 'Account.title') ->withParam('paging.Article.direction', 'asc'); $result = $this->Paginator->sort('title'); $expected = [ 'a' => ['href' => '/accounts/index?sort=title&direction=asc'], 'Title', '/a' ]; $this->assertHtml($expected, $result); } /** * test multiple pagination sort links * * @return void */ public function testSortLinksMultiplePagination() { $request = new ServerRequest([ 'url' => '/accounts/', 'params' => [ 'plugin' => null, 'controller' => 'accounts', 'action' => 'index', 'pass' => [] ], 'base' => '', 'webroot' => '/' ]); Router::setRequestInfo($request); $this->Paginator->options(['model' => 'Articles']); $this->Paginator->request = $this->Paginator->request->withParam('paging', [ 'Articles' => [ 'current' => 9, 'count' => 62, 'prevPage' => false, 'nextPage' => true, 'pageCount' => 7, 'sort' => 'date', 'direction' => 'asc', 'page' => 1, 'scope' => 'article', ], 'Tags' => [ 'current' => 1, 'count' => 100, 'prevPage' => false, 'nextPage' => true, 'pageCount' => 5, 'sort' => 'tag', 'direction' => 'asc', 'page' => 1, 'scope' => 'tags', ] ]); $result = $this->Paginator->sort('title', 'Title', ['model' => 'Articles']); $expected = [ 'a' => ['href' => '/accounts/index?article%5Bsort%5D=title&article%5Bdirection%5D=asc'], 'Title', '/a' ]; $this->assertHtml($expected, $result); $result = $this->Paginator->sort('tag', 'Tag', ['model' => 'Tags']); $expected = [ 'a' => ['class' => 'asc', 'href' => '/accounts/index?tags%5Bsort%5D=tag&tags%5Bdirection%5D=desc'], 'Tag', '/a' ]; $this->assertHtml($expected, $result); } /** * Test creating paging links for missing models. * * @return void */ public function testPagingLinksMissingModel() { $result = $this->Paginator->sort('title', 'Title', ['model' => 'Missing']); $expected = [ 'a' => ['href' => '/index?sort=title&direction=asc'], 'Title', '/a' ]; $this->assertHtml($expected, $result); $result = $this->Paginator->next('Next', ['model' => 'Missing']); $expected = [ 'li' => ['class' => 'next disabled'], 'a' => ['href' => '', 'onclick' => 'return false;'], 'Next', '/a', '/li' ]; $this->assertHtml($expected, $result); $result = $this->Paginator->prev('Prev', ['model' => 'Missing']); $expected = [ 'li' => ['class' => 'prev disabled'], 'a' => ['href' => '', 'onclick' => 'return false;'], 'Prev', '/a', '/li' ]; $this->assertHtml($expected, $result); } /** * testSortKey method * * @return void */ public function testSortKey() { $result = $this->Paginator->sortKey('Article', ['sort' => 'Article.title']); $this->assertEquals('Article.title', $result); $result = $this->Paginator->sortKey('Article', ['sort' => 'Article']); $this->assertEquals('Article', $result); } /** * Test that sortKey falls back to the default sorting options set * in the $params which are the default pagination options. * * @return void */ public function testSortKeyFallbackToParams() { $this->Paginator->request = $this->Paginator->request->withParam('paging.Article.sort', 'Article.body'); $result = $this->Paginator->sortKey(); $this->assertEquals('Article.body', $result); $result = $this->Paginator->sortKey('Article'); $this->assertEquals('Article.body', $result); $this->Paginator->request = $this->Paginator->request ->withParam('paging.Article.sort', 'Article.body') ->withParam('paging.Article.order', 'DESC'); $result = $this->Paginator->sortKey(); $this->assertEquals('Article.body', $result); $result = $this->Paginator->sortKey('Article'); $this->assertEquals('Article.body', $result); } /** * testSortDir method * * @return void */ public function testSortDir() { $result = $this->Paginator->sortDir(); $expected = 'asc'; $this->assertEquals($expected, $result); $this->Paginator->request = $this->Paginator->request ->withParam('paging.Article.sort', 'Article.title') ->withParam('paging.Article.direction', 'desc'); $result = $this->Paginator->sortDir(); $this->assertEquals('desc', $result); $this->Paginator->request = $this->Paginator->request ->withParam('paging.Article.sort', 'Article.title') ->withParam('paging.Article.direction', 'asc'); $result = $this->Paginator->sortDir(); $this->assertEquals('asc', $result); $this->Paginator->request = $this->Paginator->request ->withParam('paging.Article.sort', 'title') ->withParam('paging.Article.direction', 'desc'); $result = $this->Paginator->sortDir(); $this->assertEquals('desc', $result); $this->Paginator->request = $this->Paginator->request ->withParam('paging.Article.sort', 'title') ->withParam('paging.Article.direction', 'asc'); $result = $this->Paginator->sortDir(); $this->assertEquals('asc', $result); $this->Paginator->request = $this->Paginator->request->withParam('paging.Article.direction', null); $result = $this->Paginator->sortDir('Article', ['direction' => 'asc']); $this->assertEquals('asc', $result); $result = $this->Paginator->sortDir('Article', ['direction' => 'desc']); $this->assertEquals('desc', $result); $result = $this->Paginator->sortDir('Article', ['direction' => 'asc']); $this->assertEquals('asc', $result); } /** * Test that sortDir falls back to the default sorting options set * in the $params which are the default pagination options. * * @return void */ public function testSortDirFallbackToParams() { $this->Paginator->request = $this->Paginator->request ->withParam('paging.Article.sort', 'Article.body') ->withParam('paging.Article.direction', 'asc'); $result = $this->Paginator->sortDir(); $this->assertEquals('asc', $result); $result = $this->Paginator->sortDir('Article'); $this->assertEquals('asc', $result); $this->Paginator->request = $this->Paginator->request ->withParam('paging.Article.sort', 'Article.body') ->withParam('paging.Article.direction', 'DESC'); $result = $this->Paginator->sortDir(); $this->assertEquals('desc', $result); $result = $this->Paginator->sortDir('Article'); $this->assertEquals('desc', $result); } /** * testSortAdminLinks method * * @return void */ public function testSortAdminLinks() { Router::reload(); Router::connect('/admin/:controller/:action/*', ['prefix' => 'admin']); $request = new ServerRequest([ 'url' => '/admin/users', 'params' => [ 'plugin' => null, 'controller' => 'users', 'action' => 'index', 'prefix' => 'admin' ], 'base' => '', 'webroot' => '/' ]); Router::setRequestInfo($request); $this->Paginator->request = $this->Paginator->request->withParam('paging.Article.page', 1); $result = $this->Paginator->next('Next'); $expected = [ 'li' => ['class' => 'next'], 'a' => ['href' => '/admin/users/index?page=2', 'rel' => 'next'], 'Next', '/a', '/li' ]; $this->assertHtml($expected, $result); $this->Paginator->options(['url' => ['param']]); $result = $this->Paginator->sort('title'); $expected = [ 'a' => ['href' => '/admin/users/index/param?sort=title&direction=asc'], 'Title', '/a' ]; $this->assertHtml($expected, $result); $this->Paginator->options(['url' => ['param']]); $result = $this->Paginator->sort('Article.title', 'Title'); $expected = [ 'a' => ['href' => '/admin/users/index/param?sort=Article.title&direction=asc'], 'Title', '/a' ]; $this->assertHtml($expected, $result); } /** * Test that generated URLs work without sort defined within the request * * @return void */ public function testDefaultSortAndNoSort() { $request = new ServerRequest([ 'url' => '/articles/', 'params' => [ 'plugin' => null, 'controller' => 'articles', 'action' => 'index' ], 'base' => '', 'webroot' => '/' ]); Router::setRequestInfo($request); $this->Paginator->request = $this->Paginator->request->withParam('paging', [ 'Article' => [ 'page' => 1, 'current' => 3, 'count' => 13, 'prevPage' => false, 'nextPage' => true, 'pageCount' => 8, 'sortDefault' => 'Article.title', 'directionDefault' => 'ASC', 'sort' => null ] ]); $result = $this->Paginator->next('Next'); $expected = [ 'li' => ['class' => 'next'], 'a' => ['rel' => 'next', 'href' => '/articles/index?page=2'], 'Next', '/a', '/li' ]; $this->assertHtml($expected, $result); } /** * testUrlGeneration method * * @return void */ public function testUrlGeneration() { $result = $this->Paginator->sort('controller'); $expected = [ 'a' => ['href' => '/index?sort=controller&direction=asc'], 'Controller', '/a' ]; $this->assertHtml($expected, $result); $result = $this->Paginator->generateUrl(); $this->assertEquals('/index', $result); $this->Paginator->request = $this->Paginator->request->withParam('paging.Article.page', 2); $result = $this->Paginator->generateUrl(); $this->assertEquals('/index?page=2', $result); $options = ['sort' => 'Article', 'direction' => 'desc']; $result = $this->Paginator->generateUrl($options); $this->assertEquals('/index?page=2&sort=Article&direction=desc', $result); $this->Paginator->request = $this->Paginator->request->withParam('paging.Article.page', 3); $options = ['sort' => 'Article.name', 'direction' => 'desc']; $result = $this->Paginator->generateUrl($options); $this->assertEquals('/index?page=3&sort=Article.name&direction=desc', $result); $this->Paginator->request = $this->Paginator->request->withParam('paging.Article.page', 3); $options = ['sort' => 'Article.name', 'direction' => 'desc']; $result = $this->Paginator->generateUrl($options, null, ['escape' => false]); $this->assertEquals('/index?page=3&sort=Article.name&direction=desc', $result); $this->Paginator->request = $this->Paginator->request->withParam('paging.Article.page', 3); $options = ['sort' => 'Article.name', 'direction' => 'desc']; $result = $this->Paginator->generateUrl($options, null, ['fullBase' => true]); $this->assertEquals('http://localhost/index?page=3&sort=Article.name&direction=desc', $result); // @deprecated 3.3.5 Use fullBase array option instead. $this->Paginator->request = $this->Paginator->request->withParam('paging.Article.page', 3); $options = ['sort' => 'Article.name', 'direction' => 'desc']; $result = $this->Paginator->generateUrl($options, null, true); $this->assertEquals('http://localhost/index?page=3&sort=Article.name&direction=desc', $result); } /** * Verify that sort links always result in a url that is page 1 (page not * present in the url) * * @param string $field * @param array $options * @param string $expected * @dataProvider urlGenerationResetsToPage1Provider */ public function testUrlGenerationResetsToPage1($field, $options, $expected) { $this->Paginator->request = $this->Paginator->request ->withParam('paging.Article.page', 2) ->withParam('paging.Article.sort', 'name') ->withParam('paging.Article.direction', 'asc'); $result = $this->Paginator->sort($field, null, ['url' => $options]); $this->assertSame($expected, $result); } /** * Returns data sets of: * * the name of the field being sorted on * * url paramters to pass to paginator sort * * expected result as a string * * @return array */ public function urlGenerationResetsToPage1Provider() { return [ 'Sorting the field currently sorted asc, asc' => [ 'name', ['sort' => 'name', 'direction' => 'asc'], 'Name' ], 'Sorting the field currently sorted asc, desc' => [ 'name', ['sort' => 'name', 'direction' => 'desc'], 'Name' ], 'Sorting other asc' => [ 'other', ['sort' => 'other', 'direction' => 'asc'], 'Other' ], 'Sorting other desc' => [ 'other', ['sort' => 'other', 'direction' => 'desc'], 'Other' ] ]; } /** * test URL generation with prefix routes * * @return void */ public function testGenerateUrlWithPrefixes() { Router::reload(); Router::connect('/members/:controller/:action/*', ['prefix' => 'members']); Router::connect('/:controller/:action/*'); $request = new ServerRequest([ 'url' => '/posts/index/', 'params' => [ 'plugin' => null, 'controller' => 'posts', 'action' => 'index', 'pass' => [] ], 'base' => '', 'webroot' => '/' ]); Router::setRequestInfo($request); $this->Paginator->request = $this->Paginator->request ->withParam('paging.Article.page', 2) ->withParam('paging.Article.prevPage', true); $options = ['prefix' => 'members']; $result = $this->Paginator->generateUrl($options); $expected = '/members/posts/index?page=2'; $this->assertEquals($expected, $result); $result = $this->Paginator->sort('name', null, ['url' => $options]); $expected = [ 'a' => ['href' => '/members/posts/index?sort=name&direction=asc'], 'Name', '/a' ]; $this->assertHtml($expected, $result); $result = $this->Paginator->next('next', ['url' => $options]); $expected = [ 'li' => ['class' => 'next'], 'a' => ['href' => '/members/posts/index?page=3', 'rel' => 'next'], 'next', '/a', '/li' ]; $this->assertHtml($expected, $result); $result = $this->Paginator->prev('prev', ['url' => $options]); $expected = [ 'li' => ['class' => 'prev'], 'a' => ['href' => '/members/posts/index', 'rel' => 'prev'], 'prev', '/a', '/li' ]; $this->assertHtml($expected, $result); $options = ['prefix' => 'members', 'controller' => 'posts', 'sort' => 'name', 'direction' => 'desc']; $result = $this->Paginator->generateUrl($options); $expected = '/members/posts/index?page=2&sort=name&direction=desc'; $this->assertEquals($expected, $result); $options = ['controller' => 'posts', 'sort' => 'Article.name', 'direction' => 'desc']; $result = $this->Paginator->generateUrl($options); $expected = '/posts/index?page=2&sort=Article.name&direction=desc'; $this->assertEquals($expected, $result); } /** * test URL generation can leave prefix routes * * @return void */ public function testGenerateUrlWithPrefixesLeavePrefix() { Router::reload(); Router::connect('/members/:controller/:action/*', ['prefix' => 'members']); Router::connect('/:controller/:action/*'); $request = new ServerRequest([ 'params' => [ 'prefix' => 'members', 'controller' => 'posts', 'action' => 'index', 'plugin' => null, 'paging' => [ 'Articles' => ['page' => 2, 'prevPage' => true] ] ], 'webroot' => '/' ]); Router::setRequestInfo($request); $this->Paginator->request = $request; $result = $this->Paginator->generateUrl(); $expected = '/members/posts/index?page=2'; $this->assertEquals($expected, $result); $result = $this->Paginator->generateUrl(['prefix' => 'members']); $expected = '/members/posts/index?page=2'; $this->assertEquals($expected, $result); $result = $this->Paginator->generateUrl(['prefix' => false]); $expected = '/posts/index?page=2'; $this->assertEquals($expected, $result); $this->Paginator->options(['url' => ['prefix' => false]]); $result = $this->Paginator->generateUrl(); $this->assertEquals($expected, $result, 'Setting prefix in options should work too.'); } /** * test generateUrl with multiple pagination * * @return void */ public function testGenerateUrlMultiplePagination() { $request = new ServerRequest([ 'url' => '/posts/index/', 'params' => [ 'plugin' => null, 'controller' => 'posts', 'action' => 'index', 'pass' => [] ], 'base' => '', 'webroot' => '/' ]); Router::setRequestInfo($request); $this->Paginator->request = $this->Paginator->request ->withParam('paging.Article.scope', 'article') ->withParam('paging.Article.page', 3) ->withParam('paging.Article.prevPage', true); $this->Paginator->options(['model' => 'Article']); $result = $this->Paginator->generateUrl([]); $expected = '/posts/index?article%5Bpage%5D=3'; $this->assertEquals($expected, $result); $result = $this->Paginator->sort('name'); $expected = [ 'a' => ['href' => '/posts/index?article%5Bsort%5D=name&article%5Bdirection%5D=asc'], 'Name', '/a' ]; $this->assertHtml($expected, $result); $result = $this->Paginator->next('next'); $expected = [ 'li' => ['class' => 'next'], 'a' => ['href' => '/posts/index?article%5Bpage%5D=4', 'rel' => 'next'], 'next', '/a', '/li' ]; $this->assertHtml($expected, $result); $result = $this->Paginator->prev('prev'); $expected = [ 'li' => ['class' => 'prev'], 'a' => ['href' => '/posts/index?article%5Bpage%5D=2', 'rel' => 'prev'], 'prev', '/a', '/li' ]; $this->assertHtml($expected, $result); $result = $this->Paginator->generateUrl(['sort' => 'name']); $expected = '/posts/index?article%5Bpage%5D=3&article%5Bsort%5D=name'; $this->assertEquals($expected, $result); $result = $this->Paginator->generateUrl(['#' => 'foo']); $expected = '/posts/index?article%5Bpage%5D=3#foo'; $this->assertEquals($expected, $result); } /** * test generateUrl with multiple pagination and query string values * * @return void */ public function testGenerateUrlMultiplePaginationQueryStringData() { $request = new ServerRequest([ 'url' => '/posts/index/', 'params' => [ 'plugin' => null, 'controller' => 'posts', 'action' => 'index' ] ]); Router::setRequestInfo($request); $this->View->request = $this->Paginator->request ->withParam('paging.Article.scope', 'article') ->withParam('paging.Article.page', 3) ->withParam('paging.Article.prevPage', true) ->withQueryParams([ 'article' => [ 'puppy' => 'no' ] ]); // Need to run __construct to update _config['url'] $paginator = new PaginatorHelper($this->View); $paginator->options(['model' => 'Article']); $result = $paginator->generateUrl(['sort' => 'name']); $expected = '/posts/index?article%5Bpage%5D=3&article%5Bsort%5D=name&article%5Bpuppy%5D=no'; $this->assertEquals($expected, $result); $result = $paginator->generateUrl([]); $expected = '/posts/index?article%5Bpage%5D=3&article%5Bpuppy%5D=no'; $this->assertEquals($expected, $result); } /** * testOptions method * * @return void */ public function testOptions() { $this->Paginator->options = []; $this->Paginator->request = $this->Paginator->request->withAttribute('params', []); $options = ['paging' => ['Article' => [ 'direction' => 'desc', 'sort' => 'title' ]]]; $this->Paginator->options($options); $expected = ['Article' => [ 'direction' => 'desc', 'sort' => 'title' ]]; $this->assertEquals($expected, $this->Paginator->request->getParam('paging')); $this->Paginator->options = []; $this->Paginator->request = $this->Paginator->request->withAttribute('params', []); $options = ['Article' => [ 'direction' => 'desc', 'sort' => 'title' ]]; $this->Paginator->options($options); $this->assertEquals($expected, $this->Paginator->request->getParam('paging')); $options = ['paging' => ['Article' => [ 'direction' => 'desc', 'sort' => 'Article.title' ]]]; $this->Paginator->options($options); $expected = ['Article' => [ 'direction' => 'desc', 'sort' => 'Article.title' ]]; $this->assertEquals($expected, $this->Paginator->request->getParam('paging')); } /** * testPassedArgsMergingWithUrlOptions method * * @return void */ public function testPassedArgsMergingWithUrlOptions() { $request = new ServerRequest([ 'url' => '/articles/', 'params' => [ 'plugin' => null, 'controller' => 'articles', 'action' => 'index', 'pass' => [] ], ]); Router::setRequestInfo($request); $this->Paginator->request = $this->Paginator->request->withParam('paging', [ 'Article' => [ 'page' => 1, 'current' => 3, 'count' => 13, 'prevPage' => false, 'nextPage' => true, 'pageCount' => 8, 'sort' => null, 'direction' => null, ] ]); $this->Paginator->request = $this->Paginator->request ->withParam('pass', [2]) ->withQueryParams(['page' => 1, 'foo' => 'bar', 'x' => 'y', 'num' => 0]); $this->View->request = $this->Paginator->request; $this->Paginator = new PaginatorHelper($this->View); $result = $this->Paginator->sort('title'); $expected = [ 'a' => ['href' => '/articles/index/2?foo=bar&x=y&num=0&sort=title&direction=asc'], 'Title', '/a' ]; $this->assertHtml($expected, $result); $result = $this->Paginator->numbers(); $expected = [ ['li' => ['class' => 'active']], ' []], ['a' => ['href' => '/articles/index/2?foo=bar&x=y&num=0&page=2']], '2', '/a', '/li', ['li' => []], ['a' => ['href' => '/articles/index/2?foo=bar&x=y&num=0&page=3']], '3', '/a', '/li', ['li' => []], ['a' => ['href' => '/articles/index/2?foo=bar&x=y&num=0&page=4']], '4', '/a', '/li', ['li' => []], ['a' => ['href' => '/articles/index/2?foo=bar&x=y&num=0&page=5']], '5', '/a', '/li', ['li' => []], ['a' => ['href' => '/articles/index/2?foo=bar&x=y&num=0&page=6']], '6', '/a', '/li', ['li' => []], ['a' => ['href' => '/articles/index/2?foo=bar&x=y&num=0&page=7']], '7', '/a', '/li', ]; $this->assertHtml($expected, $result); $result = $this->Paginator->next('Next'); $expected = [ 'li' => ['class' => 'next'], 'a' => ['href' => '/articles/index/2?foo=bar&x=y&num=0&page=2', 'rel' => 'next'], 'Next', '/a', '/li' ]; $this->assertHtml($expected, $result); } /** * Test that generated URLs don't include sort and direction parameters * * @return void */ public function testDefaultSortRemovedFromUrl() { $request = new ServerRequest([ 'url' => '/articles/', 'params' => [ 'plugin' => null, 'controller' => 'articles', 'action' => 'index' ] ]); Router::setRequestInfo($request); $this->Paginator->request = $this->Paginator->request->withParam('paging', [ 'Article' => [ 'page' => 1, 'current' => 3, 'count' => 13, 'prevPage' => false, 'nextPage' => true, 'pageCount' => 8, 'sort' => 'Article.title', 'direction' => 'ASC', 'sortDefault' => 'Article.title', 'directionDefault' => 'ASC' ] ]); $result = $this->Paginator->next('Next'); $expected = [ 'li' => ['class' => 'next'], 'a' => ['rel' => 'next', 'href' => '/articles/index?page=2'], 'Next', '/a', '/li' ]; $this->assertHtml($expected, $result); } /** * Test the prev() method. * * @return void */ public function testPrev() { $this->Paginator->request = $this->Paginator->request->withParam('paging', [ 'Client' => [ 'page' => 1, 'current' => 3, 'count' => 13, 'prevPage' => false, 'nextPage' => true, 'pageCount' => 5, ] ]); $result = $this->Paginator->prev('<< Previous'); $expected = [ 'li' => ['class' => 'prev disabled'], 'a' => ['href' => '', 'onclick' => 'return false;'], '<< Previous', '/a', '/li' ]; $this->assertHtml($expected, $result); $result = $this->Paginator->prev('<< Previous', ['disabledTitle' => 'Prev']); $expected = [ 'li' => ['class' => 'prev disabled'], 'a' => ['href' => '', 'onclick' => 'return false;'], 'Prev', '/a', '/li' ]; $this->assertHtml($expected, $result); $result = $this->Paginator->prev('<< Previous', ['disabledTitle' => false]); $this->assertEquals('', $result, 'disabled + no text = no link'); $this->Paginator->request = $this->Paginator->request ->withParam('paging.Client.page', 2) ->withParam('paging.Client.prevPage', true); $result = $this->Paginator->prev('<< Previous'); $expected = [ 'li' => ['class' => 'prev'], 'a' => ['href' => '/index', 'rel' => 'prev'], '<< Previous', '/a', '/li' ]; $this->assertHtml($expected, $result); $result = $this->Paginator->prev('Prev', [ 'templates' => [ 'prevActive' => '{{text}}' ] ]); $expected = [ 'a' => ['href' => '/index', 'rel' => 'prev'], 'Prev', '/a', ]; $this->assertHtml($expected, $result); } /** * Test that prev() and the shared implementation underneath picks up from options * * @return void */ public function testPrevWithOptions() { $this->Paginator->request = $this->Paginator->request->withParam('paging', [ 'Client' => [ 'page' => 2, 'current' => 1, 'count' => 13, 'prevPage' => true, 'nextPage' => false, 'pageCount' => 2, 'limit' => 10, ] ]); $this->Paginator->options(['url' => [12, 'page' => 3]]); $result = $this->Paginator->prev('Prev', ['url' => ['foo' => 'bar']]); $expected = [ 'li' => ['class' => 'prev'], 'a' => ['href' => '/index/12?limit=10&foo=bar', 'rel' => 'prev'], 'Prev', '/a', '/li' ]; $this->assertHtml($expected, $result); } /** * test the next() method. * * @return void */ public function testNext() { $result = $this->Paginator->next('Next >>'); $expected = [ 'li' => ['class' => 'next'], 'a' => ['href' => '/index?page=2', 'rel' => 'next'], 'Next >>', '/a', '/li' ]; $this->assertHtml($expected, $result); $result = $this->Paginator->next('Next', [ 'templates' => [ 'nextActive' => '{{text}}' ] ]); $expected = [ 'a' => ['href' => '/index?page=2', 'rel' => 'next'], 'Next', '/a', ]; $this->assertHtml($expected, $result); $result = $this->Paginator->next('Next >>', ['escape' => false]); $expected = [ 'li' => ['class' => 'next'], 'a' => ['href' => '/index?page=2', 'rel' => 'next'], 'preg:/Next >>/', '/a', '/li' ]; $this->assertHtml($expected, $result); } /** * test next() with disabled links * * @return void */ public function testNextDisabled() { $this->Paginator->request = $this->Paginator->request->withParam('paging', [ 'Client' => [ 'page' => 5, 'current' => 3, 'count' => 13, 'prevPage' => true, 'nextPage' => false, 'pageCount' => 5, ] ]); $result = $this->Paginator->next('Next >>'); $expected = [ 'li' => ['class' => 'next disabled'], 'a' => ['href' => '', 'onclick' => 'return false;'], 'Next >>', '/a', '/li' ]; $this->assertHtml($expected, $result); $result = $this->Paginator->next('Next >>', ['disabledTitle' => 'Next']); $expected = [ 'li' => ['class' => 'next disabled'], 'a' => ['href' => '', 'onclick' => 'return false;'], 'Next', '/a', '/li' ]; $this->assertHtml($expected, $result); $result = $this->Paginator->next('Next >>', ['disabledTitle' => false]); $this->assertEquals('', $result, 'disabled + no text = no link'); } /** * Test next() with a model argument. * * @return void */ public function testNextAndPrevNonDefaultModel() { $this->Paginator->request = $this->Paginator->request->withParam('paging', [ 'Client' => [ 'page' => 1, 'current' => 3, 'count' => 13, 'prevPage' => false, 'nextPage' => true, 'pageCount' => 5, ], 'Server' => [ 'page' => 5, 'current' => 1, 'count' => 5, 'prevPage' => true, 'nextPage' => false, 'pageCount' => 5, ] ]); $result = $this->Paginator->next('Next', [ 'model' => 'Client' ]); $expected = [ 'li' => ['class' => 'next'], 'a' => ['href' => '/index?page=2', 'rel' => 'next'], 'Next', '/a', '/li' ]; $this->assertHtml($expected, $result); $result = $this->Paginator->prev('Prev', [ 'model' => 'Client' ]); $expected = '